Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webpack 网页包动态要求_Webpack - Fatal编程技术网

Webpack 网页包动态要求

Webpack 网页包动态要求,webpack,Webpack,我有一些使用这个约定命名的模块,framework-{function},我动态地需要它们 init(name, options) { let plugin = require(`framework-${name}`)(this, options); this.registerPlugin(name, plugin); } Webpack在Webpack配置中使用开发模式时生成此代码。它抱怨找不到模块框架数据库,即使它确实存在于我的节点_模块中 /***/ "./node_mo

我有一些使用这个约定命名的模块,
framework-{function}
,我动态地需要它们

init(name, options) {
    let plugin = require(`framework-${name}`)(this, options);
    this.registerPlugin(name, plugin);
}
Webpack在Webpack配置中使用开发模式时生成此代码。它抱怨
找不到模块框架数据库
,即使它确实存在于我的
节点_模块中

/***/ "./node_modules/framework sync recursive ^framework\\-.*$":
/*!*****************************************************!*\
  !*** ./node_modules/framework sync ^framework\-.*$ ***!
  \*****************************************************/
/*! no static exports found */
/***/ (function(module, exports) {

    eval("function webpackEmptyContext(req) {\n\tvar e = new Error('Cannot find module \"' + req + '\".');\n\te.code = 'MODULE_NOT_FOUND';\n\tthrow e;\n}\nwebpackEmptyContext.keys = function() { return []; };\nwebpackEmptyContext.resolve = webpackEmptyContext;\nmodule.exports = webpackEmptyContext;\nwebpackEmptyContext.id = \"./node_modules/framework sync recursive ^framework\\\\-.*$\";\n\n//# sourceURL=webpack:///./node_modules/framework_sync_^framework\\-.*$?");

/***/ }),
如果我硬编码

init(name, options) {
    let plugin = require(`framework-db`)(this, options);
    this.registerPlugin(name, plugin);
}
它起作用了


您知道如何解决此问题吗?

是否可能在webpack生成时不知道您的
名称
变量

如果是这样,您可以尝试在模块路径中插入字符串文字,以便webpack创建上下文模块,例如:

let plugin=require('node_modules/framework-'+name)(此选项)

在“带表达式的要求”一节中有记录:

请小心,尝试使用此字符串文字中最具体的路径,因为,如文档中所指定:

这意味着支持动态需求,但会导致捆绑包中包含所有可能的模块

因此,除非您找到任何解决方法,否则将
node\u modules
设置为该上下文模块的目录可能不是一个好主意