Skip to content

Commit e5d3690

Browse files
fix: use entrypoint specified in esbuild's metafile as source for building the worker (#292)
* fix: use entrypoint specified in esbuuild's metafile as source for building the worker When we pass a non-js file as entry to esbuild, it generates a `.js` file. (which, that's the whole job of esbuild, haha). So, given `<source>/index.ts`, it'll generate `<destination>/index.js`. However, when we try to 'find' the matching file to pass on as an input to creating the actual worker, we try to use the original file name inside the destination directory. At this point, the extension has changed, so it doesn't find the file, and hence we get the error that looks like `ENOENT: no such file or directory, open '/var/folders/3f/fwp6mt7n13bfnkd5vl3jmh1w0000gp/T/tmp-61545-4Y5kwyNI8DGU/src/worker.ts'` The actual path to the destination file is actually the key of the block in `metafile.outputs` that matches the given output.entryPoint, so this PR simply rewrites the logic to use that instead. * Update .changeset/popular-weeks-march.md Co-authored-by: Pete Bacon Darwin <[email protected]> Co-authored-by: Pete Bacon Darwin <[email protected]>
1 parent b63efe6 commit e5d3690

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

.changeset/popular-weeks-march.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
fix: use entrypoint specified in esbuuild's metafile as source for building the worker
6+
7+
When we pass a non-js file as entry to esbuild, it generates a `.js` file. (which, is the whole job of esbuild, haha). So, given `<source>/index.ts`, it'll generate `<destination>/index.js`. However, when we try to 'find' the matching file to pass on as an input to creating the actual worker, we try to use the original file name inside the destination directory. At this point, the extension has changed, so it doesn't find the file, and hence we get the error that looks like `ENOENT: no such file or directory, open '/var/folders/3f/fwp6mt7n13bfnkd5vl3jmh1w0000gp/T/tmp-61545-4Y5kwyNI8DGU/src/worker.ts'`
8+
9+
The actual path to the destination file is actually the key of the block in `metafile.outputs` that matches the given output.entryPoint, so this PR simply rewrites the logic to use that instead.

packages/wrangler/src/publish.ts

+8-11
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,8 @@ export default async function publish(props: Props): Promise<void> {
140140
// result.metafile is defined because of the `metafile: true` option above.
141141
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
142142
const metafile = result.metafile!;
143-
const entryPoints = Object.values(metafile.outputs).filter(
144-
(output) => output.entryPoint !== undefined
143+
const entryPoints = Object.entries(metafile.outputs).filter(
144+
([_path, output]) => output.entryPoint !== undefined
145145
);
146146
assert(
147147
entryPoints.length > 0,
@@ -153,13 +153,8 @@ export default async function publish(props: Props): Promise<void> {
153153
"More than one entry-point found for generated bundle." +
154154
listEntryPoints(entryPoints)
155155
);
156-
const entryPointExports = entryPoints[0].exports;
157-
const resolvedEntryPointPath = path.resolve(
158-
destination.path,
159-
// We know that entryPoint is not null because we filtered out those without above.
160-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
161-
entryPoints[0].entryPoint!
162-
);
156+
const entryPointExports = entryPoints[0][1].exports;
157+
const resolvedEntryPointPath = entryPoints[0][0];
163158
const { format } = props;
164159
const bundle = {
165160
type: entryPointExports.length > 0 ? "esm" : "commonjs",
@@ -378,8 +373,10 @@ export default async function publish(props: Props): Promise<void> {
378373
}
379374
}
380375

381-
function listEntryPoints(outputs: ValueOf<Metafile["outputs"]>[]): string {
382-
return outputs.map((output) => output.entryPoint).join("\n");
376+
function listEntryPoints(
377+
outputs: [string, ValueOf<Metafile["outputs"]>][]
378+
): string {
379+
return outputs.map(([_input, output]) => output.entryPoint).join("\n");
383380
}
384381

385382
type ValueOf<T> = T[keyof T];

0 commit comments

Comments
 (0)