-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
487 lines (426 loc) · 14 KB
/
index.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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!/usr/bin/env node
const fs = require('fs')
const ejs = require('ejs')
const util = require('util')
const exec = util.promisify(require('child_process').exec)
const sass = require('sass')
const merge = require('lodash.merge')
const globby = require('globby')
const server = require('slive-server')
const esbuild = require('esbuild')
const postcss = require('postcss')
const chokidar = require('chokidar')
const beautify = require('js-beautify').html
const condense = require('condense-newlines')
const autoprefixer = require('autoprefixer')
const { parseHTML } = require('linkedom')
const { resolve, extname, basename, join, parse, posix } = require('path')
require('colors')
const systemConfig = require(join(__dirname, 'config.js'))
const customConfig = fs.existsSync(systemConfig.customConfig) ? require(resolve(systemConfig.customConfig)) : {}
const config = merge(systemConfig, customConfig)
const htmlSrc = config.path.html.src
const htmlDst = config.path.html.dst
const htmlData = join(htmlSrc, 'data.json')
const cssSrc = config.path.css.src
const cssDst = config.path.css.dst
const jsSrc = config.path.js.src
const jsDst = config.path.js.dst
const delay = 200
fs.existsSync(cssDst) === false && fs.mkdirSync(cssDst, { recursive: true })
fs.existsSync(htmlDst) === false && fs.mkdirSync(htmlDst, { recursive: true })
function files(dir, ext) {
return fs.readdirSync(dir, 'utf-8')
.filter(i => extname(i) === '.' + ext && i.startsWith('_') === false)
}
function doneIn(start) {
console.log('Done in'.blue, new Date() - start, 'ms\n')
}
function argv(key) {
const arg = process.argv.filter(val => val.startsWith('--' + key))
return arg.length ? arg.pop().split('=').pop() : null
}
function toMin(src) {
const obj = parse(src)
return join(obj.dir, obj.name + '.min' + obj.ext)
}
function toExt(src, ext) {
const obj = parse(src)
return join(obj.dir, obj.name + ext)
}
async function copy(src, dst) {
return await new Promise(resolve => {
fs.copyFile(src, dst, resolve)
})
}
async function emptyDir(dir) {
fs.existsSync(dir) === false && fs.mkdirSync(dir, { recursive: true })
return await new Promise(resolve => {
fs.readdirSync(dir, 'utf-8').forEach(file => fs.unlinkSync(join(dir, file)))
resolve()
})
}
async function html(file, elapsed = true) {
return await new Promise(async resolve => {
const src = join(htmlSrc, file)
const dst = join(htmlDst, toExt(file, '.html'))
console.log('Compiling'.gray, src.cyan, 'to', dst.cyan)
const start = elapsed ? new Date() : 0
const result = await ejs.renderFile(src, JSON.parse(fs.readFileSync(htmlData, 'utf-8')))
fs.writeFileSync(dst, result)
elapsed && doneIn(start)
resolve()
})
}
async function htmlAll() {
await emptyDir(htmlDst)
const start = new Date()
await Promise.all(files(htmlSrc, 'ejs').map(file => html(file, false)))
doneIn(start)
}
async function htmlPartial(target) {
return await new Promise(async resolve => {
const start = new Date()
const partial = parse(target).name
let targets = []
files(htmlSrc, 'ejs').forEach(file => {
let content = fs.readFileSync(join(htmlSrc, file), 'utf-8')
.match(/include\(\s*(['"])(.+?)\1\s*(,\s*({.+?})\s*)?\)/g)
content = content ? content.join('') : ''
if (content.includes(partial + "'") || content.includes(partial + '"')) {
targets.push(file)
}
})
await Promise.all(targets.map(file => html(file, false)))
doneIn(start)
resolve()
})
}
async function htmlBeautify(file) {
return await new Promise(async resolve => {
const src = join(htmlDst, file)
fs.writeFileSync(src, beautify(condense(fs.readFileSync(src, 'utf-8')), {
indent_size: 2,
unformatted: ['pre', 'code'],
}))
resolve()
})
}
async function htmlBeautifyAll() {
console.log('Beautifying'.gray, join(htmlDst, '*.html').cyan)
const start = new Date()
await Promise.all(files(htmlDst, 'html').map(htmlBeautify))
doneIn(start)
}
async function css(file, elapsed = true) {
return await new Promise(async resolve => {
const src = join(cssSrc, file)
const dst = join(cssDst, toExt(file, '.css'))
console.log('Compiling'.gray, src.cyan, 'to', dst.cyan)
const start = elapsed ? new Date() : 0
if (config.useDartSass) {
await toggleSassJs(false)
await exec(`sass --source-map --embed-sources ${src} ${dst}`).catch(err => console.log(err.stderr))
}
else {
await toggleSassJs(true)
const result = sass.renderSync({
file: src,
sourceMap: true,
outFile: dst,
})
fs.writeFileSync(dst, result.css)
fs.writeFileSync(dst + '.map', result.map)
}
elapsed && doneIn(start)
resolve()
})
}
async function cssAll() {
await emptyDir(cssDst)
const start = new Date()
await Promise.all(files(cssSrc, 'scss').map(file => css(file, false)))
doneIn(start)
}
async function cssPartial(target) {
return await new Promise(async resolve => {
const start = new Date()
const partial = parse(target).name.substring(1)
let targets = []
files(cssSrc, 'scss').forEach(file => {
const content = fs.readFileSync(join(cssSrc, file), 'utf-8')
.split('\n').filter(i => i.startsWith('@import')).join('')
if (content.includes(partial + "'") || content.includes(partial + '"')) {
targets.push(file)
}
})
await Promise.all(targets.map(file => css(file, false)))
doneIn(start)
resolve()
})
}
async function cssAutoprefix(file, fromCssDst = true) {
return await new Promise(async resolve => {
const src = fromCssDst ? join(cssDst, file) : file
const content = fs.readFileSync(src, 'utf-8')
const result = await postcss([autoprefixer]).process(content, {
from: file,
map: {
inline: false,
annotation: true,
sourcesContent: true,
}
})
fs.writeFileSync(src, result.css)
resolve()
})
}
async function cssMinify(file, fromCssDst = true) {
return await new Promise(async resolve => {
const src = fromCssDst ? join(cssDst, file) : file
const dst = fromCssDst ? join(cssDst, toMin(file)) : toMin(file)
const content = fs.readFileSync(src, 'utf-8')
const result = esbuild.transformSync(content, {
loader: 'css',
minify: true,
})
fs.writeFileSync(dst, result.code)
resolve()
})
}
async function cssAutoprefixMinify() {
console.log('Autoprefixing & Minifying'.gray, join(cssDst, '*.css').cyan)
const start = new Date()
const cssFiles = files(cssDst, 'css').filter(i => i.slice(i.length - 8) !== '.min.css')
await Promise.all(cssFiles.map(file => cssAutoprefix(file)))
await Promise.all(cssFiles.map(file => cssMinify(file)))
doneIn(start)
}
async function js(file, elapsed = true) {
return await new Promise(async resolve => {
const src = join(jsSrc, file)
const dst = join(jsDst, file)
console.log('Compiling'.gray, src.cyan, 'to', dst.cyan)
const start = elapsed ? new Date() : 0
esbuild.buildSync({
entryPoints: [src],
bundle: true,
sourcemap: true,
outfile: dst,
})
elapsed && doneIn(start)
resolve()
})
}
async function jsAll() {
await emptyDir(jsDst)
const start = new Date()
await Promise.all(files(jsSrc, 'js').map(file => js(file, false)))
doneIn(start)
}
async function jsPartial(target) {
return await new Promise(async resolve => {
const start = new Date()
const partial = parse(target).name
let targets = []
files(jsSrc, 'js').forEach(file => {
const content = fs.readFileSync(join(jsSrc, file), 'utf-8')
.split('\n').filter(i => i.startsWith('import ')).join('')
if (content.includes(partial + "'") || content.includes(partial + '"')) {
targets.push(file)
}
})
await Promise.all(targets.map(file => js(file, false)))
doneIn(start)
resolve()
})
}
async function jsMinify(file, fromJsDst = true) {
return await new Promise(async resolve => {
const src = fromJsDst ? join(jsDst, file) : file
const dst = toMin(src)
esbuild.buildSync({
entryPoints: [src],
outfile: dst,
minify: true,
})
resolve()
})
}
async function jsMinifyAll() {
fs.existsSync(jsDst) === false && fs.mkdirSync(jsDst)
console.log('Minifying'.gray,join(jsDst, '*.js').cyan)
const start = new Date()
const jsFiles = files(jsDst, 'js').filter(i => i.slice(i.length - 7) !== '.min.js')
await Promise.all(jsFiles.map(file => jsMinify(file)))
doneIn(start)
}
async function setNonMinifiedAttribute(el, attr) {
return await new Promise(resolve => {
const src = el.getAttribute(attr)
switch (attr) {
case 'href':
src.endsWith('.min.css') && el.setAttribute(attr, src.slice(0, -7) + 'css')
break;
case 'src':
src.endsWith('.min.js') && el.setAttribute(attr, src.slice(0, -6) + 'js')
break;
}
resolve()
})
}
async function setMinifiedAttribute(el, attr) {
return await new Promise(async resolve => {
await setNonMinifiedAttribute(el, attr)
const src = el.getAttribute(attr)
switch (attr) {
case 'href':
src.endsWith('.css') && el.setAttribute(attr, src.slice(0, -3) + 'min.css')
break;
case 'src':
src.endsWith('.js') && el.setAttribute(attr, src.slice(0, -2) + 'min.js')
break;
}
resolve()
})
}
async function setAsset(file, minified = true) {
return await new Promise(async resolve => {
const src = join(htmlDst, file)
const { document } = parseHTML(fs.readFileSync(src, 'utf-8'))
await Promise.all(
document.querySelectorAll('link[href]')
.map(el => minified ? setMinifiedAttribute(el, 'href') : setNonMinifiedAttribute(el, 'href'))
)
await Promise.all(
document.querySelectorAll('script[src]')
.map(el => minified ? setMinifiedAttribute(el, 'src') : setNonMinifiedAttribute(el, 'src'))
)
fs.writeFileSync(src, '<!DOCTYPE html>\n' + document.documentElement.outerHTML)
resolve()
})
}
async function setAssetAll(minified = true) {
console.log('Adjusting assets'.gray, join(htmlDst, '*.html').cyan)
const start = new Date()
await Promise.all(files(htmlDst, 'html').map(file => setAsset(file, minified)))
doneIn(start)
}
async function lib() {
const lib = require(resolve('eses.libraries.js'))
fs.rmdirSync(lib.dst, { recursive: true })
console.log('Copying libraries to'.gray, join(lib.dst).cyan)
await libCopy(lib.lib, lib.dst)
console.log('Autoprefixing css files from'.gray, join(lib.dst).cyan)
await libAutoprefixCss(lib.dst)
console.log('Minifying css files from'.gray, join(lib.dst).cyan)
await libMinifyCss(lib.dst)
console.log('Minifying js files from'.gray, join(lib.dst).cyan)
await libMinifyJs(lib.dst)
}
async function libCopy(libs, dst) {
return await new Promise(async resolve => {
for (const [key, value] of Object.entries(libs)) {
const dstDir = join(dst, key)
fs.existsSync(dstDir) === false && fs.mkdirSync(dstDir, { recursive: true })
const files = await globby(value)
await Promise.all(files.map(file => copy(join(file), join(dstDir, basename(file)))))
}
resolve()
})
}
async function libAutoprefixCss(dst) {
return await new Promise(async resolve => {
const files = await globby(posix.join(dst, '**', '*.css'))
await Promise.all(files.map(file => cssAutoprefix(file, false)))
resolve()
})
}
async function libMinifyCss(dst) {
return await new Promise(async resolve => {
const files = await globby([posix.join(dst, '**', '*.css'), posix.join('!' + dst, '**', '*.min.css')])
await Promise.all(files.map(file => cssMinify(file, false)))
resolve()
})
}
async function libMinifyJs(dst) {
return await new Promise(async resolve => {
const files = await globby([posix.join(dst, '**', '*.js'), posix.join('!' + dst, '**', '*.min.js')])
await Promise.all(files.map(file => jsMinify(file, false)))
resolve()
})
}
async function toggleSassJs(enable) {
return await new Promise(resolve => {
const bin = join('node_modules', '.bin')
const sassBin = join(bin, 'sass')
const sassCmd = join(bin, 'sass.cmd')
const _sassBin = join(bin, '_sass')
const _sassCmd = join(bin, '_sass.cmd')
if (enable) {
fs.existsSync(_sassBin) && fs.renameSync(_sassBin, sassBin)
fs.existsSync(_sassCmd) && fs.renameSync(_sassCmd, sassCmd)
}
else {
fs.existsSync(sassBin) && fs.renameSync(sassBin, _sassBin)
fs.existsSync(sassCmd) && fs.renameSync(sassCmd, _sassCmd)
}
resolve()
})
}
void (async () => {
if (argv('dev')) {
await setAssetAll(false)
chokidar.watch(htmlSrc, { ignoreInitial: true }).on('all', (event, target) => {
setTimeout(async () => {
if ((event === 'add' || event === 'change') && target !== htmlData) {
const filename = basename(target)
filename.startsWith('_') ? await htmlPartial(filename) : await html(filename)
}
else {
await htmlAll()
}
}, delay)
})
chokidar.watch(cssSrc, { ignoreInitial: true }).on('all', (event, target) => {
setTimeout(async () => {
if (event === 'add' || event === 'change') {
const filename = basename(target)
filename.startsWith('_') ? await cssPartial(filename) : await css(filename)
}
else {
await cssAll()
}
}, delay)
})
chokidar.watch(jsSrc, { ignoreInitial: true }).on('all', (event, target) => {
setTimeout(async () => {
if (event === 'add' || event === 'change') {
const filename = basename(target)
filename.startsWith('_') ? await jsPartial(filename) : await js(filename)
}
else {
await jsAll()
}
}, delay)
})
server.start(config.server)
console.log('Ready for changes\n'.blue)
}
else if (argv('lib')) {
const start = new Date()
await lib()
doneIn(start)
}
else {
const start = new Date()
await htmlAll()
await htmlBeautifyAll()
await setAssetAll()
await cssAll()
await cssAutoprefixMinify()
await jsAll()
await jsMinifyAll()
console.log('Build finished in'.green, new Date() - start, 'ms\n')
}
})()