gulpfile.js 7.4 KB

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