Skip to content

Commit 274b72c

Browse files
bluwyshellscape
authored andcommitted
chore(repo): cherry-pick. commonjs. replace top-level this with exports name (#1618)
1 parent e2a7195 commit 274b72c

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

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

Lines changed: 7 additions & 1 deletion
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7286,8 +7286,6 @@ Generated by [AVA](https://avajs.dev).
72867286
{
72877287
'main.js': `'use strict';␊
72887288
␊
7289-
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};␊
7290-
␊
72917289
var main = {};␊
72927290
␊
72937291
var foo = {};␊
@@ -7296,7 +7294,7 @@ Generated by [AVA](https://avajs.dev).
72967294
this.x = 'x';␊
72977295
};␊
72987296
␊
7299-
commonjsGlobal.y = 'y';␊
7297+
foo.y = 'y';␊
73007298
␊
73017299
foo.classThis = class classThis {␊
73027300
constructor(){␊
@@ -7310,13 +7308,14 @@ Generated by [AVA](https://avajs.dev).
73107308
yy = this.y␊
73117309
};␊
73127310
␊
7313-
const { augmentThis, classThis } = foo;␊
7311+
const { augmentThis, classThis, y } = foo;␊
73147312
␊
73157313
const obj = {};␊
73167314
augmentThis.call(obj);␊
73177315
␊
73187316
t.is(obj.x, 'x');␊
7319-
t.is(commonjsGlobal.y, 'y');␊
7317+
t.is(main.y, undefined);␊
7318+
t.is(y, 'y');␊
73207319
␊
73217320
const instance = new classThis();␊
73227321
␊
Binary file not shown.

0 commit comments

Comments
Β (0)