Skip to content

Commit

Permalink
fix(optimizer): detect re-exports in dep entries
Browse files Browse the repository at this point in the history
fix #2219
  • Loading branch information
yyx990803 committed Feb 26, 2021
1 parent 2900a9a commit a3abf99
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
9 changes: 7 additions & 2 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,15 +152,20 @@ export function esbuildDepPlugin(
}

let contents = ''
const [imports, exports] = exportsData[id]
const data = exportsData[id]
const [imports, exports] = data
if (!imports.length && !exports.length) {
// cjs
contents += `export default require("${relativePath}");`
} else {
if (exports.includes('default')) {
contents += `import d from "${relativePath}";export default d;`
}
if (exports.length > 1 || exports[0] !== 'default') {
if (
data.hasReExports ||
exports.length > 1 ||
exports[0] !== 'default'
) {
contents += `\nexport * from "${relativePath}"`
}
}
Expand Down
15 changes: 13 additions & 2 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ import { ensureService, stopService } from '../plugins/esbuild'

const debug = createDebugger('vite:deps')

export type ExportsData = [ImportSpecifier[], string[]]
export type ExportsData = [ImportSpecifier[], string[]] & {
// es-module-lexer has a facade detection but isn't always accurate for our
// use case when the module has default export
hasReExports?: true
}

export interface DepOptimizationOptions {
/**
Expand Down Expand Up @@ -203,7 +207,14 @@ export async function optimizeDeps(
for (const id in deps) {
const flatId = flattenId(id)
flatIdDeps[flatId] = deps[id]
const exportsData = parse(fs.readFileSync(deps[id], 'utf-8'))
const entryContent = fs.readFileSync(deps[id], 'utf-8')
const exportsData = parse(entryContent) as ExportsData
for (const { ss, se } of exportsData[0]) {
const exp = entryContent.slice(ss, se)
if (/export\s+\*\s+from/.test(exp)) {
exportsData.hasReExports = true
}
}
idToExports[id] = exportsData
flatIdToExports[flatId] = exportsData
}
Expand Down

0 comments on commit a3abf99

Please sign in to comment.