gulpfile.js 8.2 KB

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