diff --git a/.changeset/gentle-ladybugs-tap.md b/.changeset/gentle-ladybugs-tap.md new file mode 100644 index 000000000000..373940bd7a9e --- /dev/null +++ b/.changeset/gentle-ladybugs-tap.md @@ -0,0 +1,7 @@ +--- +"wrangler": patch +--- + +fix: resolve asset handler for `--experimental-path` + +In https://github.com/cloudflare/wrangler2/pull/1241, we removed the vendored version of `@cloudflare/kv-asset-handler`, as well as the build configuration that would point to the vendored version when compining a worker using `--experimental-public`. However, wrangler can be used where it's not installed in the `package.json` for the worker, or even when there's no package.json at all (like when wrangler is installed globally, or used with `npx`). In this situation, if the user doesn't have `@cloudflare/kv-asset-handler` installed, then building the worker will fail. We don't want to make the user install this themselves, so instead we point to a barrel import for the library in the facade for the worker. diff --git a/packages/wrangler/kv-asset-handler.js b/packages/wrangler/kv-asset-handler.js new file mode 100644 index 000000000000..efa97cbd59f1 --- /dev/null +++ b/packages/wrangler/kv-asset-handler.js @@ -0,0 +1 @@ +export * from "@cloudflare/kv-asset-handler"; diff --git a/packages/wrangler/package.json b/packages/wrangler/package.json index 313d165fd1ca..c1846776d69b 100644 --- a/packages/wrangler/package.json +++ b/packages/wrangler/package.json @@ -114,6 +114,7 @@ "templates", "vendor", "import_meta_url.js", + "kv-asset-handler.js", "Cloudflare_CA.pem" ], "scripts": { diff --git a/packages/wrangler/src/__tests__/publish.test.ts b/packages/wrangler/src/__tests__/publish.test.ts index 9454c86a6622..91815ac68e24 100644 --- a/packages/wrangler/src/__tests__/publish.test.ts +++ b/packages/wrangler/src/__tests__/publish.test.ts @@ -1404,6 +1404,42 @@ addEventListener('fetch', event => {});` expect(std.err).toMatchInlineSnapshot(`""`); }); + it("should upload all the files in the directory specified by `--experimental-public`", async () => { + 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", + }; + writeWranglerToml({ + main: "./index.js", + }); + writeWorkerSource(); + writeAssets(assets); + mockUploadWorkerRequest({ + expectedMainModule: "stdin.js", + }); + mockSubDomainRequest(); + mockListKVNamespacesRequest(kvNamespace); + mockKeyListRequest(kvNamespace.id, []); + mockUploadAssetsToKVRequest(kvNamespace.id, assets); + await runWrangler("publish --experimental-public assets"); + + expect(std.out).toMatchInlineSnapshot(` + "Reading file-1.txt... + Uploading as file-1.2ca234f380.txt... + Reading file-2.txt... + Uploading as file-2.5938485188.txt... + ↗️ Done syncing assets + Uploaded test-name (TIMINGS) + Published test-name (TIMINGS) + test-name.test-sub-domain.workers.dev" + `); + expect(std.err).toMatchInlineSnapshot(`""`); + }); + it("should not contain backslash for assets with nested directories", async () => { const assets = [ { filePath: "subdir/file-1.txt", content: "Content of file-1" }, @@ -5311,6 +5347,7 @@ function mockUploadWorkerRequest( options: { available_on_subdomain?: boolean; expectedEntry?: string; + expectedMainModule?: string; expectedType?: "esm" | "sw"; expectedBindings?: unknown; expectedModules?: Record; @@ -5324,6 +5361,7 @@ function mockUploadWorkerRequest( const { available_on_subdomain = true, expectedEntry, + expectedMainModule = "index.js", expectedType = "esm", expectedBindings, expectedModules = {}, @@ -5358,7 +5396,7 @@ function mockUploadWorkerRequest( formBody.get("metadata") as string ) as WorkerMetadata; if (expectedType === "esm") { - expect(metadata.main_module).toEqual("index.js"); + expect(metadata.main_module).toEqual(expectedMainModule); } else { expect(metadata.body_part).toEqual("index.js"); } diff --git a/packages/wrangler/src/bundle.ts b/packages/wrangler/src/bundle.ts index 2572fffdaf09..2f1ba156881b 100644 --- a/packages/wrangler/src/bundle.ts +++ b/packages/wrangler/src/bundle.ts @@ -175,7 +175,14 @@ function getEntryPoint( path.join(__dirname, "../templates/static-asset-facade.js"), "utf8" ) - .replace("__ENTRY_POINT__", entryFile), + // on windows, escape backslashes in the path (`\`) + .replace("__ENTRY_POINT__", entryFile.replaceAll("\\", "\\\\")) + .replace( + "__KV_ASSET_HANDLER__", + path + .join(__dirname, "../kv-asset-handler.js") + .replaceAll("\\", "\\\\") + ), sourcefile: "static-asset-facade.js", resolveDir: path.dirname(entryFile), }, diff --git a/packages/wrangler/templates/static-asset-facade.js b/packages/wrangler/templates/static-asset-facade.js index 06412925d1df..8c932dceeb16 100644 --- a/packages/wrangler/templates/static-asset-facade.js +++ b/packages/wrangler/templates/static-asset-facade.js @@ -1,6 +1,6 @@ // DO NOT IMPORT THIS DIRECTLY import worker from "__ENTRY_POINT__"; -import { getAssetFromKV } from "@cloudflare/kv-asset-handler"; +import { getAssetFromKV } from "__KV_ASSET_HANDLER__"; import manifest from "__STATIC_CONTENT_MANIFEST"; const ASSET_MANIFEST = JSON.parse(manifest);