Skip to content

Commit 7d6f440

Browse files
committed
fix: add GLOBAL_PROXY_SHIM to bundle
1 parent a9f6e55 commit 7d6f440

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

packages/instrument-bundler/src/bundle.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,22 @@ import * as esbuild from './vendor/esbuild.js';
66
import type { BundleOptions } from './schemas.js';
77
import type { BuildOutput } from './types.js';
88

9+
const GLOBAL_PROXY_SHIM = `
10+
const createProxy = (name) => {
11+
return new Proxy({ name }, {
12+
get(target, property) {
13+
throw new Error("Cannot get property '" + property.toString() + "' of object '" + target.name + "' in global scope");
14+
},
15+
set(target, property) {
16+
throw new Error("Cannot set property '" + property.toString() + "' of object '" + target.name + "' in global scope");
17+
}
18+
});
19+
};
20+
const document = globalThis.document ?? createProxy('document');
21+
const self = globalThis.self ?? createProxy('self');
22+
const window = globalThis.window ?? createProxy('window');
23+
`;
24+
925
/**
1026
* Converts the bundle into an an immediately invoked function expression (IIFE) that returns the value of
1127
* a top-level variable '__exports'. The result is subject to tree shaking and minification.
@@ -19,6 +35,7 @@ export async function createBundle(output: BuildOutput, options: { minify: boole
1935
inject = `Object.defineProperty(__exports.content, '__injectHead', { value: Object.freeze({ style: "${btoa(output.css)}" }), writable: false });`;
2036
}
2137
const bundle = `(async () => {
38+
${GLOBAL_PROXY_SHIM}
2239
${output.js}
2340
${inject}
2441
return __exports;

0 commit comments

Comments
 (0)