Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(importMetaGlob): support sub imports pattern #12467

Merged
merged 5 commits into from
Jun 6, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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",
Expand Down