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
75 changes: 47 additions & 28 deletions packages/vite/src/node/optimizer/esbuildDepPlugin.ts
Expand Up @@ -34,6 +34,21 @@ const externalTypes = [
...KNOWN_ASSET_TYPES
]

const browserExternalContents = (id: string) => `\
module.exports = new Proxy({}, {
get() {
return new Proxy({}, {
get() {
throw new Error(
'Module "${id}" has been externalized for ' +
'browser compatibility and cannot be accessed in client code.'
)
}
})
}
})
`
swandir marked this conversation as resolved.
Show resolved Hide resolved

export function esbuildDepPlugin(
qualified: Record<string, string>,
exportsData: Record<string, ExportsData>,
Expand Down Expand Up @@ -68,6 +83,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 +155,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 @@ -193,26 +212,26 @@ 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.')
}
})`
}
return { contents: browserExternalContents(id) }
}
)

// 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
)
build.onLoad({ filter: /.*/ }, async (args) => ({
contents: await require('fs').promises.readFile(args.path),
Expand Down