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: enable failing dependencies to be optimised by pre-processing them with esbuild #4275

Merged
merged 6 commits into from Aug 23, 2021
Merged
Changes from 3 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
23 changes: 19 additions & 4 deletions packages/vite/src/node/optimizer/index.ts
Expand Up @@ -15,6 +15,7 @@ import {
import { esbuildDepPlugin } from './esbuildDepPlugin'
import { ImportSpecifier, init, parse } from 'es-module-lexer'
import { scanImports } from './scan'
import { transform } from 'esbuild'

const debug = createDebugger('vite:deps')

Expand Down Expand Up @@ -237,12 +238,29 @@ export async function optimizeDeps(
const idToExports: Record<string, ExportsData> = {}
const flatIdToExports: Record<string, ExportsData> = {}

const { plugins = [], ...esbuildOptions } =
config.optimizeDeps?.esbuildOptions ?? {}

await init
for (const id in deps) {
const flatId = flattenId(id)
flatIdDeps[flatId] = deps[id]
const entryContent = fs.readFileSync(deps[id], 'utf-8')
const exportsData = parse(entryContent) as ExportsData
let exportsData: ExportsData
try {
exportsData = parse(entryContent) as ExportsData
} catch {
debug(
`Unable to parse dependency: ${id}. Trying again with a JSX transform.`
)
const transformed = await transform(entryContent, {
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
loader: 'jsx'
})
// Ensure that optimization won't fail.
esbuildOptions.loader ??= {}
aleclarson marked this conversation as resolved.
Show resolved Hide resolved
esbuildOptions.loader['.js'] ??= 'jsx'
Shinigami92 marked this conversation as resolved.
Show resolved Hide resolved
exportsData = parse(transformed.code) as ExportsData
}
for (const { ss, se } of exportsData[0]) {
const exp = entryContent.slice(ss, se)
if (/export\s+\*\s+from/.test(exp)) {
Expand All @@ -263,9 +281,6 @@ export async function optimizeDeps(

const start = Date.now()

const { plugins = [], ...esbuildOptions } =
config.optimizeDeps?.esbuildOptions ?? {}

const result = await build({
absWorkingDir: process.cwd(),
entryPoints: Object.keys(flatIdDeps),
Expand Down