mirror of
https://github.com/vitejs/vite.git
synced 2024-11-21 22:59:10 +00:00
fix(glob): handle glob prop access (#9281)
This commit is contained in:
parent
bbc8318b11
commit
0580215b73
@ -5,6 +5,7 @@ import type {
|
||||
ArrayExpression,
|
||||
CallExpression,
|
||||
Literal,
|
||||
MemberExpression,
|
||||
Node,
|
||||
SequenceExpression
|
||||
} from 'estree'
|
||||
@ -118,7 +119,7 @@ export async function parseImportGlob(
|
||||
return e
|
||||
}
|
||||
|
||||
let ast: CallExpression | SequenceExpression
|
||||
let ast: CallExpression | SequenceExpression | MemberExpression
|
||||
let lastTokenPos: number | undefined
|
||||
|
||||
try {
|
||||
@ -157,6 +158,10 @@ export async function parseImportGlob(
|
||||
if (ast.type === 'SequenceExpression')
|
||||
ast = ast.expressions[0] as CallExpression
|
||||
|
||||
// immediate property access, call expression is nested
|
||||
// import.meta.glob(...)['prop']
|
||||
if (ast.type === 'MemberExpression') ast = ast.object as CallExpression
|
||||
|
||||
if (ast.type !== 'CallExpression')
|
||||
throw err(`Expect CallExpression, got ${ast.type}`)
|
||||
|
||||
|
@ -92,6 +92,12 @@ test('import glob raw', async () => {
|
||||
)
|
||||
})
|
||||
|
||||
test('import property access', async () => {
|
||||
expect(await page.textContent('.property-access')).toBe(
|
||||
JSON.stringify(rawResult['/dir/baz.json'], null, 2)
|
||||
)
|
||||
})
|
||||
|
||||
test('import relative glob raw', async () => {
|
||||
expect(await page.textContent('.relative-glob-raw')).toBe(
|
||||
JSON.stringify(relativeRawResult, null, 2)
|
||||
|
@ -1,8 +1,17 @@
|
||||
<h1>Glob import</h1>
|
||||
<h2>Normal</h2>
|
||||
<pre class="result"></pre>
|
||||
<h2>Eager</h2>
|
||||
<pre class="result-eager"></pre>
|
||||
<h2>node_modules</h2>
|
||||
<pre class="result-node_modules"></pre>
|
||||
<h2>Raw</h2>
|
||||
<pre class="globraw"></pre>
|
||||
<h2>Property access</h2>
|
||||
<pre class="property-access"></pre>
|
||||
<h2>Relative raw</h2>
|
||||
<pre class="relative-glob-raw"></pre>
|
||||
<h2>Side effect</h2>
|
||||
<pre class="side-effect-result"></pre>
|
||||
|
||||
<script type="module" src="./dir/index.js"></script>
|
||||
@ -63,6 +72,18 @@
|
||||
)
|
||||
</script>
|
||||
|
||||
<script type="module">
|
||||
const bazJson = import.meta.glob('/dir/*.json', {
|
||||
as: 'raw',
|
||||
eager: true
|
||||
})['/dir/baz.json']
|
||||
document.querySelector('.property-access').textContent = JSON.stringify(
|
||||
JSON.parse(bazJson),
|
||||
null,
|
||||
2
|
||||
)
|
||||
</script>
|
||||
|
||||
<script type="module">
|
||||
const relativeRawModules = import.meta.glob('../glob-import/dir/*.json', {
|
||||
as: 'raw',
|
||||
|
Loading…
Reference in New Issue
Block a user