@@ -44,7 +44,7 @@ export function generate_manifest({ build_data, relative_path, routes }) {
4444 ) ;
4545
4646 /** @type {(path: string) => string } */
47- const loader = ( path ) => `( ) => import('${ path } ')` ;
47+ const loader = ( path ) => `__memo(( ) => import('${ path } ') )` ;
4848
4949 const assets = build_data . manifest_data . assets . map ( ( asset ) => asset . file ) ;
5050 if ( build_data . service_worker ) {
@@ -65,8 +65,8 @@ export function generate_manifest({ build_data, relative_path, routes }) {
6565
6666 // prettier-ignore
6767 // String representation of
68- /** @type {import('@sveltejs/kit').SSRManifest } */
69- return dedent `
68+ /** @template {import('@sveltejs/kit').SSRManifest } T */
69+ const manifest_expr = dedent `
7070 {
7171 appDir: ${ s ( build_data . app_dir ) } ,
7272 appPath: ${ s ( build_data . app_path ) } ,
@@ -97,10 +97,26 @@ export function generate_manifest({ build_data, relative_path, routes }) {
9797 } ) . filter ( Boolean ) . join ( ',\n' ) }
9898 ],
9999 matchers: async () => {
100- ${ Array . from ( matchers ) . map ( type => `const { match: ${ type } } = await import ('${ ( join_relative ( relative_path , `/entries/matchers/${ type } .js` ) ) } ')` ) . join ( '\n' ) }
100+ ${ Array . from (
101+ matchers ,
102+ type => `const { match: ${ type } } = await import ('${ ( join_relative ( relative_path , `/entries/matchers/${ type } .js` ) ) } ')`
103+ ) . join ( '\n' ) }
101104 return { ${ Array . from ( matchers ) . join ( ', ' ) } };
102105 }
103106 }
104107 }
105108 ` ;
109+
110+ // Memoize the loaders to prevent Node from doing unnecessary work
111+ // on every dynamic import call
112+ return dedent `
113+ (() => {
114+ function __memo(fn) {
115+ let value;
116+ return () => value ??= (value = fn());
117+ }
118+
119+ return ${ manifest_expr }
120+ })()
121+ ` ;
106122}
0 commit comments