Skip to content

Commit 3c4b87a

Browse files
committed
fix(rollup): over-prefetching
1 parent 3dc06de commit 3c4b87a

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@builder.io/qwik': patch
3+
---
4+
5+
🐞 fix(rollup): improve manualChunks logic to minimize over-prefetching

packages/qwik/src/optimizer/src/plugins/plugin.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,47 @@ export const manifest = ${JSON.stringify(manifest)};\n`;
875875
// order by discovery time, so that related segments are more likely to group together
876876
function manualChunks(id: string, { getModuleInfo }: Rollup.ManualChunkMeta) {
877877
const module = getModuleInfo(id)!;
878-
const segment = module.meta.segment as SegmentAnalysis | undefined;
879-
return segment?.entry;
878+
const segment = module.meta.segment;
879+
880+
if (segment) {
881+
// We need to specifically return segment.entry for qwik-insights
882+
return segment.entry;
883+
}
884+
885+
// To prevent over-prefetching, we need to clearly seperate those chunks,
886+
// otherwise rollup can bundle them together with the first component chunk it finds.
887+
// For example, the core code could go into an Accordion.tsx chunk, which would make the whole app import accordion related chunks everywhere.
888+
if (/\/(qwik|core)\/dist\/core.*js$/.test(id) || id.includes('@builder.io/qwik/build')) {
889+
return 'core';
890+
}
891+
if (/\/(qwik-city|router)\/lib\/index.qwik.*js$/.test(id)) {
892+
return 'qwik-city';
893+
}
894+
if (id.endsWith('vite/preload-helper.js')) {
895+
return 'preload-helper';
896+
}
897+
898+
// We can't return a chunk for each module as that creates too many small chunks that slow down the prefetching as well,
899+
// nor can we bundle related node_modules together (e.g. all shiki modules together), as that can create very big 10MB chunks.
900+
// So here we let rollup do its job.
901+
if (id.includes('node_modules')) {
902+
return null;
903+
}
904+
905+
// Also to prevent over-prefetching, we must clearly separate those chunks so that rollup doesn't add additional imports into entry files.
906+
// We do this after the node_modules check, because some node_modules can end with .js, .ts, etc.
907+
if (/\.(qwik\.mjs|qwik\.cjs|tsx|jsx|mdx|ts|js)$/.test(id)) {
908+
const optimizer = getOptimizer();
909+
const path = optimizer.sys.path;
910+
const relativePath = path.relative(optimizer.sys.cwd(), id);
911+
const sanitizedPath = relativePath
912+
.replace(/^(\.\.\/)+/, '')
913+
.replace(/^\/+/, '')
914+
.replace(/\//g, '-');
915+
return sanitizedPath; // We return sanitizedPath for qwikVite plugin with debug:true
916+
}
917+
918+
return null;
880919
}
881920

882921
return {

0 commit comments

Comments
 (0)