webpack打包,node_modules 包就一个,结果打包完一堆?
好几个页面都是import 了插件,使用了他的组件。每多一个页面用,就多一份。
这是我webpack配置有问题吗?
// webpack.config.js
const resolve = require('path').resolve
const webpack = require('webpack')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const UglifyjsPlugin = require('uglifyjs-webpack-plugin')
const url = require('url')
const publicPath = '/project/'
module.exports = (options = {}) => ({
entry: {
vendor: './src/vendor',
index: './src/main.js'
},
output: {
path: resolve(__dirname, 'dist'),
filename: options.dev ? '[name].js' : '[name].js?[chunkhash]',
chunkFilename: '[id].js?[chunkhash]',
publicPath: options.dev ? '/assets/' : publicPath
// publicPath:publicPath
// publicPath: options.dev ? '/project/' : publicPath
},
module: {
rules: [{
test: /\.vue$/,
use: ['vue-loader']
},
{
test: /\.js$/,
use: ['babel-loader'],
exclude: /node_modules/
},
{
test: /\.css$/,
use: ['style-loader', 'css-loader', 'postcss-loader']
},
{
test: /\.scss$/,
use: ["style-loader", "css-loader", "sass-loader"]
},
{
test: /\.(png|jpg|jpeg|gif|eot|ttf|woff|woff2|svg|svgz)(\?.+)?$/,
use: [{
loader: 'url-loader',
options: {
limit: 1,
// name:'/assets/[name].[ext]'
},
}]
}
]
},
plugins: [
new webpack.optimize.CommonsChunkPlugin({
names: ['vendor', 'manifest']
}),
new HtmlWebpackPlugin({
template: 'src/index.html'
}),
new UglifyjsPlugin()
],
resolve: {
alias: {
'@': resolve(__dirname, 'src')
},
extensions: ['.js', '.vue', '.json', '.css']
},
devServer: {
// host: '172.16.31.118',
host: '127.0.0.1',
open: true,
port: 8011,
historyApiFallback: {
index: url.parse(options.dev ? '/assets/' : publicPath).pathname
}
},
// devtool: options.dev ? '#eval-source-map' : '#source-map'
})
CommonsChunkPlugin
插件没有配置好,可以参考这个看看参考链接