From f7378db9e0ef72c860879c04459a0fd92d846a66 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Sat, 14 Nov 2020 02:43:13 +0100 Subject: [PATCH] feat: add hint to dynamic require for netlify --- packages/nitro/src/rollup/dynamic-require.ts | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/packages/nitro/src/rollup/dynamic-require.ts b/packages/nitro/src/rollup/dynamic-require.ts index 5ae140fd0d..36602e520d 100644 --- a/packages/nitro/src/rollup/dynamic-require.ts +++ b/packages/nitro/src/rollup/dynamic-require.ts @@ -12,7 +12,7 @@ interface Import { import: string } -const TMPL_INLINE = ({ imports }: { imports: Import[]}) => +const TMPL_ESM_INLINE = ({ imports }: { imports: Import[]}) => `${imports.map(i => `import ${i.name} from '${i.import.replace(/\\/g, '/')}'`).join('\n')} const dynamicChunks = { ${imports.map(i => ` ['${i.id}']: ${i.name}`).join(',\n')} @@ -22,10 +22,19 @@ export default function dynamicRequire(id) { return dynamicChunks[id]; };` -const TMPL_CJS = ({ chunksDir }) => `export default function dynamicRequire(id) { -return require('./${chunksDir}/' + id); +const TMPL_CJS_LAZY = ({ imports, chunksDir }) => + `const dynamicChunks = { +${imports.map(i => ` ['${i.id}']: () => require('./${chunksDir}/${i.id}')`).join(',\n')} +}; + +export default function dynamicRequire(id) { + return dynamicChunks[id]; };` +// const TMPL_CJS = ({ chunksDir }) => `export default function dynamicRequire(id) { +// return require('./${chunksDir}/' + id); +// };` + interface Options { dir: string globbyOptions: GlobbyOptions @@ -53,7 +62,7 @@ export default function dynamicRequire ({ dir, globbyOptions, outDir, chunksDir })) if (!outDir) { - return TMPL_INLINE({ imports }) + return TMPL_ESM_INLINE({ imports }) } // Write chunks @@ -63,7 +72,7 @@ export default function dynamicRequire ({ dir, globbyOptions, outDir, chunksDir await mkdirp(dirname(dst)) await copyFile(i.import, dst) })) - return TMPL_CJS({ chunksDir }) + return TMPL_CJS_LAZY({ chunksDir, imports }) } return null }