gulpfile.js 8.8 KB

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