-
Notifications
You must be signed in to change notification settings - Fork 5
/
build.js
115 lines (105 loc) · 3.82 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
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
// This script contains all the steps for generating the output results.
// This can be triggered by running `npm run build`
//
// A majority of this file uses the UglifyJS API
// https://www.npmjs.com/package/uglify-js
//
const fs = require('fs');
const path = require('path');
const UglifyJS = require('uglify-js');
const { version } = require('./package.json');
// before we start anything, make sure the output directory exists and is empty
const outputDirectories = ['output', 'output/export', 'output/export/processors'];
outputDirectories.forEach((directory) => {
if (!fs.existsSync(directory)) {
// if it doesn't exist make it
fs.mkdirSync(directory);
} else {
// if it does, remove all files in the directory
const files = fs.readdirSync(directory);
for (const file of files) {
const filePath = path.join(directory, file);
try {
fs.unlinkSync(filePath);
} catch {
// if we tried to delete a directory, this will blow up
// but it's actually fine...
}
}
}
});
// load all source class files (these will be included in all builds)
// ORDER IS IMPORTANT! We need ComponentDefinition to be last, so that
// shadow root processors exist by the time we start processing templates
const processors = fs.readdirSync('src/processors').map((file) => `src/processors/${file}`);
const classFiles = ['src/TramLite.js', ...processors, 'src/ComponentDefinition.js'];
const loadedClassFiles = Object.fromEntries(
classFiles.map((filePath) => {
console.log('loading', filePath);
return [filePath, fs.readFileSync(filePath).toString()];
}),
);
// load all import/export scripts separately (these are only included in some builds)
console.log('loading', 'src/import-components.js');
const importComponentClass = {
'src/ImportComponent.js': fs.readFileSync('src/ImportComponent.js').toString(),
};
console.log('loading', 'src/import-script.js');
const importScript = {
'src/scripts/import-script.js': fs.readFileSync('src/scripts/import-script.js').toString(),
};
// uglify parameters to change the result of each bundle.
// `enclose` determines if the code should be wrapped in an IIFE (which prevents
// class definitions from colliding).
const buildConfigs = [
{
outputFile: 'output/tram-lite.js',
files: loadedClassFiles,
defines: { INSTALL: true },
},
{
outputFile: 'output/import-components.js',
files: { ...loadedClassFiles, ...importComponentClass, ...importScript },
defines: { INSTALL: false },
enclose: true,
},
];
buildConfigs.forEach((config) => {
console.log('building', config.outputFile);
const options = {
compress: {
global_defs: {
APP_VERSION: version,
...config.defines,
},
},
enclose: config.enclose,
output: {
comments: 'all',
beautify: true,
},
};
const result = UglifyJS.minify(config.files, options);
fs.writeFileSync(config.outputFile, result.code);
});
// for each of these, create a minified version
const minifyConfigs = [
{ inputFile: 'output/tram-lite.js', outputFile: 'output/tram-lite.min.js' },
{ inputFile: 'output/import-components.js', outputFile: 'output/import-components.min.js' },
// build files for use with the export-components script
...classFiles.map((classFile) => ({
inputFile: classFile,
outputFile: classFile.replace('src', 'output/export').replace('.js', '.min.js'),
})),
{ inputFile: 'src/ImportComponent.js', outputFile: 'output/export/ImportComponent.min.js' },
];
minifyConfigs.forEach((config) => {
console.log('minifying', config.outputFile);
const result = UglifyJS.minify(fs.readFileSync(config.inputFile, 'utf8'), {
compress: { global_defs: { APP_VERSION: version, INSTALL: false } },
});
fs.writeFileSync(config.outputFile, result.code);
});
// do a simple copy for the export-script (needs no minification)
fs.copyFileSync('src/scripts/export-script.js', 'output/export-components.js');
console.log('Tram-Lite build complete!');