Skip to content

Commit

Permalink
fix: optimizeDeps.include missing in known imports fallback (#7218)
Browse files Browse the repository at this point in the history
  • Loading branch information
patak-dev committed Mar 8, 2022
1 parent ed4d191 commit 6c08c86
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 22 deletions.
5 changes: 2 additions & 3 deletions packages/vite/src/node/build.ts
Expand Up @@ -36,8 +36,7 @@ import { buildImportAnalysisPlugin } from './plugins/importAnalysisBuild'
import { resolveSSRExternal, shouldExternalizeForSSR } from './ssr/ssrExternal'
import { ssrManifestPlugin } from './ssr/ssrManifestPlugin'
import type { DepOptimizationMetadata } from './optimizer'
import { getDepsCacheDir } from './optimizer'
import { scanImports } from './optimizer/scan'
import { getDepsCacheDir, findKnownImports } from './optimizer'
import { assetImportMetaUrlPlugin } from './plugins/assetImportMetaUrl'
import { loadFallbackPlugin } from './plugins/loadFallback'
import { watchPackageDataPlugin } from './packages'
Expand Down Expand Up @@ -411,7 +410,7 @@ async function doBuild(
} catch (e) {}
if (!knownImports) {
// no dev deps optimization data, do a fresh scan
knownImports = Object.keys((await scanImports(config)).deps)
knownImports = await findKnownImports(config)
}
external = resolveExternal(
resolveSSRExternal(config, knownImports),
Expand Down
57 changes: 38 additions & 19 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -273,25 +273,11 @@ export async function createOptimizeDepsRun(
)
}

const include = config.optimizeDeps?.include
if (include) {
const resolve = config.createResolver({ asSrc: false })
for (const id of include) {
// normalize 'foo >bar` as 'foo > bar' to prevent same id being added
// and for pretty printing
const normalizedId = normalizeId(id)
if (!deps[normalizedId]) {
const entry = await resolve(id)
if (entry) {
deps[normalizedId] = entry
} else {
processing.resolve()
throw new Error(
`Failed to resolve force included dependency: ${colors.cyan(id)}`
)
}
}
}
try {
await addManuallyIncludedOptimizeDeps(deps, config)
} catch (e) {
processing.resolve()
throw e
}

// update browser hash
Expand Down Expand Up @@ -541,6 +527,39 @@ export async function createOptimizeDepsRun(
}
}

export async function findKnownImports(
config: ResolvedConfig
): Promise<string[]> {
const deps = (await scanImports(config)).deps
await addManuallyIncludedOptimizeDeps(deps, config)
return Object.keys(deps)
}

async function addManuallyIncludedOptimizeDeps(
deps: Record<string, string>,
config: ResolvedConfig
): Promise<void> {
const include = config.optimizeDeps?.include
if (include) {
const resolve = config.createResolver({ asSrc: false })
for (const id of include) {
// normalize 'foo >bar` as 'foo > bar' to prevent same id being added
// and for pretty printing
const normalizedId = normalizeId(id)
if (!deps[normalizedId]) {
const entry = await resolve(id)
if (entry) {
deps[normalizedId] = entry
} else {
throw new Error(
`Failed to resolve force included dependency: ${colors.cyan(id)}`
)
}
}
}
}
}

export function newDepOptimizationProcessing(): DepOptimizationProcessing {
let resolve: (result?: DepOptimizationResult) => void
const promise = new Promise((_resolve) => {
Expand Down

0 comments on commit 6c08c86

Please sign in to comment.