From 18b29d2dfbfa4363f4757d64c49ca2296fdbbcb9 Mon Sep 17 00:00:00 2001 From: Katie Byers Date: Thu, 27 Oct 2022 03:45:55 -0700 Subject: [PATCH] fix(build): Prevent rollup from adding `[Symbol.toStringTag]: 'Module'` to CJS files (#6043) As of version 2.69.0, setting `output.generatedCode` to `'es2015'` (as we do) causes Rollup to [add `[Symbol.toStringTag]: 'Module'` to generated CJS files](https://github.com/rollup/rollup/pull/4378#issuecomment-1055449703). Though this is valid ES6, it causes Jest to be unable to mock our generated packages. Though [a PR](https://github.com/facebook/jest/pull/13513) has been opened to fix this, the change almost certainly won't be backported, so anyone using Jest 29.2.2 or under will run into [this problem](https://github.com/getsentry/sentry-javascript/issues/5994) if they try to mock `@sentry/xxx` 7.16. (The relevant change was introduced in https://github.com/getsentry/sentry-javascript/issues/5951, when we (semi-)accidentally upgraded Rollup from 2.67.1 to 2.78.0, and was first included in version 7.16.) This PR prevents the new Rollup behavior, since it has no known benefit. (The [Rollup docs](https://rollupjs.org/guide/en/#outputgeneratedcode) say that presence of the `'Module'` toStringTag "is used for feature detection in certain libraries and frameworks," but not having it hasn't seemed to hurt us so far.) --- rollup/npmHelpers.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/rollup/npmHelpers.js b/rollup/npmHelpers.js index b7d0fd187e14..e1657f7ef598 100644 --- a/rollup/npmHelpers.js +++ b/rollup/npmHelpers.js @@ -44,8 +44,14 @@ export function makeBaseNPMConfig(options = {}) { // output individual files rather than one big bundle preserveModules: true, - // any wrappers or helper functions generated by rollup can use ES6 features - generatedCode: 'es2015', + // Allow wrappers or helper functions generated by rollup to use any ES6 features except symbols. (Symbols in + // general are fine, but the `[Symbol.toStringTag]: 'Module'` which Rollup adds alongside `__esModule: + // true` in CJS modules makes it so that Jest <= 29.2.2 crashes when trying to mock generated `@sentry/xxx` + // packages. See https://github.com/getsentry/sentry-javascript/pull/6043.) + generatedCode: { + preset: 'es2015', + symbols: false, + }, // don't add `"use strict"` to the top of cjs files strict: false,