webpack中的动态需求无法正常工作

时间:2017-02-10 09:47:48

标签: javascript webpack browserify ace-editor

无法使用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'
};

2 个答案:

答案 0 :(得分:2)

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

只需要提一下webpack可以解决这些依赖关系的目录。你也可以探索webpack的ContextReplacementPlugin。

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

模块名称是动态的

答案 1 :(得分:0)

通过将注释行替换为下一个被选择为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']);