Skip to content

Commit e355c9c

Browse files
sun0daypatak-dev
andauthored
feat(importMetaGlob): support sub imports pattern (#12467)
Co-authored-by: patak <[email protected]>
1 parent 1e299cc commit e355c9c

File tree

7 files changed

+42
-3
lines changed

7 files changed

+42
-3
lines changed

packages/vite/src/node/plugins/importMetaGlob.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type {
1313
TemplateLiteral,
1414
} from 'estree'
1515
import { parseExpressionAt } from 'acorn'
16-
import type { RollupError } from 'rollup'
16+
import type { CustomPluginOptions, RollupError } from 'rollup'
1717
import { findNodeAt } from 'acorn-walk'
1818
import MagicString from 'magic-string'
1919
import fg from 'fast-glob'
@@ -75,7 +75,8 @@ export function importGlobPlugin(config: ResolvedConfig): Plugin {
7575
code,
7676
id,
7777
config.root,
78-
(im) => this.resolve(im, id).then((i) => i?.id || im),
78+
(im, _, options) =>
79+
this.resolve(im, id, options).then((i) => i?.id || im),
7980
config.isProduction,
8081
config.experimental.importGlobRestoreExtension,
8182
)
@@ -546,6 +547,12 @@ export async function transformGlobImport(
546547
type IdResolver = (
547548
id: string,
548549
importer?: string,
550+
options?: {
551+
assertions?: Record<string, string>
552+
custom?: CustomPluginOptions
553+
isEntry?: boolean
554+
skipSelf?: boolean
555+
},
549556
) => Promise<string | undefined> | string | undefined
550557

551558
function globSafePath(path: string) {
@@ -594,7 +601,16 @@ export async function toAbsoluteGlob(
594601
if (glob.startsWith('../')) return pre + posix.join(dir, glob)
595602
if (glob.startsWith('**')) return pre + glob
596603

597-
const resolved = normalizePath((await resolveId(glob, importer)) || glob)
604+
const isSubImportsPattern = glob.startsWith('#') && glob.includes('*')
605+
606+
const resolved = normalizePath(
607+
(await resolveId(glob, importer, {
608+
custom: { 'vite:import-glob': { isSubImportsPattern } },
609+
})) || glob,
610+
)
611+
if (isSubImportsPattern) {
612+
return join(root, resolved)
613+
}
598614
if (isAbsolute(resolved)) {
599615
return pre + globSafeResolvedPath(resolved, glob)
600616
}

packages/vite/src/node/plugins/resolve.ts

+4
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,10 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
185185
)
186186
if (resolvedImports) {
187187
id = resolvedImports
188+
189+
if (resolveOpts.custom?.['vite:import-glob']?.isSubImportsPattern) {
190+
return id
191+
}
188192
}
189193

190194
if (importer) {

playground/glob-import/__tests__/glob-import.spec.ts

+4
Original file line numberDiff line numberDiff line change
@@ -240,3 +240,7 @@ test('escapes special chars in globs without mangling user supplied glob suffix'
240240
.sort()
241241
expect(expectedNames).toEqual(foundAliasNames)
242242
})
243+
244+
test('sub imports', async () => {
245+
expect(await page.textContent('.sub-imports')).toMatch('bar foo')
246+
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'bar'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default 'foo'

playground/glob-import/index.html

+10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ <h2>Escape relative glob</h2>
2121
<pre class="escape-relative"></pre>
2222
<h2>Escape alias glob</h2>
2323
<pre class="escape-alias"></pre>
24+
<h2>Sub imports</h2>
25+
<pre class="sub-imports"></pre>
2426

2527
<script type="module" src="./dir/index.js"></script>
2628
<script type="module">
@@ -141,6 +143,14 @@ <h2>Escape alias glob</h2>
141143
document.querySelector('.escape-alias').textContent = alias.sort().join('\n')
142144
</script>
143145

146+
<script type="module">
147+
const subImports = import.meta.glob('#imports/*', { eager: true })
148+
149+
document.querySelector('.sub-imports').textContent = Object.values(subImports)
150+
.map((mod) => mod.default)
151+
.join(' ')
152+
</script>
153+
144154
<script type="module">
145155
console.log('Ran scripts')
146156
</script>

playground/glob-import/package.json

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"private": true,
44
"version": "0.0.0",
55
"type": "module",
6+
"imports": {
7+
"#imports/*": "./imports-path/*"
8+
},
69
"scripts": {
710
"dev": "vite",
811
"build": "vite build",

0 commit comments

Comments
 (0)