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 个文件使用静态 require,只有 1 个文件使用动态 require。
我的 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 可以解析的目录 dependencies.You 同样可以探索 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']);
无法使用 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 个文件使用静态 require,只有 1 个文件使用动态 require。
我的 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 可以解析的目录 dependencies.You 同样可以探索 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']);