-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
replaceWithPlugin.ts
64 lines (55 loc) · 2.08 KB
/
replaceWithPlugin.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
import type * as esbuild from 'esbuild'
import fs from 'fs'
import { builtinModules } from 'module'
type Replacement = [RegExp, string | ((regex: RegExp, contents: string) => string | Promise<string>)]
async function applyReplacements(contents: string, replacements: Replacement[]) {
for (const [regex, replacement] of replacements) {
if (typeof replacement === 'string') {
contents = contents.replace(regex, replacement)
} else {
contents = await replacement(regex, contents)
}
}
return contents
}
/**
* Replace the contents of a file with the given replacements.
* @param replacements
* @returns
*/
export const replaceWithPlugin = (replacements: Replacement[]): esbuild.Plugin => {
return {
name: 'replaceWithPlugin',
setup(build) {
build.onLoad({ filter: /.*/ }, async (args) => {
// we skip, don't attempt to edit files that aren't js
if (builtinModules.includes(args.path)) return {}
if (!/.*?(.js|.mjs)$/.exec(args.path)) return {}
const contents = await fs.promises.readFile(args.path, 'utf8')
return { contents: await applyReplacements(contents, replacements) }
})
},
}
}
/**
* Example
*/
// const inlineUndiciWasm = replaceWithPlugin([
// [
// /(await WebAssembly\.compile\().*?'(.*?)'\)\)\)/g,
// async (regex, contents) => {
// for (const match of contents.matchAll(regex)) {
// if (match[2].includes('simd') === false) {
// // we only bundle lhttp wasm files that are not simd compiled
// const engineCoreDir = resolve.sync('@prisma/engine-core')
// const undiciPackage = resolve.sync('undici/package.json', { basedir: engineCoreDir })
// const lhttpWasmPath = path.join(path.dirname(undiciPackage), 'lib', match[2])
// const wasmContents = (await fs.promises.readFile(lhttpWasmPath)).toString('base64')
// const inlineWasm = `${match[1]}(Buffer.from("${wasmContents}", "base64")))`
// contents = contents.replace(match[0], inlineWasm)
// }
// }
// return contents
// },
// ],
// ])