Skip to content

Commit

Permalink
fix(globImport): import.meta.glob second arguments compatible with te…
Browse files Browse the repository at this point in the history
…mplate literal (vitejs#10947)
  • Loading branch information
ivan committed Nov 17, 2022
1 parent 02c334a commit 96d8840
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
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')

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

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

0 comments on commit 96d8840

Please sign in to comment.