feat(importMetaGlob): support sub imports pattern (#12467)

Co-authored-by: patak <matias.capeletto@gmail.com>
This commit is contained in:
sun0day 2023-06-06 21:29:05 +08:00 committed by GitHub
parent 1e299cc4b2
commit e355c9ccc5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 42 additions and 3 deletions

View File

@ -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)
}

View File

@ -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) {

View File

@ -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')
})

View File

@ -0,0 +1 @@
export default 'bar'

View File

@ -0,0 +1 @@
export default 'foo'

View File

@ -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>

View File

@ -3,6 +3,9 @@
"private": true,
"version": "0.0.0",
"type": "module",
"imports": {
"#imports/*": "./imports-path/*"
},
"scripts": {
"dev": "vite",
"build": "vite build",