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

reproduce the prebundling module issue #7935

Draft
wants to merge 4 commits 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { rmSync } from "fs";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { expect, test } from "vitest";
import { getTextResponse, viteServer } from "../../__test-utils__";

test("prebundling module issue reproduction", async () => {
const viteCacheDir = path.resolve(
fileURLToPath(import.meta.url),
"../../node_modules/.vite"
);

rmSync(viteCacheDir, { recursive: true });

await viteServer.close();
await viteServer.restart();

const result = await getTextResponse();
expect(result).not.toContain("context mismatch");
expect(result).toBe('Message from virtual module: "Hello from lib-b"');
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@playground/prebundling-module-issue",
"private": true,
"type": "module",
"scripts": {
"build": "vite build --app",
"check:types": "tsc --build",
"dev": "vite dev",
"preview": "vite preview"
},
"devDependencies": {
"@cloudflare/vite-plugin": "workspace:*",
"@cloudflare/workers-tsconfig": "workspace:*",
"@cloudflare/workers-types": "^4.20250121.0",
"@packages/lib-a": "file:./packages/lib-a",
"@packages/lib-b": "file:./packages/lib-b",
"typescript": "catalog:vite-plugin",
"vite": "catalog:vite-plugin",
"wrangler": "catalog:vite-plugin"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const context: {};

export const setup: () => {};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const context = {};

export function setup() {
globalThis.context = context;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@playground/prebundling-module-issue-lib-a",
"version": "1.0.0",
"type": "module",
"exports": {
".": {
"default": "./index.js",
"types": "./index.d.ts"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const msg: string;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as libA from "@packages/lib-a";

if (libA.context !== globalThis.context) {
throw new Error('context mismatch');
}

export const msg = 'Hello from lib-b';
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@playground/prebundling-module-issue-lib-b",
"version": "1.0.0",
"type": "module",
"exports": {
".": {
"default": "./index.js",
"types": "./index.d.ts"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { setup } from "@packages/lib-a";

setup();

export default {
async fetch() {
const { msg } = await import("virtual:my-module");
return new Response(`Message from virtual module: "${msg}"`);
},
} satisfies ExportedHandler;
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"files": [],
"references": [
{ "path": "./tsconfig.node.json" },
{ "path": "./tsconfig.worker.json" }
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["@cloudflare/workers-tsconfig/base.json"],
"include": ["vite.config.ts", "__tests__"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"extends": ["@cloudflare/workers-tsconfig/worker.json"],
"include": ["src", "virtual-module.d.ts"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"$schema": "http://turbo.build/schema.json",
"extends": ["//"],
"tasks": {
"build": {
"outputs": ["dist/**"]
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
declare module "virtual:my-module" {
export const msg: string;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { cloudflare } from "@cloudflare/vite-plugin";
import { defineConfig } from "vite";

export default defineConfig({
plugins: [
cloudflare({ persistState: false }),
{
name: "my-plugin",
resolveId(id) {
if (id === "virtual:my-module") {
return "\0virtual:my-module";
}
},
load(id) {
if (id === "\0virtual:my-module") {
return `export { msg } from "@packages/lib-b";`;
}
},
},
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name = "worker"
main = "./src/index.ts"
compatibility_date = "2024-12-30"
60 changes: 38 additions & 22 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading