Skip to content

Commit bb6be68

Browse files
committed
perf(bundle-graph): only include segments for dynamic imports
If the dev wants to include other dynamic imports, they can just make a qrl()
1 parent 537d95a commit bb6be68

File tree

8 files changed

+141
-160
lines changed

8 files changed

+141
-160
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@
406406
}
407407
],
408408
"kind": "Interface",
409-
"content": "```typescript\nexport interface QwikBundle \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[dynamicImports?](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\\[\\]\n\n\n</td><td>\n\n_(Optional)_\n\n\n</td></tr>\n<tr><td>\n\n[hasSymbols?](#)\n\n\n</td><td>\n\n\n</td><td>\n\nboolean\n\n\n</td><td>\n\n_(Optional)_\n\n\n</td></tr>\n<tr><td>\n\n[imports?](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\\[\\]\n\n\n</td><td>\n\n_(Optional)_\n\n\n</td></tr>\n<tr><td>\n\n[origins?](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\\[\\]\n\n\n</td><td>\n\n_(Optional)_\n\n\n</td></tr>\n<tr><td>\n\n[size](#)\n\n\n</td><td>\n\n\n</td><td>\n\nnumber\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[symbols?](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\\[\\]\n\n\n</td><td>\n\n_(Optional)_\n\n\n</td></tr>\n</tbody></table>",
409+
"content": "```typescript\nexport interface QwikBundle \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[dynamicImports?](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\\[\\]\n\n\n</td><td>\n\n_(Optional)_ Dynamic imports\n\n\n</td></tr>\n<tr><td>\n\n[imports?](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\\[\\]\n\n\n</td><td>\n\n_(Optional)_ Direct imports\n\n\n</td></tr>\n<tr><td>\n\n[origins?](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\\[\\]\n\n\n</td><td>\n\n_(Optional)_ Source files of the bundle\n\n\n</td></tr>\n<tr><td>\n\n[size](#)\n\n\n</td><td>\n\n\n</td><td>\n\nnumber\n\n\n</td><td>\n\nSize of the bundle\n\n\n</td></tr>\n<tr><td>\n\n[symbols?](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\\[\\]\n\n\n</td><td>\n\n_(Optional)_ Symbols in the bundle\n\n\n</td></tr>\n</tbody></table>",
410410
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/optimizer/src/types.ts",
411411
"mdFile": "qwik.qwikbundle.md"
412412
},
@@ -476,7 +476,7 @@
476476
}
477477
],
478478
"kind": "Interface",
479-
"content": "```typescript\nexport interface QwikSymbol \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[canonicalFilename](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[captures](#)\n\n\n</td><td>\n\n\n</td><td>\n\nboolean\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[ctxKind](#)\n\n\n</td><td>\n\n\n</td><td>\n\n'function' \\| 'event'\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[ctxName](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[displayName](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[hash](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[loc](#)\n\n\n</td><td>\n\n\n</td><td>\n\n\\[number, number\\]\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[origin](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[parent](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring \\| null\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>",
479+
"content": "```typescript\nexport interface QwikSymbol \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[canonicalFilename](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[captures](#)\n\n\n</td><td>\n\n\n</td><td>\n\nboolean\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[ctxKind](#)\n\n\n</td><td>\n\n\n</td><td>\n\n'function' \\| 'eventHandler'\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[ctxName](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[displayName](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[hash](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[loc](#)\n\n\n</td><td>\n\n\n</td><td>\n\n\\[number, number\\]\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[origin](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[parent](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring \\| null\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>",
480480
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/optimizer/src/types.ts",
481481
"mdFile": "qwik.qwiksymbol.md"
482482
},
@@ -608,7 +608,7 @@
608608
}
609609
],
610610
"kind": "Interface",
611-
"content": "```typescript\nexport interface SegmentAnalysis \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[canonicalFilename](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[captures](#)\n\n\n</td><td>\n\n\n</td><td>\n\nboolean\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[ctxKind](#)\n\n\n</td><td>\n\n\n</td><td>\n\n'event' \\| 'function'\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[ctxName](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[displayName](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[entry](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring \\| null\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[extension](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[hash](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[loc](#)\n\n\n</td><td>\n\n\n</td><td>\n\n\\[number, number\\]\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[name](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[origin](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[parent](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring \\| null\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>",
611+
"content": "```typescript\nexport interface SegmentAnalysis \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[canonicalFilename](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[captures](#)\n\n\n</td><td>\n\n\n</td><td>\n\nboolean\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[ctxKind](#)\n\n\n</td><td>\n\n\n</td><td>\n\n'eventHandler' \\| 'function'\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[ctxName](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[displayName](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[entry](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring \\| null\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[extension](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[hash](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[loc](#)\n\n\n</td><td>\n\n\n</td><td>\n\n\\[number, number\\]\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[name](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[origin](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\n[parent](#)\n\n\n</td><td>\n\n\n</td><td>\n\nstring \\| null\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>",
612612
"editUrl": "https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/optimizer/src/types.ts",
613613
"mdFile": "qwik.segmentanalysis.md"
614614
},

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

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,22 +1267,7 @@ string[]
12671267

