From 76a49daa91e0fe44b2662f61c378c5e22afda752 Mon Sep 17 00:00:00 2001 From: swandir Date: Thu, 27 Jan 2022 21:14:11 +0300 Subject: [PATCH] fix(optimizer): browser-external proxy --- .../src/node/optimizer/esbuildDepPlugin.ts | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) 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) } } )