Skip to content

Commit

Permalink
fix(nitro): filter out duplicate imports (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe authored Jul 26, 2021
1 parent ec207e9 commit 6ce6f68
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/rollup/plugins/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import stdenv from 'std-env'
import type { ServerMiddleware } from '../../server/middleware'
import virtual from './virtual'

const unique = (arr: any[]) => Array.from(new Set(arr))

export function middleware (getMiddleware: () => ServerMiddleware[]) {
const getImportId = p => '_' + hasha(p).substr(0, 6)

Expand All @@ -26,10 +28,16 @@ export function middleware (getMiddleware: () => ServerMiddleware[]) {
}
}

// Imports take priority
const imports = unique(middleware.filter(m => m.lazy === false).map(m => m.handle))

// Lazy imports should fill in the gaps
const lazyImports = unique(middleware.filter(m => m.lazy !== false && !imports.includes(m.handle)).map(m => m.handle))

return `
${middleware.filter(m => m.lazy === false).map(m => `import ${getImportId(m.handle)} from '${m.handle}';`).join('\n')}
${imports.map(handle => `import ${getImportId(handle)} from '${handle}';`).join('\n')}
${middleware.filter(m => m.lazy !== false).map(m => `const ${getImportId(m.handle)} = () => import('${m.handle}');`).join('\n')}
${lazyImports.map(handle => `const ${getImportId(handle)} = () => import('${handle}');`).join('\n')}
const middleware = [
${middleware.map(m => `{ route: '${m.route}', handle: ${getImportId(m.handle)}, lazy: ${m.lazy || true}, promisify: ${m.promisify !== undefined ? m.promisify : true} }`).join(',\n')}
Expand Down

0 comments on commit 6ce6f68

Please sign in to comment.