-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.js
56 lines (51 loc) · 1.43 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import { minify as minifyCSS } from 'csso';
import { minify as minifyHTML } from 'html-minifier-terser';
import { minify as minifyJS } from 'terser';
import { readFileSync, writeFileSync } from 'fs';
const cssInputFilename = 'src/index.css';
const htmlInputFilename = 'src/index.html';
const jsInputFilename = 'src/index.js';
const outputFilename = 'index.js';
const cssToken = '{{COMPONENT_CSS}}';
const htmlToken = '{{COMPONENT_HTML}}';
const cssOptions = {
restructure: false,
};
const htmlOptions = {
collapseWhitespace: true,
};
const jsOptions = {
mangle: {
toplevel: true,
properties: {
reserved: [
'preventScroll',
'connectedCallback',
'attributeChangedCallback',
'observedAttributes',
],
},
},
};
const cssSource = readFileSync(cssInputFilename).toString();
const htmlSource = readFileSync(htmlInputFilename).toString();
const jsSource = readFileSync(jsInputFilename).toString();
const debug = process.argv[2] === '--debug';
if (debug) {
writeFileSync(outputFilename, jsSource.replace(cssToken, cssSource).replace(htmlToken, htmlSource));
}
else {
const minifiedCSS = minifyCSS(
cssSource,
cssOptions,
).css;
const minifiedHTML = await minifyHTML(
htmlSource,
htmlOptions,
);
const minifiedJS = await minifyJS(
jsSource.replace(cssToken, minifiedCSS).replace(htmlToken, minifiedHTML),
jsOptions,
);
writeFileSync(outputFilename, minifiedJS.code);
}