-
Notifications
You must be signed in to change notification settings - Fork 2
/
build.js
45 lines (38 loc) · 1.74 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
const fs = require('fs')
const browserify = require('browserify')
const { pipeline } = require('stream')
// "autogen": "--plugin [ lavamoat-browserify --writeAutoConfig --config ./lavamoat-config.json ] > /dev/null",
// "default": "--plugin [ lavamoat-browserify --config ./lavamoat-config.json ] > bundle.js",
// "harden": "--plugin [ lavamoat-browserify --config ./lavamoat-config-harden.json ] > bundle.js",
// "unsafe": "> bundle.js",
const buildTarget = process.env.TARGET
const useLavamoat = buildTarget !== 'unsafe'
const configPath = buildTarget === 'harden' ? './lavamoat-config-harden.json' : './lavamoat-config.json'
const destPath = buildTarget === 'autogen' ? '/dev/null' : './bundle.js'
const writeAutoConfig = buildTarget === 'autogen'
// bugfix: globally replace readable-stream
const builtins = require('browserify/lib/builtins')
builtins['readable-stream/readable.js'] = require.resolve('readable-stream-patch/lib/_stream_readable.js')
builtins['readable-stream/writable.js'] = require.resolve('readable-stream-patch/lib/_stream_writable.js')
builtins['readable-stream/duplex.js'] = require.resolve('readable-stream-patch/lib/_stream_duplex.js')
builtins['readable-stream/transform.js'] = require.resolve('readable-stream-patch/lib/_stream_transform.js')
builtins['readable-stream/passthrough.js'] = require.resolve('readable-stream-patch/lib/_stream_passthrough.js')
const plugin = []
// add lavamoat
if (useLavamoat) {
plugin.push(['lavamoat-browserify', {
writeAutoConfig,
config: configPath,
}])
}
// configure bundler
const bundler = browserify(['./entry.js'], {
builtins,
plugin,
})
console.log({ buildTarget, useLavamoat, configPath, destPath, writeAutoConfig })
// build
pipeline(
bundler.bundle(),
fs.createWriteStream(destPath),
)