diff --git a/playground/module-resolution/__tests__/module-resolution.spec.ts b/playground/module-resolution/__tests__/module-resolution.spec.ts index df393de6..3f90891a 100644 --- a/playground/module-resolution/__tests__/module-resolution.spec.ts +++ b/playground/module-resolution/__tests__/module-resolution.spec.ts @@ -8,13 +8,9 @@ import { viteTestUrl, } from '../../__test-utils__'; -// TODO: test build -describe.runIf(!isBuild)('module resolution', async () => { +describe('module resolution', async () => { afterAll(() => { - const unexpectedErrors = serverLogs.errors.filter( - (error) => !error.includes('@non-existing/pkg'), - ); - expect(unexpectedErrors).toEqual([]); + expect(serverLogs.errors).toEqual([]); }); describe('basic module resolution', () => { @@ -48,9 +44,13 @@ describe.runIf(!isBuild)('module resolution', async () => { test('internal imports from `cloudflare:*`', async () => { const result = await getJsonResponse('/cloudflare-imports'); + // Note: in some cases the DurableObject class name (erroneously) includes + // the `Base` suffix, that's a workerd bug that happens for us on builds + const durableObjectName = isBuild ? 'DurableObjectBase' : 'DurableObject'; + expect(result).toEqual({ '(cloudflare:workers) WorkerEntrypoint.name': 'WorkerEntrypoint', - '(cloudflare:workers) DurableObject.name': 'DurableObject', + '(cloudflare:workers) DurableObject.name': durableObjectName, '(cloudflare:sockets) typeof connect': 'function', }); }); @@ -58,8 +58,12 @@ describe.runIf(!isBuild)('module resolution', async () => { test('external imports from `cloudflare:*`', async () => { const result = await getJsonResponse('/external-cloudflare-imports'); + // Note: in some cases the DurableObject class name (erroneously) includes + // the `Base` suffix, that's a workerd bug that happens for us on builds + const durableObjectName = isBuild ? 'DurableObjectBase' : 'DurableObject'; + expect(result).toEqual({ - '(EXTERNAL) (cloudflare:workers) DurableObject.name': 'DurableObject', + '(EXTERNAL) (cloudflare:workers) DurableObject.name': durableObjectName, }); }); }); @@ -72,7 +76,9 @@ describe.runIf(!isBuild)('module resolution', async () => { * special meaning to us. */ describe('third party packages resolutions', () => { - test('react', async () => { + // TODO: we skip this test on build because a `ReferenceError: process is not defined` is thrown + // (https://github.com/flarelabs-net/vite-plugin-cloudflare/issues/82) + test.skipIf(isBuild)('react', async () => { const result = await getJsonResponse('/third-party/react'); expect(result).toEqual({ '(react) reactVersionsMatch': true, @@ -81,7 +87,11 @@ describe.runIf(!isBuild)('module resolution', async () => { }); }); - test('@remix-run/cloudflare', async () => { + // Note: this test is skipped during build because the remix import does not work in preview + // because there seem to be an I/O operation being performed at the top level of the + // generated remix bundled module, this is a legitimate issue and a workerd known quirk/bug + // (https://github.com/flarelabs-net/vite-plugin-cloudflare/issues/83) + test.skipIf(isBuild)('@remix-run/cloudflare', async () => { const result = await getJsonResponse('/third-party/remix'); expect(result).toEqual({ '(remix) remixRunCloudflareCookieName': @@ -115,14 +125,4 @@ describe.runIf(!isBuild)('module resolution', async () => { expect(result).toBe('OK!'); }); }); - - describe('user errors', () => { - test('imports from a non existing package', async () => { - await page.goto(`${viteTestUrl}/@non-existing/pkg`); - const errorText = await page - .locator('vite-error-overlay pre.message') - .textContent(); - expect(errorText).toContain("Cannot find module '@non-existing/pkg'"); - }); - }); }); diff --git a/playground/module-resolution/__tests__/no-prebundling/module-respolution-no-prebundling.spec.ts b/playground/module-resolution/__tests__/no-prebundling/module-respolution-no-prebundling.spec.ts index f6473ad4..7976bf13 100644 --- a/playground/module-resolution/__tests__/no-prebundling/module-respolution-no-prebundling.spec.ts +++ b/playground/module-resolution/__tests__/no-prebundling/module-respolution-no-prebundling.spec.ts @@ -47,14 +47,4 @@ describe.runIf(!isBuild)('module resolution without prebundling', async () => { expect(result).toBe('OK!'); }); }); - - describe('user errors', () => { - test('imports from a non existing package', async () => { - await page.goto(`${viteTestUrl}/@non-existing/pkg`); - const errorText = await page - .locator('vite-error-overlay pre.message') - .textContent(); - expect(errorText).toContain("Cannot find module '@non-existing/pkg'"); - }); - }); }); diff --git a/playground/module-resolution/src/index.ts b/playground/module-resolution/src/index.ts index 81938d63..30bd1d30 100644 --- a/playground/module-resolution/src/index.ts +++ b/playground/module-resolution/src/index.ts @@ -1,24 +1,15 @@ -const allowedPaths = new Set([ - '/require-ext', - '/require-no-ext', - '/require-json', - '/cloudflare-imports', - '/external-cloudflare-imports', - - '/third-party/react', - '/third-party/remix', - '/third-party/discord-api-types', - '/third-party/slash-create', -]); +const modules = import.meta.glob('../src/**/*.ts'); export default { async fetch(request) { const url = new URL(request.url); const path = url.pathname; - if (allowedPaths.has(path)) { - const mod = await import(/* @vite-ignore */ `./${path}`); - return Response.json(mod.default); + const filePath = `${path.replace(/^\//, './')}.ts`; + + if (modules[filePath]) { + const mod = await modules[filePath](); + return Response.json((mod as { default: unknown }).default); } if (path === '/@alias/test') { @@ -26,11 +17,13 @@ export default { return test(); } - if (path === '/@non-existing/pkg') { - const { test } = await import('@non-existing/pkg'); - return test(); - } - - return new Response(`path not found: '${path}'`, { status: 404 }); + return new Response( + `path not found: '${path}' (the available paths are: ${Object.keys( + modules, + ) + .map((path) => path.replace(/^\.\//, '/').replace(/\.ts$/, '')) + .join(', ')})`, + { status: 404 }, + ); }, } satisfies ExportedHandler; diff --git a/playground/module-resolution/tsconfig.worker.json b/playground/module-resolution/tsconfig.worker.json index ac6fbbee..89edeb9f 100644 --- a/playground/module-resolution/tsconfig.worker.json +++ b/playground/module-resolution/tsconfig.worker.json @@ -2,8 +2,7 @@ "extends": ["@vite-plugin-cloudflare/typescript-config/worker.json"], "compilerOptions": { "paths": { - "@alias/test": ["./src/aliasing.ts"], - "@non-existing/pkg": ["./src/aliasing.ts"] + "@alias/test": ["./src/aliasing.ts"] } }, "include": ["src"] diff --git a/playground/module-resolution/vite.config.no-prebundling.ts b/playground/module-resolution/vite.config.no-prebundling.ts index 9520a7ac..0a6e9464 100644 --- a/playground/module-resolution/vite.config.no-prebundling.ts +++ b/playground/module-resolution/vite.config.no-prebundling.ts @@ -8,13 +8,6 @@ export default defineConfig({ '@alias/test': resolve(__dirname, './src/aliasing.ts'), }, }, - build: { - rollupOptions: { - // let's externalize this non existing package just to make the build command pass - // (so that we can validate the dev user error for trying to import it) - external: ['@non-existing/pkg'], - }, - }, environments: { worker: { optimizeDeps: { diff --git a/playground/module-resolution/vite.config.ts b/playground/module-resolution/vite.config.ts index db4f05b7..3ff957f7 100644 --- a/playground/module-resolution/vite.config.ts +++ b/playground/module-resolution/vite.config.ts @@ -8,12 +8,5 @@ export default defineConfig({ '@alias/test': resolve(__dirname, './src/aliasing.ts'), }, }, - build: { - rollupOptions: { - // let's externalize this non existing package just to make the build command pass - // (so that we can validate the dev user error for trying to import it) - external: ['@non-existing/pkg'], - }, - }, plugins: [cloudflare()], });