vue-cli 打包优化

1,069 阅读1分钟

添加icon

参考文档

1. 在项目根目录(index.html同级目录)下添加favicon.ico文件

2. 在项目index.html中引入图标

<link rel="shortcut icon" type="image/x-icon" href="./favicon.ico" />

3. 配置webpack配置文件(build文件夹下面)

在 webpack.dev.conf.js 和 webpack.prod.config.js 中添加如下代码

favicon: path.resolve('./favicon.ico')

具体添加位置如下: webpack.dev.conf.js

new HtmlWebpackPlugin({
      filename: 'index.html',
      template: 'index.html',
      favicon: path.resolve('./favicon.ico'),
      inject: true
    }),

webpack.prod.config.js

new HtmlWebpackPlugin({
      filename: process.env.NODE_ENV === 'testing'
        ? 'index.html'
        : config.build.index,
      template: 'index.html',
      favicon: path.resolve('./favicon.ico'),
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      },
      // necessary to consistently work with multiple chunks via CommonsChunkPlugin
      chunksSortMode: 'dependency'
    }),

大功告成,运行npm run dev重启项目查看

build打包删除 debugger 和 console.log

在 UglifyJsPlugin 插件下添加下列代码 如图,找到build/webpack.prod.conf.js

 drop_debugger: true, 
 drop_console: true