Skip to content

Commit

Permalink
re-enable the module resolution build tests (#80)
Browse files Browse the repository at this point in the history
* re-enable the module resolution build tests
* completely remove the non-existing package module-resolution test
  • Loading branch information
dario-piotrowicz authored Dec 6, 2024
1 parent 2b0ec21 commit 40c0c52
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 67 deletions.
40 changes: 20 additions & 20 deletions playground/module-resolution/__tests__/module-resolution.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -48,18 +44,26 @@ 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',
});
});

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,
});
});
});
Expand All @@ -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,
Expand All @@ -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':
Expand Down Expand Up @@ -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'");
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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'");
});
});
});
35 changes: 14 additions & 21 deletions playground/module-resolution/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,29 @@
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') {
const { test } = await import('@alias/test');
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;
3 changes: 1 addition & 2 deletions playground/module-resolution/tsconfig.worker.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
7 changes: 0 additions & 7 deletions playground/module-resolution/vite.config.no-prebundling.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
7 changes: 0 additions & 7 deletions playground/module-resolution/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
});

0 comments on commit 40c0c52

Please sign in to comment.