/
build.ts
168 lines (158 loc) · 4.8 KB
/
build.ts
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { basename, join, parse, resolve } from 'path'
import { Options, build } from 'tsup'
import alias from 'esbuild-plugin-alias'
import fs from 'fs'
const BUNDLE_OPTIONS: Options = {
bundle: true,
keepNames: true,
format: ['cjs'],
platform: 'node',
}
async function bundlePackage() {
const entryFolder = resolve(__dirname, '../src/primitives')
const filesExt = await fs.promises.readdir(entryFolder)
const entryPoints = filesExt.map((file) => join(entryFolder, file))
const outdir = resolve(__dirname, '../dist')
await fs.promises.mkdir(outdir).catch(() => {})
await build({
bundle: true,
dts: {
resolve: true,
},
format: [],
target: ['node12.22'],
entry: filesExt
.map((f) => f.replace(/\.(js|ts)$/, '.d.ts'))
.map((f) => join(__dirname, '../type-definitions', f)),
outDir: resolve(__dirname, '../types'),
})
for (const file of await fs.promises.readdir(join(__dirname, '../types'))) {
const fullPath = join(__dirname, '../types', file)
await fs.promises.rename(fullPath, fullPath.replace(/\.d\.d\.ts$/, '.d.ts'))
}
await build({
...BUNDLE_OPTIONS,
entryPoints,
outDir: outdir,
minify: false,
target: ['node12.22'],
esbuildOptions(opts, _context) {
opts.legalComments = 'external'
},
define: {
process: JSON.stringify({ env: {}, versions: { node: '16.6.0' } }),
},
esbuildPlugins: [
alias({
buffer: resolve('src/patches/buffer.js'),
http: resolve('src/patches/http.js'),
'util/types': resolve('src/patches/util-types.js'),
}),
{
name: 'alias-undici-core-request',
setup: (build) => {
build.onResolve({ filter: /^\.\/core\/request$/ }, async (args) => {
// validate it's resolved by the expected path
if (args.importer.endsWith('node_modules/undici/lib/client.js')) {
return {
path: resolve('src/patches/undici-core-request.js'),
}
}
})
},
},
/**
* Make sure that depdendencies between primitives are consumed
* externally instead of being bundled. Also polyfills stream/web
* with the web streams polyfill.
*/
{
name: 'import-path',
setup: (build) => {
build.onResolve({ filter: /.*$/ }, ({ kind, importer, path }) => {
if (path === 'stream/web') {
return {
path: './streams',
external: true,
}
}
const fullpath = resolve(importer, '..', path)
const isEntry = entryPoints.includes(`${fullpath}.js`)
if (kind !== 'entry-point' && isEntry && path.startsWith('.')) {
return {
path: `./${basename(fullpath)}`,
external: true,
}
}
})
},
},
/**
* Make sure that undici has defined the FinalizationRegistry global
* available globally which is missing in older node.js versions.
*/
{
name: 'add-finalization-registry',
setup: (build) => {
build.onLoad(
{ filter: /undici\/lib\/fetch\/request/ },
async (args) => {
return {
contents: Buffer.concat([
Buffer.from(
`global.FinalizationRegistry = function () { return { register: function () {} } }`
),
await fs.promises.readFile(args.path),
]),
}
}
)
},
},
/**
* Modern Node.js versions include Blob globally which makes the Blob
* polyfill fail as if it was running on the browser so we attempt to
* remove if from `global` always.
*/
{
name: 'hide-builtin-blob',
setup: (build) => {
build.onLoad({ filter: /blob-polyfill/ }, async (args) => {
return {
contents: Buffer.concat([
Buffer.from(
`(() => { try { global.Blob = undefined; } catch {} })(); `
),
await fs.promises.readFile(args.path),
]),
}
})
},
},
],
})
for (const file of filesExt.map((file) => parse(file).name)) {
if (file !== 'index') {
await fs.promises.mkdir(resolve(__dirname, `../${file}`)).catch(() => {})
await fs.promises.writeFile(
resolve(__dirname, `../${file}/package.json`),
JSON.stringify(
{
main: `../dist/${file}.js`,
types: `../types/${file}.d.ts`,
},
null,
2
)
)
}
}
}
bundlePackage()
.then(() => {
process.exit(0)
})
.catch((error) => {
console.log('Errored', error)
process.exit(1)
})