繁体   English   中英

webpack 动态模块加载器按要求

[英]webpack dynamic module loader by require

好的,我已经搜索了高低,但无法可靠地确定 webpack 是否可行。

https://github.com/webpack/webpack/tree/master/examples/require.context似乎表明可以将字符串传递给函数并加载模块...

但我的尝试不起作用:webpack.config.js

'use strict';
let webpack     = require('webpack'),
    jsonLoader  = require("json-loader"),
    path        = require("path"),
    fs          = require('fs'),
    nodeModules = {};

fs.readdirSync('node_modules')
    .filter(function(x) {
        return ['.bin'].indexOf(x) === -1;
    })
    .forEach(function(mod) {
        nodeModules[mod] = 'commonjs ' + mod;
    });


let PATHS = {
    app: __dirname + '/src'
};

module.exports = {
    context: PATHS.app,
    entry: {
        app: PATHS.app+'/server.js'
    },
    target: 'node',
    output: {
        path: PATHS.app,
        filename: '../build/server.js'
    },
    externals: nodeModules,
    performance: {
        hints: "warning"
    },
    plugins: [
        jsonLoader
    ],
    resolve: {
        modules: [
            './node_modules',
            path.resolve(__dirname),
            path.resolve(__dirname + "/src"),
            path.resolve('./config')
        ]
    },
    node: {
        fs: "empty"
    }
};

服务器.js

let _ = require('lodash');
let modules = [ "modules/test" ];

require( 'modules/test' )();

_.map( modules, function( module ){
    require( module );
});

modules/ 中的模块名为 test.js

module.exports = () => {
    console.log('hello world');
};

但结果总是一样的...... pm2 日志只是说 hello world 静态需求......但对于同一模块的动态加载

错误:找不到模块“。”

我想要做的就是循环遍历模块的路径数组,然后加载...

您不能使用变量作为require参数。 Webpack 需要知道在编译时捆绑哪些文件。 由于它不进行程序流分析,因此无法知道您传递给函数的内容。 在那种情况下它可能很明显,但是这可能会使用用户输入来决定需要哪个模块,并且 webpack 不可能知道在编译时包含哪些模块,因此 webpack 不允许这样做。

您发布的示例有点不同。 您可以将require与连接字符串一起使用。 例如:

require(`./src/${moduleName}/test`);

webpack 需要在 bundle 中包含哪些模块? 变量moduleName可以是任何东西,因此在编译时不知道确切的模块。 相反,它包括可能与上述表达式匹配的所有模块。 假设目录结构如下:

src
├─ one
│   └─ test.js
├─ two
│   ├─ subdir
│   │   └─ test.js
│   └─ test.js
└─ three
    └─ test.js

所有这些test.js文件都将包含在包中,因为moduleName可以是one或嵌套的东西,例如two/subdir

更多详细信息请参见官方文档中的require 和表达式

您不能循环遍历数组并导入数组的每个模块,上述例外情况是通过连接字符串,但这会导致包含所有可能的模块,通常应该避免。

我在电子环境中遇到了这个问题。 我的用例能够在像应用程序这样的 IDE 中require动态创建的文件。 我想使用电子require ,它基本上是一个 NodeJS 通用模块加载器。 经过一番反复之后,我找到了一个使用 webpack 的noParse模块配置的解决方案。

首先创建一个将被 webpack 解析器忽略的模块:

// file: native-require.js
// webpack replaces calls to `require()` from within a bundle. This module
// is not parsed by webpack and exports the real `require`
// NOTE: since the module is unparsed, do not use es6 exports
module.exports = require

在我的 webpack 配置中,在module下,指示 bundler 不要解析这个模块:

{
  module: {
    noParse: /\/native-require.js$/,
  }
}

最后,在您想要访问原始需求的任何包中:

import nativeRequire from './native-require'
const someModule = nativeRequire('/some/module.js') // dynamic imports

有点晚了......但是......因为你捆绑到target: 'node' ,有一个动态需要模块的解决方法,并绕过“包括所有可能模块的影响”

解决方案来自:

在不解析或捆绑目标模块的情况下对节点目标使用动态 require · 问题 #4175 · webpack/webpack

引自那条评论:

const requireFunc = typeof __webpack_require__ === "function" ? __non_webpack_require__ : require;
const foo = requireFunc(moduleName);

捆绑到:

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

暂无
暂无

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

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