Skip to content

Commit

Permalink
fix(resolve): support pkg?query
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Aug 20, 2023
1 parent 7ca3597 commit 8698140
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
17 changes: 11 additions & 6 deletions packages/vite/src/node/plugins/resolve.ts
Expand Up @@ -697,7 +697,9 @@ export function tryNodeResolve(

// check for deep import, e.g. "my-lib/foo"
const deepMatch = id.match(deepImportRE)
const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : id
// package name doesn't include postfixes
// trim them to support importing package with queries (e.g. `import css from 'normalize.css?inline'`)
const pkgId = deepMatch ? deepMatch[1] || deepMatch[2] : cleanUrl(id)

let basedir: string
if (dedupe?.includes(pkgId)) {
Expand Down Expand Up @@ -739,7 +741,7 @@ export function tryNodeResolve(
}

const resolveId = deepMatch ? resolveDeepImport : resolvePackageEntry
const unresolvedId = deepMatch ? '.' + id.slice(pkgId.length) : pkgId
const unresolvedId = deepMatch ? '.' + id.slice(pkgId.length) : id

let resolved: string | undefined
try {
Expand Down Expand Up @@ -951,10 +953,13 @@ export function resolvePackageEntry(
targetWeb: boolean,
options: InternalResolveOptions,
): string | undefined {
const { file: idWithoutPostfix, postfix } = splitFileAndPostfix(id)

const cached = getResolvedCache('.', targetWeb)
if (cached) {
return cached
return cached + postfix
}

try {
let entryPoint: string | undefined

Expand Down Expand Up @@ -1067,12 +1072,12 @@ export function resolvePackageEntry(
)
if (resolvedEntryPoint) {
debug?.(
`[package entry] ${colors.cyan(id)} -> ${colors.dim(
`[package entry] ${colors.cyan(idWithoutPostfix)} -> ${colors.dim(
resolvedEntryPoint,
)}`,
)} (postfix: ${postfix})`,
)
setResolvedCache('.', resolvedEntryPoint, targetWeb)
return resolvedEntryPoint
return resolvedEntryPoint + postfix
}
}
} catch (e) {
Expand Down
2 changes: 1 addition & 1 deletion playground/resolve/index.html
Expand Up @@ -349,7 +349,7 @@ <h2>resolve package that contains # in path</h2>
import '@vitejs/test-resolve-browser-field/multiple.dot.path'

// css entry
import css from 'normalize.css/normalize.css?inline'
import css from 'normalize.css?inline'
if (typeof css === 'string') {
text('.css', '[success] resolve package with css entry file')
}
Expand Down

0 comments on commit 8698140

Please sign in to comment.