Skip to content

Commit

Permalink
ensure require.main === module behaviour (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
guybedford authored and rauchg committed Jan 18, 2019
1 parent 216dc00 commit 91fa4d4
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 0 deletions.
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;
/******/ };
/******/ }();
/******/
/******/ }
);

0 comments on commit 91fa4d4

Please sign in to comment.