forked from permaweb/ao
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathesbuild.js
More file actions
76 lines (71 loc) · 2 KB
/
esbuild.js
File metadata and controls
76 lines (71 loc) · 2 KB
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
import { readFileSync } from 'node:fs'
import * as esbuild from 'esbuild'
import { nodeModulesPolyfillPlugin } from 'esbuild-plugins-node-modules-polyfill'
/**
* By importing from manifest, build will always be in sync with the manifest
*/
const manifest = JSON.parse(readFileSync('./package.json'))
/**
* Some dependencies are ESM only, and so cannot be required from a CJS project.
* So for those dependencies, we make sure to include them in distribution bundle,
* so CJS projects can use the code, without having to import the dependency at runtime.
*
* ie. hyper-async
*/
function allDepsExcept (excluded = []) {
return Object.keys(manifest.dependencies).filter((dep) =>
!excluded.includes(dep)
)
}
// CJS
await esbuild.build({
entryPoints: ['src/index.js'],
platform: 'node',
format: 'cjs',
external: allDepsExcept(['hyper-async']),
bundle: true,
outfile: manifest.exports['.'].require
})
// ESM
await esbuild.build({
entryPoints: ['src/index.js'],
platform: 'node',
format: 'esm',
external: allDepsExcept(['hyper-async']),
bundle: true,
outfile: manifest.exports['.'].import
})
// Browser ESM
await esbuild.build({
entryPoints: ['src/index.browser.js'],
/**
* node-http-signatures uses the crypto module, and
* so we have to polyfill some node modules to build.
*
* This unforuntately massively bloats the browser bundle,
* so we should look at maybe PR'ing to remove the crypto
* module dep, or fork. Truly unfortunate.
*
* TODO: maybe we can just simply import createSignatureBase
* from 'node-http-signatures/cavage/index.js'
* and that will treeshake enough?
*
* "events" and "stream" have to be polyfilled for @dha-team/arbundles --
* really not great.
*/
plugins: [
nodeModulesPolyfillPlugin({
modules: {
crypto: true,
constants: true,
events: true,
stream: true
}
})
],
platform: 'browser',
format: 'esm',
bundle: true,
minify: true,
outfile: manifest.exports['.'].browser
})