diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 63841d398c5256..021904657e8372 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -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)) { @@ -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 { @@ -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 @@ -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) { diff --git a/playground/resolve/index.html b/playground/resolve/index.html index 026d79fd33c844..3d52ad6722e28c 100644 --- a/playground/resolve/index.html +++ b/playground/resolve/index.html @@ -349,7 +349,7 @@

resolve package that contains # in path

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') }