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(globImport): import.meta.glob second arguments compatible with template literal #10961

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
19 changes: 16 additions & 3 deletions packages/vite/src/node/plugins/importMetaGlob.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,30 @@ export async function parseImportGlob(

if (!(name in knownOptions)) throw err(`Unknown options ${name}`)

if (property.value.type !== 'Literal')
const isTemplateLiteral = property.value.type === 'TemplateLiteral'
if (property.value.type !== 'Literal' && !isTemplateLiteral)
throw err('Could only use literals')
sun0day marked this conversation as resolved.
Show resolved Hide resolved

const valueType = typeof property.value.value
if (
isTemplateLiteral &&
(property.value as TemplateLiteral).expressions.length > 0
) {
throw err(
`Expected glob to be a string, but got dynamic template literal`
)
sun0day marked this conversation as resolved.
Show resolved Hide resolved
}

const optionValue = isTemplateLiteral
? (property.value as TemplateLiteral).quasis[0].value.raw
: (property.value as Literal).value
const valueType = typeof optionValue
if (valueType === 'undefined') continue

if (valueType !== knownOptions[name])
throw err(
`Expected the type of option "${name}" to be "${knownOptions[name]}", but got "${valueType}"`
)
options[name] = property.value.value as any
options[name] = optionValue as any
}
}

Expand Down
2 changes: 1 addition & 1 deletion playground/glob-import/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ <h2>Escape alias glob</h2>

<script type="module">
const rawModules = import.meta.glob('/dir/*.json', {
as: 'raw',
as: `raw`,
eager: true
})
const globraw = {}
Expand Down