Skip to content

Commit

Permalink
Merge branch 'master' into commonjs-strict-requires-true
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy committed Sep 23, 2024
2 parents e2d74c0 + 1e73857 commit 175e98f
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
8 changes: 8 additions & 0 deletions packages/commonjs/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# @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_
Expand Down
2 changes: 1 addition & 1 deletion packages/commonjs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rollup/plugin-commonjs",
"version": "26.0.1",
"version": "26.0.2",
"publishConfig": {
"access": "public"
},
Expand Down
8 changes: 7 additions & 1 deletion packages/commonjs/src/transform-commonjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export default async function transformCommonjs(
const topLevelAssignments = new Set();
const topLevelDefineCompiledEsmExpressions = [];
const replacedGlobal = [];
const replacedThis = [];
const replacedDynamicRequires = [];
const importedVariables = new Set();
const indentExclusionRanges = [];
Expand Down Expand Up @@ -369,7 +370,7 @@ export default async function transformCommonjs(
if (lexicalDepth === 0 && !classBodyDepth) {
uses.global = true;
if (!ignoreGlobal) {
replacedGlobal.push(node);
replacedThis.push(node);
}
}
return;
Expand Down Expand Up @@ -444,6 +445,11 @@ 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,
Expand Down
5 changes: 3 additions & 2 deletions packages/commonjs/test/fixtures/function/this/main.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
const { augmentThis, classThis } = require('./foo');
const { augmentThis, classThis, y } = require('./foo');

const obj = {};
augmentThis.call(obj);

t.is(obj.x, 'x');
t.is(this.y, 'y');
t.is(this.y, undefined);
t.is(y, 'y');

const instance = new classThis();

Expand Down
9 changes: 4 additions & 5 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -9230,8 +9230,6 @@ 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 : {};␊
␊
function getDefaultExportFromCjs (x) {␊
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
}␊
Expand All @@ -9249,7 +9247,7 @@ Generated by [AVA](https://avajs.dev).
this.x = 'x';␊
};␊
␊
commonjsGlobal.y = 'y';␊
foo.y = 'y';␊
␊
foo.classThis = class classThis {␊
constructor(){␊
Expand All @@ -9270,13 +9268,14 @@ Generated by [AVA](https://avajs.dev).
function requireMain () {␊
if (hasRequiredMain) return main$1;␊
hasRequiredMain = 1;␊
const { augmentThis, classThis } = requireFoo();␊
const { augmentThis, classThis, y } = requireFoo();␊
␊
const obj = {};␊
augmentThis.call(obj);␊
␊
t.is(obj.x, 'x');␊
t.is(commonjsGlobal.y, 'y');␊
t.is(main$1.y, undefined);␊
t.is(y, 'y');␊
␊
const instance = new classThis();␊
␊
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.
Binary file modified packages/commonjs/test/snapshots/test.js.snap
Binary file not shown.

0 comments on commit 175e98f

Please sign in to comment.