@@ -875,8 +875,47 @@ export const manifest = ${JSON.stringify(manifest)};\n`;
875
875
// order by discovery time, so that related segments are more likely to group together
876
876
function manualChunks ( id : string , { getModuleInfo } : Rollup . ManualChunkMeta ) {
877
877
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 ( / \/ ( q w i k | c o r e ) \/ d i s t \/ c o r e .* j s $ / . test ( id ) || id . includes ( '@builder.io/qwik/build' ) ) {
889
+ return 'core' ;
890
+ }
891
+ if ( / \/ ( q w i k - c i t y | r o u t e r ) \/ l i b \/ i n d e x .q w i k .* j s $ / . 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 ( / \. ( 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 ) ) {
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 ;
880
919
}
881
920
882
921
return {
0 commit comments