diff --git a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts index 1f91847e77138b..4b78944e89d2c4 100644 --- a/packages/vite/src/node/optimizer/esbuildDepPlugin.ts +++ b/packages/vite/src/node/optimizer/esbuildDepPlugin.ts @@ -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.' + ) + } + }) + } +}) +` + export function esbuildDepPlugin( qualified: Record, exportsData: Record, @@ -197,15 +212,7 @@ 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) } } )