From b4daf024910172b8af99a18c9d8dca66926a3ffd Mon Sep 17 00:00:00 2001 From: sapphi-red Date: Sun, 20 Aug 2023 17:57:05 +0900 Subject: [PATCH] fix(resolve): support `pkg?query` --- packages/vite/src/node/plugins/resolve.ts | 10 +++++++--- playground/resolve/index.html | 2 +- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 63841d398c5256..f9a6289a46cc7e 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 { @@ -955,6 +957,8 @@ export function resolvePackageEntry( if (cached) { return cached } + + const { postfix } = splitFileAndPostfix(id) try { let entryPoint: string | undefined @@ -1072,7 +1076,7 @@ export function resolvePackageEntry( )}`, ) 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') }