-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.js
55 lines (44 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
const fs = require("fs");
const ncp = require('ncp').ncp;
const path = require("path");
const yargs = require('yargs/yargs');
const {hideBin} = require('yargs/helpers');
const babelify = require("babelify");
const watchify = require('watchify');
const browserify = require("browserify");
const browserifyShim = require("browserify-shim");
const argv = yargs(hideBin(process.argv)).argv;
// START Build options/paths
const srcDir = "src";
const distDir = "public/js/";
const mainJSPath = path.join(srcDir, "Main.js");
const bundleJSPath = path.join(distDir, "bundle.js");
// END Build options/paths
// Delete 'distDir'
if (fs.existsSync(distDir)) {
console.info("Deleting contents of build directory: %s", distDir);
fs.rmdirSync(distDir, {recursive: true});
}
fs.mkdirSync(distDir)
console.info("Browserifying...");
let iBrowserify = browserify(mainJSPath, {cache: {}, packageCache: {}});
applyTransforms(iBrowserify);
bundle();
if (argv.watchify) {
console.info("Watching JS file: " + mainJSPath);
iBrowserify.plugin(watchify);
iBrowserify.on("update", bundle);
iBrowserify.on('log', function (msg) {
console.log(msg)
});
}
function applyTransforms(iBrowserify) {
iBrowserify.transform(babelify, {
presets: ["@babel/preset-env"]
}).transform(browserifyShim, {
global: true
})
}
function bundle() {
iBrowserify.bundle().on("error", console.error).pipe(fs.createWriteStream(bundleJSPath));
}