Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(commonjs): replace top-level this with exports name #1618

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion packages/commonjs/src/transform-commonjs.js
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 @@ -364,7 +365,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 @@ -439,6 +440,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
@@ -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
Expand Up @@ -7262,8 +7262,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 : {};␊
var main = {};␊
var foo = {};␊
Expand All @@ -7272,7 +7270,7 @@ Generated by [AVA](https://avajs.dev).
this.x = 'x';␊
};␊
commonjsGlobal.y = 'y';␊
foo.y = 'y';␊
foo.classThis = class classThis {␊
constructor(){␊
Expand All @@ -7286,13 +7284,14 @@ Generated by [AVA](https://avajs.dev).
yy = this.y␊
};␊
const { augmentThis, classThis } = foo;␊
const { augmentThis, classThis, y } = foo;␊
const obj = {};␊
augmentThis.call(obj);␊
t.is(obj.x, 'x');␊
t.is(commonjsGlobal.y, 'y');␊
t.is(main.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.