Skip to content

Commit 1bae861

Browse files
authored
fix(vite-plugin): Support __ in additional module IDs (#9322)
* fix(vite-plugin): Anchor ADDITIONAL_MODULE_PATTERN regex to end of string ## Problem There's a regex used to detect if certain files (wasm, .bin, .txt, .html) are being imported at runtime. The current regex looks like this: ```ts const ADDITIONAL_MODULE_PATTERN = `__CLOUDFLARE_MODULE__(${ADDITIONAL_MODULE_TYPES.join("|")})__(.*?)__`; ``` This means if there are files with `__`s in them, those `__`s will get matched first, rather than the final `__` in the module pattern being matched on. ## Solution Add a `$` to the end of the regex so, to anchor to the end of the string. ## Background context `pnpm` stores files for packages in locations like this (note the `__`s), which uncovered this. ``` /path/to/project/node_modules/.pnpm/@prisma[email protected][email protected][email protected][email protected]/node_modules/@prisma/client/runtime/query_engine_bg.sqlite.wasm ``` * Make regex more resilient * Add test to playground * Revert fix to demonstrate failing test * Revert "Revert fix to demonstrate failing test" This reverts commit d7e57e0. * Revert fix again so we can make sure test failing first * Revert "Revert fix again so we can make sure test failing first" This reverts commit 50093d4. * Add changeset
1 parent 0b98f60 commit 1bae861

File tree

6 files changed

+19
-2
lines changed

6 files changed

+19
-2
lines changed

.changeset/neat-jokes-prove.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@cloudflare/vite-plugin": patch
3+
---
4+
5+
Fix regex to correctly detect additional module imports with \_\_ in path

packages/vite-plugin-cloudflare/playground/additional-modules/__tests__/additional-modules.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ test("supports Text modules with a '.txt' extension", async () => {
2222
expect(result).toBe("Example text content.\n");
2323
});
2424

25+
test("supports modules with `__`s in the filename", async () => {
26+
const result = await getTextResponse("/text2");
27+
expect(result).toBe("Example text content 2");
28+
});
29+
2530
test("supports CompiledWasm modules with a '.wasm' extension", async () => {
2631
const result = await getJsonResponse("/wasm");
2732
expect(result).toEqual({ result: 7 });

packages/vite-plugin-cloudflare/playground/additional-modules/src/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import bin from "./modules/bin-example.bin";
22
import html from "./modules/html-example.html";
3+
import text2 from "./modules/text__example__2.txt";
34
import text from "./modules/text-example.txt";
45
import wasm from "./modules/wasm-example.wasm";
56
import init from "./modules/wasm-example.wasm?init";
@@ -21,6 +22,11 @@ export default {
2122
headers: { "Content-Type": "text/plain" },
2223
});
2324
}
25+
case "/text2": {
26+
return new Response(text2, {
27+
headers: { "Content-Type": "text/plain" },
28+
});
29+
}
2430
case "/wasm": {
2531
const instance = await WebAssembly.instantiate(wasm);
2632
const result = instance.exports.add(3, 4);
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Example text content 2

packages/vite-plugin-cloudflare/src/additional-modules.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,5 @@ export function matchAdditionalModule(source: string) {
2626
}
2727

2828
export function createModuleReference(type: AdditionalModuleType, id: string) {
29-
return `__CLOUDFLARE_MODULE__${type}__${id}__`;
29+
return `__CLOUDFLARE_MODULE__${type}__${id}__CLOUDFLARE_MODULE__`;
3030
}

packages/vite-plugin-cloudflare/src/shared.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ADDITIONAL_MODULE_TYPES } from "./constants";
33
export const UNKNOWN_HOST = "http://localhost";
44
export const INIT_PATH = "/__vite_plugin_cloudflare_init__";
55

6-
const ADDITIONAL_MODULE_PATTERN = `__CLOUDFLARE_MODULE__(${ADDITIONAL_MODULE_TYPES.join("|")})__(.*?)__`;
6+
const ADDITIONAL_MODULE_PATTERN = `__CLOUDFLARE_MODULE__(${ADDITIONAL_MODULE_TYPES.join("|")})__(.*?)__CLOUDFLARE_MODULE__`;
77
export const additionalModuleRE = new RegExp(ADDITIONAL_MODULE_PATTERN);
88
export const additionalModuleGlobalRE = new RegExp(
99
ADDITIONAL_MODULE_PATTERN,

0 commit comments

Comments
 (0)