使用webpack的DllPlugin分别打包了两个插件,DllReferencePlugin单独用没有问题,两个一起用后面一个插件的方法就会找不到。
项目目录
-build
-example
|--example.html
|--example.js
-dll
|--dll1.config.js
|--manifest_dll1.json
|--dll2.config.js
|--manifest_dll2.json
-thirdparty
|--jquery.cookie.min.js
-node_modules
-webpack.config.js
dll1.config.js
var webpack = require('webpack');
var path = require('path');
var glob = require('glob');
var entries = ['jquery',"jquery-ui"];
var jqueryuis = [];
glob.sync("../node_modules/jquery-ui/ui/widgets/*.js").forEach(function (entry) {
jqueryuis.push("jquery-ui/ui/widgets/"+path.basename(entry));
});
entries = entries.concat(jqueryuis);
module.exports = {
entry:{
"dll1":entries
},
output: {
path: 'build',
filename: '[name]-min.js',
library: '[name]',
},
plugins: [
new webpack.ProvidePlugin({
"$": 'jquery',
"jQuery": 'jquery',
}),
new webpack.DllPlugin({
path: path.resolve( __dirname,'./manifest_[name].json'),
name: '[name]',
context: path.resolve(__dirname,"../"),
})
]
};
dll2.config.js
var webpack = require('webpack');
var path = require('path');
var entries = [
path.resolve( __dirname,'../thirdparty/jquery.cookie.min.js'),
];
module.exports = {
entry:{
"dll2":entries
},
output: {
path: 'build',
filename: '[name]-min.js',
library: '[name]',
},
plugins: [
new webpack.ProvidePlugin({
"$": 'jquery',
"jQuery": 'jquery'
}),
new webpack.DllPlugin({
path: path.resolve( __dirname,'./manifest_[name].json'),
name: '[name]',
context: path.resolve(__dirname,"../"),
})
]
};
webpack.config.js
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./dll/manifest_dll1.json'),
}),
new webpack.DllReferencePlugin({
context: __dirname,
manifest: require('./dll/manifest_dll2.json')
}),
example.html
<script src="./dll/dll1-min.js"></script>
<script src="./dll/dll2-min.js"></script>
example.js
require("../thirdparty/jquery.cookie.min.js");
$.cookie("dll","dllall");
报如下错误:
我发现require("../thirdparty/jquery.cookie.min.js");返回的是一个空的对象。
遇到了类似的问题。发现jQuery和zepto在使用ProvidePlugin和dllplugin时在dll中和config中都写,可以解决。