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 1 commit
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
12 changes: 11 additions & 1 deletion 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 @@ -242,7 +243,16 @@ export async function optimizeDeps(
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 {
config.logger.warn(
`Unable to parse dependency: ${id}. Trying again with an esbuild transform.`
aleclarson marked this conversation as resolved.
Show resolved Hide resolved
)
const transformed = await transform(entryContent, config.esbuild || {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not use { loader: { '.js': 'jsx' } } automatically, removing the need for user to configure it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good question. Even if we hardcode this here, you still do need the following to make it work, because it would otherwise fail at a later stage when optimising deps:

        optimizeDeps: {
          esbuildOptions: {
            loader: {
              ".js": "jsx",
            },
          },
        },

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could move this statement up:

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

..then mutate the esbuildOptions from here:

esbuildOptions.loader ??= {}
esbuildOptions.loader['.js'] ??= 'jsx'
const transformed = await transform(entryContent, esbuildOptions)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea. I did that, although couldn't directly use esbuildOptions for the transform call (since the loader field type isn't compatible). Let me know what you think!

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 Down