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: ensure version query for direct node_modules imports #9848

Merged
merged 3 commits into from
Aug 27, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions packages/vite/src/node/plugins/preAlias.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import type {
} from '..'
import type { Plugin } from '../plugin'
import { createIsConfiguredAsSsrExternal } from '../ssr/ssrExternal'
import { bareImportRE, isOptimizable, moduleListContains } from '../utils'
import {
bareImportRE,
cleanUrl,
isOptimizable,
moduleListContains
} from '../utils'
import { getDepsOptimizer } from '../optimizer'
import { tryOptimizedResolve } from './resolve'

Expand Down Expand Up @@ -48,7 +53,7 @@ export function preAliasPlugin(config: ResolvedConfig): Plugin {
})
if (resolved && !depsOptimizer.isOptimizedDepFile(resolved.id)) {
const optimizeDeps = depsOptimizer.options
const resolvedId = resolved.id
const resolvedId = cleanUrl(resolved.id)
const isVirtual = resolvedId === id || resolvedId.includes('\0')
if (
!isVirtual &&
Expand Down
60 changes: 35 additions & 25 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { resolve as _resolveExports } from 'resolve.exports'
import { hasESMSyntax } from 'mlly'
import type { Plugin } from '../plugin'
import {
CLIENT_ENTRY,
DEFAULT_EXTENSIONS,
DEFAULT_MAIN_FIELDS,
DEP_VERSION_RE,
ENV_ENTRY,
FS_PREFIX,
OPTIMIZABLE_ENTRY_RE,
SPECIAL_QUERY_RE
Expand Down Expand Up @@ -42,6 +44,9 @@ import type { SSROptions } from '..'
import type { PackageCache, PackageData } from '../packages'
import { loadPackageData, resolvePackageData } from '../packages'

const normalizedClientEntry = normalizePath(CLIENT_ENTRY)
const normalizedEnvEntry = normalizePath(ENV_ENTRY)

// special id for paths marked with browser: false
// https://github.com/defunctzombie/package-browser-field-spec#ignore-a-module
export const browserExternalId = '__vite-browser-external'
Expand Down Expand Up @@ -155,14 +160,38 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
return optimizedPath
}

const ensureVersionQuery = (resolved: string): string => {
if (
!options.isBuild &&
depsOptimizer &&
!(
resolved === normalizedClientEntry ||
resolved === normalizedEnvEntry
)
) {
// Ensure that direct imports of node_modules have the same version query
// as if they would have been imported through a bare import
// Use the original id to do the check as the resolved id may be the real
// file path after symlinks resolution
const isNodeModule = normalizePath(id).includes('/node_modules/')
if (isNodeModule && !resolved.match(DEP_VERSION_RE)) {
const versionHash = depsOptimizer.metadata.browserHash
if (versionHash && OPTIMIZABLE_ENTRY_RE.test(resolved)) {
resolved = injectQuery(resolved, `v=${versionHash}`)
}
}
}
return resolved
}

// explicit fs paths that starts with /@fs/*
if (asSrc && id.startsWith(FS_PREFIX)) {
const fsPath = fsPathFromId(id)
res = tryFsResolve(fsPath, options)
isDebug && debug(`[@fs] ${colors.cyan(id)} -> ${colors.dim(res)}`)
// always return here even if res doesn't exist since /@fs/ is explicit
// if the file doesn't exist it should be a 404
return res || fsPath
return ensureVersionQuery(res || fsPath)
}

// URL
Expand All @@ -171,7 +200,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
const fsPath = path.resolve(root, id.slice(1))
if ((res = tryFsResolve(fsPath, options))) {
isDebug && debug(`[url] ${colors.cyan(id)} -> ${colors.dim(res)}`)
return res
return ensureVersionQuery(res)
}
}

Expand Down Expand Up @@ -201,26 +230,6 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
return normalizedFsPath
}

const pathFromBasedir = normalizedFsPath.slice(basedir.length)
if (pathFromBasedir.startsWith('/node_modules/')) {
// normalize direct imports from node_modules to bare imports, so the
// hashing logic is shared and we avoid duplicated modules #2503
const bareImport = pathFromBasedir.slice('/node_modules/'.length)
if (
(res = tryNodeResolve(
bareImport,
importer,
options,
targetWeb,
depsOptimizer,
ssr
)) &&
res.id.startsWith(normalizedFsPath)
) {
return res
}
}

if (
targetWeb &&
(res = tryResolveBrowserMapping(fsPath, importer, options, true))
Expand All @@ -229,6 +238,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
}

if ((res = tryFsResolve(fsPath, options))) {
res = ensureVersionQuery(res)
isDebug &&
debug(`[relative] ${colors.cyan(id)} -> ${colors.dim(res)}`)
const pkg = importer != null && idToPkgMap.get(importer)
Expand All @@ -250,7 +260,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
if ((res = tryFsResolve(fsPath, options))) {
isDebug &&
debug(`[drive-relative] ${colors.cyan(id)} -> ${colors.dim(res)}`)
return res
return ensureVersionQuery(res)
}
}

Expand All @@ -260,7 +270,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
(res = tryFsResolve(id, options))
) {
isDebug && debug(`[fs] ${colors.cyan(id)} -> ${colors.dim(res)}`)
return res
return ensureVersionQuery(res)
}

// external
Expand Down Expand Up @@ -405,7 +415,7 @@ function tryFsResolve(

let res: string | undefined

// if we fould postfix exist, we should first try resolving file with postfix. details see #4703.
// if there is a postfix, try resolving it as a complete path first (#4703)
if (
postfix &&
(res = tryResolveFile(
Expand Down