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

Protect all module globals for CJS output from being redefined #2889

Merged
merged 1 commit into from
Jun 1, 2019
Merged
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
10 changes: 6 additions & 4 deletions src/Chunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1058,10 +1058,12 @@ export default class Chunk {
if (options.format !== 'es') {
usedNames.add('exports');
if (options.format === 'cjs') {
usedNames.add(INTEROP_DEFAULT_VARIABLE);
if (this.exportMode === 'default') {
usedNames.add('module');
}
usedNames
.add(INTEROP_DEFAULT_VARIABLE)
.add('require')
.add('module')
.add('__filename')
.add('__dirname');
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/form/samples/protect-cjs-globals/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
description: 'prevent conflicts with cjs module globals',
options: {
output: { name: 'bundle' }
}
};
17 changes: 17 additions & 0 deletions test/form/samples/protect-cjs-globals/_expected/amd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
define(['exports'], function (exports) { 'use strict';

const exports$1 = 1;
const require = 2;
const module = 3;
const __filename = 4;
const __dirname = 5;

exports.__dirname = __dirname;
exports.__filename = __filename;
exports.exports = exports$1;
exports.module = module;
exports.require = require;

Object.defineProperty(exports, '__esModule', { value: true });

});
15 changes: 15 additions & 0 deletions test/form/samples/protect-cjs-globals/_expected/cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

Object.defineProperty(exports, '__esModule', { value: true });

const exports$1 = 1;
const require$1 = 2;
const module$1 = 3;
const __filename$1 = 4;
const __dirname$1 = 5;

exports.__dirname = __dirname$1;
exports.__filename = __filename$1;
exports.exports = exports$1;
exports.module = module$1;
exports.require = require$1;
7 changes: 7 additions & 0 deletions test/form/samples/protect-cjs-globals/_expected/es.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const exports = 1;
const require = 2;
const module = 3;
const __filename = 4;
const __dirname = 5;

export { __dirname, __filename, exports, module, require };
18 changes: 18 additions & 0 deletions test/form/samples/protect-cjs-globals/_expected/iife.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var bundle = (function (exports) {
'use strict';

const exports$1 = 1;
const require = 2;
const module = 3;
const __filename = 4;
const __dirname = 5;

exports.__dirname = __dirname;
exports.__filename = __filename;
exports.exports = exports$1;
exports.module = module;
exports.require = require;

return exports;

}({}));
14 changes: 14 additions & 0 deletions test/form/samples/protect-cjs-globals/_expected/system.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
System.register('bundle', [], function (exports) {
'use strict';
return {
execute: function () {

const exports$1 = exports('exports', 1);
const require = exports('require', 2);
const module = exports('module', 3);
const __filename = exports('__filename', 4);
const __dirname = exports('__dirname', 5);

}
};
});
21 changes: 21 additions & 0 deletions test/form/samples/protect-cjs-globals/_expected/umd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
typeof define === 'function' && define.amd ? define(['exports'], factory) :
(global = global || self, factory(global.bundle = {}));
}(this, function (exports) { 'use strict';

const exports$1 = 1;
const require = 2;
const module = 3;
const __filename = 4;
const __dirname = 5;

exports.__dirname = __dirname;
exports.__filename = __filename;
exports.exports = exports$1;
exports.module = module;
exports.require = require;

Object.defineProperty(exports, '__esModule', { value: true });

}));
5 changes: 5 additions & 0 deletions test/form/samples/protect-cjs-globals/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const exports = 1;
export const require = 2;
export const module = 3;
export const __filename = 4;
export const __dirname = 5;
6 changes: 3 additions & 3 deletions test/form/samples/sequence-expression/_expected/cjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ var e = (foo$1());
var g = ((() => {console.log(foo$1());})(), 1);

// should maintain this context
var module = {};
module.bar = function () { console.log( 'bar' );};
var h = (0, module.bar)();
var module$1 = {};
module$1.bar = function () { console.log( 'bar' );};
var h = (0, module$1.bar)();
3 changes: 3 additions & 0 deletions test/function/samples/protect-cjs-globals/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
description: 'prevent conflicts with cjs module globals'
};
5 changes: 5 additions & 0 deletions test/function/samples/protect-cjs-globals/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const exports = 1;
export const require = 2;
export const module = 3;
export const __filename = 4;
export const __dirname = 5;