Skip to content

Commit b255ad5

Browse files
authored
fix(v2/core): Make moduleMetadata injection code ES5-compliant (#773)
Makes the v2 `moduleMetadata` code snippet ES5-compliant by replacing the `Object.assign` merge strategy of the module with a pure ES5 merge strategy: iterating over the object property of both objects to merge to the final object. Obviously, this increases the bundle size footprint but I think we need to prioritize correctness. Also took the opportunity to minify the code and further isolate the `var`-declared variables by wrapping the code in an IIFE. Adjusted to the tests to also use `toMatchSnapshot`, since the inline snapshots would no longer be readable.
1 parent 01404ae commit b255ad5

File tree

3 files changed

+10
-70
lines changed

3 files changed

+10
-70
lines changed

packages/bundler-plugin-core/src/utils.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -343,28 +343,9 @@ export function generateModuleMetadataInjectorCode(metadata: any) {
343343
// The code below is mostly ternary operators because it saves bundle size.
344344
// The checks are to support as many environments as possible. (Node.js, Browser, webworkers, etc.)
345345
// We are merging the metadata objects in case modules are bundled twice with the plugin
346-
return `{
347-
var _sentryModuleMetadataGlobal =
348-
typeof window !== "undefined"
349-
? window
350-
: typeof global !== "undefined"
351-
? global
352-
: typeof globalThis !== "undefined"
353-
? globalThis
354-
: typeof self !== "undefined"
355-
? self
356-
: {};
357-
358-
_sentryModuleMetadataGlobal._sentryModuleMetadata =
359-
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};
360-
361-
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack] =
362-
Object.assign(
363-
{},
364-
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
365-
${JSON.stringify(metadata)}
366-
);
367-
}`;
346+
return `!function(){var e="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:{};e._sentryModuleMetadata=e._sentryModuleMetadata||{},function(){var n,t=(new e.Error).stack,a=e._sentryModuleMetadata[t]||{},o=${JSON.stringify(
347+
metadata
348+
)},d={};for(n in a)a.hasOwnProperty(n)&&(d[n]=a[n]);for(n in o)o.hasOwnProperty(n)&&(d[n]=o[n]);e._sentryModuleMetadata[t]=d}()}();`;
368349
}
369350

370351
function getBuildInformation() {
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`generateModuleMetadataInjectorCode generates code with empty metadata object 1`] = `"!function(){var e=\\"undefined\\"!=typeof window?window:\\"undefined\\"!=typeof global?global:\\"undefined\\"!=typeof globalThis?globalThis:\\"undefined\\"!=typeof self?self:{};e._sentryModuleMetadata=e._sentryModuleMetadata||{},function(){var n,t=(new e.Error).stack,a=e._sentryModuleMetadata[t]||{},o={},d={};for(n in a)a.hasOwnProperty(n)&&(d[n]=a[n]);for(n in o)o.hasOwnProperty(n)&&(d[n]=o[n]);e._sentryModuleMetadata[t]=d}()}();"`;
4+
5+
exports[`generateModuleMetadataInjectorCode generates code with metadata object 1`] = `"!function(){var e=\\"undefined\\"!=typeof window?window:\\"undefined\\"!=typeof global?global:\\"undefined\\"!=typeof globalThis?globalThis:\\"undefined\\"!=typeof self?self:{};e._sentryModuleMetadata=e._sentryModuleMetadata||{},function(){var n,t=(new e.Error).stack,a=e._sentryModuleMetadata[t]||{},o={\\"file1.js\\":{\\"foo\\":\\"bar\\"},\\"file2.js\\":{\\"bar\\":\\"baz\\"}},d={};for(n in a)a.hasOwnProperty(n)&&(d[n]=a[n]);for(n in o)o.hasOwnProperty(n)&&(d[n]=o[n]);e._sentryModuleMetadata[t]=d}()}();"`;

packages/bundler-plugin-core/test/utils.test.ts

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -219,30 +219,7 @@ if (false && true) {
219219
describe("generateModuleMetadataInjectorCode", () => {
220220
it("generates code with empty metadata object", () => {
221221
const generatedCode = generateModuleMetadataInjectorCode({});
222-
expect(generatedCode).toMatchInlineSnapshot(`
223-
"{
224-
var _sentryModuleMetadataGlobal =
225-
typeof window !== \\"undefined\\"
226-
? window
227-
: typeof global !== \\"undefined\\"
228-
? global
229-
: typeof globalThis !== \\"undefined\\"
230-
? globalThis
231-
: typeof self !== \\"undefined\\"
232-
? self
233-
: {};
234-
235-
_sentryModuleMetadataGlobal._sentryModuleMetadata =
236-
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};
237-
238-
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack] =
239-
Object.assign(
240-
{},
241-
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
242-
{}
243-
);
244-
}"
245-
`);
222+
expect(generatedCode).toMatchSnapshot();
246223
});
247224

248225
it("generates code with metadata object", () => {
@@ -254,29 +231,6 @@ describe("generateModuleMetadataInjectorCode", () => {
254231
bar: "baz",
255232
},
256233
});
257-
expect(generatedCode).toMatchInlineSnapshot(`
258-
"{
259-
var _sentryModuleMetadataGlobal =
260-
typeof window !== \\"undefined\\"
261-
? window
262-
: typeof global !== \\"undefined\\"
263-
? global
264-
: typeof globalThis !== \\"undefined\\"
265-
? globalThis
266-
: typeof self !== \\"undefined\\"
267-
? self
268-
: {};
269-
270-
_sentryModuleMetadataGlobal._sentryModuleMetadata =
271-
_sentryModuleMetadataGlobal._sentryModuleMetadata || {};
272-
273-
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack] =
274-
Object.assign(
275-
{},
276-
_sentryModuleMetadataGlobal._sentryModuleMetadata[new _sentryModuleMetadataGlobal.Error().stack],
277-
{\\"file1.js\\":{\\"foo\\":\\"bar\\"},\\"file2.js\\":{\\"bar\\":\\"baz\\"}}
278-
);
279-
}"
280-
`);
234+
expect(generatedCode).toMatchSnapshot();
281235
});
282236
});

0 commit comments

Comments
 (0)