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 b4daf02
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
10 changes: 7 additions & 3 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 @@ -955,6 +957,8 @@ export function resolvePackageEntry(
if (cached) {
return cached
}

const { postfix } = splitFileAndPostfix(id)
try {
let entryPoint: string | undefined

Expand Down Expand Up @@ -1072,7 +1076,7 @@ export function resolvePackageEntry(
)}`,
)
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 b4daf02

Please sign in to comment.