Skip to content

Commit

Permalink
ensure that any __non_webpack_require__ is retained in subsequent bui…
Browse files Browse the repository at this point in the history
…lds (#271)
  • Loading branch information
guybedford authored and rauchg committed Feb 4, 2019
1 parent 60b5729 commit 82336fa
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/loaders/relocate-loader.js
Expand Up @@ -35,7 +35,7 @@ function isExpressionReference(node, parent) {
return true;
}

const relocateRegEx = /_\_dirname|_\_filename|require\.main|node-pre-gyp|bindings|define|require\(\s*[^'"]/;
const relocateRegEx = /_\_dirname|_\_filename|require\.main|node-pre-gyp|bindings|define|require\(\s*[^'"]|__non_webpack_require__/;

module.exports = function (code) {
if (this.cacheable)
Expand Down Expand Up @@ -300,6 +300,12 @@ module.exports = function (code) {
}
}
}
// __non_webpack_require__ -> eval('require')
else if (node.name === '__non_webpack_require__' && parent.type !== 'UnaryExpression') {
magicString.overwrite(node.start, node.end, 'eval("require")');
transformed = true;
return this.skip();
}
}
// require('bindings')('asdf')
else if (node.type === 'CallExpression' && !isESM &&
Expand Down Expand Up @@ -363,17 +369,19 @@ module.exports = function (code) {
else {
magicString.overwrite(parent.start, parent.end, "false");
transformed = true;
return;
return this.skip();
}
}
}
magicString.overwrite(node.object.start, node.object.end, '__non_webpack_require__');
transformed = true;
return this.skip();
}
else if (!isESM && node.type === 'Property' && node.value.type === 'Identifier' &&
node.value.name === 'require' && knownBindings.require.shadowDepth === 0) {
magicString.overwrite(node.value.start, node.value.end, '__non_webpack_require__');
transformed = true;
return this.skip();
}
else if (node.type === 'VariableDeclaration') {
for (const decl of node.declarations) {
Expand Down
2 changes: 2 additions & 0 deletions test/unit/require-check/input.js
@@ -0,0 +1,2 @@
const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
const foo = requireFunc(moduleName);
54 changes: 54 additions & 0 deletions test/unit/require-check/output-coverage.js
@@ -0,0 +1,54 @@
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;
/******/ }
/******/
/******/
/******/
/******/ // the startup function
/******/ function startup() {
/******/ // Load entry module and return exports
/******/ return __webpack_require__(155);
/******/ };
/******/
/******/ // run startup
/******/ return startup();
/******/ })
/************************************************************************/
/******/ ({

/***/ 155:
/***/ (function() {

const requireFunc = true ? eval("require") : undefined;
const foo = requireFunc(moduleName);


/***/ })

/******/ });
54 changes: 54 additions & 0 deletions test/unit/require-check/output.js
@@ -0,0 +1,54 @@
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;
/******/ }
/******/
/******/
/******/
/******/ // the startup function
/******/ function startup() {
/******/ // Load entry module and return exports
/******/ return __webpack_require__(805);
/******/ };
/******/
/******/ // run startup
/******/ return startup();
/******/ })
/************************************************************************/
/******/ ({

/***/ 805:
/***/ (function() {

const requireFunc = true ? eval("require") : undefined;
const foo = requireFunc(moduleName);


/***/ })

/******/ });

0 comments on commit 82336fa

Please sign in to comment.