Skip to content

Commit

Permalink
Preserve entrypoint filename when building a Worker with --outdir (#2836
Browse files Browse the repository at this point in the history
)

* Preserve entrypoint filename when building a Worker with --outdir

* Apply suggestions from code review

Co-authored-by: Pete Bacon Darwin <pete@bacondarwin.com>

---------

Co-authored-by: Pete Bacon Darwin <pete@bacondarwin.com>
  • Loading branch information
GregBrimble and petebacondarwin committed Mar 7, 2023
1 parent 8d462c0 commit 42fb97e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 7 deletions.
7 changes: 7 additions & 0 deletions .changeset/lucky-peas-compete.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: preserve the entrypoint filename when running `wrangler publish --outdir <dir>`.

Previously, this entrypoint filename would sometimes be overwritten with some internal filenames. It should now be based off of the entrypoint you provide for your Worker.
53 changes: 47 additions & 6 deletions packages/wrangler/src/__tests__/publish.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1649,7 +1649,7 @@ addEventListener('fetch', event => {});`
};
writeAssets(assets);
mockUploadWorkerRequest({
expectedMainModule: "serve-static-assets.entry.js",
expectedMainModule: "no-op-worker.js",
});
mockSubDomainRequest();
mockListKVNamespacesRequest(kvNamespace);
Expand Down Expand Up @@ -1740,7 +1740,7 @@ addEventListener('fetch', event => {});`
writeWorkerSource();
writeAssets(assets);
mockUploadWorkerRequest({
expectedMainModule: "serve-static-assets.entry.js",
expectedMainModule: "index.js",
});
mockSubDomainRequest();
mockListKVNamespacesRequest(kvNamespace);
Expand Down Expand Up @@ -1925,7 +1925,7 @@ addEventListener('fetch', event => {});`
writeWorkerSource();
writeAssets(assets);
mockUploadWorkerRequest({
expectedMainModule: "serve-static-assets.entry.js",
expectedMainModule: "index.js",
});
mockSubDomainRequest();
mockListKVNamespacesRequest(kvNamespace);
Expand Down Expand Up @@ -1972,7 +1972,7 @@ addEventListener('fetch', event => {});`
writeWorkerSource();
writeAssets(assets);
mockUploadWorkerRequest({
expectedMainModule: "serve-static-assets.entry.js",
expectedMainModule: "index.js",
});
mockSubDomainRequest();
mockListKVNamespacesRequest(kvNamespace);
Expand Down Expand Up @@ -3015,7 +3015,7 @@ addEventListener('fetch', event => {});`
writeWorkerSource();
writeAssets(assets, "my-assets");
mockUploadWorkerRequest({
expectedMainModule: "serve-static-assets.entry.js",
expectedMainModule: "index.js",
});
mockSubDomainRequest();
mockListKVNamespacesRequest(kvNamespace);
Expand Down Expand Up @@ -5846,7 +5846,7 @@ addEventListener('fetch', event => {});`
"
`);
const output = fs.readFileSync("tmp/d1-beta-facade.entry.js", "utf-8");
const output = fs.readFileSync("tmp/index.js", "utf-8");
expect(output).toContain(
`var ExampleDurableObject2 = maskDurableObjectDefinition(ExampleDurableObject);`
);
Expand Down Expand Up @@ -6579,6 +6579,47 @@ addEventListener('fetch', event => {});`
`);
});

it("should preserve the entry point file name, even when using a facade", async () => {
writeWranglerToml();
writeWorkerSource();
mockSubDomainRequest();
mockUploadWorkerRequest();
const assets = [
{ filePath: "file-1.txt", content: "Content of file-1" },
{ filePath: "file-2.txt", content: "Content of file-2" },
];
const kvNamespace = {
title: "__test-name-workers_sites_assets",
id: "__test-name-workers_sites_assets-id",
};
writeAssets(assets);
mockListKVNamespacesRequest(kvNamespace);
mockKeyListRequest(kvNamespace.id, []);
mockUploadAssetsToKVRequest(kvNamespace.id, assets);
await runWrangler("publish index.js --outdir some-dir --assets assets");
expect(fs.existsSync("some-dir/index.js")).toBe(true);
expect(fs.existsSync("some-dir/index.js.map")).toBe(true);
expect(std).toMatchInlineSnapshot(`
Object {
"debug": "",
"err": "",
"out": "Reading file-1.txt...
Uploading as file-1.2ca234f380.txt...
Reading file-2.txt...
Uploading as file-2.5938485188.txt...
↗️ Done syncing assets
Total Upload: xx KiB / gzip: xx KiB
Uploaded test-name (TIMINGS)
Published test-name (TIMINGS)
https://test-name.test-sub-domain.workers.dev
Current Deployment ID: Galaxy-Class",
"warn": "▲ [WARNING] The --assets argument is experimental and may change or break at any time
",
}
`);
});

it("should copy any module imports related assets to --outdir if specified", async () => {
writeWranglerToml();
fs.writeFileSync(
Expand Down
2 changes: 1 addition & 1 deletion packages/wrangler/src/bundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ export async function bundleWorker(
bundle: true,
absWorkingDir: entry.directory,
outdir: destination,
entryNames: entryName,
entryNames: entryName || path.parse(entry.file).name,
...(isOutfile
? {
outdir: undefined,
Expand Down

0 comments on commit 42fb97e

Please sign in to comment.