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(optimizer): browser mapping for yarn pnp #6493

Merged
merged 8 commits into from
Jun 16, 2022
73 changes: 39 additions & 34 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,24 @@ export function esbuildDepPlugin(
return resolver(id, _importer, undefined, ssr)
}

const resolveResult = (id: string, resolved: string) => {
if (resolved.startsWith(browserExternalId)) {
return {
path: id,
namespace: 'browser-external'
}
}
if (isExternalUrl(resolved)) {
return {
path: resolved,
external: true
}
}
return {
path: path.resolve(resolved)
}
}

return {
name: 'vite:dep-pre-bundle',
setup(build) {
Expand Down Expand Up @@ -122,21 +140,7 @@ export function esbuildDepPlugin(
// use vite's own resolver
const resolved = await resolve(id, importer, kind)
if (resolved) {
if (resolved.startsWith(browserExternalId)) {
return {
path: id,
namespace: 'browser-external'
}
}
if (isExternalUrl(resolved)) {
return {
path: resolved,
external: true
}
}
return {
path: path.resolve(resolved)
}
return resolveResult(id, resolved)
}
}
)
Expand Down Expand Up @@ -190,30 +194,31 @@ export function esbuildDepPlugin(
}
})

build.onLoad(
{ filter: /.*/, namespace: 'browser-external' },
({ path: id }) => {
return {
contents:
`export default new Proxy({}, {
get() {
throw new Error('Module "${id}" has been externalized for ` +
`browser compatibility and cannot be accessed in client code.')
}
})`
}
}
)

// yarn 2 pnp compat
if (isRunningWithYarnPnp) {
build.onResolve(
{ filter: /.*/ },
async ({ path, importer, kind, resolveDir }) => ({
// pass along resolveDir for entries
path: await resolve(path, importer, kind, resolveDir)
})
async ({ path: id, importer, kind, resolveDir, namespace }) => {
const resolved = await resolve(
id,
importer,
kind,
// pass along resolveDir for entries
namespace === 'dep' ? resolveDir : undefined
Comment on lines +270 to +271
Copy link
Contributor Author

Choose a reason for hiding this comment

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

browser-external proxy from #8338 fixes build-time crashed, but something has changed elsewhere and this workaround doesn't work anymore 🥲 Will have to look into the changes since 2.9.9

Copy link
Contributor Author

Choose a reason for hiding this comment

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

)
if (resolved) {
return resolveResult(id, resolved)
}
}
swandir marked this conversation as resolved.
Show resolved Hide resolved
)

// Without pnp esbuild is transforming modules disabled for the browser
// to empty CommonJS modules. In pnp mode we achieve the same by passing
// empty contents causing esbuild to produce an empty CommonJS module.
build.onLoad({ filter: /.*/, namespace: 'browser-external' }, () => {
return { contents: '' }
})
swandir marked this conversation as resolved.
Show resolved Hide resolved

build.onLoad({ filter: /.*/ }, async (args) => ({
contents: await require('fs').promises.readFile(args.path),
loader: 'default'
Expand Down