Skip to content

Commit

Permalink
fix: unoptimized package can resolve to optimized deps
Browse files Browse the repository at this point in the history
  • Loading branch information
csr632 committed Dec 18, 2022
1 parent 716055e commit 68bdeef
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
33 changes: 18 additions & 15 deletions packages/vite/src/node/plugins/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { getDepsOptimizer } from '../optimizer'
import { shouldExternalizeForSSR } from '../ssr/ssrExternal'
import { jsonPlugin } from './json'
import { resolvePlugin } from './resolve'
import type { InternalResolveOptions } from './resolve'
import { optimizedDepsBuildPlugin, optimizedDepsPlugin } from './optimizedDeps'
import { esbuildPlugin } from './esbuild'
import { importAnalysisPlugin } from './importAnalysis'
Expand Down Expand Up @@ -38,10 +39,25 @@ export async function resolvePlugins(
: { pre: [], post: [] }
const { modulePreload } = config.build

const resolveOptions: InternalResolveOptions = {
...config.resolve,
root: config.root,
isProduction: config.isProduction,
isBuild,
packageCache: config.packageCache,
ssrConfig: config.ssr,
asSrc: true,
getDepsOptimizer: (ssr: boolean) => getDepsOptimizer(config, ssr),
shouldExternalize:
isBuild && config.build.ssr && config.ssr?.format !== 'cjs'
? (id) => shouldExternalizeForSSR(id, config)
: undefined,
}

return [
isWatch ? ensureWatchPlugin() : null,
isBuild ? metadataPlugin() : null,
preAliasPlugin(config),
preAliasPlugin(config, resolveOptions),
aliasPlugin({ entries: config.resolve.alias }),
...prePlugins,
modulePreload === true ||
Expand All @@ -56,20 +72,7 @@ export async function resolvePlugins(
: optimizedDepsPlugin(config),
]
: []),
resolvePlugin({
...config.resolve,
root: config.root,
isProduction: config.isProduction,
isBuild,
packageCache: config.packageCache,
ssrConfig: config.ssr,
asSrc: true,
getDepsOptimizer: (ssr: boolean) => getDepsOptimizer(config, ssr),
shouldExternalize:
isBuild && config.build.ssr && config.ssr?.format !== 'cjs'
? (id) => shouldExternalizeForSSR(id, config)
: undefined,
}),
resolvePlugin(resolveOptions),
htmlInlineProxyPlugin(config),
cssPlugin(config),
config.esbuild !== false ? esbuildPlugin(config.esbuild) : null,
Expand Down
10 changes: 8 additions & 2 deletions packages/vite/src/node/plugins/preAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,15 @@ import {
} from '../utils'
import { getDepsOptimizer } from '../optimizer'
import { tryOptimizedResolve } from './resolve'
import type { InternalResolveOptions } from './resolve'

/**
* A plugin to avoid an aliased AND optimized dep from being aliased in src
*/
export function preAliasPlugin(config: ResolvedConfig): Plugin {
export function preAliasPlugin(
config: ResolvedConfig,
resolveOptions: InternalResolveOptions,
): Plugin {
const findPatterns = getAliasPatterns(config.resolve.alias)
const isConfiguredAsExternal = createIsConfiguredAsSsrExternal(config)
const isBuild = config.command === 'build'
Expand All @@ -39,9 +43,11 @@ export function preAliasPlugin(config: ResolvedConfig): Plugin {
) {
if (findPatterns.find((pattern) => matches(pattern, id))) {
const optimizedId = await tryOptimizedResolve(
depsOptimizer,
id,
importer,
resolveOptions,
depsOptimizer,
ssr,
)
if (optimizedId) {
return optimizedId // aliased dep already optimized
Expand Down
36 changes: 29 additions & 7 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import {
lookupFile,
nestedResolveFrom,
normalizePath,
resolveFrom,
slash,
} from '../utils'
import { optimizedDepInfoFromFile, optimizedDepInfoFromId } from '../optimizer'
Expand Down Expand Up @@ -310,7 +309,13 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
asSrc &&
depsOptimizer &&
!options.scan &&
(res = await tryOptimizedResolve(depsOptimizer, id, importer))
(res = await tryOptimizedResolve(
id,
importer,
options,
depsOptimizer,
ssr,
))
) {
return res
}
Expand Down Expand Up @@ -788,7 +793,7 @@ export function tryNodeResolve(
if (
!options.ssrOptimizeCheck &&
(!resolved.includes('node_modules') || // linked
!depsOptimizer || // resolving before listening to the server
!depsOptimizer || // resolving before listening to the server, or called by tryOptimizedResolve
options.scan) // initial esbuild scan phase
) {
return { id: resolved }
Expand Down Expand Up @@ -862,9 +867,11 @@ export function tryNodeResolve(
}

export async function tryOptimizedResolve(
depsOptimizer: DepsOptimizer,
id: string,
importer?: string,
importer: string | null | undefined,
resolveOptions: InternalResolveOptions,
depsOptimizer: DepsOptimizer,
ssr: boolean,
): Promise<string | undefined> {
// TODO: we need to wait until scanning is done here as this function
// is used in the preAliasPlugin to decide if an aliased dep is optimized,
Expand Down Expand Up @@ -899,8 +906,23 @@ export async function tryOptimizedResolve(
// lazily initialize resolvedSrc
if (resolvedSrc == null) {
try {
// this may throw errors if unable to resolve, e.g. aliased id
resolvedSrc = normalizePath(resolveFrom(id, path.dirname(importer)))
const { target: ssrTarget } = resolveOptions.ssrConfig ?? {}
const targetWeb = !ssr || ssrTarget === 'webworker'
const resolved = tryNodeResolve(
id,
importer,
resolveOptions,
targetWeb,
undefined,
ssr,
false,
)
if (resolved?.id) {
resolvedSrc = normalizePath(resolved.id)
} else {
// no resolvedSrc, no need to continue
break
}
} catch {
// this is best-effort only so swallow errors
break
Expand Down

0 comments on commit 68bdeef

Please sign in to comment.