Skip to content

Commit

Permalink
[vite-plugin-cloudflare] chore: simplify the modules externalization …
Browse files Browse the repository at this point in the history
…logic
  • Loading branch information
dario-piotrowicz committed Feb 21, 2025
1 parent 1427535 commit 13b6038
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
17 changes: 14 additions & 3 deletions packages/vite-plugin-cloudflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,11 @@ export function cloudflare(pluginConfig: PluginConfig = {}): vite.Plugin[] {

return {
external: true,
id: createModuleReference("CompiledWasm", resolved.id),
id: createModuleReference(
"CompiledWasm",
resolved.id,
this.environment.mode === "dev"
),
};
},
renderChunk(code, chunk) {
Expand Down Expand Up @@ -518,6 +522,13 @@ function getDotDevDotVarsContent(
return null;
}

function createModuleReference(type: ModuleType, id: string) {
return `__CLOUDFLARE_MODULE__${type}__${id}__`;
function createModuleReference(type: ModuleType, id: string, devMode: boolean) {
// Note: in dev we need to add a prefix such as `external://` to signal
// to the default vite DevEnvironment `fetchModule`
// logic that this is a reference for an external module
// (source: https://github.com/vitejs/vite/blob/e01573a575/packages/vite/src/node/ssr/fetchModule.ts#L41-L43)
// (`fetchModule` doesn't try to resolve the module so `resolveId` hooks aren't taken into account here,
// meaning that returning a resolved id with `external: true` is not sufficient)
const prefix = devMode ? "external://" : "";
return `${prefix}__CLOUDFLARE_MODULE__${type}__${id}__`;
}
16 changes: 0 additions & 16 deletions packages/vite-plugin-cloudflare/src/miniflare-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,22 +325,6 @@ export function getDevMiniflareOptions(
`Invalid invoke event: ${invokePayloadData.name}`
);

const [moduleId] = invokePayloadData.data;
const moduleRE = new RegExp(MODULE_PATTERN);

const shouldExternalize =
// Worker modules (CompiledWasm, Text, Data)
moduleRE.test(moduleId);

if (shouldExternalize) {
const result = {
externalize: moduleId,
type: "module",
} satisfies vite.FetchResult;

return MiniflareResponse.json({ result });
}

const devEnvironment = viteDevServer.environments[
environmentName
] as CloudflareDevEnvironment;
Expand Down

0 comments on commit 13b6038

Please sign in to comment.