-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
build.js
133 lines (121 loc) · 3.93 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const alias = require('esbuild-plugin-alias')
const packageJson = require('./package.json')
const esbuild = require('esbuild')
const fs = require('fs')
const developmentMode = process.argv.includes('--dev')
const formatFilters = process.argv.filter(e => e.startsWith('--only=')).map(e => e.substr(7))
const formats = [{
// to be loaded with <script>
name: 'browser-umd',
bundleExternals: true,
minifyResult: true,
entryPoints: ['lib/mega.js'],
bundleFormat: 'iife',
globalName: 'mega',
platform: 'browser',
targets: { browsers: 'defaults' }
}, {
// to be loaded with ES Module compatible loader
name: 'browser-es',
bundleExternals: true,
minifyResult: false,
entryPoints: ['lib/mega.mjs'],
bundleFormat: 'esm',
platform: 'browser',
targets: {
// Only browsers that support <script type="module"> are supported because
// usually when ES modules are loaded in older browsers a transpiler is used.
// Data from https://caniuse.com/#feat=es6-module
browsers: [
'Edge >= 16',
'Firefox >= 60',
'Chrome >= 64',
'Safari >= 11'
]
}
}, {
// to allow the old commonjs usage
name: 'node-cjs',
bundleExternals: false,
minifyResult: false,
entryPoints: ['lib/mega.js'],
bundleFormat: 'cjs',
platform: 'node'
}, {
// to be loaded with ES Module compatible loader
name: 'node-es',
bundleExternals: false,
minifyResult: false,
entryPoints: ['lib/mega.mjs'],
bundleFormat: 'esm',
platform: 'node'
}]
async function doBundle (format) {
const result = await esbuild.build({
entryPoints: format.entryPoints,
bundle: true,
define: {
'process.env.IS_BROWSER_BUILD': JSON.stringify(format.name.includes('browser')),
'process.env.PACKAGE_VERSION': JSON.stringify(packageJson.version)
},
format: format.bundleFormat,
globalName: format.globalName,
minify: !developmentMode && format.minifyResult,
platform: format.platform,
sourcemap: developmentMode && 'inline',
inject: format.bundleExternals
? ['./browser/process-shim.mjs']
: [],
external: format.bundleExternals
? []
: [
'abort-controller',
'agentkeepalive',
'multistream',
'node-fetch',
'crypto',
'events',
'secure-random',
'stream',
'pumpify',
'stream-skip',
'through'
],
plugins: !format.bundleExternals
? []
: [alias({
http: require.resolve('./browser/noop.mjs'),
https: require.resolve('./browser/noop.mjs'),
'abort-controller': require.resolve('./browser/noop.mjs'),
'node-fetch': require.resolve('./browser/fetch.mjs'),
'./crypto/rsa.mjs': require.resolve('./browser/rsa.mjs'),
'./aes.mjs': require.resolve('./browser/aes.mjs'),
stream: require.resolve('readable-stream')
})],
write: false
})
const ext = format.bundleFormat === 'esm' ? 'mjs' : 'js'
return fs.promises.writeFile('dist/main.' + format.name + '.' + ext, result.outputFiles[0].contents)
}
async function doBuild () {
console.error('Starting', developmentMode ? 'development' : 'production', 'build...')
console.error('Building 0 of %d', formats.length)
// Make dist folder if not exists
await fs.promises.mkdir('dist').catch(error => {
if (error.code !== 'EEXIST') throw error
})
for (let index = 0; index < formats.length; index++) {
const format = formats[index]
if (formatFilters.length > 0 && !formatFilters.includes(format.name)) continue
// return the previous line (A), then to the first character (G), clean the line (2K) and print state
console.log('\x1b[A\x1b[G\x1b[2KBuilding %d of %d: %s', index + 1, formats.length, format.name)
await doBundle(format)
}
console.log('Build completed with success')
}
function handleBuildErrors (error) {
console.error(error.stack || error)
process.exit(1)
}
// Init build
doBuild().catch(handleBuildErrors)