From 48a3fd08c2b4c57de58d7f89d68bd2f565f92c2b Mon Sep 17 00:00:00 2001 From: bluwy Date: Tue, 23 Jul 2024 14:46:33 +0800 Subject: [PATCH 1/2] fix(importMetaGlob): handle alias that starts with hash --- .../vite/src/node/plugins/importMetaGlob.ts | 6 +++-- .../glob-import/__tests__/glob-import.spec.ts | 8 ++++-- playground/glob-import/index.html | 27 +++++++++++-------- playground/glob-import/vite.config.ts | 1 + 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/packages/vite/src/node/plugins/importMetaGlob.ts b/packages/vite/src/node/plugins/importMetaGlob.ts index 8f2475709003c0..14d96a6aede5ca 100644 --- a/packages/vite/src/node/plugins/importMetaGlob.ts +++ b/packages/vite/src/node/plugins/importMetaGlob.ts @@ -563,10 +563,12 @@ export async function toAbsoluteGlob( custom: { 'vite:import-glob': { isSubImportsPattern } }, })) || glob, ) - if (isSubImportsPattern) { + // If matching a subpath import, the returned path is relative and doesn't look absolute. + // If it matches an alias that starts with `#`, the path will be absolute. + if (isSubImportsPattern && !isAbsolute(resolved)) { return join(root, resolved) } - if (isAbsolute(resolved)) { + if (isSubImportsPattern || isAbsolute(resolved)) { return pre + globSafeResolvedPath(resolved, glob) } diff --git a/playground/glob-import/__tests__/glob-import.spec.ts b/playground/glob-import/__tests__/glob-import.spec.ts index e7c7719357fd2c..bdfb4e92119c23 100644 --- a/playground/glob-import/__tests__/glob-import.spec.ts +++ b/playground/glob-import/__tests__/glob-import.spec.ts @@ -240,6 +240,10 @@ test('escapes special chars in globs without mangling user supplied glob suffix' expect(expectedNames).toEqual(foundAliasNames) }) -test('sub imports', async () => { - expect(await page.textContent('.sub-imports')).toMatch('bar foo') +test('subpath imports', async () => { + expect(await page.textContent('.subpath-imports')).toMatch('bar foo') +}) + +test('#alias imports', async () => { + expect(await page.textContent('.hash-alias-imports')).toMatch('bar foo') }) diff --git a/playground/glob-import/index.html b/playground/glob-import/index.html index 41e6409aea4bab..8f8d833b56625b 100644 --- a/playground/glob-import/index.html +++ b/playground/glob-import/index.html @@ -21,20 +21,16 @@

Escape relative glob


 

Escape alias glob


-

Sub imports

-

+

Subpath imports

+

+

#alias imports

+

 

In package


 
 
 
 
 
 
-  document.querySelector('.sub-imports').textContent = Object.values(subImports)
+
diff --git a/playground/glob-import/vite.config.ts b/playground/glob-import/vite.config.ts
index 87a057ba6112ad..896c7c05768a7a 100644
--- a/playground/glob-import/vite.config.ts
+++ b/playground/glob-import/vite.config.ts
@@ -19,6 +19,7 @@ export default defineConfig({
     alias: {
       ...escapeAliases,
       '@dir': path.resolve(__dirname, './dir/'),
+      '#alias': path.resolve(__dirname, './imports-path/'),
     },
   },
   build: {

From 052a18c5abb3e86c335fb072d6e9a0bfa5101073 Mon Sep 17 00:00:00 2001
From: bluwy 
Date: Tue, 23 Jul 2024 14:52:31 +0800
Subject: [PATCH 2/2] refactor: better fix

---
 packages/vite/src/node/plugins/importMetaGlob.ts | 7 +------
 packages/vite/src/node/plugins/resolve.ts        | 2 +-
 2 files changed, 2 insertions(+), 7 deletions(-)

diff --git a/packages/vite/src/node/plugins/importMetaGlob.ts b/packages/vite/src/node/plugins/importMetaGlob.ts
index 14d96a6aede5ca..d596d39d1a62e9 100644
--- a/packages/vite/src/node/plugins/importMetaGlob.ts
+++ b/packages/vite/src/node/plugins/importMetaGlob.ts
@@ -563,12 +563,7 @@ export async function toAbsoluteGlob(
       custom: { 'vite:import-glob': { isSubImportsPattern } },
     })) || glob,
   )
-  // If matching a subpath import, the returned path is relative and doesn't look absolute.
-  // If it matches an alias that starts with `#`, the path will be absolute.
-  if (isSubImportsPattern && !isAbsolute(resolved)) {
-    return join(root, resolved)
-  }
-  if (isSubImportsPattern || isAbsolute(resolved)) {
+  if (isAbsolute(resolved)) {
     return pre + globSafeResolvedPath(resolved, glob)
   }
 
diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts
index ccffd1c152972c..d31b40dd7a7cbd 100644
--- a/packages/vite/src/node/plugins/resolve.ts
+++ b/packages/vite/src/node/plugins/resolve.ts
@@ -203,7 +203,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
         id = resolvedImports
 
         if (resolveOpts.custom?.['vite:import-glob']?.isSubImportsPattern) {
-          return id
+          return normalizePath(path.join(root, id))
         }
       }