diff --git a/src/loaders/relocate-loader.js b/src/loaders/relocate-loader.js index 02002f9d..34c00609 100644 --- a/src/loaders/relocate-loader.js +++ b/src/loaders/relocate-loader.js @@ -583,6 +583,7 @@ 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' && @@ -590,6 +591,12 @@ module.exports = function (code) { 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; } diff --git a/test/unit/main-equal/input.js b/test/unit/main-equal/input.js new file mode 100644 index 00000000..680b0a9a --- /dev/null +++ b/test/unit/main-equal/input.js @@ -0,0 +1 @@ +console.log(require.main === module); \ No newline at end of file diff --git a/test/unit/main-equal/output-coverage.js b/test/unit/main-equal/output-coverage.js new file mode 100644 index 00000000..c680db06 --- /dev/null +++ b/test/unit/main-equal/output-coverage.js @@ -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; +/******/ }; +/******/ }(); +/******/ +/******/ } +); \ No newline at end of file diff --git a/test/unit/main-equal/output.js b/test/unit/main-equal/output.js new file mode 100644 index 00000000..c680db06 --- /dev/null +++ b/test/unit/main-equal/output.js @@ -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; +/******/ }; +/******/ }(); +/******/ +/******/ } +); \ No newline at end of file