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