From d969174095ca341b06c09deaab616b710c9d3238 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 17 Mar 2023 23:43:17 +0800 Subject: [PATCH 1/3] feat(importMetaGlob): support sub imports pattern (fix #12465) --- .../vite/src/node/plugins/importMetaGlob.ts | 22 ++++++++++++++++--- packages/vite/src/node/plugins/resolve.ts | 4 ++++ .../glob-import/__tests__/glob-import.spec.ts | 4 ++++ playground/glob-import/imports-path/bar.js | 1 + playground/glob-import/imports-path/foo.js | 1 + playground/glob-import/index.html | 10 +++++++++ playground/glob-import/package.json | 3 +++ playground/resolve/index.html | 2 ++ 8 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 playground/glob-import/imports-path/bar.js create mode 100644 playground/glob-import/imports-path/foo.js diff --git a/packages/vite/src/node/plugins/importMetaGlob.ts b/packages/vite/src/node/plugins/importMetaGlob.ts index 11d495f76baecf..4b6de753cb74bf 100644 --- a/packages/vite/src/node/plugins/importMetaGlob.ts +++ b/packages/vite/src/node/plugins/importMetaGlob.ts @@ -13,7 +13,7 @@ import type { TemplateLiteral, } from 'estree' import { parseExpressionAt } from 'acorn' -import type { RollupError } from 'rollup' +import type { CustomPluginOptions, RollupError } from 'rollup' import { findNodeAt } from 'acorn-walk' import MagicString from 'magic-string' import fg from 'fast-glob' @@ -75,7 +75,8 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin { code, id, config.root, - (im) => this.resolve(im, id).then((i) => i?.id || im), + (im, _, options) => + this.resolve(im, id, options).then((i) => i?.id || im), config.isProduction, config.experimental.importGlobRestoreExtension, ) @@ -546,6 +547,12 @@ export async function transformGlobImport( type IdResolver = ( id: string, importer?: string, + options?: { + assertions?: Record + custom?: CustomPluginOptions + isEntry?: boolean + skipSelf?: boolean + }, ) => Promise | string | undefined function globSafePath(path: string) { @@ -594,7 +601,16 @@ export async function toAbsoluteGlob( if (glob.startsWith('../')) return pre + posix.join(dir, glob) if (glob.startsWith('**')) return pre + glob - const resolved = normalizePath((await resolveId(glob, importer)) || glob) + const isSubImportsPattern = glob.startsWith('#') && glob.includes('*') + + const resolved = normalizePath( + (await resolveId(glob, importer, { + custom: { 'glob-imports': isSubImportsPattern }, + })) || glob, + ) + if (isSubImportsPattern) { + return join(root, 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 4cdd2043cc41b7..46b93e854a4c19 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -186,6 +186,10 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { const resolvedImports = resolveSubpathImports(id, importer) if (resolvedImports) { id = resolvedImports + + if (resolveOpts.custom?.['glob-imports']) { + return id + } } if (importer) { diff --git a/playground/glob-import/__tests__/glob-import.spec.ts b/playground/glob-import/__tests__/glob-import.spec.ts index 34b77127cdb1e0..cf1b6c199e73aa 100644 --- a/playground/glob-import/__tests__/glob-import.spec.ts +++ b/playground/glob-import/__tests__/glob-import.spec.ts @@ -240,3 +240,7 @@ test('escapes special chars in globs without mangling user supplied glob suffix' .sort() expect(expectedNames).toEqual(foundAliasNames) }) + +test('sub imports', async () => { + expect(await page.textContent('.sub-imports')).toMatch('bar foo') +}) diff --git a/playground/glob-import/imports-path/bar.js b/playground/glob-import/imports-path/bar.js new file mode 100644 index 00000000000000..4548a26ba14dc8 --- /dev/null +++ b/playground/glob-import/imports-path/bar.js @@ -0,0 +1 @@ +export default 'bar' diff --git a/playground/glob-import/imports-path/foo.js b/playground/glob-import/imports-path/foo.js new file mode 100644 index 00000000000000..7e942cf45c8a37 --- /dev/null +++ b/playground/glob-import/imports-path/foo.js @@ -0,0 +1 @@ +export default 'foo' diff --git a/playground/glob-import/index.html b/playground/glob-import/index.html index a899d244326f68..b726965ff62067 100644 --- a/playground/glob-import/index.html +++ b/playground/glob-import/index.html @@ -21,6 +21,8 @@

Escape relative glob


 

Escape alias glob


+

Sub imports

+

 
 
 
 
+
+
 
diff --git a/playground/glob-import/package.json b/playground/glob-import/package.json
index f3ac7ae9313db5..47aa90ea0f40f9 100644
--- a/playground/glob-import/package.json
+++ b/playground/glob-import/package.json
@@ -2,6 +2,9 @@
   "name": "@vitejs/test-import-context",
   "private": true,
   "version": "0.0.0",
+  "imports": {
+    "#imports/*": "./imports-path/*"
+  },
   "scripts": {
     "dev": "vite",
     "build": "vite build",
diff --git a/playground/resolve/index.html b/playground/resolve/index.html
index 158af31b3f413c..372db6786f10a0 100644
--- a/playground/resolve/index.html
+++ b/playground/resolve/index.html
@@ -218,6 +218,8 @@ 

resolve package that contains # in path

import { msg as importsStar } from '#star/index.js' text('.imports-star', importsStar) + import aaaa from '#star/*' + import { msg as importsSlash } from '#slash/index.js' text('.imports-slash', importsSlash) From 1fd27a43b4e397e62be66448107db823df411900 Mon Sep 17 00:00:00 2001 From: ivan Date: Fri, 17 Mar 2023 23:48:23 +0800 Subject: [PATCH 2/3] test(resolve): remove useless sub import --- playground/resolve/index.html | 2 -- 1 file changed, 2 deletions(-) diff --git a/playground/resolve/index.html b/playground/resolve/index.html index 372db6786f10a0..158af31b3f413c 100644 --- a/playground/resolve/index.html +++ b/playground/resolve/index.html @@ -218,8 +218,6 @@

resolve package that contains # in path

import { msg as importsStar } from '#star/index.js' text('.imports-star', importsStar) - import aaaa from '#star/*' - import { msg as importsSlash } from '#slash/index.js' text('.imports-slash', importsSlash) From 4c69e4fbc190c27ded4c571c52f6ffc2b843857d Mon Sep 17 00:00:00 2001 From: patak Date: Wed, 19 Apr 2023 10:46:51 +0200 Subject: [PATCH 3/3] chore: update this.resolve custom naming --- packages/vite/src/node/plugins/importMetaGlob.ts | 2 +- packages/vite/src/node/plugins/resolve.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/vite/src/node/plugins/importMetaGlob.ts b/packages/vite/src/node/plugins/importMetaGlob.ts index 3dc5c3e46a420a..7ff08e544a78c6 100644 --- a/packages/vite/src/node/plugins/importMetaGlob.ts +++ b/packages/vite/src/node/plugins/importMetaGlob.ts @@ -605,7 +605,7 @@ export async function toAbsoluteGlob( const resolved = normalizePath( (await resolveId(glob, importer, { - custom: { 'glob-imports': isSubImportsPattern }, + custom: { 'vite:import-glob': { isSubImportsPattern } }, })) || glob, ) if (isSubImportsPattern) { diff --git a/packages/vite/src/node/plugins/resolve.ts b/packages/vite/src/node/plugins/resolve.ts index 34a570ec111ad2..76df369ec6af7d 100644 --- a/packages/vite/src/node/plugins/resolve.ts +++ b/packages/vite/src/node/plugins/resolve.ts @@ -186,7 +186,7 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin { if (resolvedImports) { id = resolvedImports - if (resolveOpts.custom?.['glob-imports']) { + if (resolveOpts.custom?.['vite:import-glob']?.isSubImportsPattern) { return id } }