diff --git a/packages/commonjs/test/fixtures/function/toplevel-this-wont-affected-by-option-context/_config.js b/packages/commonjs/test/fixtures/function/toplevel-this-wont-affected-by-option-context/_config.js new file mode 100644 index 000000000..ea825fdca --- /dev/null +++ b/packages/commonjs/test/fixtures/function/toplevel-this-wont-affected-by-option-context/_config.js @@ -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: [] + } +}; diff --git a/packages/commonjs/test/fixtures/function/toplevel-this-wont-affected-by-option-context/foo.js b/packages/commonjs/test/fixtures/function/toplevel-this-wont-affected-by-option-context/foo.js new file mode 100644 index 000000000..08d6f0d34 --- /dev/null +++ b/packages/commonjs/test/fixtures/function/toplevel-this-wont-affected-by-option-context/foo.js @@ -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); diff --git a/packages/commonjs/test/fixtures/function/toplevel-this-wont-affected-by-option-context/main.js b/packages/commonjs/test/fixtures/function/toplevel-this-wont-affected-by-option-context/main.js new file mode 100644 index 000000000..9c10fa89b --- /dev/null +++ b/packages/commonjs/test/fixtures/function/toplevel-this-wont-affected-by-option-context/main.js @@ -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); diff --git a/packages/commonjs/test/snapshots/function.js.md b/packages/commonjs/test/snapshots/function.js.md index e36cd7414..a4f6d2723 100644 --- a/packages/commonjs/test/snapshots/function.js.md +++ b/packages/commonjs/test/snapshots/function.js.md @@ -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 diff --git a/packages/commonjs/test/snapshots/function.js.snap b/packages/commonjs/test/snapshots/function.js.snap index f04456243..0dfe2e671 100644 Binary files a/packages/commonjs/test/snapshots/function.js.snap and b/packages/commonjs/test/snapshots/function.js.snap differ