gulpfile.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* eslint-env node */
  2. const autoprefix = require('autoprefixer')
  3. const browserSync = require('browser-sync').create()
  4. const del = require('del')
  5. const { src, dest, lastRun, watch, series, parallel } = require('gulp')
  6. const cleanCss = require('gulp-clean-css')
  7. const eslint = require('gulp-eslint-new')
  8. const fileinclude = require('gulp-file-include')
  9. const validator = require('gulp-html')
  10. const gulpIf = require('gulp-if')
  11. const postcss = require('gulp-postcss')
  12. const rename = require('gulp-rename')
  13. const sass = require('gulp-sass')(require('sass'))
  14. const gulpStylelint = require('@ronilaukkarinen/gulp-stylelint')
  15. const terser = require('gulp-terser')
  16. const rollup = require('rollup')
  17. const rollupTypescript = require('@rollup/plugin-typescript')
  18. const rtlcss = require('rtlcss')
  19. const pkg = require('./package')
  20. const year = new Date().getFullYear()
  21. const banner = `/*!
  22. * AdminLTE v${pkg.version} (${pkg.homepage})
  23. * Copyright 2014-${year} ${pkg.author}
  24. * Licensed under MIT (https://github.com/ColorlibHQ/AdminLTE/blob/master/LICENSE)
  25. */`
  26. // Define paths
  27. const paths = {
  28. dist: {
  29. base: './dist/',
  30. css: './dist/css',
  31. js: './dist/js',
  32. html: './dist/pages',
  33. assets: './dist/assets'
  34. },
  35. src: {
  36. base: './src/',
  37. html: './src/pages/**/*.html',
  38. assets: './src/assets/**/*.*',
  39. partials: './src/partials/**/*.html',
  40. scss: './src/scss',
  41. ts: './src/ts'
  42. },
  43. temp: {
  44. base: './.temp/',
  45. css: './.temp/css',
  46. js: './.temp/js',
  47. html: './.temp/pages',
  48. assets: './.temp/assets'
  49. }
  50. }
  51. const sassOptions = {
  52. outputStyle: 'expanded',
  53. includePaths: ['./node_modules/']
  54. }
  55. const postcssOptions = [
  56. autoprefix({ cascade: false })
  57. ]
  58. const postcssRtlOptions = [
  59. autoprefix({ cascade: false }),
  60. rtlcss({})
  61. ]
  62. // From here Dev mode will Start
  63. // Lint SCSS
  64. const lintScss = () => src([paths.src.scss + '/**/*.scss'], { since: lastRun(lintScss) })
  65. .pipe(gulpStylelint({
  66. failAfterError: false,
  67. reporters: [
  68. { formatter: 'string', console: true }
  69. ],
  70. debug: true
  71. }))
  72. // Compile SCSS
  73. const scss = () => src(paths.src.scss + '/adminlte.scss', { sourcemaps: true })
  74. .pipe(sass(sassOptions).on('error', sass.logError))
  75. .pipe(postcss(postcssOptions))
  76. .pipe(dest(paths.temp.css, { sourcemaps: '.' }))
  77. .pipe(browserSync.stream())
  78. // Compile SCSS Dark
  79. const scssDark = () => src(paths.src.scss + '/dark/adminlte-dark-addon.scss', { sourcemaps: true })
  80. .pipe(sass(sassOptions).on('error', sass.logError))
  81. .pipe(postcss(postcssOptions))
  82. .pipe(dest(paths.temp.css + '/dark', { sourcemaps: '.' }))
  83. .pipe(browserSync.stream())
  84. // Lint TS
  85. function isFixed(file) {
  86. // Has ESLint fixed the file contents?
  87. return file.eslint !== null && file.eslint.fixed
  88. }
  89. const lintTs = () => src([paths.src.ts + '/**/*.ts'], { since: lastRun(lintTs) })
  90. .pipe(eslint({ fix: true }))
  91. .pipe(eslint.format())
  92. .pipe(gulpIf(isFixed, dest(paths.src.ts)))
  93. .pipe(eslint.failAfterError())
  94. // Compile TS
  95. const tsCompile = () =>
  96. rollup.rollup({
  97. input: paths.src.ts + '/adminlte.ts',
  98. output: {
  99. banner
  100. },
  101. plugins: [
  102. rollupTypescript()
  103. ]
  104. }).then(bundle => bundle.write({
  105. file: paths.temp.js + '/adminlte.js',
  106. format: 'umd',
  107. name: 'adminlte',
  108. sourcemap: true
  109. }))
  110. const assets = () => src([paths.src.assets])
  111. .pipe(dest(paths.temp.assets))
  112. .pipe(browserSync.stream())
  113. const index = () => src([paths.src.base + '*.html'])
  114. .pipe(fileinclude({
  115. prefix: '@@',
  116. basepath: './src/partials/',
  117. context: {
  118. environment: 'development'
  119. }
  120. }))
  121. .pipe(dest(paths.temp.base))
  122. .pipe(browserSync.stream())
  123. const html = () => src([paths.src.html])
  124. .pipe(fileinclude({
  125. prefix: '@@',
  126. basepath: './src/partials/',
  127. context: {
  128. environment: 'development'
  129. }
  130. }))
  131. .pipe(dest(paths.temp.html))
  132. .pipe(browserSync.stream())
  133. const lintHtml = () => src([paths.temp.html + '/**/*.html', paths.temp.base + '*.html'], { since: lastRun(lintHtml) })
  134. .pipe(validator())
  135. const serve = () => {
  136. browserSync.init({
  137. server: paths.temp.base
  138. })
  139. watch([paths.src.scss], series(lintScss))
  140. watch([paths.src.scss + '/**/*.scss', '!' + paths.src.scss + '/bootstrap-dark/**/*.scss', '!' + paths.src.scss + '/dark/**/*.scss'], series(scss))
  141. watch([paths.src.scss + '/bootstrap-dark/', paths.src.scss + '/dark/'], series(scssDark))
  142. watch([paths.src.ts], series(lintTs, tsCompile))
  143. watch([paths.src.html, paths.src.base + '*.html', paths.src.partials], series(html, index, lintHtml))
  144. watch([paths.src.assets], series(assets))
  145. }
  146. // From here Dist will Start
  147. // Clean
  148. const cleanDist = () => del([paths.dist.base])
  149. const lintDistScss = () => src([paths.src.scss + '/**/*.scss'], ['./.temp/**/*.css'])
  150. .pipe(gulpStylelint({
  151. failAfterError: false,
  152. reporters: [
  153. { formatter: 'string', console: true }
  154. ]
  155. }))
  156. // Compile and copy all scss/css
  157. const copyDistCssAll = () => src([paths.src.scss + '/**/*.scss'], {
  158. base: paths.src.scss,
  159. sourcemaps: true
  160. })
  161. .pipe(sass(sassOptions).on('error', sass.logError))
  162. .pipe(postcss(postcssOptions))
  163. .pipe(dest(paths.dist.css, { sourcemaps: '.' }))
  164. const copyDistCssRtl = () => src(paths.dist.css + '/*.css', { sourcemaps: true })
  165. .pipe(postcss(postcssRtlOptions))
  166. .pipe(rename({ suffix: '.rtl' }))
  167. .pipe(dest(paths.dist.css + '/rtl', { sourcemaps: '.' }))
  168. // Minify CSS
  169. const minifyDistCss = () => src([
  170. paths.dist.css + '/**/*.css'
  171. ], {
  172. base: paths.dist.css,
  173. sourcemaps: true
  174. })
  175. .pipe(cleanCss({ format: { breakWith: 'lf' } }))
  176. .pipe(rename({ suffix: '.min' }))
  177. .pipe(dest(paths.dist.css, { sourcemaps: '.' }))
  178. const lintDistTs = () => src([paths.src.ts + '/**/*.ts'])
  179. .pipe(eslint())
  180. .pipe(eslint.failAfterError())
  181. // Compile and copy ts/js
  182. const copyDistJs = () =>
  183. rollup.rollup({
  184. input: paths.src.ts + '/adminlte.ts',
  185. output: {
  186. banner
  187. },
  188. plugins: [
  189. rollupTypescript()
  190. ]
  191. }).then(bundle => bundle.write({
  192. file: paths.dist.js + '/adminlte.js',
  193. format: 'umd',
  194. name: 'adminlte',
  195. sourcemap: true
  196. }))
  197. // Minify JS
  198. const minifyDistJs = () => src(paths.dist.js + '/adminlte.js', { sourcemaps: true })
  199. .pipe(terser({ compress: { passes: 2 } }))
  200. .pipe(rename({ suffix: '.min' }))
  201. .pipe(dest(paths.dist.js, { sourcemaps: '.' }))
  202. // Copy assets
  203. const copyDistAssets = () => src(paths.src.assets)
  204. .pipe(dest(paths.dist.assets))
  205. // Copy index
  206. const copyDistHtmlIndex = () => src([paths.src.base + '*.html'])
  207. .pipe(fileinclude({
  208. prefix: '@@',
  209. basepath: './src/partials/',
  210. context: {
  211. environment: 'production'
  212. }
  213. }))
  214. .pipe(dest(paths.dist.base))
  215. // Copy Html
  216. const copyDistHtml = () => src([paths.src.html])
  217. .pipe(fileinclude({
  218. prefix: '@@',
  219. basepath: './src/partials/',
  220. context: {
  221. environment: 'production'
  222. }
  223. }))
  224. .pipe(dest(paths.dist.html))
  225. // HTML Lint
  226. // Copy index for Lint
  227. const copyDistHtmlIndexForLint = () => src([paths.src.base + '*.html'])
  228. .pipe(fileinclude({
  229. prefix: '@@',
  230. basepath: './src/partials/',
  231. context: {
  232. environment: 'production'
  233. }
  234. }))
  235. .pipe(dest(paths.temp.base))
  236. // Copy Html for Lint
  237. const copyDistHtmlForLint = () => src([paths.src.html])
  238. .pipe(fileinclude({
  239. prefix: '@@',
  240. basepath: './src/partials/',
  241. context: {
  242. environment: 'production'
  243. }
  244. }))
  245. .pipe(dest(paths.temp.html))
  246. // Now Lint
  247. const lintDistHtmlCopied = () => src([paths.temp.html + '/**/*.html', paths.temp.base + '*.html'])
  248. .pipe(validator())
  249. const lintDistHtml = series(copyDistHtmlIndexForLint, copyDistHtmlForLint, lintDistHtmlCopied)
  250. const lint = parallel(
  251. lintDistScss,
  252. lintDistTs,
  253. lintDistHtml
  254. )
  255. exports.lint = lint
  256. const compile = series(
  257. cleanDist,
  258. parallel(
  259. series(copyDistCssAll, copyDistCssRtl, minifyDistCss),
  260. series(copyDistJs, minifyDistJs),
  261. copyDistAssets,
  262. copyDistHtmlIndex,
  263. copyDistHtml
  264. )
  265. )
  266. exports.compile = compile
  267. // For Production Release
  268. exports.production = series(lint, compile)
  269. // Default - Only for light mode AdminLTE
  270. exports.default = series(scss, scssDark, tsCompile, html, index, assets, serve)