Skip to content

Commit

Permalink
fix: handle edge case and simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Apr 3, 2022
1 parent 19e7168 commit 5837ce9
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 36 deletions.
55 changes: 20 additions & 35 deletions packages/vite/src/node/optimizer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ export interface DepOptimizationOptions {

export interface DepOptimizationResult {
metadata: DepOptimizationMetadata
needsInteropMismatch: string[]
/**
* When doing a re-run, if there are newly discovered dependendencies
* the page reload will be delayed until the next rerun so we need
Expand Down Expand Up @@ -381,7 +380,6 @@ export async function runOptimizeDeps(

const processingResult: DepOptimizationResult = {
metadata,
needsInteropMismatch: [],
commit() {
// Write metadata file, delete `deps` folder and rename the `processing` folder to `deps`
// Processing is done, we can now replace the depsCacheDir with processingCacheDir
Expand Down Expand Up @@ -427,8 +425,6 @@ export async function runOptimizeDeps(
flatIdDeps[flatId] = src
idToExports[id] = exportsData
flatIdToExports[flatId] = exportsData

depsInfo[id].needsInterop ??= needsInterop(id, idToExports[id], config)
}

const define: Record<string, string> = {
Expand Down Expand Up @@ -481,17 +477,11 @@ export async function runOptimizeDeps(
fileHash: getHash(
metadata.hash + depsInfo[id].file + JSON.stringify(output.imports)
),
browserHash: metadata.browserHash
browserHash: metadata.browserHash,
// After bundling we have more information and can warn the user about legacy packages
// that require manual configuration
needsInterop: needsInterop(config, id, idToExports[id], output)
})

// After bundling we have more information and can warn the user about legacy packages
// that require manual configuration
if (
!depsInfo[id].needsInterop &&
hasExportsMismatch(idToExports[id], output)
) {
processingResult.needsInteropMismatch.push(id)
}
}

for (const o of Object.keys(meta.outputs)) {
Expand Down Expand Up @@ -778,9 +768,10 @@ export async function extractExportsData(
const KNOWN_INTEROP_IDS = new Set(['moment'])

function needsInterop(
config: ResolvedConfig,
id: string,
exportsData: ExportsData,
config: ResolvedConfig
output?: { exports: string[] }
): boolean {
if (
config.optimizeDeps?.needsInterop?.includes(id) ||
Expand All @@ -793,26 +784,20 @@ function needsInterop(
if (!exports.length && !imports.length) {
return true
}
// ESM module
return false
}

function hasExportsMismatch(
exportsData: ExportsData,
output: { exports: string[] }
): boolean {
const [, exports] = exportsData

// if a peer dependency used require() on a ESM dependency, esbuild turns the
// ESM dependency's entry chunk into a single default export... we can detect
// such cases by checking exports mismatch, and warn the user.
const generatedExports: string[] = output.exports
if (output) {
// if a peer dependency used require() on a ESM dependency, esbuild turns the
// ESM dependency's entry chunk into a single default export... detect
// such cases by checking exports mismatch, and force interop.
const generatedExports: string[] = output.exports

if (
!generatedExports ||
(isSingleDefaultExport(generatedExports) && !isSingleDefaultExport(exports))
) {
return true
if (
!generatedExports ||
(isSingleDefaultExport(generatedExports) &&
!isSingleDefaultExport(exports))
) {
return true
}
}
return false
}
Expand Down Expand Up @@ -906,9 +891,9 @@ export async function optimizedDepNeedsInterop(
if (depInfo?.src && depInfo.needsInterop === undefined) {
depInfo.exportsData ??= extractExportsData(depInfo.src, config)
depInfo.needsInterop = needsInterop(
config,
depInfo.id,
await depInfo.exportsData,
config
await depInfo.exportsData
)
}
return depInfo?.needsInterop
Expand Down
18 changes: 17 additions & 1 deletion packages/vite/src/node/optimizer/registerMissing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,23 @@ export function createOptimizedDeps(server: ViteDevServer): OptimizedDeps {
try {
const processingResult = await runOptimizeDeps(config, newDeps)

const { metadata: newData, needsInteropMismatch } = processingResult
const newData = processingResult.metadata

const needsInteropMismatch = []
for (const dep in metadata.discovered) {
const discoveredDepInfo = metadata.discovered[dep]
const depInfo = newData.optimized[dep]
if (depInfo) {
if (
discoveredDepInfo.needsInterop !== undefined &&
depInfo.needsInterop !== discoveredDepInfo.needsInterop
) {
// This only happens when a discovered dependency has mixed ESM and CJS syntax
// and it hasn't been manually added to optimizeDeps.needsInterop
needsInteropMismatch.push(dep)
}
}
}

// After a re-optimization, if the internal bundled chunks change a full page reload
// is required. If the files are stable, we can avoid the reload that is expensive
Expand Down

0 comments on commit 5837ce9

Please sign in to comment.