Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const path = require('path');
const ID_MAIN = path.join(__dirname, 'main.js');

module.exports = {
description: 'top-level `this` in commonjs module is not affected by the context option',
options: {
input: [ID_MAIN],
context: 'window',
output: {
chunkFileNames: '[name].js'
},
plugins: []
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.augmentThis = function augmentThis() {
this.x = 'x';
};

this.y = 'y';

const window = {};
// Top level `this` in commonjs module is not affected by the context option
t.is(this === window, false);

t.is(this, module.exports);
t.is(module.exports.y, 'y');
t.is(this.augmentThis, module.exports.augmentThis);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { augmentThis, y } from './foo';

const obj = {};
augmentThis.call(obj);
t.is(obj.x, 'x');

t.is(y, 'y');


// Top level `this` will be replaced by the context option
const window = {};
t.is(this, window);
t.is(this.y, undefined);
50 changes: 50 additions & 0 deletions packages/commonjs/test/snapshots/function.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -9601,6 +9601,56 @@ Generated by [AVA](https://avajs.dev).
`,
}

## toplevel-this-wont-affected-by-option-context

> Snapshot 1

{
'main.js': `'use strict';␊
var foo$1 = {exports: {}};␊
var foo = foo$1.exports;␊
var hasRequiredFoo;␊
function requireFoo () {␊
if (hasRequiredFoo) return foo$1.exports;␊
hasRequiredFoo = 1;␊
(function (module, exports) {␊
exports.augmentThis = function augmentThis() {␊
this.x = 'x';␊
};␊
foo.y = 'y';␊
const window = {};␊
// Top level \`this\` in commonjs module is not affected by the context option␊
t.is(foo === window, false);␊
t.is(foo, module.exports);␊
t.is(module.exports.y, 'y');␊
t.is(foo.augmentThis, module.exports.augmentThis); ␊
} (foo$1, foo$1.exports));␊
return foo$1.exports;␊
}␊
var fooExports = requireFoo();␊
const obj = {};␊
fooExports.augmentThis.call(obj);␊
t.is(obj.x, 'x');␊
t.is(fooExports.y, 'y');␊
// Top level \`this\` will be replaced by the context option␊
const window = {};␊
t.is(window, window);␊
t.is(window.y, undefined);␊
`,
}

## trailing-slash

> Snapshot 1
Expand Down
Binary file modified packages/commonjs/test/snapshots/function.js.snap
Binary file not shown.