From 0cc7be13a1ec125d888bc40f00be280bdc81c8fe Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Fri, 6 May 2022 14:33:37 +0200 Subject: [PATCH] fix: use distinct prefix for lazy handler imports --- src/rollup/plugins/handlers.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/rollup/plugins/handlers.ts b/src/rollup/plugins/handlers.ts index 974d450a63..590ee046d0 100644 --- a/src/rollup/plugins/handlers.ts +++ b/src/rollup/plugins/handlers.ts @@ -9,7 +9,7 @@ import { virtual } from './virtual' const unique = (arr: any[]) => Array.from(new Set(arr)) export function handlers (nitro: Nitro) { - const getImportId = p => '_' + hasha(p).slice(0, 6) + const getImportId = (p: string, lazy?: boolean) => (lazy ? '_lazy_' : '_') + hasha(p).slice(0, 6) let lastDump = '' @@ -40,15 +40,16 @@ export function handlers (nitro: Nitro) { const imports = unique(handlers.filter(h => !h.lazy).map(h => h.handler)) // Lazy imports should fill in the gaps - const lazyImports = unique(handlers.filter(h => h.lazy && !imports.includes(h.handler)).map(h => h.handler)) + // TODO: At least warn if a handler is imported both lazy and non lazy + const lazyImports = unique(handlers.filter(h => h.lazy).map(h => h.handler)) const code = ` ${imports.map(handler => `import ${getImportId(handler)} from '${handler}';`).join('\n')} -${lazyImports.map(handler => `const ${getImportId(handler)} = () => import('${handler}');`).join('\n')} +${lazyImports.map(handler => `const ${getImportId(handler, true)} = () => import('${handler}');`).join('\n')} export const handlers = [ -${handlers.map(h => ` { route: '${h.route || ''}', handler: ${getImportId(h.handler)}, lazy: ${!!h.lazy}, middleware: ${!!h.middleware}, method: ${JSON.stringify(h.method)} }`).join(',\n')} +${handlers.map(h => ` { route: '${h.route || ''}', handler: ${getImportId(h.handler, h.lazy)}, lazy: ${!!h.lazy}, middleware: ${!!h.middleware}, method: ${JSON.stringify(h.method)} }`).join(',\n')} ]; `.trim() return code