Skip to content

Commit

Permalink
fix(scan): handle import glob in jsx/tsx files
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Feb 3, 2021
1 parent 1d5437d commit 24695fe
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions packages/vite/src/node/optimizer/scan.ts
Expand Up @@ -201,7 +201,7 @@ function esbuildScanPlugin(
}

if (js.includes('import.meta.glob')) {
return transformGlob(js, path).then((contents) => ({
return transformGlob(js, path, loader).then((contents) => ({
loader,
contents
}))
Expand Down Expand Up @@ -304,12 +304,19 @@ function esbuildScanPlugin(
build.onLoad({ filter: jsTypesRE }, ({ path: id }) => {
let ext = path.extname(id).slice(1)
if (ext === 'mjs') ext = 'js'
const contents = fs.readFileSync(id, 'utf-8')

let contents = fs.readFileSync(id, 'utf-8')
if (ext.endsWith('x') && config.esbuild && config.esbuild.jsxInject) {
contents = config.esbuild.jsxInject + `\n` + contents
}

if (contents.includes('import.meta.glob')) {
return transformGlob(contents, id).then((contents) => ({
loader: ext as Loader,
contents
}))
return transformGlob(contents, id, ext as Loader).then(
(contents) => ({
loader: ext as Loader,
contents
})
)
}
return {
loader: ext as Loader,
Expand All @@ -320,7 +327,12 @@ function esbuildScanPlugin(
}
}

async function transformGlob(source: string, importer: string) {
async function transformGlob(source: string, importer: string, loader: Loader) {
// transform the content first since es-module-lexer can't handle non-js
if (loader !== 'js') {
source = (await (await ensureService()).transform(source, { loader })).code
}

await init
const imports = parse(source)[0]
const s = new MagicString(source)
Expand Down

0 comments on commit 24695fe

Please sign in to comment.