Skip to content

fix(qwik): over-preloading of all non-node_modules dynamic imports #7549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@
"program": "${workspaceFolder}/packages/docs/node_modules/vite/bin/vite.js",
"args": ["build", "-c", "adapters/cloudflare-pages/vite.config.mts"]
},
{
"type": "node",
"name": "preloader-test build.client",
"request": "launch",
"runtimeExecutable": "pnpm",
"runtimeArgs": [
"run",
"build.client"
],
"skipFiles": ["<node_internals>/**"],
"cwd": "${workspaceFolder}/starters/apps/preloader-test"
},
{
"type": "node",
"name": "e2e.test",
Expand Down
10 changes: 0 additions & 10 deletions packages/qwik-city/src/buildtime/vite/get-route-imports.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { QwikBundle, QwikManifest } from '@builder.io/qwik/optimizer';
import { removeExtension } from '../../utils/fs';
import type { BuildRoute } from '../types';
import { QWIK_CITY_PLAN_ID } from './plugin';

export function getRouteImports(routes: BuildRoute[], manifest: QwikManifest) {
const result: Record<string, { imports?: string[]; dynamicImports?: string[] }> = {};
Expand All @@ -23,15 +22,6 @@ export function getRouteImports(routes: BuildRoute[], manifest: QwikManifest) {
result[route.routeName] = { dynamicImports: bundles };
}
});
for (const bundleName of Object.keys(manifest.bundles)) {
const bundle = manifest.bundles[bundleName];
if (bundle.origins?.some((s) => s.endsWith(QWIK_CITY_PLAN_ID))) {
// Don't consider the city plan for preloading
// we keep imports because something might be bundled with it
result[bundleName] = { imports: bundle.imports, dynamicImports: [] };
break;
}
}
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,6 @@ describe('modifyBundleGraph', () => {
"fake-bundle-part-of-layout.js",
],
},
"q-city-plan.js": {
"dynamicImports": [],
"imports": undefined,
},
}
`);
});
Expand Down
21 changes: 13 additions & 8 deletions packages/qwik/src/optimizer/src/plugins/bundle-graph.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import type { QwikBundle, QwikBundleGraph, QwikManifest } from '../types';

const minimumSpeed = 300; // kbps
Expand Down Expand Up @@ -69,16 +70,20 @@ export function convertManifestToBundleGraph(
for (const bundleName of Object.keys(graph)) {
const bundle = graph[bundleName];
const imports = bundle.imports?.filter((dep) => graph[dep]) || [];

const dynamicImports =
bundle.dynamicImports?.filter(
// we only want to include dynamic imports that belong to the app
// e.g. not all languages supported by shiki
(dep) =>
graph[dep] &&
// either there are qrls
(graph[dep].symbols ||
// or it's a dynamic import from the app source
graph[dep].origins?.some((o) => !o.includes('node_modules')))
// we only want to include symbols to avoid preloading all the dynamically imports of external modules
// e.g. syntax files from shiki (> 10MB) or import.meta.glob files (dep) =>
(dep) => {
return (
graph[dep] &&
// either there are qrls
(graph[dep].symbols ||
// or it's a dynamic import from the app source
graph[dep].origins?.some((o) => /@qwik-city-plan|index\.tsx$|layout\.tsx$/.test(o)))
);
}
) || [];

/**
Expand Down
Loading