Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[vite-plugin-cloudflare] chore: simplify the modules externalization logic #8214

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading