Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: optimizeDeps.include missing in known imports fallback #7218

Merged
merged 4 commits into from Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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