mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 14:48:41 +00:00
feat(importMetaGlob): support sub imports pattern (#12467)
Co-authored-by: patak <matias.capeletto@gmail.com>
This commit is contained in:
parent
1e299cc4b2
commit
e355c9ccc5
@ -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<string, string>
|
||||
custom?: CustomPluginOptions
|
||||
isEntry?: boolean
|
||||
skipSelf?: boolean
|
||||
},
|
||||
) => Promise<string | undefined> | 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: { 'vite:import-glob': { isSubImportsPattern } },
|
||||
})) || glob,
|
||||
)
|
||||
if (isSubImportsPattern) {
|
||||
return join(root, resolved)
|
||||
}
|
||||
if (isAbsolute(resolved)) {
|
||||
return pre + globSafeResolvedPath(resolved, glob)
|
||||
}
|
||||
|
@ -185,6 +185,10 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
|
||||
)
|
||||
if (resolvedImports) {
|
||||
id = resolvedImports
|
||||
|
||||
if (resolveOpts.custom?.['vite:import-glob']?.isSubImportsPattern) {
|
||||
return id
|
||||
}
|
||||
}
|
||||
|
||||
if (importer) {
|
||||
|
@ -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')
|
||||
})
|
||||
|
1
playground/glob-import/imports-path/bar.js
Normal file
1
playground/glob-import/imports-path/bar.js
Normal file
@ -0,0 +1 @@
|
||||
export default 'bar'
|
1
playground/glob-import/imports-path/foo.js
Normal file
1
playground/glob-import/imports-path/foo.js
Normal file
@ -0,0 +1 @@
|
||||
export default 'foo'
|
@ -21,6 +21,8 @@
|
||||
<pre class="escape-relative"></pre>
|
||||
<h2>Escape alias glob</h2>
|
||||
<pre class="escape-alias"></pre>
|
||||
<h2>Sub imports</h2>
|
||||
<pre class="sub-imports"></pre>
|
||||
|
||||
<script type="module" src="./dir/index.js"></script>
|
||||
<script type="module">
|
||||
@ -141,6 +143,14 @@
|
||||
document.querySelector('.escape-alias').textContent = alias.sort().join('\n')
|
||||
</script>
|
||||
|
||||
<script type="module">
|
||||
const subImports = import.meta.glob('#imports/*', { eager: true })
|
||||
|
||||
document.querySelector('.sub-imports').textContent = Object.values(subImports)
|
||||
.map((mod) => mod.default)
|
||||
.join(' ')
|
||||
</script>
|
||||
|
||||
<script type="module">
|
||||
console.log('Ran scripts')
|
||||
</script>
|
||||
|
@ -3,6 +3,9 @@
|
||||
"private": true,
|
||||
"version": "0.0.0",
|
||||
"type": "module",
|
||||
"imports": {
|
||||
"#imports/*": "./imports-path/*"
|
||||
},
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "vite build",
|
||||
|
Loading…
Reference in New Issue
Block a user