Skip to content

Commit bdc32df

Browse files
authored
Merge pull request #7488 from wmertens/fixes
chore: some small fixes
2 parents 46e83fb + 8395737 commit bdc32df

File tree

8 files changed

+114
-41
lines changed

8 files changed

+114
-41
lines changed

packages/docs/src/entry.ssr.tsx

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
11
import { renderToStream, type RenderToStreamOptions } from '@builder.io/qwik/server';
22
import Root from './root';
33

4+
// You can pass these as query parameters, as well as `preloadDebug`
5+
const preloaderSettings = [
6+
'maxPreloads',
7+
'minProbability',
8+
'maxSimultaneousPreloads',
9+
'minPreloadProbability',
10+
] as const;
11+
412
export default function (opts: RenderToStreamOptions) {
13+
const { serverData } = opts;
14+
const urlStr = serverData?.url;
15+
if (urlStr) {
16+
const { searchParams } = new URL(urlStr);
17+
if (searchParams.size) {
18+
opts = {
19+
...opts,
20+
prefetchStrategy: {
21+
...opts.prefetchStrategy,
22+
implementation: { ...opts.prefetchStrategy?.implementation },
23+
},
24+
};
25+
if (searchParams.has('preloadDebug')) {
26+
opts.prefetchStrategy!.implementation!.debug = true;
27+
}
28+
for (const type of preloaderSettings) {
29+
if (searchParams.has(type)) {
30+
opts.prefetchStrategy!.implementation![type] = Number(searchParams.get(type));
31+
}
32+
}
33+
}
34+
}
535
return renderToStream(<Root />, {
636
qwikLoader: {
737
// The docs can be long so make sure to intercept events before the end of the document.

packages/qwik/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
}
2626
],
2727
"dependencies": {
28-
"csstype": "^3.1"
28+
"csstype": "^3.1",
29+
"rollup": ">= 4.39.0"
2930
},
3031
"devDependencies": {
3132
"@builder.io/qwik": "workspace:^",

packages/qwik/src/core/preloader/bundle-graph.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
} from './constants';
88
import { adjustProbabilities, bundles, log, trigger } from './queue';
99
import type { BundleGraph, BundleImport, ImportProbability } from './types';
10-
import { BundleImportState } from './types';
10+
import { BundleImportState_None, BundleImportState_Alias } from './types';
1111

1212
export let base: string | undefined;
1313
export let graph: BundleGraph;
@@ -21,7 +21,7 @@ const makeBundle = (name: string, deps?: ImportProbability[]) => {
2121
return {
2222
$name$: name,
2323
$url$: url,
24-
$state$: url ? BundleImportState.None : BundleImportState.Alias,
24+
$state$: url ? BundleImportState_None : BundleImportState_Alias,
2525
$deps$: deps,
2626
$inverseProbability$: 1,
2727
$createdTs$: Date.now(),

packages/qwik/src/core/preloader/queue.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import {
99
rel,
1010
} from './constants';
1111
import type { BundleImport, BundleImports } from './types';
12-
import { BundleImportState } from './types';
12+
import {
13+
BundleImportState_None,
14+
BundleImportState_Queued,
15+
BundleImportState_Preload,
16+
BundleImportState_Loaded,
17+
} from './types';
1318

1419
export const bundles: BundleImports = new Map();
1520
let queueDirty: boolean;
@@ -81,7 +86,8 @@ export const trigger = () => {
8186
Math.max(1, config[maxSimultaneousPreloadsStr] * probability)
8287
: // While the graph is not available, we limit to 2 preloads
8388
2;
84-
if (preloadCount < allowedPreloads) {
89+
// When we're 100% sure, everything needs to be queued
90+
if (probability === 1 || preloadCount < allowedPreloads) {
8591
queue.shift();
8692
preloadOne(bundle);
8793
} else {
@@ -93,7 +99,7 @@ export const trigger = () => {
9399
* for other resources, so we cycle between 4 and 10 outstanding modulepreloads.
94100
*/
95101
if (config.DEBUG && !queue.length) {
96-
const loaded = [...bundles.values()].filter((b) => b.$state$ > BundleImportState.None);
102+
const loaded = [...bundles.values()].filter((b) => b.$state$ > BundleImportState_None);
97103
const waitTime = loaded.reduce((acc, b) => acc + b.$waitedMs$, 0);
98104
const loadTime = loaded.reduce((acc, b) => acc + b.$loadedMs$, 0);
99105
log(
@@ -103,16 +109,20 @@ export const trigger = () => {
103109
};
104110

105111
const preloadOne = (bundle: BundleImport) => {
106-
if (bundle.$state$ >= BundleImportState.Preload) {
112+
if (bundle.$state$ >= BundleImportState_Preload) {
107113
return;
108114
}
109115
preloadCount++;
110116

111117
const start = Date.now();
112118
bundle.$waitedMs$ = start - bundle.$createdTs$;
113-
bundle.$state$ = BundleImportState.Preload;
119+
bundle.$state$ = BundleImportState_Preload;
114120

115-
config.DEBUG && log(`<< load after ${`${bundle.$waitedMs$}ms`}`, bundle.$name$);
121+
config.DEBUG &&
122+
log(
123+
`<< load ${Math.round((1 - bundle.$inverseProbability$) * 100)}% after ${`${bundle.$waitedMs$}ms`}`,
124+
bundle.$name$
125+
);
116126

117127
const link = doc.createElement('link');
118128
link.href = bundle.$url$!;
@@ -124,7 +134,7 @@ const preloadOne = (bundle: BundleImport) => {
124134
preloadCount--;
125135
const end = Date.now();
126136
bundle.$loadedMs$ = end - start;
127-
bundle.$state$ = BundleImportState.Loaded;
137+
bundle.$state$ = BundleImportState_Loaded;
128138
config.DEBUG && log(`>> done after ${bundle.$loadedMs$}ms`, bundle.$name$);
129139
// Keep the <head> clean
130140
link.remove();
@@ -151,11 +161,11 @@ export const adjustProbabilities = (
151161
}
152162

153163
if (
154-
bundle.$state$ < BundleImportState.Preload &&
164+
bundle.$state$ < BundleImportState_Preload &&
155165
bundle.$inverseProbability$ < config[maxSignificantInverseProbabilityStr]
156166
) {
157-
if (bundle.$state$ === BundleImportState.None) {
158-
bundle.$state$ = BundleImportState.Queued;
167+
if (bundle.$state$ === BundleImportState_None) {
168+
bundle.$state$ = BundleImportState_Queued;
159169
queue.push(bundle);
160170
config.DEBUG &&
161171
log(`queued ${Math.round((1 - bundle.$inverseProbability$) * 100)}%`, bundle.$name$);

packages/qwik/src/core/preloader/types.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
export const enum BundleImportState {
2-
None,
3-
Queued,
4-
Preload,
5-
Alias,
6-
Loaded,
7-
}
1+
export const BundleImportState_None = 0;
2+
export const BundleImportState_Queued = 1;
3+
export const BundleImportState_Preload = 2;
4+
export const BundleImportState_Alias = 3;
5+
export const BundleImportState_Loaded = 4;
86

97
export type BundleInfo = {
108
$inverseProbability$: number;
@@ -15,7 +13,7 @@ export type BundleInfo = {
1513
export type BundleImport = BundleInfo & {
1614
$name$: string;
1715
$url$: string | null;
18-
$state$: BundleImportState;
16+
$state$: number;
1917
$createdTs$: number;
2018
$waitedMs$: number;
2119
$loadedMs$: number;

packages/qwik/src/server/prefetch-strategy.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,14 @@ export function getPreloadPaths(
4848
}
4949

5050
// If we have a bundle graph, all we need is the symbols
51-
return (snapshotResult?.qrls as QRLInternal[])
52-
?.map((qrl) => getSymbolHash(qrl.$refSymbol$ || qrl.$symbol$))
53-
.filter(Boolean) as string[];
51+
const symbols = new Set<string>();
52+
for (const qrl of (snapshotResult?.qrls || []) as QRLInternal[]) {
53+
const symbol = getSymbolHash(qrl.$refSymbol$ || qrl.$symbol$);
54+
if (symbol && symbol.length >= 10) {
55+
symbols.add(symbol);
56+
}
57+
}
58+
return [...symbols];
5459
}
5560

5661
export const expandBundles = (names: string[], resolvedManifest?: ResolvedManifest) => {

pnpm-lock.yaml

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/submodule-preloader.ts

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,47 @@
11
import { join } from 'node:path';
22
import { build } from 'vite';
33
import { fileSize, type BuildConfig } from './util';
4+
import { minify } from 'terser';
5+
import type { Plugin } from 'vite';
6+
7+
/**
8+
* Custom plugin to apply terser during the bundle generation. Vite doesn't minify library ES
9+
* modules.
10+
*/
11+
function customTerserPlugin(): Plugin {
12+
return {
13+
name: 'custom-terser',
14+
async renderChunk(code, chunk) {
15+
// Only process JavaScript chunks
16+
if (!chunk.fileName.endsWith('.mjs') && !chunk.fileName.endsWith('.js')) {
17+
return null;
18+
}
19+
20+
// Keep the result readable for debugging
21+
const result = await minify(code, {
22+
compress: {
23+
defaults: false,
24+
module: true,
25+
hoist_props: true,
26+
unused: true,
27+
booleans_as_integers: true,
28+
},
29+
mangle: {
30+
toplevel: false,
31+
properties: {
32+
// use short attribute names for internal properties
33+
regex: '^\\$.+\\$$|^[A-Z][a-zA-Z]+$',
34+
},
35+
},
36+
format: {
37+
comments: true,
38+
},
39+
});
40+
41+
return result.code || null;
42+
},
43+
};
44+
}
445

546
/**
647
* Builds the qwikloader javascript files using Vite. These files can be used by other tooling, and
@@ -18,24 +59,10 @@ export async function submodulePreloader(config: BuildConfig) {
1859
rollupOptions: {
1960
external: ['@builder.io/qwik/build'],
2061
},
21-
minify: 'terser',
22-
terserOptions: {
23-
compress: {
24-
dead_code: true,
25-
unused: true,
26-
conditionals: true,
27-
},
28-
mangle: {
29-
toplevel: false,
30-
module: false,
31-
keep_fnames: true,
32-
properties: {
33-
regex: '^\\$.+\\$$',
34-
},
35-
},
36-
},
62+
minify: false, // This is the default, just to be explicit
3763
outDir: config.distQwikPkgDir,
3864
},
65+
plugins: [customTerserPlugin()],
3966
});
4067

4168
const preloaderSize = await fileSize(join(config.distQwikPkgDir, 'preloader.mjs'));

0 commit comments

Comments
 (0)