Skip to content

Commit ea509f0

Browse files
committed
refactor(bundle-graph): plugin API, tests, optimization
1 parent b19b956 commit ea509f0

File tree

12 files changed

+1060
-149
lines changed

12 files changed

+1060
-149
lines changed

packages/docs/src/routes/api/qwik-optimizer/api.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@
2020
"mdFile": "qwik.path.basename.md"
2121
},
2222
{
23-
"name": "BundleGraphModifier",
24-
"id": "bundlegraphmodifier",
23+
"name": "BundleGraphAdder",
24+
"id": "bundlegraphadder",
2525
"hierarchy": [
2626
{
27-
"name": "BundleGraphModifier",
28-
"id": "bundlegraphmodifier"
27+
"name": "BundleGraphAdder",
28+
"id": "bundlegraphadder"
2929
}
3030
],
3131
"kind": "TypeAlias",
32-
"content": "A function that creates a modified version of the bundle graph. Used to inject routes and their dependencies into the bundle graph.\n\n\n```typescript\nexport type BundleGraphModifier = (graph: QwikBundleGraph, manifest: QwikManifest) => QwikBundleGraph;\n```\n**References:** [QwikBundleGraph](#qwikbundlegraph)<!-- -->, [QwikManifest](#qwikmanifest)",
32+
"content": "A function that returns a map of bundle names to their dependencies.\n\n\n```typescript\nexport type BundleGraphAdder = (manifest: QwikManifest) => Record<string, {\n imports?: string[];\n dynamicImports?: string[];\n}>;\n```\n**References:** [QwikManifest](#qwikmanifest)",
3333
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/optimizer/src/plugins/bundle-graph.ts",
34-
"mdFile": "qwik.bundlegraphmodifier.md"
34+
"mdFile": "qwik.bundlegraphadder.md"
3535
},
3636
{
3737
"name": "ComponentEntryStrategy",
@@ -546,7 +546,7 @@
546546
}
547547
],
548548
"kind": "Interface",
549-
"content": "```typescript\nexport interface QwikVitePluginApi \n```\n\n\n<table><thead><tr><th>\n\nProperty\n\n\n</th><th>\n\nModifiers\n\n\n</th><th>\n\nType\n\n\n</th><th>\n\nDescription\n\n\n</th></tr></thead>\n<tbody><tr><td>\n\n[getAssetsDir](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; string \\| undefined\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getClientOutDir](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; string \\| null\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getClientPublicOutDir](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; string \\| null\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getInsightsManifest](#)\n\n\n</td><td>\n\n\n</td><td>\n\n(clientOutDir?: string \\| null) =&gt; Promise&lt;[InsightManifest](#insightmanifest) \\| null&gt;\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getManifest](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; [QwikManifest](#qwikmanifest) \\| null\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getOptimizer](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; [Optimizer](#optimizer) \\| null\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getOptions](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; NormalizedQwikPluginOptions\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getRootDir](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; string \\| null\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[registerBundleGraphModifier](#)\n\n\n</td><td>\n\n\n</td><td>\n\n(modifier: [BundleGraphModifier](#bundlegraphmodifier)<!-- -->) =&gt; void\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>",
549+
"content": "```typescript\nexport interface QwikVitePluginApi \n```\n\n\n<table><thead><tr><th>\n\nProperty\n\n\n</th><th>\n\nModifiers\n\n\n</th><th>\n\nType\n\n\n</th><th>\n\nDescription\n\n\n</th></tr></thead>\n<tbody><tr><td>\n\n[getAssetsDir](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; string \\| undefined\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getClientOutDir](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; string \\| null\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getClientPublicOutDir](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; string \\| null\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getInsightsManifest](#)\n\n\n</td><td>\n\n\n</td><td>\n\n(clientOutDir?: string \\| null) =&gt; Promise&lt;[InsightManifest](#insightmanifest) \\| null&gt;\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getManifest](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; [QwikManifest](#qwikmanifest) \\| null\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getOptimizer](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; [Optimizer](#optimizer) \\| null\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getOptions](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; NormalizedQwikPluginOptions\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[getRootDir](#)\n\n\n</td><td>\n\n\n</td><td>\n\n() =&gt; string \\| null\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[registerBundleGraphAdder](#)\n\n\n</td><td>\n\n\n</td><td>\n\n(adder: [BundleGraphAdder](#bundlegraphadder)<!-- -->) =&gt; void\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>",
550550
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/optimizer/src/plugins/vite.ts",
551551
"mdFile": "qwik.qwikvitepluginapi.md"
552552
},

