Skip to content

Commit

Permalink
fix(optimizer): fix entry matching edge case
Browse files Browse the repository at this point in the history
fix #1661 again
  • Loading branch information
yyx990803 committed Jan 26, 2021
1 parent 29d149d commit c5fe45f
Showing 1 changed file with 22 additions and 15 deletions.
37 changes: 22 additions & 15 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,25 +267,32 @@ export async function optimizeDeps(
if (/\.vite[\/\\]chunk\.\w+\.js$/.test(output) || output.endsWith('.map'))
continue
const { inputs, exports: generatedExports } = meta.outputs[output]
const relativeOutput = normalizePath(
path.relative(cacheDir, path.resolve(output))
)
for (const input in inputs) {
const entry = normalizePath(path.resolve(input))
if (!entry.endsWith(relativeOutput)) {
continue
}
const id = entryToIdMap[entry.toLowerCase()]
if (id) {
// check if this is a cjs dep.
const [imports, exports] = parse(fs.readFileSync(entry, 'utf-8'))
data.optimized[id] = {
file: normalizePath(path.resolve(output)),
needsInterop:
// entry has no ESM syntax - likely CJS or UMD
(!exports.length && !imports.length) ||
// if a peer dep used require() on a ESM dep, esbuild turns the
// ESM dep's entry chunk into a single default export... detect
// such cases by checking exports mismatch, and force interop.
(isSingleDefaultExport(generatedExports) &&
!isSingleDefaultExport(exports))
}
break
if (!id) {
continue
}
// check if this is a cjs dep.
const [imports, exports] = parse(fs.readFileSync(entry, 'utf-8'))
data.optimized[id] = {
file: normalizePath(path.resolve(output)),
needsInterop:
// entry has no ESM syntax - likely CJS or UMD
(!exports.length && !imports.length) ||
// if a peer dep used require() on a ESM dep, esbuild turns the
// ESM dep's entry chunk into a single default export... detect
// such cases by checking exports mismatch, and force interop.
(isSingleDefaultExport(generatedExports) &&
!isSingleDefaultExport(exports))
}
break
}
}

Expand Down

0 comments on commit c5fe45f

Please sign in to comment.