fix(glob): handle glob prop access (#9281)

This commit is contained in:
Bjorn Lu 2022-07-22 01:41:06 +08:00 committed by GitHub
parent bbc8318b11
commit 0580215b73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View File

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

View File

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

View File

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