Skip to content

Commit

Permalink
fix: use distinct prefix for lazy handler imports
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed May 6, 2022
1 parent 5a83ec6 commit 0cc7be1
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/rollup/plugins/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ''

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 0cc7be1

Please sign in to comment.