forked from mqttjs/MQTT.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
esbuild.js
87 lines (75 loc) · 2.38 KB
/
esbuild.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
const { build } = require('esbuild')
const { polyfillNode } = require('esbuild-plugin-polyfill-node');
const { rimraf } = require('rimraf')
const fs = require('fs')
const { version } = require('./package.json');
const outdir = 'dist'
/**
* @type {import('esbuild').BuildOptions}
*/
const options = {
entryPoints: ['build/index.js'],
bundle: true,
outfile: `${outdir}/mqtt.js`,
format: 'iife',
platform: 'browser',
globalName: 'mqtt',
sourcemap: false, // this can be enabled while debugging, if we decide to keep this enabled we should also ship the `src` folder to npm
plugins: [
polyfillNode({
polyfills: [
'readable-stream'
],
globals: {
global: false,
__dirname: false,
__filename: false,
buffer: true,
process: true,
navigator: true, // Needed for WeChat, ref #1789
}
}),
{
name: 'resolve-package-json',
setup(build) {
// when importing 'package.json' we want to provide a custom object like { version: '1.2.3' }
build.onResolve({ filter: /package\.json$/ }, args => {
return {
path: args.path,
namespace: 'package-json'
}
})
build.onLoad({ filter: /.*/, namespace: 'package-json' }, args => {
return {
contents: JSON.stringify({ version }),
loader: 'json'
}
}
)
}
},
],
}
async function run() {
const start = Date.now()
await rimraf(outdir)
await build(options)
options.minify = true
options.outfile = `${outdir}/mqtt.min.js`
await build(options)
options.outfile = `${outdir}/mqtt.esm.js`
options.format = 'esm'
await build(options)
console.log(`Build time: ${Date.now() - start}ms`)
console.log('Build output:')
// log generated files with their size in KB
const files = fs.readdirSync(outdir)
for (const file of files) {
const stat = fs.statSync(`${outdir}/${file}`)
console.log(`- ${file} ${Math.round(stat.size / 1024 * 100) / 100} KB`)
}
}
run().catch((e) => {
console.error(e)
process.exit(1)
})