gulpfile.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /* eslint-disable no-console */
  2. /* eslint-disable camelcase */
  3. /* eslint-disable no-undef */
  4. /* eslint-disable unicorn/prefer-module */
  5. const browserSync = require('browser-sync').create()
  6. const del = require('del')
  7. const esbuild = require('esbuild')
  8. const { src, dest, lastRun, watch, series, parallel } = require('gulp')
  9. const cleanCss = require('gulp-clean-css')
  10. const eslint = require('gulp-eslint7')
  11. const fileinclude = require('gulp-file-include')
  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 sourcemaps = require('gulp-sourcemaps')
  17. const gulpStylelint = require('gulp-stylelint')
  18. const wait = require('gulp-wait')
  19. sass.compiler = require('sass')
  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. img: './dist/assets/img',
  36. vendor: './dist/vendor'
  37. },
  38. base: {
  39. base: './',
  40. node: './node_modules'
  41. },
  42. src: {
  43. base: './src/',
  44. css: './src/css',
  45. html: './src/pages/**/*.html',
  46. assets: './src/assets/**/*.*',
  47. partials: './src/partials/**/*.html',
  48. scss: './src/scss',
  49. ts: './src/ts',
  50. node_modules: './node_modules/',
  51. vendor: './vendor'
  52. },
  53. temp: {
  54. base: './.temp/',
  55. css: './.temp/css',
  56. js: './.temp/js',
  57. html: './.temp/pages',
  58. assets: './.temp/assets',
  59. vendor: './.temp/vendor'
  60. }
  61. }
  62. const sassOptions = {
  63. outputStyle: 'expanded',
  64. includePaths: ['./node_modules/']
  65. }
  66. function postcssOptions() {
  67. return {
  68. map: {
  69. inline: false,
  70. annotation: true,
  71. sourcesContent: true
  72. },
  73. plugins: [
  74. require('autoprefixer')({
  75. cascade: false
  76. })
  77. ]
  78. }
  79. }
  80. function postcssRtlOptions() {
  81. return {
  82. map: {
  83. inline: false,
  84. annotation: true,
  85. sourcesContent: true
  86. },
  87. plugins: [
  88. require('autoprefixer')({
  89. cascade: false
  90. }),
  91. require('rtlcss')({})
  92. ]
  93. }
  94. }
  95. // Compile SCSS
  96. const scss = () => {
  97. return src(paths.src.scss + '/adminlte.scss')
  98. .pipe(wait(500))
  99. .pipe(sourcemaps.init())
  100. .pipe(sass(sassOptions).on('error', sass.logError))
  101. .pipe(postcss(postcssOptions))
  102. .pipe(sourcemaps.write('.'))
  103. .pipe(dest(paths.temp.css))
  104. .pipe(browserSync.stream())
  105. }
  106. // Lint SCSS
  107. const lintScss = () => {
  108. return src([paths.src.scss + '/**/*.scss'], { since: lastRun(lintScss) })
  109. .pipe(gulpStylelint({
  110. reporters: [
  111. { formatter: 'string', console: true }
  112. ]
  113. }))
  114. }
  115. const ts = () => {
  116. return esbuild.build({
  117. entryPoints: [paths.src.ts + '/adminlte.ts'],
  118. banner: {
  119. js: banner
  120. },
  121. bundle: true,
  122. format: 'iife',
  123. sourcemap: true,
  124. target: ['chrome60'],
  125. outfile: paths.temp.js + '/adminlte.js'
  126. }).catch(
  127. error => console.error(error)
  128. )
  129. }
  130. // Lint TS
  131. const lintTs = () => {
  132. return src([paths.src.ts + '/**/*.ts'], { since: lastRun(lintTs) })
  133. .pipe(eslint())
  134. .pipe(eslint.format())
  135. }
  136. const index = () => {
  137. return src([paths.src.base + '*.html'])
  138. .pipe(fileinclude({
  139. prefix: '@@',
  140. basepath: './src/partials/',
  141. context: {
  142. environment: 'development'
  143. }
  144. }))
  145. .pipe(dest(paths.temp.base))
  146. .pipe(browserSync.stream())
  147. }
  148. const html = () => {
  149. return src([paths.src.html])
  150. .pipe(fileinclude({
  151. prefix: '@@',
  152. basepath: './src/partials/',
  153. context: {
  154. environment: 'development'
  155. }
  156. }))
  157. .pipe(dest(paths.temp.html))
  158. .pipe(browserSync.stream())
  159. }
  160. const assets = () => {
  161. return src([paths.src.assets])
  162. .pipe(dest(paths.temp.assets))
  163. .pipe(browserSync.stream())
  164. }
  165. const vendor = () => {
  166. return src(npmDist({ copyUnminified: true }), { base: paths.src.node_modules })
  167. .pipe(dest(paths.temp.vendor))
  168. }
  169. const serve = () => {
  170. browserSync.init({
  171. server: paths.temp.base
  172. })
  173. watch([paths.src.scss], series(lintScss, scss))
  174. watch([paths.src.ts], series(lintTs, ts))
  175. watch([paths.src.html, paths.src.base + '*.html', paths.src.partials], series(html, index))
  176. watch([paths.src.assets], series(assets))
  177. watch([paths.src.vendor], series(vendor))
  178. }
  179. // Minify CSS
  180. const minifyDistCss = () => {
  181. return src([
  182. paths.dist.css + '/**/*.css'
  183. ], { base: paths.dist.css })
  184. .pipe(sourcemaps.init({ loadMaps: true }))
  185. .pipe(cleanCss({ format: { breakWith: 'lf' } }))
  186. .pipe(rename({ suffix: '.min' }))
  187. .pipe(sourcemaps.write('.'))
  188. .pipe(dest(paths.dist.css))
  189. }
  190. // Minify JS
  191. const minifyDistJs = () => {
  192. return esbuild.build({
  193. entryPoints: [paths.dist.js + '/adminlte.js'],
  194. format: 'iife',
  195. minify: true,
  196. sourcemap: true,
  197. target: ['chrome60'],
  198. outfile: paths.dist.js + '/adminlte.min.js'
  199. }).catch(
  200. error => console.error(error)
  201. )
  202. }
  203. // Copy assets
  204. const copyDistAssets = () => {
  205. return src(paths.src.assets)
  206. .pipe(dest(paths.dist.assets))
  207. }
  208. // Clean
  209. const cleanDist = () => {
  210. return del([paths.dist.base])
  211. }
  212. // Compile and copy all scss/css
  213. const copyDistCssAll = () => {
  214. return src([
  215. paths.src.scss + '/adminlte.scss',
  216. paths.src.scss + '/dark/*.scss'
  217. ], { base: paths.src.scss })
  218. .pipe(wait(500))
  219. .pipe(sourcemaps.init())
  220. .pipe(sass(sassOptions).on('error', sass.logError))
  221. .pipe(postcss(postcssOptions))
  222. .pipe(sourcemaps.write('.'))
  223. .pipe(dest(paths.dist.css))
  224. }
  225. const copyDistCssRtl = () => {
  226. return src(paths.dist.css + '/**/*.css')
  227. .pipe(wait(500))
  228. .pipe(sourcemaps.init({ loadMaps: true }))
  229. .pipe(postcss(postcssRtlOptions))
  230. .pipe(rename({ suffix: '.rtl' }))
  231. .pipe(sourcemaps.write('.'))
  232. .pipe(dest(paths.dist.css + '/rtl'))
  233. }
  234. // Compile and copy ts/js
  235. const copyDistJs = () => {
  236. return esbuild.build({
  237. entryPoints: [paths.src.ts + '/adminlte.ts'],
  238. banner: {
  239. js: banner
  240. },
  241. bundle: true,
  242. format: 'iife',
  243. sourcemap: true,
  244. target: ['chrome60'],
  245. outfile: paths.dist.js + '/adminlte.js'
  246. }).catch(
  247. error => console.error(error)
  248. )
  249. }
  250. // Copy Html
  251. const copyDistHtml = () => {
  252. return src([paths.src.html])
  253. .pipe(fileinclude({
  254. prefix: '@@',
  255. basepath: './src/partials/',
  256. context: {
  257. environment: 'production'
  258. }
  259. }))
  260. .pipe(dest(paths.dist.html))
  261. }
  262. // Copy index
  263. const copyDistHtmlIndex = () => {
  264. return src([paths.src.base + '*.html'])
  265. .pipe(fileinclude({
  266. prefix: '@@',
  267. basepath: './src/partials/',
  268. context: {
  269. environment: 'production'
  270. }
  271. }))
  272. .pipe(dest(paths.dist.base))
  273. }
  274. // Copy node_modules to vendor
  275. const copyDistVendor = () => {
  276. return src(npmDist({ copyUnminified: true }), { base: paths.src.node_modules })
  277. .pipe(dest(paths.dist.vendor))
  278. }
  279. exports.build = series(lintScss, lintTs, cleanDist, copyDistCssAll, copyDistCssRtl, minifyDistCss, copyDistJs, minifyDistJs, copyDistHtml, copyDistHtmlIndex, copyDistAssets, copyDistVendor)
  280. // Default
  281. exports.default = series(parallel(lintScss, scss, lintTs, ts, html, index, assets, vendor), serve)