Skip to content

Commit cb4065e

Browse files
committed
build: extract esbuild helpers
1 parent 9a194de commit cb4065e

File tree

3 files changed

+56
-29
lines changed

3 files changed

+56
-29
lines changed

package-lock.json

+26-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@
9393
"esbuild-node-externals": "^1.13.0",
9494
"esbuild-plugin-entry-chunks": "^0.1.12",
9595
"esbuild-plugin-hybrid-export": "^0.2.0",
96-
"esbuild-plugin-transform-hook": "^0.0.0",
96+
"esbuild-plugin-transform-hook": "^0.0.1",
97+
"esbuild-plugin-extract-helpers": "^0.0.1",
9798
"fs-extra": "^11.2.0",
9899
"fx": "*",
99100
"globby": "^14.0.1",

scripts/build-js.mjs

+28-24
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import { nodeExternalsPlugin } from 'esbuild-node-externals'
2020
import { entryChunksPlugin } from 'esbuild-plugin-entry-chunks'
2121
import { hybridExportPlugin } from 'esbuild-plugin-hybrid-export'
2222
import { transformHookPlugin } from 'esbuild-plugin-transform-hook'
23+
import { extractHelpersPlugin } from 'esbuild-plugin-extract-helpers'
2324
import minimist from 'minimist'
2425
import glob from 'fast-glob'
2526

@@ -50,30 +51,7 @@ const {
5051
cwd: _cwd,
5152
} = argv
5253

53-
const plugins = [
54-
transformHookPlugin({
55-
hooks: [
56-
{
57-
on: 'end',
58-
pattern: /\.cjs/,
59-
transform(contents) {
60-
const annotationIdx = contents.indexOf(
61-
'// Annotate the CommonJS export names for ESM import in node:'
62-
)
63-
return contents
64-
.slice(0, annotationIdx > 0 ? annotationIdx : contents.length)
65-
.replaceAll('"node:', '"')
66-
.replaceAll(
67-
'require("stream/promises")',
68-
'require("stream").promises'
69-
)
70-
.replaceAll('require("fs/promises")', 'require("fs").promises')
71-
.replaceAll('}).prototype', '}).prototype || {}')
72-
},
73-
},
74-
],
75-
}),
76-
]
54+
const plugins = []
7755
const cwd = Array.isArray(_cwd) ? _cwd[_cwd.length - 1] : _cwd
7856
const entries = entry.split(/,\s?/)
7957
const entryPoints = entry.includes('*')
@@ -103,6 +81,32 @@ if (hybrid) {
10381
)
10482
}
10583

84+
plugins.push(
85+
transformHookPlugin({
86+
hooks: [
87+
{
88+
on: 'end',
89+
pattern: /\.cjs/,
90+
transform(contents) {
91+
return contents
92+
.toString()
93+
.replaceAll('"node:', '"')
94+
.replaceAll(
95+
'require("stream/promises")',
96+
'require("stream").promises'
97+
)
98+
.replaceAll('require("fs/promises")', 'require("fs").promises')
99+
.replaceAll('}).prototype', '}).prototype || {}')
100+
},
101+
},
102+
],
103+
}),
104+
extractHelpersPlugin({
105+
cwd: 'build',
106+
include: /\.cjs/,
107+
})
108+
)
109+
106110
const formats = format.split(',')
107111
const banner =
108112
argv.banner && bundle === 'all'

0 commit comments

Comments
 (0)