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

fix(glob): support static template literals #9352

Merged
merged 9 commits into from Jul 25, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -272,10 +272,40 @@ describe('parse negatives', async () => {
expect(
await runError('import.meta.glob(`hi ${hey}`)')
).toMatchInlineSnapshot(
'[Error: Invalid glob import syntax: Could only use literals]'
'[Error: Invalid glob import syntax: Expected glob to be a string, but got contains expressions]'
)
})

it('template with unicode', async () => {
expect(await run('import.meta.glob(`/\u0068\u0065\u006c\u006c\u006f`)'))
.toMatchInlineSnapshot(`
[
{
"globs": [
"/hello",
],
"options": {},
"start": 0,
},
]
`)
})

it('template without expressions', async () => {
expect(await run('import.meta.glob(`/**/*.page.client.*([a-zA-Z0-9])`)'))
.toMatchInlineSnapshot(`
[
{
"globs": [
"/**/*.page.client.*([a-zA-Z0-9])",
],
"options": {},
"start": 0,
},
]
`)
})
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved

it('be string', async () => {
expect(await runError('import.meta.glob(1)')).toMatchInlineSnapshot(
'[Error: Invalid glob import syntax: Expected glob to be a string, but got "number"]'
Expand Down
40 changes: 26 additions & 14 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Expand Up @@ -4,10 +4,13 @@ import { stripLiteral } from 'strip-literal'
import type {
ArrayExpression,
CallExpression,
Expression,
Literal,
MemberExpression,
Node,
SequenceExpression
SequenceExpression,
SpreadElement,
TemplateLiteral
} from 'estree'
import { parseExpressionAt } from 'acorn'
import MagicString from 'magic-string'
Expand Down Expand Up @@ -168,29 +171,38 @@ export async function parseImportGlob(
if (ast.arguments.length < 1 || ast.arguments.length > 2)
throw err(`Expected 1-2 arguments, but got ${ast.arguments.length}`)

const arg1 = ast.arguments[0] as ArrayExpression | Literal
const arg1 = ast.arguments[0] as ArrayExpression | Literal | TemplateLiteral
const arg2 = ast.arguments[1] as Node | undefined

const globs: string[] = []
if (arg1.type === 'ArrayExpression') {
for (const element of arg1.elements) {
if (!element) continue
if (element.type !== 'Literal') throw err('Could only use literals')

const validateLiteral = (element: Expression | SpreadElement) => {
if (!element) return
sapphi-red marked this conversation as resolved.
Show resolved Hide resolved
if (element.type === 'Literal') {
if (typeof element.value !== 'string')
throw err(
`Expected glob to be a string, but got "${typeof element.value}"`
)

globs.push(element.value)
} else if (element.type === 'TemplateLiteral') {
if (element.expressions.length !== 0) {
throw err(
`Expected glob to be a string, but got contains expressions`
antfu marked this conversation as resolved.
Show resolved Hide resolved
)
}
globs.push(element.quasis[0].value.raw)
} else {
throw err('Could only use literals')
}
}

if (arg1.type === 'ArrayExpression') {
for (const element of arg1.elements) {
if (!element) continue
validateLiteral(element)
}
} else if (arg1.type === 'Literal') {
if (typeof arg1.value !== 'string')
throw err(
`Expected glob to be a string, but got "${typeof arg1.value}"`
)
globs.push(arg1.value)
} else {
throw err('Could only use literals')
validateLiteral(arg1)
}

// arg2
Expand Down