Skip to content

Commit fddcd2f

Browse files
authored
Merge pull request #8567 from QwikDev/fix-server-fns
fix(router - server-fns.ts): dynamicallyImportedIdResolutions don't exist in rolldown
2 parents 8e07b9b + 2acb652 commit fddcd2f

3 files changed

Lines changed: 50 additions & 47 deletions

File tree

packages/qwik-router/src/buildtime/vite/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ function serverFnsPlugin(buildContextRef: BuildContextRef): Plugin {
435435
if (!ctx) {
436436
return;
437437
}
438-
const moduleIds = await collectServerFnModuleIds(ctx, RESOLVED_ID, (id) => this.load({ id }));
438+
const moduleIds = await collectServerFnModuleIds(ctx, RESOLVED_ID, this);
439439
for (let i = 0; i < moduleIds.length; i++) {
440440
serverFnModules.add(moduleIds[i]);
441441
}

packages/qwik-router/src/buildtime/vite/server-fns.ts

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
import type { Rollup } from 'vite';
22
import type { RoutingContext } from '../types';
33

4-
type ServerFnModuleInfo = Pick<
5-
Rollup.ModuleInfo,
6-
'code' | 'dynamicallyImportedIdResolutions' | 'id' | 'importedIdResolutions'
7-
>;
8-
4+
export type ServerFnPluginContext = Pick<Rollup.PluginContext, 'resolve' | 'load'>;
5+
export type ServerFnRoutingContext = Pick<RoutingContext, 'layouts' | 'routes' | 'serverPlugins'>;
96
export async function collectServerFnModuleIds(
10-
ctx: Pick<RoutingContext, 'layouts' | 'routes' | 'serverPlugins'>,
7+
routingContext: ServerFnRoutingContext,
118
resolvedVirtualId: string,
12-
loadModule: (id: string) => Promise<ServerFnModuleInfo>
9+
pluginContext: ServerFnPluginContext
1310
) {
1411
const serverFnModules = new Set<string>();
1512
const queuedModuleIds = new Set<string>();
1613
const seenModuleIds = new Set<string>();
17-
const routes = ctx.routes;
18-
const layouts = ctx.layouts;
19-
const serverPlugins = ctx.serverPlugins;
14+
const routes = routingContext.routes;
15+
const layouts = routingContext.layouts;
16+
const serverPlugins = routingContext.serverPlugins;
2017

2118
for (let i = 0; i < routes.length; i++) {
2219
const route = routes[i];
@@ -42,18 +39,25 @@ export async function collectServerFnModuleIds(
4239
}
4340
seenModuleIds.add(id);
4441

45-
const moduleInfo = await loadModule(id);
46-
if (moduleInfo.code?.includes('serverQrl(')) {
42+
const resolved = await pluginContext.resolve(id, undefined, { skipSelf: true });
43+
if (!resolved || resolved.external) {
44+
continue;
45+
}
46+
47+
const moduleInfo = await pluginContext.load({ id: resolved.id });
48+
if (moduleInfo.code == null) {
49+
continue;
50+
}
51+
52+
if (moduleInfo.code.includes('serverQrl(')) {
4753
serverFnModules.add(moduleInfo.id);
4854
}
4955

50-
const resolvedImports = moduleInfo.importedIdResolutions.concat(
51-
moduleInfo.dynamicallyImportedIdResolutions
52-
);
56+
const resolvedImports = moduleInfo.importedIds.concat(moduleInfo.dynamicallyImportedIds);
5357
for (let i = 0; i < resolvedImports.length; i++) {
5458
const resolvedImport = resolvedImports[i];
55-
if (!resolvedImport.external && !seenModuleIds.has(resolvedImport.id)) {
56-
queuedModuleIds.add(resolvedImport.id);
59+
if (resolvedImport && !seenModuleIds.has(resolvedImport)) {
60+
queuedModuleIds.add(resolvedImport);
5761
}
5862
}
5963
}

packages/qwik-router/src/buildtime/vite/server-fns.unit.ts

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'vitest';
2-
import type { BuiltLayout, BuiltRoute, BuiltServerPlugin, RoutingContext } from '../types';
3-
import { collectServerFnModuleIds } from './server-fns';
2+
import type { BuiltLayout, BuiltRoute, BuiltServerPlugin } from '../types';
3+
import { collectServerFnModuleIds, type ServerFnRoutingContext } from './server-fns';
44

55
describe('collectServerFnModuleIds', () => {
66
it('walks the reachable module graph and collects modules containing serverQrl', async () => {
@@ -12,59 +12,53 @@ describe('collectServerFnModuleIds', () => {
1212
{
1313
id: '/app/routes/index.tsx',
1414
code: 'export default component$(() => null);',
15-
importedIdResolutions: [
16-
{ id: '/app/shared.ts', external: false },
17-
{ id: '/node_modules/@qwik.dev/core/index.mjs', external: false },
18-
],
19-
dynamicallyImportedIdResolutions: [{ id: '/app/lazy.ts', external: false }],
15+
importedIds: ['/app/shared.ts', '/node_modules/@qwik.dev/core/index.mjs'],
16+
dynamicallyImportedIds: ['/app/lazy.ts'],
2017
},
2118
],
2219
[
2320
'/app/layout.tsx',
2421
{
2522
id: '/app/layout.tsx',
2623
code: 'export default component$(() => null);',
27-
importedIdResolutions: [{ id: '/app/shared.ts', external: false }],
28-
dynamicallyImportedIdResolutions: [],
24+
importedIds: ['/app/shared.ts'],
25+
dynamicallyImportedIds: [],
2926
},
3027
],
3128
[
3229
'/app/shared.ts',
3330
{
3431
id: '/app/shared.ts',
3532
code: 'export const shared = true;',
36-
importedIdResolutions: [
37-
{ id: resolvedVirtualId, external: false },
38-
{ id: 'node:fs', external: true },
39-
],
40-
dynamicallyImportedIdResolutions: [],
33+
importedIds: [resolvedVirtualId],
34+
dynamicallyImportedIds: [],
4135
},
4236
],
4337
[
4438
'/app/lazy.ts',
4539
{
4640
id: '/app/lazy.ts',
4741
code: 'export const fn = serverQrl(() => null);',
48-
importedIdResolutions: [],
49-
dynamicallyImportedIdResolutions: [],
42+
importedIds: [],
43+
dynamicallyImportedIds: [],
5044
},
5145
],
5246
[
5347
'/node_modules/@qwik.dev/core/index.mjs',
5448
{
5549
id: '/node_modules/@qwik.dev/core/index.mjs',
5650
code: 'export {};',
57-
importedIdResolutions: [],
58-
dynamicallyImportedIdResolutions: [],
51+
importedIds: [],
52+
dynamicallyImportedIds: [],
5953
},
6054
],
6155
[
6256
'/app/plugin.ts',
6357
{
6458
id: '/app/plugin.ts',
6559
code: 'export const pluginFn = serverQrl(() => null);',
66-
importedIdResolutions: [],
67-
dynamicallyImportedIdResolutions: [],
60+
importedIds: [],
61+
dynamicallyImportedIds: [],
6862
},
6963
],
7064
]);
@@ -92,20 +86,25 @@ describe('collectServerFnModuleIds', () => {
9286
filePath: '/app/plugin.ts',
9387
ext: 'ts',
9488
};
95-
const ctx: Pick<RoutingContext, 'layouts' | 'routes' | 'serverPlugins'> = {
89+
const ctx: ServerFnRoutingContext = {
9690
routes: [route],
9791
layouts: [layout],
9892
serverPlugins: [serverPlugin],
9993
};
10094

101-
const serverFnModules = await collectServerFnModuleIds(ctx, resolvedVirtualId, async (id) => {
102-
loads.push(id);
103-
const moduleInfo = modules.get(id);
104-
if (!moduleInfo) {
105-
throw new Error(`Unexpected module load: ${id}`);
106-
}
107-
return moduleInfo as any;
108-
});
95+
const pluginContext = {
96+
resolve: async (id: string) => ({ id, external: false }) as any,
97+
load: async ({ id }: { id: string }) => {
98+
loads.push(id);
99+
const moduleInfo = modules.get(id);
100+
if (!moduleInfo) {
101+
throw new Error(`Unexpected module load: ${id}`);
102+
}
103+
return moduleInfo as any;
104+
},
105+
};
106+
107+
const serverFnModules = await collectServerFnModuleIds(ctx, resolvedVirtualId, pluginContext);
109108

110109
expect(serverFnModules.sort()).toEqual(['/app/lazy.ts', '/app/plugin.ts']);
111110
expect(loads).toEqual([

0 commit comments

Comments
 (0)