12681268
</td><td>
12691269

1270-
_(Optional)_
1271-
1272-
</td></tr>
1273-
<tr><td>
1274-
1275-
[hasSymbols?](#)
1276-
1277-
</td><td>
1278-
1279-
</td><td>
1280-
1281-
boolean
1282-
1283-
</td><td>
1284-
1285-
_(Optional)_
1270+
_(Optional)_ Dynamic imports
12861271

12871272
</td></tr>
12881273
<tr><td>
@@ -1297,7 +1282,7 @@ string[]
12971282

12981283
</td><td>
12991284

1300-
_(Optional)_
1285+
_(Optional)_ Direct imports
13011286

13021287
</td></tr>
13031288
<tr><td>
@@ -1312,7 +1297,7 @@ string[]
13121297

13131298
</td><td>
13141299

1315-
_(Optional)_
1300+
_(Optional)_ Source files of the bundle
13161301

13171302
</td></tr>
13181303
<tr><td>
@@ -1327,6 +1312,8 @@ number
13271312

13281313
</td><td>
13291314

1315+
Size of the bundle
1316+
13301317
</td></tr>
13311318
<tr><td>
13321319

@@ -1340,7 +1327,7 @@ string[]
13401327

13411328
</td><td>
13421329

1343-
_(Optional)_
1330+
_(Optional)_ Symbols in the bundle
13441331

13451332
</td></tr>
13461333
</tbody></table>
@@ -1859,7 +1846,7 @@ boolean
18591846

18601847
</td><td>
18611848

1862-
'function' \| 'event'
1849+
'function' \| 'eventHandler'
18631850

18641851
</td><td>
18651852

@@ -2390,7 +2377,7 @@ boolean
23902377

23912378
</td><td>
23922379

2393-
'event' \| 'function'
2380+
'eventHandler' \| 'function'
23942381

23952382
</td><td>
23962383

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,10 @@ export type QwikBuildTarget = 'client' | 'ssr' | 'lib' | 'test';
167167

168168
// @public (undocumented)
169169
export interface QwikBundle {
170-
// (undocumented)
171170
dynamicImports?: string[];
172-
// (undocumented)
173-
hasSymbols?: boolean;
174-
// (undocumented)
175171
imports?: string[];
176-
// (undocumented)
177172
origins?: string[];
178-
// (undocumented)
179173
size: number;
180-
// (undocumented)
181174
symbols?: string[];
182175
}
183176

@@ -244,7 +237,7 @@ export interface QwikSymbol {
244237
// (undocumented)
245238
captures: boolean;
246239
// (undocumented)
247-
ctxKind: 'function' | 'event';
240+
ctxKind: 'function' | 'eventHandler';
248241
// (undocumented)
249242
ctxName: string;
250243
// (undocumented)
@@ -320,7 +313,7 @@ interface SegmentAnalysis {
320313
// (undocumented)
321314
captures: boolean;
322315
// (undocumented)
323-
ctxKind: 'event' | 'function';
316+
ctxKind: 'eventHandler' | 'function';
324317
// (undocumented)
325318
ctxName: string;
326319
// (undocumented)

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ function prioritizeSymbolNames(manifest: QwikManifest) {
1515
const b = symbols[symbolNameB];
1616

1717
// events should sort highest
18-
if (a.ctxKind === 'event' && b.ctxKind !== 'event') {
18+
if (a.ctxKind === 'eventHandler' && b.ctxKind !== 'eventHandler') {
1919
return -1;
2020
}
21-
if (a.ctxKind !== 'event' && b.ctxKind === 'event') {
21+
if (a.ctxKind !== 'eventHandler' && b.ctxKind === 'eventHandler') {
2222
return 1;
2323
}
2424

25-
if (a.ctxKind === 'event' && b.ctxKind === 'event') {
26-
// both are an event
25+
if (a.ctxKind === 'eventHandler' && b.ctxKind === 'eventHandler') {
26+
// both are an event handler
2727
const aIndex = EVENT_PRIORITY.indexOf(a.ctxName.toLowerCase());
2828
const bIndex = EVENT_PRIORITY.indexOf(b.ctxName.toLowerCase());
2929

@@ -281,20 +281,15 @@ export function generateManifestFromBundles(
281281
size: outputBundle.code.length,
282282
};
283283

284-
let hasSymbols = false;
285284
for (const symbol of outputBundle.exports) {
286285
if (qrlNames.has(symbol)) {
287286
// When not minifying we see both the entry and the segment file
288287
// The segment file will only have 1 export, we want the entry
289288
if (!manifest.mapping[symbol] || outputBundle.exports.length !== 1) {
290-
hasSymbols = true;
291289
manifest.mapping[symbol] = bundleFileName;
292290
}
293291
}
294292
}
295-
if (hasSymbols) {
296-
bundle.hasSymbols = true;
297-
}
298293

299294
const bundleImports = outputBundle.imports
300295
// Tree shaking might remove imports

packages/qwik/src/optimizer/src/plugins/bundle-graph.ts

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,30 @@ export function convertManifestToBundleGraph(
5454
}
5555
}
5656

57+
// Filter out external and non-segment dynamic imports
58+
for (const bundleName of Object.keys(graph)) {
59+
const bundle = graph[bundleName];
60+
const imports = bundle.imports?.filter((dep) => graph[dep]) || [];
61+
62+
// We only include dynamic imports that have qrl segments
63+
// If the dev wants to include other dynamic imports, they can just make a qrl()
64+
const dynamicImports = bundle.dynamicImports?.filter((dep) => graph[dep]?.hasSegments) || [];
65+
66+
// Overwrite so we don't mutate
67+
graph[bundleName] = {
68+
imports,
69+
dynamicImports,
70+
size: bundle.size,
71+
};
72+
}
73+
5774
// Remove unused bundles
5875
const notUsed = new Set(Object.keys(graph));
5976
for (const bundleName of Object.keys(graph)) {
60-
for (const dep of graph[bundleName].imports || []) {
77+
for (const dep of graph[bundleName].imports!) {
6178
notUsed.delete(dep);
6279
}
63-
for (const dep of graph[bundleName].dynamicImports || []) {
80+
for (const dep of graph[bundleName].dynamicImports!) {
6481
notUsed.delete(dep);
6582
}
6683
}
@@ -79,11 +96,7 @@ export function convertManifestToBundleGraph(
7996
seen: Set<string> = new Set()
8097
) => {
8198
const bundle = graph[bundleName];
82-
if (!bundle) {
83-
// external dependency
84-
return;
85-
}
86-
for (const dep of bundle.imports || []) {
99+
for (const dep of bundle.imports!) {
87100
if (parentDeps.has(dep)) {
88101
parentDeps.delete(dep);
89102
}
@@ -93,9 +106,6 @@ export function convertManifestToBundleGraph(
93106
}
94107
}
95108
};
96-
const withoutExternalDependencies = (imports: string[] | undefined) => {
97-
return imports?.filter((dep) => graph[dep]) || [];
98-
};
99109

100110
/**
101111
* First pass to collect minimal dependency lists and allocate space for dependencies. Minimal
@@ -104,11 +114,12 @@ export function convertManifestToBundleGraph(
104114
*/
105115
for (const bundleName of names) {
106116
const bundle = graph[bundleName];
107-
const deps = new Set(withoutExternalDependencies(bundle.imports));
117+
// external dependencies are not included in `graph`
118+
const deps = new Set(bundle.imports!);
108119
for (const depName of deps) {
109120
clearTransitiveDeps(deps, depName);
110121
}
111-
const dynDeps = new Set(withoutExternalDependencies(bundle.dynamicImports));
122+
const dynDeps = new Set(bundle.dynamicImports!);
112123
for (const depName of dynDeps) {
113124
clearTransitiveDeps(dynDeps, depName);
114125
}
@@ -130,11 +141,7 @@ export function convertManifestToBundleGraph(
130141

131142
// Second pass to set the dependency indices
132143
for (const bundleName of names) {
133-
const bundle = map.get(bundleName);
134-
if (!bundle) {
135-
console.warn(`Bundle ${bundleName} not found in the bundle graph.`);
136-
continue;
137-
}
144+
const bundle = map.get(bundleName)!;
138145
// eslint-disable-next-line prefer-const
139146
let { index, deps } = bundle;
140147
index++;

0 commit comments

Comments
 (0)