diff --git a/packages/commonjs/CHANGELOG.md b/packages/commonjs/CHANGELOG.md index e5a16d3fc..26909f198 100644 --- a/packages/commonjs/CHANGELOG.md +++ b/packages/commonjs/CHANGELOG.md @@ -1,13 +1,5 @@ # @rollup/plugin-commonjs ChangeLog -## v26.0.2 - -_2024-09-23_ - -### Bugfixes - -- fix: replace top-level this with exports name (#1618) - ## v26.0.1 _2024-06-05_ diff --git a/packages/commonjs/src/transform-commonjs.js b/packages/commonjs/src/transform-commonjs.js index ba348dedf..4601094e9 100644 --- a/packages/commonjs/src/transform-commonjs.js +++ b/packages/commonjs/src/transform-commonjs.js @@ -102,7 +102,6 @@ export default async function transformCommonjs( const topLevelAssignments = new Set(); const topLevelDefineCompiledEsmExpressions = []; const replacedGlobal = []; - const replacedThis = []; const replacedDynamicRequires = []; const importedVariables = new Set(); const indentExclusionRanges = []; @@ -370,7 +369,7 @@ export default async function transformCommonjs( if (lexicalDepth === 0 && !classBodyDepth) { uses.global = true; if (!ignoreGlobal) { - replacedThis.push(node); + replacedGlobal.push(node); } } return; @@ -445,11 +444,6 @@ export default async function transformCommonjs( storeName: true }); } - for (const node of replacedThis) { - magicString.overwrite(node.start, node.end, exportsName, { - storeName: true - }); - } for (const node of replacedDynamicRequires) { magicString.overwrite( node.start, diff --git a/packages/commonjs/test/fixtures/function/this/main.js b/packages/commonjs/test/fixtures/function/this/main.js index c0be15432..d0c275253 100644 --- a/packages/commonjs/test/fixtures/function/this/main.js +++ b/packages/commonjs/test/fixtures/function/this/main.js @@ -1,11 +1,10 @@ -const { augmentThis, classThis, y } = require('./foo'); +const { augmentThis, classThis } = require('./foo'); const obj = {}; augmentThis.call(obj); t.is(obj.x, 'x'); -t.is(this.y, undefined); -t.is(y, 'y'); +t.is(this.y, 'y'); const instance = new classThis(); diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index c6e8d6b30..2c31aab43 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -7286,6 +7286,8 @@ Generated by [AVA](https://avajs.dev). { 'main.js': `'use strict';␊ ␊ + var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊ + ␊ var main = {};␊ ␊ var foo = {};␊ @@ -7294,7 +7296,7 @@ Generated by [AVA](https://avajs.dev). this.x = 'x';␊ };␊ ␊ - foo.y = 'y';␊ + commonjsGlobal.y = 'y';␊ ␊ foo.classThis = class classThis {␊ constructor(){␊ @@ -7308,14 +7310,13 @@ Generated by [AVA](https://avajs.dev). yy = this.y␊ };␊ ␊ - const { augmentThis, classThis, y } = foo;␊ + const { augmentThis, classThis } = foo;␊ ␊ const obj = {};␊ augmentThis.call(obj);␊ ␊ t.is(obj.x, 'x');␊ - t.is(main.y, undefined);␊ - t.is(y, 'y');␊ + t.is(commonjsGlobal.y, 'y');␊ ␊ const instance = new classThis();␊ ␊ diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index 726565e0d..97a1e03b0 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