简体   繁体   English

__non_webpack_require__ 未定义

[英]__non_webpack_require__ is not defined

I am new to webpack and node, and I am wondering how to use the我是 webpack 和 node 的新手,我想知道如何使用

__non_webpack_require__ 

function.功能。 I've visited webpack's website but am still confused as to what this function is and how I can use it.我访问了webpack 的网站,但仍然对这个功能是什么以及如何使用它感到困惑。 Could you provide a short description of a use case for this function and then how to use it in a node / react app?您能否提供此功能用例的简短描述,然后是如何在节点/反应应用程序中使用它?

Webpack processes every module that you use in your application starting from the entry point(s) and including every module you import ( import or require ) and includes it in your bundle. Webpack 从入口点开始处理您在应用程序中使用的每个模块,包括您导入的每个模块( importrequire )并将其包含在您的包中。 The __non_webpack_require__ is a function that will result in a plain require call. __non_webpack_require__是一个函数,它将导致一个简单的require调用。

Let's take this entry point as an example:我们以这个入口点为例:

const processedByWebpack = require("./module");
const notProcessed = __non_webpack_require__("./non-webpack");

console.log(processedByWebpack);
console.log(notProcessed);

In this case webpack will bundle that application and include every module you import, which in this case is only the ./module.js .在这种情况下,webpack 将捆绑该应用程序并包含您导入的每个模块,在这种情况下只是./module.js So the output will be:所以输出将是:

/******/ ([
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

const processedByWebpack = __webpack_require__(1);
const notProcessed = require("./non-webpack");

console.log(processedByWebpack);
console.log(notProcessed);


/***/ }),
/* 1 */
/***/ (function(module, exports) {

module.exports = "This module is bundled with webpack"


/***/ })
/******/ ]);

The ./module.js module was included in the bundle and would also have been processed by any loaders if there were any applicable rules present. ./module.js模块包含在包中,如果存在任何适用的规则,它也会被任何加载器处理。 On the other hand, the ./non-webpack.js is not included in the bundle and webpack made it a call to require .另一方面, ./non-webpack.js不包含在包中,webpack 调用了require This means that the ./non-webpack.js will be resolved when it's executed and it will fail with a runtime error if it isn't available or contains invalid JavaScript.这意味着./non-webpack.js将在执行时被解析,如果它不可用或包含无效的 JavaScript,它将失败并显示运行时错误。

__non_webpack_require__ is a way to work around the fact that webpack processes all require calls. __non_webpack_require__是一种解决 webpack 进程都require调用这一事实的方法。 Because webpack bundles up all the modules, it must know which modules to include at compile time.因为 webpack 捆绑了所有模块,所以它必须在编译时知道要包含哪些模块。 This makes require more restrictive than it actually is in Node.js.这使得require比实际在 Node.js 中更具限制性。 For instance, you can't use dynamic require s, that means you can't use a variable as the module's path (see also webpack dynamic module loader by require ).例如,您不能使用动态require ,这意味着您不能使用变量作为模块的路径(另请参阅require 的 webpack 动态模块加载器)。 For example:例如:

// Path to module as a variable (could be an argument to a function)
const modulePath = "./module";

const processedByWebpack = require(modulePath); // Fails
const notProcessed = __non_webpack_require__(modulePath);

In the regular require webpack will fail, because it doesn't know which modules to include to cover all the modules that could be referenced at runtime.在常规的require webpack 中会失败,因为它不知道要包含哪些模块来覆盖所有可以在运行时引用的模块。 In this example it might seem obvious, but it could go as far as using user input to determine the module to load.在这个例子中,它可能看起来很明显,但它可以使用用户输入来确定要加载的模块。 With __non_webpack_require__ it simply creates a require call and you'll have to deal with possible Module not found exceptions at runtime.使用__non_webpack_require__它只是创建一个require调用,您必须在运行时处理可能的Module not found异常。

When should you use it?你应该什么时候使用它?

Probably never .可能永远不会 That's one of these functions that should be considered as last resort where you need to sidestep webpack to have some dynamic module resolution.这是这些功能之一,应该被视为最后的手段,您需要避开 webpack 以获得一些动态模块解析。 In most situations there are other solutions to achieve the same goal (eg deferring imports to runtime by using Externals ), everything else is an edge case.在大多数情况下,还有其他解决方案可以实现相同的目标(例如,通过使用Externals将导入推迟到运行时),其他一切都是边缘情况。

You will have noticed that __non_webpack_require__ is transformed into a require call.你会注意到__non_webpack_require__被转换成一个require调用。 This means that it only works in Node.js and fails in any browser environment unless you have a global require defined that may or may not do something special.这意味着它只能在 Node.js 中工作并且在任何浏览器环境中都失败,除非您定义了一个可能会或可能不会做一些特殊事情的全局require Another downside is that it is webpack specific and when you want to use another tool (for instance for testing), it won't work or you'll have a hard time trying to work around it.另一个缺点是它是特定于 webpack 的,当您想使用其他工具(例如用于测试)时,它不起作用,或者您将很难尝试解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM