var webpack = require('webpack'),
path = require('path'),
CommonsChunkPlugin = require("webpack/lib/optimize/CommonsChunkPlugin");
var chunks = {
"index": './src/js/index.js'
};
module.exports = {
entry: chunks,
output: {
path: './dist/js/',
filename: '[name].js'
},
module: {
loaders: [
{
test: /\.js[x]?$/,
loaders: ['babel-loader?presets[]=es2015'],
exclude: /node_modules/,
include: path.join(__dirname, '.')
}
]
},
// resolve: {
// alias: {
// js: path.join(__dirname, "./src/js")
// },
// extensions: ['', '.js', '.json']
// },
plugins: [
// new webpack.optimize.UglifyJsPlugin(),
new CommonsChunkPlugin({
name: "home.min",
minChunks: 2,
chunks: chunks
}),
]
};
//index.js
var Hello = require("./lib/lib.js");
new Hello();
//lib.js
var Hello = require("./lib/lib.js");
new Hello();
高洛峰2017-04-17 15:00:33
new CommonsChunkPlugin({
name: "home.min",
minChunks: 2,
chunks: chunks
}),
minChunks is set to 2, which means that the js module referenced by at least two chunks will be placed in your home.min.js. Your lib.js is only referenced once by index.js, so lib. The content of js will be placed in the packaged and generated index.js