繁体   English   中英

Webpack中的动态需求不起作用

[英]Dynamic require in webpack not working

无法使用webpack解决动态需求。 遇到错误

WARNING in ./ace/config.js
112:21-40 Critical dependency: the request of a dependency is an expression

WARNING in ./ace/config.js
142:39-46 Critical dependency: require function is used in a way in which
dependencies cannot be statically extracted        

WARNING in ./ace/config.js
124:12-131:14 Critical dependency: the request of a dependency is an
expression 

有5个使用静态需求的文件,只有1个使用动态需求的文件。

我的webpack配置文件是这样的

var webpack = require('webpack');

module.exports = {
 context: __dirname + '/app',
 entry: {
    services: ["./init.js"]
},
output: {
    path: __dirname + '/public/javascript',
    filename: "[name].bundle.js?v=[hash]"
},
module: {
    loaders: [
        { test: /\.json$/, loader: 'json-loader' },
        {
            loader: 'babel-loader',
            query: {
                presets: ['es2015', 'stage-0']
            }
        },
        { test: /\.css$/, loader: "css-loader" }
    ]
},
node: {
    console: true,
    fs: 'empty',
    net: 'empty',
    tls: 'empty'
},
target: 'node'
};

我们需要使用webpack功能解决这些动态需求[ https://webpack.github.io/docs/context.html][1]

只需提及webpack可以解决这些依赖关系的目录。您也可以探索webpack的ContextReplacementPlugin。

   this =>  require([module])
   to => require(['./directory/' + module + '.js'])

模块名称是动态的

通过将注释行替换为下一个将目录选择为node_modules注释行来解决。

// var dns = require('dns');
var directory = './node_modules/';
var dns = require([directory + 'dns' + '.js']);
// var request = require('request');
var request = require([directory + 'request' + '.js']);

暂无
暂无

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

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