-
Notifications
You must be signed in to change notification settings - Fork 27
/
build-js.js
69 lines (60 loc) · 2.01 KB
/
build-js.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
'use strict';
const fs = require('fs');
const path = require('path');
const mkdirp = require('mkdirp');
const pify = require('pify');
const UglifyJS = require('uglify-js');
const optimizeJs = require('optimize-js');
const timelog = require('./timelog');
const buildSvgLoader = require('./build-svg-loader');
const jsSrcDir = path.join(__dirname, '../src/js');
// If you create a new script for assembly.js you must add it here
const jsFiles = [
path.join(jsSrcDir, 'focus-control.js'),
path.join(jsSrcDir, 'icon-functions.js'),
];
/**
* Build JS.
*
* @param {Object} [options]
* @param {string} [options.outfile] - Path to which built JS should be written.
* @param {Object} [options.quiet] - Suppress logs.
* @param {Array<string>} [options.icons] - Array of icon names to include in the
* loader. icon names correspond to SVG file names excluding the `.svg` suffix.
* @return {Promise<void>}
*/
function buildJs(options) {
options = options || {};
if (!options.quiet) timelog('Building JS');
const outfile = options.outfile || path.join(__dirname, '../dist/assembly.js');
return Promise.all([
buildSvgLoader(options.icons || []),
concatJs()
])
.then((data) => {
const allJs = data.join('');
if (options.unminified) return allJs;
const minifiedJs = UglifyJS.minify(allJs, { fromString: true }).code;
return optimizeJs(minifiedJs);
})
.then((optimizedJs) => {
return pify(mkdirp)(path.dirname(outfile)).then(() => {
return pify(fs.writeFile)(outfile, optimizedJs);
});
}).then(() => {
if (!options.quiet) timelog('Done building JS');
});
}
function concatJs() {
const result = [];
// Deterministic order needed for tests
return Promise.all(jsFiles.map((jsFile, index) => {
return pify(fs.readFile)(jsFile, 'utf8').then((jsFileContent) => {
result[index] = jsFileContent;
});
})).then(() => result.join(''));
}
module.exports = buildJs;
if (require.main === module) {
buildJs().catch((err) => console.error(err.stack));
}