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

Ensure require.main === module behaviour #229

Merged
merged 1 commit into from
Jan 18, 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
7 changes: 7 additions & 0 deletions src/loaders/relocate-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,13 +583,20 @@ module.exports = function (code) {
}

// require.main -> __non_webpack_require__.main
// (unless it is a require.main === module check)
else if (!isESM && node.type === 'MemberExpression' &&
node.object.type === 'Identifier' &&
node.object.name === 'require' &&
!shadowDepths.require &&
node.property.type === 'Identifier' &&
node.property.name === 'main' &&
!node.computed) {
if (parent && parent.type === 'BinaryExpression' && (parent.operator === '==' || parent.operator === '===')) {
let other;
other = parent.right === node ? parent.left : parent.right;
if (other.type === 'Identifier' && other.name === 'module')
return;
}
magicString.overwrite(node.object.start, node.object.end, '__non_webpack_require__');
transformed = true;
}
Expand Down
1 change: 1 addition & 0 deletions test/unit/main-equal/input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log(require.main === module);
74 changes: 74 additions & 0 deletions test/unit/main-equal/output-coverage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module.exports =
/******/ (function(modules, runtime) { // webpackBootstrap
/******/ "use strict";
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // initialize runtime
/******/ runtime(__webpack_require__);
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 336);
/******/ })
/************************************************************************/
/******/ ({

/***/ 336:
/***/ (function(module, __unusedexports, __webpack_require__) {

/* module decorator */ module = __webpack_require__.nmd(module);
console.log(__webpack_require__.c[__webpack_require__.s] === module);

/***/ })

/******/ },
/******/ function(__webpack_require__) { // webpackRuntimeModules
/******/ "use strict";
/******/
/******/ /* webpack/runtime/node module decorator */
/******/ !function() {
/******/ __webpack_require__.nmd = function(module) {
/******/ module.paths = [];
/******/ if (!module.children) module.children = [];
/******/ Object.defineProperty(module, 'loaded', {
/******/ enumerable: true,
/******/ get: function() { return module.l; }
/******/ });
/******/ Object.defineProperty(module, 'id', {
/******/ enumerable: true,
/******/ get: function() { return module.i; }
/******/ });
/******/ return module;
/******/ };
/******/ }();
/******/
/******/ }
);
74 changes: 74 additions & 0 deletions test/unit/main-equal/output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
module.exports =
/******/ (function(modules, runtime) { // webpackBootstrap
/******/ "use strict";
/******/ // The module cache
/******/ var installedModules = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/
/******/ // Check if module is in cache
/******/ if(installedModules[moduleId]) {
/******/ return installedModules[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = installedModules[moduleId] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = true;
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/******/
/******/ // expose the module cache
/******/ __webpack_require__.c = installedModules;
/******/
/******/ // initialize runtime
/******/ runtime(__webpack_require__);
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 336);
/******/ })
/************************************************************************/
/******/ ({

/***/ 336:
/***/ (function(module, __unusedexports, __webpack_require__) {

/* module decorator */ module = __webpack_require__.nmd(module);
console.log(__webpack_require__.c[__webpack_require__.s] === module);

/***/ })

/******/ },
/******/ function(__webpack_require__) { // webpackRuntimeModules
/******/ "use strict";
/******/
/******/ /* webpack/runtime/node module decorator */
/******/ !function() {
/******/ __webpack_require__.nmd = function(module) {
/******/ module.paths = [];
/******/ if (!module.children) module.children = [];
/******/ Object.defineProperty(module, 'loaded', {
/******/ enumerable: true,
/******/ get: function() { return module.l; }
/******/ });
/******/ Object.defineProperty(module, 'id', {
/******/ enumerable: true,
/******/ get: function() { return module.i; }
/******/ });
/******/ return module;
/******/ };
/******/ }();
/******/
/******/ }
);