Skip to content

Commit fd3961b

Browse files
authored
Avoid __webpack_require__ conflicts (#633)
* avoid __webpack_require__ conflicts * do not replace webpack require when bundling webpack
1 parent 26222e3 commit fd3961b

23 files changed

Lines changed: 173 additions & 167 deletions

File tree

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,12 @@ function ncc (
444444
map.mappings = ";" + map.mappings;
445445
}
446446

447+
// __webpack_require__ can conflict with webpack injections in module scopes
448+
// to avoid this without recomputing the source map we replace it with an
449+
// identical length identifier
450+
if (code.indexOf('"__webpack_require__"') === -1)
451+
code = code.replace(/__webpack_require__/g, '__nccwpck_require__');
452+
447453
// for each .js / .mjs / .cjs file in the asset list, build that file with ncc itself
448454
if (!noAssetBuilds) {
449455
const compilation = compilationStack[compilationStack.length - 1];

test/unit/bundle-subasset/output-coverage.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ module.exports =
44
/******/ var __webpack_modules__ = ({
55

66
/***/ 929:
7-
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
7+
/***/ ((__unused_webpack_module, __webpack_exports__, __nccwpck_require__) => {
88

9-
__webpack_require__.r(__webpack_exports__);
10-
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(622);
11-
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_0__);
9+
__nccwpck_require__.r(__webpack_exports__);
10+
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(622);
11+
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(path__WEBPACK_IMPORTED_MODULE_0__);
1212

1313

14-
const file = __webpack_require__.ab + "pi-bridge.js";
14+
const file = __nccwpck_require__.ab + "pi-bridge.js";
1515

1616
const obscureRequire = eval(`function obscureRequire (file) {
1717
require(file);
1818
}`);
1919

20-
console.log(obscureRequire(__webpack_require__.ab + "pi-bridge.js"));
20+
console.log(obscureRequire(__nccwpck_require__.ab + "pi-bridge.js"));
2121

2222

2323
/***/ }),
@@ -35,7 +35,7 @@ module.exports = require("path");
3535
/******/ var __webpack_module_cache__ = {};
3636
/******/
3737
/******/ // The require function
38-
/******/ function __webpack_require__(moduleId) {
38+
/******/ function __nccwpck_require__(moduleId) {
3939
/******/ // Check if module is in cache
4040
/******/ if(__webpack_module_cache__[moduleId]) {
4141
/******/ return __webpack_module_cache__[moduleId].exports;
@@ -50,7 +50,7 @@ module.exports = require("path");
5050
/******/ // Execute the module function
5151
/******/ var threw = true;
5252
/******/ try {
53-
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
53+
/******/ __webpack_modules__[moduleId](module, module.exports, __nccwpck_require__);
5454
/******/ threw = false;
5555
/******/ } finally {
5656
/******/ if(threw) delete __webpack_module_cache__[moduleId];
@@ -64,21 +64,21 @@ module.exports = require("path");
6464
/******/ /* webpack/runtime/compat get default export */
6565
/******/ (() => {
6666
/******/ // getDefaultExport function for compatibility with non-harmony modules
67-
/******/ __webpack_require__.n = (module) => {
67+
/******/ __nccwpck_require__.n = (module) => {
6868
/******/ var getter = module && module.__esModule ?
6969
/******/ () => module['default'] :
7070
/******/ () => module;
71-
/******/ __webpack_require__.d(getter, { a: getter });
71+
/******/ __nccwpck_require__.d(getter, { a: getter });
7272
/******/ return getter;
7373
/******/ };
7474
/******/ })();
7575
/******/
7676
/******/ /* webpack/runtime/define property getters */
7777
/******/ (() => {
7878
/******/ // define getter functions for harmony exports
79-
/******/ __webpack_require__.d = (exports, definition) => {
79+
/******/ __nccwpck_require__.d = (exports, definition) => {
8080
/******/ for(var key in definition) {
81-
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
81+
/******/ if(__nccwpck_require__.o(definition, key) && !__nccwpck_require__.o(exports, key)) {
8282
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
8383
/******/ }
8484
/******/ }
@@ -87,13 +87,13 @@ module.exports = require("path");
8787
/******/
8888
/******/ /* webpack/runtime/hasOwnProperty shorthand */
8989
/******/ (() => {
90-
/******/ __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
90+
/******/ __nccwpck_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
9191
/******/ })();
9292
/******/
9393
/******/ /* webpack/runtime/make namespace object */
9494
/******/ (() => {
9595
/******/ // define __esModule on exports
96-
/******/ __webpack_require__.r = (exports) => {
96+
/******/ __nccwpck_require__.r = (exports) => {
9797
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
9898
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
9999
/******/ }
@@ -103,10 +103,10 @@ module.exports = require("path");
103103
/******/
104104
/******/ /* webpack/runtime/compat */
105105
/******/
106-
/******/ __webpack_require__.ab = __dirname + "/";/************************************************************************/
106+
/******/ __nccwpck_require__.ab = __dirname + "/";/************************************************************************/
107107
/******/ // module exports must be returned from runtime so entry inlining is disabled
108108
/******/ // startup
109109
/******/ // Load entry module and return exports
110-
/******/ return __webpack_require__(929);
110+
/******/ return __nccwpck_require__(929);
111111
/******/ })()
112112
;

test/unit/bundle-subasset/output.js

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@ module.exports =
44
/******/ var __webpack_modules__ = ({
55

66
/***/ 743:
7-
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
7+
/***/ ((__unused_webpack_module, __webpack_exports__, __nccwpck_require__) => {
88

9-
__webpack_require__.r(__webpack_exports__);
10-
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(622);
11-
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(path__WEBPACK_IMPORTED_MODULE_0__);
9+
__nccwpck_require__.r(__webpack_exports__);
10+
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0__ = __nccwpck_require__(622);
11+
/* harmony import */ var path__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__nccwpck_require__.n(path__WEBPACK_IMPORTED_MODULE_0__);
1212

1313

14-
const file = __webpack_require__.ab + "pi-bridge.js";
14+
const file = __nccwpck_require__.ab + "pi-bridge.js";
1515

1616
const obscureRequire = eval(`function obscureRequire (file) {
1717
require(file);
1818
}`);
1919

20-
console.log(obscureRequire(__webpack_require__.ab + "pi-bridge.js"));
20+
console.log(obscureRequire(__nccwpck_require__.ab + "pi-bridge.js"));
2121

2222

2323
/***/ }),
@@ -35,7 +35,7 @@ module.exports = require("path");
3535
/******/ var __webpack_module_cache__ = {};
3636
/******/
3737
/******/ // The require function
38-
/******/ function __webpack_require__(moduleId) {
38+
/******/ function __nccwpck_require__(moduleId) {
3939
/******/ // Check if module is in cache
4040
/******/ if(__webpack_module_cache__[moduleId]) {
4141
/******/ return __webpack_module_cache__[moduleId].exports;
@@ -50,7 +50,7 @@ module.exports = require("path");
5050
/******/ // Execute the module function
5151
/******/ var threw = true;
5252
/******/ try {
53-
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
53+
/******/ __webpack_modules__[moduleId](module, module.exports, __nccwpck_require__);
5454
/******/ threw = false;
5555
/******/ } finally {
5656
/******/ if(threw) delete __webpack_module_cache__[moduleId];
@@ -64,21 +64,21 @@ module.exports = require("path");
6464
/******/ /* webpack/runtime/compat get default export */
6565
/******/ (() => {
6666
/******/ // getDefaultExport function for compatibility with non-harmony modules
67-
/******/ __webpack_require__.n = (module) => {
67+
/******/ __nccwpck_require__.n = (module) => {
6868
/******/ var getter = module && module.__esModule ?
6969
/******/ () => module['default'] :
7070
/******/ () => module;
71-
/******/ __webpack_require__.d(getter, { a: getter });
71+
/******/ __nccwpck_require__.d(getter, { a: getter });
7272
/******/ return getter;
7373
/******/ };
7474
/******/ })();
7575
/******/
7676
/******/ /* webpack/runtime/define property getters */
7777
/******/ (() => {
7878
/******/ // define getter functions for harmony exports
79-
/******/ __webpack_require__.d = (exports, definition) => {
79+
/******/ __nccwpck_require__.d = (exports, definition) => {
8080
/******/ for(var key in definition) {
81-
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
81+
/******/ if(__nccwpck_require__.o(definition, key) && !__nccwpck_require__.o(exports, key)) {
8282
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
8383
/******/ }
8484
/******/ }
@@ -87,13 +87,13 @@ module.exports = require("path");
8787
/******/
8888
/******/ /* webpack/runtime/hasOwnProperty shorthand */
8989
/******/ (() => {
90-
/******/ __webpack_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
90+
/******/ __nccwpck_require__.o = (obj, prop) => Object.prototype.hasOwnProperty.call(obj, prop)
9191
/******/ })();
9292
/******/
9393
/******/ /* webpack/runtime/make namespace object */
9494
/******/ (() => {
9595
/******/ // define __esModule on exports
96-
/******/ __webpack_require__.r = (exports) => {
96+
/******/ __nccwpck_require__.r = (exports) => {
9797
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
9898
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
9999
/******/ }
@@ -103,10 +103,10 @@ module.exports = require("path");
103103
/******/
104104
/******/ /* webpack/runtime/compat */
105105
/******/
106-
/******/ __webpack_require__.ab = __dirname + "/";/************************************************************************/
106+
/******/ __nccwpck_require__.ab = __dirname + "/";/************************************************************************/
107107
/******/ // module exports must be returned from runtime so entry inlining is disabled
108108
/******/ // startup
109109
/******/ // Load entry module and return exports
110-
/******/ return __webpack_require__(743);
110+
/******/ return __nccwpck_require__(743);
111111
/******/ })()
112112
;

test/unit/bundle-subasset2/output-coverage.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports =
44
/******/ var __webpack_modules__ = ({
55

66
/***/ 448:
7-
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
7+
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
88

99

1010
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
@@ -43,10 +43,10 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
4343
}
4444
};
4545
exports.__esModule = true;
46-
var piscina_1 = __webpack_require__(409);
47-
var path = __webpack_require__(622);
46+
var piscina_1 = __nccwpck_require__(409);
47+
var path = __nccwpck_require__(622);
4848
var piscina = new piscina_1["default"]({
49-
filename: __webpack_require__.ab + "pi-bridge.js"
49+
filename: __nccwpck_require__.ab + "pi-bridge.js"
5050
});
5151
(function () {
5252
return __awaiter(this, void 0, void 0, function () {
@@ -86,7 +86,7 @@ module.exports = require("piscina");
8686
/******/ var __webpack_module_cache__ = {};
8787
/******/
8888
/******/ // The require function
89-
/******/ function __webpack_require__(moduleId) {
89+
/******/ function __nccwpck_require__(moduleId) {
9090
/******/ // Check if module is in cache
9191
/******/ if(__webpack_module_cache__[moduleId]) {
9292
/******/ return __webpack_module_cache__[moduleId].exports;
@@ -101,7 +101,7 @@ module.exports = require("piscina");
101101
/******/ // Execute the module function
102102
/******/ var threw = true;
103103
/******/ try {
104-
/******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
104+
/******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __nccwpck_require__);
105105
/******/ threw = false;
106106
/******/ } finally {
107107
/******/ if(threw) delete __webpack_module_cache__[moduleId];
@@ -114,10 +114,10 @@ module.exports = require("piscina");
114114
/************************************************************************/
115115
/******/ /* webpack/runtime/compat */
116116
/******/
117-
/******/ __webpack_require__.ab = __dirname + "/";/************************************************************************/
117+
/******/ __nccwpck_require__.ab = __dirname + "/";/************************************************************************/
118118
/******/ // module exports must be returned from runtime so entry inlining is disabled
119119
/******/ // startup
120120
/******/ // Load entry module and return exports
121-
/******/ return __webpack_require__(448);
121+
/******/ return __nccwpck_require__(448);
122122
/******/ })()
123123
;

test/unit/bundle-subasset2/output.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module.exports =
44
/******/ var __webpack_modules__ = ({
55

66
/***/ 886:
7-
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {
7+
/***/ (function(__unused_webpack_module, exports, __nccwpck_require__) {
88

99

1010
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
@@ -43,10 +43,10 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
4343
}
4444
};
4545
exports.__esModule = true;
46-
var piscina_1 = __webpack_require__(409);
47-
var path = __webpack_require__(622);
46+
var piscina_1 = __nccwpck_require__(409);
47+
var path = __nccwpck_require__(622);
4848
var piscina = new piscina_1["default"]({
49-
filename: __webpack_require__.ab + "pi-bridge.js"
49+
filename: __nccwpck_require__.ab + "pi-bridge.js"
5050
});
5151
(function () {
5252
return __awaiter(this, void 0, void 0, function () {
@@ -86,7 +86,7 @@ module.exports = require("piscina");
8686
/******/ var __webpack_module_cache__ = {};
8787
/******/
8888
/******/ // The require function
89-
/******/ function __webpack_require__(moduleId) {
89+
/******/ function __nccwpck_require__(moduleId) {
9090
/******/ // Check if module is in cache
9191
/******/ if(__webpack_module_cache__[moduleId]) {
9292
/******/ return __webpack_module_cache__[moduleId].exports;
@@ -101,7 +101,7 @@ module.exports = require("piscina");
101101
/******/ // Execute the module function
102102
/******/ var threw = true;
103103
/******/ try {
104-
/******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __webpack_require__);
104+
/******/ __webpack_modules__[moduleId].call(module.exports, module, module.exports, __nccwpck_require__);
105105
/******/ threw = false;
106106
/******/ } finally {
107107
/******/ if(threw) delete __webpack_module_cache__[moduleId];
@@ -114,10 +114,10 @@ module.exports = require("piscina");
114114
/************************************************************************/
115115
/******/ /* webpack/runtime/compat */
116116
/******/
117-
/******/ __webpack_require__.ab = __dirname + "/";/************************************************************************/
117+
/******/ __nccwpck_require__.ab = __dirname + "/";/************************************************************************/
118118
/******/ // module exports must be returned from runtime so entry inlining is disabled
119119
/******/ // startup
120120
/******/ // Load entry module and return exports
121-
/******/ return __webpack_require__(886);
121+
/******/ return __nccwpck_require__(886);
122122
/******/ })()
123123
;

test/unit/externals/output-coverage.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ module.exports =
33
/******/ var __webpack_modules__ = ({
44

55
/***/ 157:
6-
/***/ ((__unused_webpack_module, __unused_webpack_exports, __webpack_require__) => {
6+
/***/ ((__unused_webpack_module, __unused_webpack_exports, __nccwpck_require__) => {
77

8-
const external = __webpack_require__(306);
8+
const external = __nccwpck_require__(306);
99

1010
console.log(external);
1111

@@ -26,7 +26,7 @@ module.exports = require("externalmapped");
2626
/******/ var __webpack_module_cache__ = {};
2727
/******/
2828
/******/ // The require function
29-
/******/ function __webpack_require__(moduleId) {
29+
/******/ function __nccwpck_require__(moduleId) {
3030
/******/ // Check if module is in cache
3131
/******/ if(__webpack_module_cache__[moduleId]) {
3232
/******/ return __webpack_module_cache__[moduleId].exports;
@@ -41,7 +41,7 @@ module.exports = require("externalmapped");
4141
/******/ // Execute the module function
4242
/******/ var threw = true;
4343
/******/ try {
44-
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
44+
/******/ __webpack_modules__[moduleId](module, module.exports, __nccwpck_require__);
4545
/******/ threw = false;
4646
/******/ } finally {
4747
/******/ if(threw) delete __webpack_module_cache__[moduleId];
@@ -54,10 +54,10 @@ module.exports = require("externalmapped");
5454
/************************************************************************/
5555
/******/ /* webpack/runtime/compat */
5656
/******/
57-
/******/ __webpack_require__.ab = __dirname + "/";/************************************************************************/
57+
/******/ __nccwpck_require__.ab = __dirname + "/";/************************************************************************/
5858
/******/ // module exports must be returned from runtime so entry inlining is disabled
5959
/******/ // startup
6060
/******/ // Load entry module and return exports
61-
/******/ return __webpack_require__(157);
61+
/******/ return __nccwpck_require__(157);
6262
/******/ })()
6363
;

0 commit comments

Comments
 (0)