webpack打包出来好多重复的node包?

image.png
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'
})
阅读 3.4k
2 个回答

CommonsChunkPlugin 插件没有配置好,可以参考这个看看参考链接

你可以在main.js引入一次作为全局的组件,而不是在每个使用到的地方都引入。
你的图中出现了三次,应该是你在三个页面分别都import了

推荐问题
宣传栏