Skip to content

Commit

Permalink
fix: esbuild optimizer yarn 2 pnp compat
Browse files Browse the repository at this point in the history
ref #1688
  • Loading branch information
yyx990803 committed Jan 24, 2021
1 parent 0bed9c4 commit 028c3bb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
13 changes: 12 additions & 1 deletion packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Expand Up @@ -3,7 +3,7 @@ import { Plugin } from 'esbuild'
import { knownAssetTypes } from '../constants'
import { ResolvedConfig } from '..'
import chalk from 'chalk'
import { deepImportRE, isBuiltin } from '../utils'
import { deepImportRE, isBuiltin, isRunningWithYarnPnp } from '../utils'
import { tryNodeResolve } from '../plugins/resolve'
import { PluginContainer } from '../server/pluginContainer'

Expand Down Expand Up @@ -90,6 +90,17 @@ export function esbuildDepPlugin(
}
}
)

// yarn 2 pnp compat
if (isRunningWithYarnPnp) {
build.onResolve({ filter: /\.yarn.*/ }, (args) => ({
path: require.resolve(args.path, { paths: [args.resolveDir] })
}))
build.onLoad({ filter: /\.yarn.*/ }, async (args) => ({
contents: await require('fs').promises.readFile(args.path),
loader: 'default'
}))
}
}
}
}
2 changes: 1 addition & 1 deletion packages/vite/src/node/utils.ts
Expand Up @@ -16,7 +16,7 @@ export function isBuiltin(id: string): boolean {
export const bareImportRE = /^[\w@](?!.*:\/\/)/
export const deepImportRE = /^([^@][^/]*)\/|^(@[^/]+\/[^/]+)\//

let isRunningWithYarnPnp: boolean
export let isRunningWithYarnPnp: boolean
try {
isRunningWithYarnPnp = Boolean(require('pnpapi'))
} catch {}
Expand Down

0 comments on commit 028c3bb

Please sign in to comment.