Skip to content

Commit

Permalink
fix: fix import.meta.glob check during scan
Browse files Browse the repository at this point in the history
  • Loading branch information
hi-ogawa committed Jul 18, 2024
1 parent 9fdf94a commit c5f38bf
Showing 1 changed file with 16 additions and 19 deletions.
35 changes: 16 additions & 19 deletions packages/vite/src/node/optimizer/scan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import glob from 'fast-glob'
import type { Plugin } from 'rolldown'
import * as rolldown from 'rolldown'
import type { Loader } from 'esbuild'
import { transform } from 'esbuild'
import { transform } from 'esbuild'
import colors from 'picocolors'
import type { ResolvedConfig } from '..'
import {
Expand Down Expand Up @@ -41,6 +41,7 @@ type ResolveIdOptions = Parameters<PluginContainer['resolveId']>[2]

const debug = createDebugger('vite:deps')

// eslint-disable-next-line regexp/no-unused-capturing-group
const htmlTypesRE = /\.(html|vue|svelte|astro|imba)$/

// A simple regex to detect import sources. This is only used on
Expand Down Expand Up @@ -100,15 +101,13 @@ export function scanImports(config: ResolvedConfig): {
if (!context || scanContext?.cancelled) {
return { deps: {}, missing: {} }
}
return context
.build()
.then(() => {
return {
// Ensure a fixed order so hashes are stable and improve logs
deps: orderedDependencies(deps),
missing,
}
})
return context.build().then(() => {
return {
// Ensure a fixed order so hashes are stable and improve logs
deps: orderedDependencies(deps),
missing,
}
})
})
.catch(async (e) => {
// if (e.errors && e.message.includes('The build was canceled')) {
Expand All @@ -129,7 +128,7 @@ export function scanImports(config: ResolvedConfig): {
// })
// e.message = prependMessage + msgs.join('\n')
// } else {
e.message = prependMessage + e.message
e.message = prependMessage + e.message
// }
throw e
})
Expand Down Expand Up @@ -807,10 +806,10 @@ function rolldownScanPlugin(
content + (loader.startsWith('ts') ? extractImportPaths(content) : '')

const key = `${p}?id=${scriptId++}&loader=${loader}`
if (loader !== 'js') {
contents = (await transform(contents, { loader })).code
}
if (contents.includes('import.meta.glob')) {
if (loader !== 'js') {
contents = (await transform(contents, { loader })).code
}
scripts[key] = await doTransformGlobImport(contents, p)
} else {
scripts[key] = contents
Expand Down Expand Up @@ -1020,11 +1019,10 @@ function rolldownScanPlugin(

const loader = ext as Loader

if (loader !== 'js') {
contents = (await transform(contents, { loader })).code
}

if (contents.includes('import.meta.glob')) {
if (loader !== 'js') {
contents = (await transform(contents, { loader })).code
}
return {
code: await doTransformGlobImport(contents, id),
}
Expand All @@ -1038,7 +1036,6 @@ function rolldownScanPlugin(
}
}


/**
* when using TS + (Vue + `<script setup>`) or Svelte, imports may seem
* unused to esbuild and dropped in the build output, which prevents
Expand Down

0 comments on commit c5f38bf

Please sign in to comment.