Skip to content

Commit 5963fb5

Browse files
committed
feat: add __ODC_BUNDLER_ERROR_CONTEXT
1 parent 7d6f440 commit 5963fb5

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

packages/instrument-bundler/src/bundle.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,24 @@ import type { BundleOptions } from './schemas.js';
77
import type { BuildOutput } from './types.js';
88

99
const GLOBAL_PROXY_SHIM = `
10-
const createProxy = (name) => {
10+
let __ODC_BUNDLER_ERROR_CONTEXT;
11+
const __createProxy = (name) => {
12+
const formatErrorMessage = (method, propertyName, targetName) => {
13+
const contextName = __ODC_BUNDLER_ERROR_CONTEXT ?? 'UNKNOWN'
14+
return "Cannot " + method + " property '" + propertyName + "' of object '" + targetName + "' in global scope of context '" + contextName + "'"
15+
}
1116
return new Proxy({ name }, {
1217
get(target, property) {
13-
throw new Error("Cannot get property '" + property.toString() + "' of object '" + target.name + "' in global scope");
18+
throw new Error(formatErrorMessage('get', property.toString(), target.name))
1419
},
1520
set(target, property) {
16-
throw new Error("Cannot set property '" + property.toString() + "' of object '" + target.name + "' in global scope");
21+
throw new Error(formatErrorMessage('set', property.toString(), target.name))
1722
}
1823
});
1924
};
20-
const document = globalThis.document ?? createProxy('document');
21-
const self = globalThis.self ?? createProxy('self');
22-
const window = globalThis.window ?? createProxy('window');
25+
const document = globalThis.document ?? __createProxy('document');
26+
const self = globalThis.self ?? __createProxy('self');
27+
const window = globalThis.window ?? __createProxy('window');
2328
`;
2429

2530
/**
@@ -43,7 +48,9 @@ export async function createBundle(output: BuildOutput, options: { minify: boole
4348
const result = await esbuild.transform(bundle, {
4449
charset: 'ascii',
4550
format: 'esm',
46-
minify: options.minify,
51+
minifyIdentifiers: false,
52+
minifySyntax: options.minify,
53+
minifyWhitespace: options.minify,
4754
platform: 'browser',
4855
target: 'es2022',
4956
treeShaking: true

packages/instrument-bundler/src/plugin.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,14 @@ export const plugin = (options: { inputs: BundlerInput[] }): Plugin => {
4444
]
4545
};
4646
}
47-
return {
48-
contents: input.content,
49-
loader: inferLoader(input.name)
50-
};
47+
let contents: typeof input.content;
48+
const loader = inferLoader(input.name);
49+
if (loader === 'js' || loader === 'jsx' || loader === 'ts' || loader == 'tsx') {
50+
contents = [`__ODC_BUNDLER_ERROR_CONTEXT = "${input.name}";`, input.content as string].join('\n');
51+
} else {
52+
contents = input.content;
53+
}
54+
return { contents, loader };
5155
});
5256
}
5357
};

0 commit comments

Comments
 (0)