-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
74 lines (64 loc) · 2.6 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
(async function() {
const fs = require('fs-extra');
const rm = require('rimraf');
const path = require('path');
const zip = require('cross-zip');
const walk = require('klaw-sync');
const minifyHTML = require('html-minifier').minify;
const minifyJS = require('terser').minify;
const webext = require('web-ext');
const NAME = require('./package.json').name;
const VERSION = require('./package.json').version;
const BUILD_DIR = path.resolve(__dirname, `./${NAME}-${VERSION}`);
const SRC_AMO_DIR = `${BUILD_DIR}-src`;
// Remove potential artifacts from previous builds
rm.sync('*.zip');
// Create a folder to package the web extension
fs.emptyDirSync(BUILD_DIR);
// Copy the source and assets
console.info('Copying source & assets...');
copy(['./src', './icons', './manifest.json'], BUILD_DIR);
// Remove the '--dev' suffix in the extension ID
fs.writeFileSync(`${BUILD_DIR}/manifest.json`, fs.readFileSync(`${BUILD_DIR}/manifest.json`).toString().replace('--dev', ''));
// Create a zipped copy of the unminified source with some meta files for AMO
console.info('Creating zipped source dir for AMO...');
fs.ensureDirSync(SRC_AMO_DIR);
fs.copySync(BUILD_DIR, SRC_AMO_DIR);
copy(['./package.json', 'LICENSE', 'README.md'], SRC_AMO_DIR);
zip.zipSync(SRC_AMO_DIR, `${SRC_AMO_DIR}.zip`);
// Minify all the HTML and JS files in the build dir
await Promise.all(walk(BUILD_DIR).map(async function(file) {
switch (path.parse(file.path).ext) {
case '.js': fs.writeFileSync(file.path, (await minifyJS(fs.readFileSync(file.path).toString())).code); break;
case '.html': fs.writeFileSync(file.path, minifyHTML(fs.readFileSync(file.path).toString(), {
decodeEntities: true,
collapseBooleanAttributes: true,
collapseWhitespace: true,
collapseInlineTagWhitespace: true,
conservativeCollapse: true,
}));
break;
}
}));
// Package the extension using web-ext
console.info('Packaging extension using web-ext...');
webext.cmd.build({
noInput: true,
sourceDir: BUILD_DIR,
artifactsDir: __dirname,
overwriteDest: true,
}, { shouldExitProgram: false,
}).then(function() {
// Clean up
fs.removeSync(BUILD_DIR);
fs.removeSync(SRC_AMO_DIR);
});
function copy(sources, dest) {
(Array.isArray(sources) ? sources : [sources]).forEach(function(src) {
const srcpath = path.resolve(__dirname, src);
const target = `${dest}/${path.parse(srcpath).base}`;
if (fs.lstatSync(srcpath).isDirectory()) fs.ensureDirSync(target);
fs.copySync(srcpath, target);
});
}
})();