@@ -877,32 +877,41 @@ export const manifest = ${JSON.stringify(manifest)};\n`;
877
877
const module = getModuleInfo ( id ) ! ;
878
878
const segment = module . meta . segment ;
879
879
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 ;
890
883
}
891
884
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' ;
894
890
}
895
-
896
891
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' )
905
894
) {
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 ( / \. ( q w i k \. m j s | q w i k \. c j s | t s x | j s x | m d x | t s | j s ) $ / . 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, '-' ) ;
906
915
return sanitizedPath ;
907
916
}
908
917
0 commit comments