packages/docs/src/routes/api/qwik-optimizer/index.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,21 @@ _(Optional)_
5252

5353
string
5454

55-
## BundleGraphModifier
55+
## BundleGraphAdder
5656

57-
A function that creates a modified version of the bundle graph. Used to inject routes and their dependencies into the bundle graph.
57+
A function that returns a map of bundle names to their dependencies.
5858

5959
```typescript
60-
export type BundleGraphModifier = (
61-
graph: QwikBundleGraph,
62-
manifest: QwikManifest,
63-
) => QwikBundleGraph;
60+
export type BundleGraphAdder = (manifest: QwikManifest) => Record<
61+
string,
62+
{
63+
imports?: string[];
64+
dynamicImports?: string[];
65+
}
66+
>;
6467
```
6568

66-
**References:** [QwikBundleGraph](#qwikbundlegraph), [QwikManifest](#qwikmanifest)
69+
**References:** [QwikManifest](#qwikmanifest)
6770

6871
[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/optimizer/src/plugins/bundle-graph.ts)
6972

@@ -2250,13 +2253,13 @@ Description
22502253
</td></tr>
22512254
<tr><td>
22522255

2253-
[registerBundleGraphModifier](#)
2256+
[registerBundleGraphAdder](#)
22542257

22552258
</td><td>
22562259

22572260
</td><td>
22582261

2259-
(modifier: [BundleGraphModifier](#bundlegraphmodifier)) =&gt; void
2262+
(adder: [BundleGraphAdder](#bundlegraphadder)) =&gt; void
22602263

22612264
</td><td>
22622265

packages/qwik-city/src/buildtime/vite/bundle-graph-modifier.ts renamed to packages/qwik-city/src/buildtime/vite/get-route-imports.ts

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,28 @@
1-
import type { QwikBundle, QwikBundleGraph, QwikManifest } from '@builder.io/qwik/optimizer';
1+
import type { QwikBundle, QwikManifest } from '@builder.io/qwik/optimizer';
22
import { removeExtension } from '../../utils/fs';
33
import type { BuildRoute } from '../types';
44

5-
export function modifyBundleGraph(
6-
routes: BuildRoute[],
7-
originalGraph: QwikBundleGraph,
8-
manifest: QwikManifest
9-
) {
10-
const graph = [...originalGraph];
11-
5+
export function getRouteImports(routes: BuildRoute[], manifest: QwikManifest) {
6+
const result: Record<string, { imports?: string[] }> = {};
127
routes.forEach((route) => {
138
const routePath = removeExtension(route.filePath);
149
const layoutPaths = route.layouts
1510
? route.layouts.map((layout) => removeExtension(layout.filePath))
1611
: [];
1712
const routeAndLayoutPaths = [routePath, ...layoutPaths];
1813

19-
const routeDeps = [];
14+
const imports = [];
2015

2116
for (const [bundleName, bundle] of Object.entries(manifest.bundles)) {
2217
if (isBundlePartOfRoute(bundle, routeAndLayoutPaths)) {
23-
const bundleIndex = originalGraph.indexOf(bundleName);
24-
if (bundleIndex !== -1) {
25-
routeDeps.push(bundleIndex);
26-
}
18+
imports.push(bundleName);
2719
}
2820
}
29-
if (routeDeps.length > 0) {
30-
graph.push(route.routeName, ...routeDeps);
21+
if (imports.length > 0) {
22+
result[route.routeName] = { imports };
3123
}
3224
});
33-
return graph;
25+
return result;
3426
}
3527

3628
function isBundlePartOfRoute(bundle: QwikBundle, routeAndLayoutPaths: string[]) {

packages/qwik-city/src/buildtime/vite/bundle-graph-modifier.unit.ts renamed to packages/qwik-city/src/buildtime/vite/get-route-imports.unit.ts

Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
1-
import {
2-
type QwikBundle,
3-
type QwikBundleGraph,
4-
type QwikManifest,
5-
} from '@builder.io/qwik/optimizer';
1+
import { type QwikBundle, type QwikManifest } from '@builder.io/qwik/optimizer';
62
import { describe, expect, test } from 'vitest';
73
import type { BuildLayout, BuildRoute } from '../types';
8-
import { modifyBundleGraph } from './bundle-graph-modifier';
4+
import { getRouteImports } from './get-route-imports';
95

106
describe('modifyBundleGraph', () => {
117
test(`GIVEN 2 routes, one with a layout
@@ -36,17 +32,6 @@ describe('modifyBundleGraph', () => {
3632
} as Record<string, QwikBundle>,
3733
} as QwikManifest;
3834

39-
const fakeBundleGraph: QwikBundleGraph = [
40-
'fake-bundle1.js',
41-
2,
42-
'fake-bundle-static-dep.js',
43-
-1,
44-
4,
45-
'fake-bundle-dynamic-dep.js',
46-
'fake-bundle-part-of-sub-route.js',
47-
'fake-bundle-part-of-layout.js',
48-
];
49-
5035
const fakeRoutes: BuildRoute[] = [
5136
{
5237
routeName: '/',
@@ -63,18 +48,22 @@ describe('modifyBundleGraph', () => {
6348
},
6449
] as BuildRoute[];
6550

66-
const actualResult = modifyBundleGraph(fakeRoutes, fakeBundleGraph, fakeManifest);
67-
68-
const expectedResult: QwikBundleGraph = [
69-
...fakeBundleGraph,
70-
'/',
71-
0, // fake-bundle1.js
72-
'/subroute',
73-
6, // fake-bundle-part-of-sub-route.js
74-
7, // fake-bundle-part-of-layout.js
75-
];
76-
77-
expect(actualResult).toEqual(expectedResult);
51+
const actualResult = getRouteImports(fakeRoutes, fakeManifest);
52+
expect(actualResult).toMatchInlineSnapshot(`
53+
{
54+
"/": {
55+
"imports": [
56+
"fake-bundle1.js",
57+
],
58+
},
59+
"/subroute": {
60+
"imports": [
61+
"fake-bundle-part-of-sub-route.js",
62+
"fake-bundle-part-of-layout.js",
63+
],
64+
},
65+
}
66+
`);
7867
});
7968

8069
test(`GIVEN a mismatch between the bundle graph and the manifest
@@ -93,23 +82,24 @@ describe('modifyBundleGraph', () => {
9382
} as Record<string, QwikBundle>,
9483
} as QwikManifest;
9584

96-
const fakeBundleGraph: QwikBundleGraph = ['fake-bundle1.js'];
97-
9885
const fakeRoutes: BuildRoute[] = [
9986
{
10087
routeName: '/',
10188
filePath: '/home/qwik-app/src/routes/index.tsx',
10289
},
10390
] as BuildRoute[];
10491

105-
const actualResult = modifyBundleGraph(fakeRoutes, fakeBundleGraph, fakeManifest);
106-
107-
const expectedResult: QwikBundleGraph = [
108-
...fakeBundleGraph,
109-
'/',
110-
0, // fake-bundle1.js
111-
];
92+
const actualResult = getRouteImports(fakeRoutes, fakeManifest);
11293

113-
expect(actualResult).toEqual(expectedResult);
94+
expect(actualResult).toMatchInlineSnapshot(`
95+
{
96+
"/": {
97+
"imports": [
98+
"fake-bundle1.js",
99+
"fake-bundle2.js",
100+
],
101+
},
102+
}
103+
`);
114104
});
115105
});

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { generateQwikCityEntries } from '../runtime-generation/generate-entries'
2121
import { generateQwikCityPlan } from '../runtime-generation/generate-qwik-city-plan';
2222
import { generateServiceWorkerRegister } from '../runtime-generation/generate-service-worker';
2323
import type { BuildContext } from '../types';
24-
import { modifyBundleGraph } from './bundle-graph-modifier';
24+
import { getRouteImports } from './get-route-imports';
2525
import { ssrDevMiddleware, staticDistMiddleware } from './dev-server';
2626
import { imagePlugin } from './image-jsx';
2727
import type { QwikCityPluginApi, QwikCityVitePluginOptions } from './types';
@@ -236,8 +236,8 @@ function qwikCityPlugin(userOpts?: QwikCityVitePluginOptions): any {
236236
generateBundle(_, bundles) {
237237
// client bundles
238238
if (ctx?.target === 'client') {
239-
qwikPlugin!.api.registerBundleGraphModifier((graph, manifest) => {
240-
return modifyBundleGraph(ctx!.routes, graph, manifest);
239+
qwikPlugin!.api.registerBundleGraphAdder((manifest) => {
240+
return getRouteImports(ctx!.routes, manifest);
241241
});
242242

243243
const entries = [...ctx.entries, ...ctx.serviceWorkers].map((entry) => {

packages/qwik/src/optimizer/src/api.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
import type { Plugin as Plugin_2 } from 'vite';
88

99
// @public
10-
export type BundleGraphModifier = (graph: QwikBundleGraph, manifest: QwikManifest) => QwikBundleGraph;
10+
export type BundleGraphAdder = (manifest: QwikManifest) => Record<string, {
11+
imports?: string[];
12+
dynamicImports?: string[];
13+
}>;
1114

1215
// @public (undocumented)
1316
export interface ComponentEntryStrategy {
@@ -308,7 +311,7 @@ export interface QwikVitePluginApi {
308311
// (undocumented)
309312
getRootDir: () => string | null;
310313
// (undocumented)
311-
registerBundleGraphModifier: (modifier: BundleGraphModifier) => void;
314+
registerBundleGraphAdder: (adder: BundleGraphAdder) => void;
312315
}
313316

314317
// Warning: (ae-forgotten-export) The symbol "QwikVitePluginCSROptions" needs to be exported by the entry point index.d.ts

packages/qwik/src/optimizer/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ export type {
4848
QwikVitePluginOptions,
4949
} from './plugins/vite';
5050

51-
export type { BundleGraphModifier } from './plugins/bundle-graph';
51+
export type { BundleGraphAdder } from './plugins/bundle-graph';
5252

5353
export { qwikRollup } from './plugins/rollup';
5454
export { qwikVite } from './plugins/vite';

packages/qwik/src/optimizer/src/manifest.ts

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { OutputBundle } from 'rollup';
22
import { type NormalizedQwikPluginOptions } from './plugins/plugin';
3-
import type { GlobalInjections, SegmentAnalysis, Path, QwikBundle, QwikManifest } from './types';
3+
import type { GlobalInjections, Path, QwikBundle, QwikManifest, SegmentAnalysis } from './types';
44

55
// This is just the initial prioritization of the symbols and entries
66
// at build time so there's less work during each SSR. However, SSR should
@@ -279,7 +279,6 @@ export function generateManifestFromBundles(
279279

280280
const bundle: QwikBundle = {
281281
size: outputBundle.code.length,
282-
hasSymbols: false,
283282
};
284283

285284
let hasSymbols = false;
@@ -347,27 +346,40 @@ export function generateManifestFromBundles(
347346
};
348347
}
349348
// To inspect the bundles, uncomment the following lines
350-
// and temporarily add the writeFileSync import from fs
351-
// writeFileSync(
352-
// 'output-bundles.json',
353-
// JSON.stringify(
354-
// Object.entries(outputBundles).map(([n, b]) => [
355-
// n,
349+
// import('node:fs').then((fs) =>
350+
// fs.writeFileSync(
351+
// 'output-bundles.json',
352+
// JSON.stringify(
356353
// {
357-
// ...b,
358-
// code: '<removed>',
359-
// map: '<removed>',
360-
// source: '<removed>',
361-
// modules:
362-
// 'modules' in b
363-
// ? Object.fromEntries(
364-
// Object.entries(b.modules).map(([k, v]) => [k, { ...v, code: '<removed>' }])
365-
// )
366-
// : undefined,
354+
// segments,
355+
// bundles: Object.fromEntries(
356+
// Object.entries(outputBundles).map(([n, b]) => [
357+
// n,
358+
// {
359+
// ...b,
360+
// // code: 'code' in b ? `<removed ${b.code.length} bytes>` : undefined,
361+
// map: 'map' in b ? `<removed>` : undefined,
362+
// source: 'source' in b ? `<removed ${b.source.length} bytes>` : undefined,
363+
// modules:
364+
// 'modules' in b
365+
// ? Object.fromEntries(
366+
// Object.entries(b.modules).map(([k, v]) => [
367+
// k,
368+
// {
369+
// ...v,
370+
// code:
371+
// 'code' in v ? `<removed ${v.code?.length || 0} bytes>` : undefined,
372+
// },
373+
// ])
374+
// )
375+
// : undefined,
376+
// },
377+
// ])
378+
// ),
367379
// },
368-
// ]),
369-
// null,
370-
// '\t'
380+
// null,
381+
// '\t'
382+
// ).replaceAll(process.cwd(), '')
371383
// )
372384
// );
373385

0 commit comments

Comments
 (0)