-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
91 lines (83 loc) · 2.59 KB
/
build.js
File metadata and controls
91 lines (83 loc) · 2.59 KB
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
const minify = require('@node-minify/core');
const terser = require('@node-minify/terser');
const cleanCSS = require('@node-minify/clean-css');
const htmlMini = require('@node-minify/html-minifier');
const glob = require("glob");
const fs = require('fs-extra');
const archiver = require('archiver');
const path = require('path');
function negativeArrayIndex(array, negativeIndex=1) {
return array[array.length-Math.abs(negativeIndex)];
}
// Adapted from https://www.npmjs.com/package/archiver
function packageZip() {
console.log('Zipping...');
// create a file to stream archive data to.
const output = fs.createWriteStream(__dirname + '/extension.zip');
const archive = archiver('zip', {
zlib: { level: 9 } // Sets the compression level.
});
// pipe archive data to the file
archive.pipe(output);
// append files from a sub-directory, putting its contents at the root of archive
archive.directory(__dirname + '/build/', false);
output.on('finish', function() {
console.log('Done!');
});
archive.finalize();
}
function minifyJSON(file) {
return new Promise((res, rej) => {
fs.readFile(file, (err, data) =>
void fs.writeFile(file.replace('src', 'build'), JSON.stringify(JSON.parse(data)), () => { res() })
);
});
}
// Make folders
fs.removeSync(__dirname + "/build");
fs.copySync(__dirname + "/src", __dirname + "/build", {
filter: (src, dest) => !['.ai', '.xcf'].includes(path.extname(src))
});
fs.copyFile(__dirname + '/LICENSE', __dirname + '/build/LICENSE');
glob("src/**/*.{html,js,css,json,svg}", function (er, files) {
const promises = [];
for (const file of files) {
console.log(file);
const dst = file.replace('src', 'build');
switch (negativeArrayIndex(file.split('.'))) {
case 'js':
promises.push(minify({
compressor: terser,
input: file,
output: dst,
options: {
compress: {drop_console: true}
}
})); break;
case 'css':
promises.push(minify({
compressor: cleanCSS,
input: file,
output: dst
})); break;
case 'json':
promises.push(minifyJSON(file)); break;
case 'html':
promises.push(minify({
compressor: htmlMini,
input: file,
output: dst
})); break;
case 'svg':
promises.push(minify({
compressor: htmlMini,
input: file,
output: dst,
options: {
removeAttributeQuotes: false
}
})); break;
}
}
Promise.all(promises).then(packageZip).catch(err => console.error(err));
});