vue.js - 解決vue-cli打包後自動壓縮代碼

當咱們用vue腳手架作完項目後,npm run build打包以後,css

有沒有查看源碼,全是壓縮好的。可是我就不想讓它壓縮,該怎麼辦呢?html

困惑了幾天,查了各類資料。終於終於...vue

來,上乾貨:webpack

首先,咱們得了解一點點webpack的知識。git

webpack中壓縮js 的插件叫 uglifyjs-webpack-plugin,github

壓縮css 的插件叫 optimize-css-assets-webpack-pluginweb

而後咱們找到/build/webpack.prod.conf.js 文件,npm

而後你會發現:ui

const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

而後咱們就能夠在頁面中搜索OptimizeCSSPlugin 和 UglifyJsPlugin 這兩個關鍵詞所在的地方spa

配置以下:

    // css 壓縮代碼,將下面代碼註釋掉
    new OptimizeCSSPlugin({
      cssProcessorOptions: config.build.productionSourceMap
        ? { safe: true, map: { inline: false } }
        : { safe: true }
    }),
   // 壓縮js代碼,將下面代碼註釋掉
    new UglifyJsPlugin({
      uglifyOptions: {
        compress: {
          warnings: false
        }
      },
      sourceMap: config.build.productionSourceMap,
      parallel: true
    }),

只要將上面代碼註釋掉,npm run build 你就會發現,歐了。

而後是html 了,

配置以下:

new HtmlWebpackPlugin({
      filename: process.env.NODE_ENV === 'testing'
        ? 'index.html'
        : config.build.index,
      template: 'index.html',
      inject: true,
      minify: {
        removeComments: true,
        collapseWhitespace: true,
        removeAttributeQuotes: true
        // more options:
        // https://github.com/kangax/html-minifier#options-quick-reference
      },

這裏咱們將上面的 minify 改爲 minify:false

是的,就能夠了。

相關文章
相關標籤/搜索