Skip to content

Commit 3eb278f

Browse files
committed
chore: cleanup and add comments
1 parent df777a8 commit 3eb278f

File tree

1 file changed

+30
-21
lines changed
  • packages/qwik/src/optimizer/src/plugins

1 file changed

+30
-21
lines changed

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

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -877,32 +877,41 @@ export const manifest = ${JSON.stringify(manifest)};\n`;
877877
const module = getModuleInfo(id)!;
878878
const segment = module.meta.segment;
879879

880-
const optimizer = getOptimizer();
881-
const path = optimizer.sys.path;
882-
const relativePath = path.relative(optimizer.sys.cwd(), id);
883-
const sanitizedPath = relativePath.replace(/^\/+/, '').replace(/\//g, '-');
884-
885-
if (sanitizedPath.includes('node_modules')) {
886-
if (sanitizedPath.includes('core.prod')) {
887-
return 'core';
888-
}
889-
return null;
880+
if (segment) {
881+
// We need to specifically return segment.entry for qwik-insights
882+
return segment.entry;
890883
}
891884

892-
if (sanitizedPath.includes('preload-helper')) {
893-
return 'preload-helper';
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 (id.endsWith('qwik/dist/core.prod.mjs') || id.endsWith('qwik/dist/core.prod.cjs')) {
889+
return 'core';
894890
}
895-
896891
if (
897-
segment ||
898-
sanitizedPath.endsWith('.qwik.mjs') ||
899-
sanitizedPath.endsWith('.qwik.cjs') ||
900-
sanitizedPath.endsWith('.tsx') ||
901-
sanitizedPath.endsWith('.jsx') ||
902-
sanitizedPath.endsWith('.mdx') ||
903-
sanitizedPath.endsWith('.ts') ||
904-
sanitizedPath.endsWith('.js')
892+
id.endsWith('qwik-city/lib/index.qwik.mjs') ||
893+
id.endsWith('qwik-city/lib/index.qwik.cjs')
905894
) {
895+
return 'qwik-city';
896+
}
897+
if (id.endsWith('vite/preload-helper.js')) {
898+
return 'preload-helper';
899+
}
900+
901+
// We can't return a chunk for each module as that creates too many small chunks that slow down the prefetching as well,
902+
// nor can we bundle related node_modules together (e.g. all shiki modules together), as that can create very big 10MB chunks.
903+
// So here we let rollup do its job.
904+
if (id.includes('node_modules')) {
905+
return null;
906+
}
907+
908+
// Also to prevent over-prefetching, we must clearly separate those chunks so that rollup doesn't add additional imports into entry files.
909+
// We do this after the node_modules check, because some node_modules can end with .js, .ts, etc.
910+
if (/\.(qwik\.mjs|qwik\.cjs|tsx|jsx|mdx|ts|js)$/.test(id)) {
911+
const optimizer = getOptimizer();
912+
const path = optimizer.sys.path;
913+
const relativePath = path.relative(optimizer.sys.cwd(), id);
914+
const sanitizedPath = relativePath.replace(/^\/+/, '').replace(/\//g, '-');
906915
return sanitizedPath;
907916
}
908917

0 commit comments

Comments
 (0)