Skip to content

Commit 699bb48

Browse files
committed
feat: support generate for esbuild
1 parent 0b91e5c commit 699bb48

File tree

5 files changed

+166
-116
lines changed

5 files changed

+166
-116
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"dependencies": {
9393
"@rollup/pluginutils": "^5.1.0",
9494
"oxc-transform": "^0.21.0",
95-
"unplugin": "^1.11.0"
95+
"unplugin": "^1.11.1"
9696
},
9797
"devDependencies": {
9898
"@swc/core": "^1.7.0",

pnpm-lock.yaml

Lines changed: 71 additions & 55 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

Lines changed: 57 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -89,55 +89,67 @@ export const IsolatedDecl: UnpluginInstance<Options | undefined, false> =
8989
addOutput(id, sourceText)
9090
},
9191

92-
// esbuild only
93-
async buildEnd() {
94-
if (meta.framework === 'esbuild') {
95-
const esbuildOptions = meta.build!.initialOptions
96-
97-
const entries = esbuildOptions.entryPoints
98-
if (
99-
!(
100-
entries &&
101-
Array.isArray(entries) &&
102-
entries.every((entry) => typeof entry === 'string')
92+
esbuild: {
93+
setup(build) {
94+
build.onEnd(async (result) => {
95+
const esbuildOptions = build.initialOptions
96+
97+
const entries = esbuildOptions.entryPoints
98+
if (
99+
!(
100+
entries &&
101+
Array.isArray(entries) &&
102+
entries.every((entry) => typeof entry === 'string')
103+
)
103104
)
104-
)
105-
throw new Error('unsupported entryPoints, must be an string[]')
106-
107-
const outBase = lowestCommonAncestor(...entries)
108-
const jsExt = esbuildOptions.outExtension?.['.js']
109-
let outExt: string
110-
switch (jsExt) {
111-
case '.cjs':
112-
outExt = 'cts'
113-
break
114-
case '.mjs':
115-
outExt = 'mts'
116-
break
117-
default:
118-
outExt = 'ts'
119-
break
120-
}
105+
throw new Error('unsupported entryPoints, must be an string[]')
106+
107+
const outBase = lowestCommonAncestor(...entries)
108+
const jsExt = esbuildOptions.outExtension?.['.js']
109+
let outExt: string
110+
switch (jsExt) {
111+
case '.cjs':
112+
outExt = 'cts'
113+
break
114+
case '.mjs':
115+
outExt = 'mts'
116+
break
117+
default:
118+
outExt = 'ts'
119+
break
120+
}
121121

122-
const build = meta.build!
123-
if (
124-
build.initialOptions.outdir &&
125-
(build.initialOptions.write ?? true)
126-
)
127-
for (const [filename, source] of Object.entries(outputFiles)) {
128-
const outFile = `${path.relative(outBase, filename)}.d.${outExt}`
129-
130-
const filePath = path.resolve(
131-
build.initialOptions.outdir,
132-
outFile,
133-
)
134-
await mkdir(path.dirname(filePath), { recursive: true })
135-
await writeFile(filePath, source)
122+
if (build.initialOptions.write ?? true) {
123+
if (!build.initialOptions.outdir)
124+
throw new Error('outdir is required when write is true')
125+
126+
for (const [filename, source] of Object.entries(outputFiles)) {
127+
const outFile = `${path.relative(outBase, filename)}.d.${outExt}`
128+
129+
const filePath = path.resolve(
130+
build.initialOptions.outdir,
131+
outFile,
132+
)
133+
await mkdir(path.dirname(filePath), { recursive: true })
134+
await writeFile(filePath, source)
135+
}
136+
} else {
137+
result.outputFiles ||= []
138+
const textEncoder = new TextEncoder()
139+
140+
for (const [filename, source] of Object.entries(outputFiles)) {
141+
const outFile = `${path.relative(outBase, filename)}.d.${outExt}`
142+
result.outputFiles.push({
143+
path: outFile,
144+
contents: textEncoder.encode(source),
145+
hash: '',
146+
text: source,
147+
})
148+
}
136149
}
137-
}
150+
})
151+
},
138152
},
139-
140-
// esbuild,
141153
rollup,
142154
rolldown: rollup,
143155
vite: {

tests/__snapshots__/esbuild.test.ts.snap

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`esbuild 1`] = `
3+
exports[`esbuild > generate mode 1`] = `
4+
"export type Str = string;
5+
export declare function hello(s: Str): Str;
6+
"
7+
`;
8+
9+
exports[`esbuild > write mode 1`] = `
410
"export type Str = string;
511
export declare function hello(s: Str): Str;
612
"

0 commit comments

Comments
 (0)