gulpfile.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. const gulp = require('gulp'),
  2. debug = require('gulp-debug'),
  3. clean = require('gulp-clean'),
  4. sass = require('gulp-sass')(require('sass')),
  5. postcss = require('gulp-postcss'),
  6. header = require('gulp-header'),
  7. cleanCSS = require('gulp-clean-css'),
  8. rtlcss = require('gulp-rtlcss'),
  9. minifyJS = require('gulp-terser'),
  10. rename = require('gulp-rename'),
  11. purgecss = require('gulp-purgecss'),
  12. rollupStream = require('@rollup/stream'),
  13. rollupBabel = require('rollup-plugin-babel'),
  14. rollupCleanup = require('rollup-plugin-cleanup'),
  15. { nodeResolve } = require('@rollup/plugin-node-resolve'),
  16. rollupCommonjs = require('@rollup/plugin-commonjs'),
  17. rollupReplace = require('@rollup/plugin-replace'),
  18. vinylSource = require('vinyl-source-stream'),
  19. vinylBuffer = require('vinyl-buffer'),
  20. browserSync = require('browser-sync'),
  21. glob = require('glob'),
  22. spawn = require('cross-spawn'),
  23. fs = require('fs'),
  24. path = require('path'),
  25. yargs = require('yargs/yargs'),
  26. cp = require('child_process'),
  27. pkg = require('./package.json'),
  28. year = new Date().getFullYear(),
  29. argv = yargs(process.argv).argv
  30. let BUILD = false,
  31. distDir = './.tmp',
  32. demoDir = './.tmp',
  33. srcDir = './src'
  34. /**
  35. * Enable BUILD mode and set directories
  36. */
  37. gulp.task('build-on', (cb) => {
  38. BUILD = true
  39. distDir = './dist'
  40. demoDir = './demo'
  41. cb()
  42. })
  43. /**
  44. * Return banner added to CSS and JS dist files
  45. */
  46. const getBanner = () => {
  47. return `/*!
  48. * Tabler v${pkg.version} (${pkg.homepage})
  49. * @version ${pkg.version}
  50. * @link ${pkg.homepage}
  51. * Copyright 2018-${year} The Tabler Authors
  52. * Copyright 2018-${year} codecalm.net Paweł Kuna
  53. * Licensed under MIT (https://github.com/tabler/tabler/blob/master/LICENSE)
  54. */
  55. `
  56. }
  57. /**
  58. * Array.flat polyfill
  59. */
  60. if (!Array.prototype.flat) {
  61. Object.defineProperty(Array.prototype, 'flat', {
  62. value: function (depth = 1) {
  63. return this.reduce(function (flat, toFlatten) {
  64. return flat.concat((Array.isArray(toFlatten) && (depth > 1)) ? toFlatten.flat(depth - 1) : toFlatten)
  65. }, [])
  66. }
  67. })
  68. }
  69. /**
  70. * Check unused Jekyll partials
  71. */
  72. gulp.task('unused-files', (cb) => {
  73. let foundFiles = []
  74. glob.sync(`${srcDir}/pages/**/*.{html,md}`).forEach((file) => {
  75. let fileContent = fs.readFileSync(file)
  76. fileContent.toString().replace(/\{% include(_cached)? ([a-z0-9\/_-]+\.html)/g, (f, c, filename) => {
  77. filename = `${srcDir}/pages/_includes/${filename}`
  78. if (!foundFiles.includes(filename)) {
  79. foundFiles.push(filename)
  80. }
  81. })
  82. })
  83. let includeFiles = glob.sync(`${srcDir}/pages/_includes/**/*.html`)
  84. includeFiles.forEach((file) => {
  85. if (!foundFiles.includes(file)) {
  86. console.log('file', file)
  87. }
  88. })
  89. cb()
  90. })
  91. /**
  92. * Clean `dist` folder before build
  93. */
  94. gulp.task('clean-dirs', () => {
  95. return gulp
  96. .src(`{${distDir}/*,${demoDir}/*}`, { read: false })
  97. .pipe(clean())
  98. })
  99. gulp.task('clean-jekyll', (cb) => {
  100. return spawn('bundle', ['exec', 'jekyll', 'clean'], { stdio: 'inherit' })
  101. .on('close', cb)
  102. })
  103. /**
  104. * Compile SASS to CSS and move it to dist directory
  105. */
  106. gulp.task('sass', () => {
  107. return gulp
  108. .src(argv.withPlugins || BUILD ? `${srcDir}/scss/!(_)*.scss` : `${srcDir}/scss/+(tabler|demo).scss`)
  109. .pipe(debug())
  110. .pipe(sass({
  111. includePaths: ['node_modules'],
  112. style: 'expanded',
  113. precision: 7,
  114. importer: (url, prev, done) => {
  115. if (url[0] === '~') {
  116. url = path.resolve('node_modules', url.substr(1))
  117. }
  118. return { file: url }
  119. },
  120. }).on('error', sass.logError))
  121. .pipe(postcss([
  122. require('autoprefixer'),
  123. ]))
  124. .pipe(gulp.dest(`${distDir}/css/`))
  125. .pipe(browserSync.reload({
  126. stream: true,
  127. }));
  128. })
  129. gulp.task('css-rtl', function () {
  130. return gulp.src(`${distDir}/css/*.css`)
  131. .pipe(rtlcss())
  132. .pipe(rename((path) => {
  133. path.basename += '.rtl'
  134. }))
  135. .pipe(gulp.dest(`${distDir}/css/`))
  136. });
  137. /**
  138. * CSS minify
  139. */
  140. gulp.task('css-minify', function () {
  141. return gulp.src(`${distDir}/css/!(*.min).css`)
  142. .pipe(debug())
  143. .pipe(cleanCSS())
  144. .pipe(rename((path) => {
  145. path.basename += '.min'
  146. }))
  147. .pipe(gulp.dest(`${distDir}/css/`))
  148. })
  149. /**
  150. * Compile JS files to dist directory
  151. */
  152. let cache = {}
  153. const compileJs = function (name, mjs = false) {
  154. if (!cache[name]) {
  155. cache[name] = null
  156. }
  157. const g = rollupStream({
  158. input: `${srcDir}/js/${name}.js`,
  159. cache: cache[name],
  160. output: {
  161. name: `${name}.js`,
  162. format: mjs ? 'es' : 'umd',
  163. ...(mjs ? { exports: 'named' } : {})
  164. },
  165. plugins: [
  166. rollupReplace({
  167. 'process.env.NODE_ENV': JSON.stringify(BUILD ? 'production' : 'development'),
  168. preventAssignment: false
  169. }),
  170. rollupBabel({
  171. exclude: 'node_modules/**'
  172. }),
  173. nodeResolve(),
  174. rollupCommonjs(),
  175. rollupCleanup()
  176. ]
  177. })
  178. .on('bundle', (bundle) => {
  179. cache[name] = bundle
  180. })
  181. .pipe(vinylSource(`${name}.js`))
  182. .pipe(vinylBuffer())
  183. .pipe(rename((path) => {
  184. path.dirname = ''
  185. }))
  186. .pipe(gulp.dest(`${distDir}/js/`))
  187. .pipe(browserSync.reload({
  188. stream: true,
  189. }))
  190. if (BUILD) {
  191. g.pipe(minifyJS())
  192. .pipe(rename((path) => {
  193. path.extname = '.min.js'
  194. }))
  195. .pipe(gulp.dest(`${distDir}/js/`))
  196. }
  197. return g
  198. }
  199. /**
  200. * Compile JS files to dist directory
  201. */
  202. gulp.task('js', () => {
  203. return compileJs('tabler')
  204. })
  205. gulp.task('js-demo', () => {
  206. return compileJs('demo')
  207. })
  208. gulp.task('js-demo-theme', () => {
  209. return compileJs('demo-theme')
  210. })
  211. /**
  212. * Compile JS module files to dist directory
  213. */
  214. gulp.task('mjs', () => {
  215. return compileJs('tabler.esm', true)
  216. })
  217. let cacheEsm
  218. gulp.task('mjs', () => {
  219. const g = rollupStream({
  220. input: `${srcDir}/js/tabler.esm.js`,
  221. cache: cacheEsm,
  222. output: {
  223. name: 'tabler.esm.js',
  224. format: 'es',
  225. exports: 'named'
  226. },
  227. plugins: [
  228. rollupReplace({
  229. 'process.env.NODE_ENV': JSON.stringify(BUILD ? 'production' : 'development'),
  230. preventAssignment: false
  231. }),
  232. rollupBabel({
  233. exclude: 'node_modules/**'
  234. }),
  235. nodeResolve(),
  236. rollupCommonjs(),
  237. rollupCleanup()
  238. ]
  239. })
  240. .on('bundle', (bundle) => {
  241. cacheEsm = bundle
  242. })
  243. .pipe(vinylSource('tabler.esm.js'))
  244. .pipe(vinylBuffer())
  245. .pipe(rename((path) => {
  246. path.dirname = ''
  247. }))
  248. .pipe(gulp.dest(`${distDir}/js/`))
  249. .pipe(browserSync.reload({
  250. stream: true,
  251. }))
  252. if (BUILD) {
  253. g.pipe(minifyJS())
  254. .pipe(rename((path) => {
  255. path.extname = '.min.js'
  256. }))
  257. .pipe(gulp.dest(`${distDir}/js/`))
  258. }
  259. return g
  260. })
  261. /**
  262. * Watch Jekyll files and build it to demo directory
  263. */
  264. gulp.task('watch-jekyll', (cb) => {
  265. browserSync.notify('Building Jekyll')
  266. return spawn('bundle', ['exec', 'jekyll', 'build', '--watch', '--destination', demoDir, '--trace'], { stdio: 'inherit' })
  267. .on('close', cb)
  268. })
  269. /**
  270. * Build Jekyll files do demo directory
  271. */
  272. gulp.task('build-jekyll', (cb) => {
  273. var env = Object.create(process.env)
  274. if (argv.preview) {
  275. env.JEKYLL_ENV = 'preview'
  276. } else {
  277. env.JEKYLL_ENV = 'production'
  278. }
  279. return spawn('bundle', ['exec', 'jekyll', 'build', '--destination', demoDir, '--trace', '--config', '_config.yml,_config_prod.yml'], {
  280. env: env,
  281. stdio: 'inherit'
  282. })
  283. .on('close', cb)
  284. })
  285. gulp.task('build-cleanup', () => {
  286. return gulp
  287. .src(`${demoDir}/redirects.json`, { read: false, allowEmpty: true })
  288. .pipe(clean())
  289. })
  290. gulp.task('build-purgecss', (cb) => {
  291. if (argv.preview) {
  292. return gulp.src('demo/dist/{libs,css}/**/*.css')
  293. .pipe(purgecss({
  294. content: ['demo/**/*.html']
  295. }))
  296. .pipe(gulp.dest('demo/dist/css'))
  297. }
  298. cb()
  299. })
  300. /**
  301. * Watch JS and SCSS files
  302. */
  303. gulp.task('watch', (cb) => {
  304. gulp.watch('./src/scss/**/*.scss', gulp.series('sass'))
  305. gulp.watch('./src/js/**/*.js', gulp.parallel('js', 'mjs', gulp.parallel('js-demo', 'js-demo-theme')))
  306. cb()
  307. })
  308. /**
  309. * Create BrowserSync server
  310. */
  311. gulp.task('browser-sync', () => {
  312. browserSync({
  313. watch: true,
  314. server: {
  315. baseDir: demoDir,
  316. routes: {
  317. '/node_modules': 'node_modules',
  318. '/dist/css': `${distDir}/css`,
  319. '/dist/js': `${distDir}/js`,
  320. '/dist/img': `${srcDir}/img`,
  321. '/static': `${srcDir}/static`,
  322. },
  323. },
  324. port: 3000,
  325. open: false,
  326. host: 'localhost',
  327. notify: false,
  328. reloadOnRestart: true
  329. })
  330. })
  331. /**
  332. * Copy libs used in tabler from npm to dist directory
  333. */
  334. gulp.task('copy-libs', (cb) => {
  335. const allLibs = require(`${srcDir}/pages/_data/libs`)
  336. let files = []
  337. Object.keys(allLibs.js).forEach((lib) => {
  338. files.push(Array.isArray(allLibs.js[lib]) ? allLibs.js[lib] : [allLibs.js[lib]])
  339. })
  340. Object.keys(allLibs.css).forEach((lib) => {
  341. files.push(Array.isArray(allLibs.css[lib]) ? allLibs.css[lib] : [allLibs.css[lib]])
  342. })
  343. Object.keys(allLibs['js-copy']).forEach((lib) => {
  344. files.push(allLibs['js-copy'][lib])
  345. })
  346. files = files.flat()
  347. files.forEach((file) => {
  348. if (!file.match(/^https?/)) {
  349. let dirname = path.dirname(file).replace('@', '')
  350. let cmd = `mkdir -p "${distDir}/libs/${dirname}" && cp -r node_modules/${path.dirname(file)}/* ${distDir}/libs/${dirname}`
  351. cp.exec(cmd)
  352. }
  353. })
  354. cb()
  355. })
  356. /**
  357. * Copy static files (flags, payments images, etc) to dist directory
  358. */
  359. gulp.task('copy-images', () => {
  360. return gulp
  361. .src(`${srcDir}/img/**/*`)
  362. .pipe(gulp.dest(`${distDir}/img`))
  363. })
  364. /**
  365. * Copy static files (demo images, etc) to demo directory
  366. */
  367. gulp.task('copy-static', () => {
  368. return gulp
  369. .src(`${srcDir}/static/**/*`)
  370. .pipe(gulp.dest(`${demoDir}/static`))
  371. })
  372. /**
  373. * Copy Tabler dist files to demo directory
  374. */
  375. gulp.task('copy-dist', () => {
  376. return gulp
  377. .src(`${distDir}/**/*`)
  378. .pipe(gulp.dest(`${demoDir}/dist/`))
  379. })
  380. /**
  381. * Add banner to build JS and CSS files
  382. */
  383. gulp.task('add-banner', () => {
  384. return gulp.src(`${distDir}/{css,js}/**/*.{js,css}`)
  385. .pipe(header(getBanner()))
  386. .pipe(gulp.dest(`${distDir}`))
  387. })
  388. gulp.task('clean', gulp.series('clean-dirs', 'clean-jekyll'))
  389. gulp.task('start', gulp.series('clean', 'sass', 'js', gulp.parallel('js-demo', 'js-demo-theme'), 'mjs', 'build-jekyll', gulp.parallel('watch-jekyll', 'watch', 'browser-sync')))
  390. gulp.task('build-core', gulp.series('build-on', 'clean', 'sass', 'css-rtl', 'css-minify', 'js', gulp.parallel('js-demo', 'js-demo-theme'), 'mjs', 'copy-images', 'copy-libs', 'add-banner'))
  391. gulp.task('build-demo', gulp.series('build-on', 'build-jekyll', 'copy-static', 'copy-dist', 'build-cleanup', 'build-purgecss'))
  392. gulp.task('build', gulp.series('build-core', 'build-demo'))