-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
40 lines (37 loc) · 1.35 KB
/
build.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
/* eslint-env node */
const { readFileSync, writeFileSync, readdirSync, existsSync, mkdirSync } = require('fs')
const path = require('path')
const { minify } = require('terser')
const cheerio = require('cheerio')
// Create dist/ folder with uifactory.min.js and components
async function build() {
let srcRoot = path.join(__dirname, 'src')
let tgtRoot = path.join(__dirname, 'dist')
// Create dist/ if required
if (!existsSync(tgtRoot))
mkdirSync(tgtRoot)
// Build files
for (let filename of readdirSync(srcRoot)) {
let source = path.join(srcRoot, filename)
let target = path.join(tgtRoot, filename)
let contents = readFileSync(source, { encoding: 'utf-8' })
// Compress all JS files into .min.js
if (filename.match(/\.js$/i)) {
let result = await minify(contents, {
sourceMap: {
filename: filename,
url: filename + '.map'
}
})
writeFileSync(target.replace(/\.js$/i, '.min.js'), result.code, { encoding: 'utf8' })
writeFileSync(target + '.map', result.map, { encoding: 'utf8' })
}
// Build all HTML files as components. TODO: Minify them
else if (filename.match(/\.html$/i)) {
let $ = cheerio.load(contents)
contents = $('template').map((i, v) => $.html(v)).get().join('\n')
writeFileSync(target, contents, { encoding: 'utf8' })
}
}
}
build()