Skip to content

Commit bfc679e

Browse files
committed
fix: refactor browser external proxy module to avoid missing export error
1 parent f6333b5 commit bfc679e

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

packages/vite/src/node/plugins/resolve.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,15 +447,26 @@ export function resolvePlugin(resolveOptions: InternalResolveOptions): Plugin {
447447
load(id) {
448448
if (id.startsWith(browserExternalId)) {
449449
if (isProduction) {
450-
return `export default {}`
450+
// return `export default {}`
451+
// The rolldown missing export is always error level, it will break build.
452+
// So here using the cjs module to avoid it.
453+
return `module.exports = {}`
451454
} else {
452455
id = id.slice(browserExternalId.length + 1)
456+
// The rolldown using esbuild interop helper, so here copy the proxy module from https://github.com/vitejs/vite/blob/main/packages/vite/src/node/optimizer/esbuildDepPlugin.ts#L259
453457
return `\
454-
export default new Proxy({}, {
458+
module.exports = Object.create(new Proxy({}, {
455459
get(_, key) {
456-
throw new Error(\`Module "${id}" has been externalized for browser compatibility. Cannot access "${id}.\${key}" in client code. See https://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.\`)
460+
if (
461+
key !== '__esModule' &&
462+
key !== '__proto__' &&
463+
key !== 'constructor' &&
464+
key !== 'splice'
465+
) {
466+
throw new Error(\`Module "${id}" has been externalized for browser compatibility. Cannot access "${id}.\${key}" in client code. See https://vitejs.dev/guide/troubleshooting.html#module-externalized-for-browser-compatibility for more details.\`)
467+
}
457468
}
458-
})`
469+
}))`
459470
}
460471
}
461472
if (id.startsWith(optionalPeerDepId)) {

playground/optimize-deps/vite.config.js

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ export default defineConfig({
6262
minify: false,
6363
rollupOptions: {
6464
onwarn(msg, warn) {
65-
// filter `"Buffer" is not exported by "__vite-browser-external"` warning
66-
if (msg.message.includes('Buffer')) return
65+
// // filter `"Buffer" is not exported by "__vite-browser-external"` warning
66+
// if (msg.message.includes('Buffer')) return
6767
warn(msg)
6868
},
6969
},
@@ -97,19 +97,20 @@ export default defineConfig({
9797
}
9898
},
9999
},
100-
// TODO: Remove this one support for prebundling in build lands.
101-
// It is expected that named importing in build doesn't work
102-
// as it incurs a lot of overhead in build.
103-
{
104-
name: 'polyfill-named-fs-build',
105-
apply: 'build',
106-
enforce: 'pre',
107-
load(id) {
108-
if (id === '__vite-browser-external') {
109-
return `export default {}; export function readFileSync() {}`
110-
}
111-
},
112-
},
100+
// Remove this one because the resolve plugin already handle '__vite-browser-external'
101+
// // TODO: Remove this one support for prebundling in build lands.
102+
// // It is expected that named importing in build doesn't work
103+
// // as it incurs a lot of overhead in build.
104+
// {
105+
// name: 'polyfill-named-fs-build',
106+
// apply: 'build',
107+
// enforce: 'pre',
108+
// load(id) {
109+
// if (id === '__vite-browser-external') {
110+
// return `export default {}; export function readFileSync() {}`
111+
// }
112+
// },
113+
// },
113114
],
114115
})
115116

0 commit comments

Comments
 (0)