Skip to content

Commit 175e98f

Browse files
committed
Merge branch 'master' into commonjs-strict-requires-true
2 parents e2d74c0 + 1e73857 commit 175e98f

File tree

7 files changed

+23
-9
lines changed

7 files changed

+23
-9
lines changed

β€Žpackages/commonjs/CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# @rollup/plugin-commonjs ChangeLog
22

3+
## v26.0.2
4+
5+
_2024-09-23_
6+
7+
### Bugfixes
8+
9+
- fix: replace top-level this with exports name (#1618)
10+
311
## v26.0.1
412

513
_2024-06-05_

β€Žpackages/commonjs/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@rollup/plugin-commonjs",
3-
"version": "26.0.1",
3+
"version": "26.0.2",
44
"publishConfig": {
55
"access": "public"
66
},

β€Žpackages/commonjs/src/transform-commonjs.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export default async function transformCommonjs(
102102
const topLevelAssignments = new Set();
103103
const topLevelDefineCompiledEsmExpressions = [];
104104
const replacedGlobal = [];
105+
const replacedThis = [];
105106
const replacedDynamicRequires = [];
106107
const importedVariables = new Set();
107108
const indentExclusionRanges = [];
@@ -369,7 +370,7 @@ export default async function transformCommonjs(
369370
if (lexicalDepth === 0 && !classBodyDepth) {
370371
uses.global = true;
371372
if (!ignoreGlobal) {
372-
replacedGlobal.push(node);
373+
replacedThis.push(node);
373374
}
374375
}
375376
return;
@@ -444,6 +445,11 @@ export default async function transformCommonjs(
444445
storeName: true
445446
});
446447
}
448+
for (const node of replacedThis) {
449+
magicString.overwrite(node.start, node.end, exportsName, {
450+
storeName: true
451+
});
452+
}
447453
for (const node of replacedDynamicRequires) {
448454
magicString.overwrite(
449455
node.start,

β€Žpackages/commonjs/test/fixtures/function/this/main.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
const { augmentThis, classThis } = require('./foo');
1+
const { augmentThis, classThis, y } = require('./foo');
22

33
const obj = {};
44
augmentThis.call(obj);
55

66
t.is(obj.x, 'x');
7-
t.is(this.y, 'y');
7+
t.is(this.y, undefined);
8+
t.is(y, 'y');
89

910
const instance = new classThis();
1011

β€Žpackages/commonjs/test/snapshots/function.js.md

+4-5
Original file line numberDiff line numberDiff line change
@@ -9230,8 +9230,6 @@ Generated by [AVA](https://avajs.dev).
92309230
{
92319231
'main.js': `'use strict';␊
92329232
␊
9233-
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
9234-
␊
92359233
function getDefaultExportFromCjs (x) {␊
92369234
return x && x.__esModule && Object.prototype.hasOwnProperty.call(x, 'default') ? x['default'] : x;␊
92379235
}␊
@@ -9249,7 +9247,7 @@ Generated by [AVA](https://avajs.dev).
92499247
this.x = 'x';␊
92509248
};␊
92519249
␊
9252-
commonjsGlobal.y = 'y';␊
9250+
foo.y = 'y';␊
92539251
␊
92549252
foo.classThis = class classThis {␊
92559253
constructor(){␊
@@ -9270,13 +9268,14 @@ Generated by [AVA](https://avajs.dev).
92709268
function requireMain () {␊
92719269
if (hasRequiredMain) return main$1;␊
92729270
hasRequiredMain = 1;␊
9273-
const { augmentThis, classThis } = requireFoo();␊
9271+
const { augmentThis, classThis, y } = requireFoo();␊
92749272
␊
92759273
const obj = {};␊
92769274
augmentThis.call(obj);␊
92779275
␊
92789276
t.is(obj.x, 'x');␊
9279-
t.is(commonjsGlobal.y, 'y');␊
9277+
t.is(main$1.y, undefined);␊
9278+
t.is(y, 'y');␊
92809279
␊
92819280
const instance = new classThis();␊
92829281
␊
Binary file not shown.
-2 Bytes
Binary file not shown.

0 commit comments

Comments
Β (0)