Skip to content

Commit

Permalink
feat(importMetaGlob): support sub imports pattern (#12467)
Browse files Browse the repository at this point in the history
Co-authored-by: patak <matias.capeletto@gmail.com>
  • Loading branch information
sun0day and patak-dev authored Jun 6, 2023
1 parent 1e299cc commit e355c9c
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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,
)
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions packages/vite/src/node/plugins/resolve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions playground/glob-import/__tests__/glob-import.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 change: 1 addition & 0 deletions playground/glob-import/imports-path/bar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'bar'
1 change: 1 addition & 0 deletions playground/glob-import/imports-path/foo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 'foo'
10 changes: 10 additions & 0 deletions playground/glob-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ <h2>Escape relative glob</h2>
<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">
Expand Down Expand Up @@ -141,6 +143,14 @@ <h2>Escape alias glob</h2>
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 changes: 3 additions & 0 deletions playground/glob-import/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"private": true,
"version": "0.0.0",
"type": "module",
"imports": {
"#imports/*": "./imports-path/*"
},
"scripts": {
"dev": "vite",
"build": "vite build",
Expand Down

0 comments on commit e355c9c

Please sign in to comment.