vuecli3項目打包優化配置要點

1、新建項目

使用 vue-cli3 構建一個初始的Vue項目css

由於使用了cli3,不少目錄結構不見了,而相關配置是放在vue.config.js裏面的,所以在根目錄,新建一個vue.config.jshtml

module.exports = {}
複製代碼

2、正式優化

一、將 productionSourceMap 設爲 false

(1) 在vue.config.js中module.exports寫入:vue

module.exports = {
    productionSourceMap: false
}
複製代碼

二、圖片壓縮

vue正常打包以後一些圖片文件很大,使打包體積很大,經過image-webpack-loader插件可將大的圖片進行壓縮從而縮小打包體積node

(1) 先安裝依賴:cnpm install image-webpack-loader --save-devwebpack

(2) 在vue.config.js中module.exports寫入:web

module.exports = {
    productionSourceMap: false,
    chainWebpack: config => {
        // ============壓縮圖片 start============
        config.module
            .rule('images')
            .use('image-webpack-loader')
            .loader('image-webpack-loader')
            .options({ bypassOnDebug: true })
            .end()
        // ============壓縮圖片 end============
    }
}
複製代碼

三、cdn配置(可選)

(1) 在vue.config.js 最上邊寫入:vue-router

// 是否爲生產環境
const isProduction = process.env.NODE_ENV !== 'development'

// 本地環境是否須要使用cdn
const devNeedCdn = false

// cdn連接
const cdn = {
    // cdn:模塊名稱和模塊做用域命名(對應window裏面掛載的變量名稱)
    externals: {
        vue: 'Vue',
        vuex: 'Vuex',
        'vue-router': 'VueRouter'
    },
    // cdn的css連接
    css: [],
    // cdn的js連接
    js: [
        'https://cdn.staticfile.org/vue/2.6.10/vue.min.js',
        'https://cdn.staticfile.org/vuex/3.0.1/vuex.min.js',
        'https://cdn.staticfile.org/vue-router/3.0.3/vue-router.min.js'
    ]
}
(2) 在vue.config.js module.exports chainWebpack中寫入:

// ============注入cdn start============
config.plugin('html').tap(args => {
    // 生產環境或本地須要cdn時,才注入cdn
    if (isProduction || devNeedCdn) args[0].cdn = cdn
    return args
})
// ============注入cdn start============
(3) 在vue.config.js module.exports configureWebpack中寫入:

configureWebpack: config => {
    // 用cdn方式引入,則構建時要忽略相關資源
    if (isProduction || devNeedCdn) config.externals = cdn.externals
}
(4) 當前配置的vue.config.js

// 是否爲生產環境
const isProduction = process.env.NODE_ENV !== 'development'

// 本地環境是否須要使用cdn
const devNeedCdn = false

// cdn連接
const cdn = {
    // cdn:模塊名稱和模塊做用域命名(對應window裏面掛載的變量名稱)
    externals: {
        vue: 'Vue',
        vuex: 'Vuex',
        'vue-router': 'VueRouter'
    },
    // cdn的css連接
    css: [],
    // cdn的js連接
    js: [
        'https://cdn.staticfile.org/vue/2.6.10/vue.min.js',
        'https://cdn.staticfile.org/vuex/3.0.1/vuex.min.js',
        'https://cdn.staticfile.org/vue-router/3.0.3/vue-router.min.js'
    ]
}

module.exports = {
    productionSourceMap: false,
    chainWebpack: config => {
        // ============壓縮圖片 start============
        config.module
            .rule('images')
            .use('image-webpack-loader')
            .loader('image-webpack-loader')
            .options({ bypassOnDebug: true })
            .end()
        // ============壓縮圖片 end============

        // ============注入cdn start============
        config.plugin('html').tap(args => {
            // 生產環境或本地須要cdn時,才注入cdn
            if (isProduction || devNeedCdn) args[0].cdn = cdn
            return args
        })
        // ============注入cdn start============
    },
    configureWebpack: config => {
        // 用cdn方式引入,則構建時要忽略相關資源
        if (isProduction || devNeedCdn) config.externals = cdn.externals
    }
}
複製代碼

(5) 在public index.html 寫入vuex

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width,initial-scale=1.0" />
        <link rel="icon" href="<%= BASE_URL %>favicon.ico" />
        <!-- 使用CDN的CSS文件 -->
        <% for (var i in htmlWebpackPlugin.options.cdn &&
        htmlWebpackPlugin.options.cdn.css) { %>
        <link
            href="<%= htmlWebpackPlugin.options.cdn.css[i] %>"
            rel="stylesheet"
        />
        <% } %>
        <!-- 使用CDN的CSS文件 -->
        <title>cli3_base</title>
    </head>
    <body>
        <noscript>
            <strong
                >We're sorry but cli3_base doesn't work properly without
                JavaScript enabled. Please enable it to continue.</strong
            >
        </noscript>
        <div id="app"></div>

        <!-- 使用CDN的JS文件 -->
        <% for (var i in htmlWebpackPlugin.options.cdn &&
        htmlWebpackPlugin.options.cdn.js) { %>
        <script src="<%= htmlWebpackPlugin.options.cdn.js[i] %>"></script>
        <% } %>
        <!-- 使用CDN的JS文件 -->

        <!-- built files will be auto injected -->
    </body>
</html>
複製代碼

(6) 重啓項目npm run servevue-cli

(7) 在src/router.js修改npm

Vue.use(Router)
複製代碼

改成

if (!window.VueRouter) Vue.use(Router)
複製代碼

(8) 從新啓動npm run serve便可,如今的配置是開發環境,在瀏覽器的Network JS裏面是看不到的。若想查看,請將vue.config.js裏面的

// 本地環境是否須要使用cdn
const devNeedCdn = false
複製代碼

改成

// 本地環境是否須要使用cdn
const devNeedCdn = true
複製代碼

而後再次重啓npm run serve,而後瀏覽器查看Network JS

Network JS

四、代碼壓縮

(1) 安裝依賴:cnpm i -D uglifyjs-webpack-plugin

(2) 在vue.config.js 最上邊引入依賴

// 代碼壓縮
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
複製代碼

(3) 在vue.config.js module.exports configureWebpack 裏面新增

// 生產環境相關配置
if (isProduction) {
    // 代碼壓縮
    config.plugins.push(
        new UglifyJsPlugin({
            uglifyOptions: {
                //生產環境自動刪除console
                compress: {
                    warnings: false, // 若打包錯誤,則註釋這行
                    drop_debugger: true,
                    drop_console: true,
                    pure_funcs: ['console.log']
                }
            },
            sourceMap: false,
            parallel: true
        })
    )
}
複製代碼

五、開啓Gzip

(1) 安裝依賴:cnpm install --save-dev compression-webpack-plugin

(2) 在vue.config.js 頂部引入依賴

// gzip壓縮
const CompressionWebpackPlugin = require('compression-webpack-plugin')
複製代碼

(3) 在vue.config.js module.exports configureWebpack 裏面新增,直接放在代碼壓縮下邊便可

// 生產環境相關配置
if (isProduction) {
    // 代碼壓縮
    // ..................
    // gzip壓縮
    const productionGzipExtensions = ['html', 'js', 'css']
    config.plugins.push(
        new CompressionWebpackPlugin({
            filename: '[path].gz[query]',
            algorithm: 'gzip',
            test: new RegExp(
                '\\.(' + productionGzipExtensions.join('|') + ')$'
            ),
            threshold: 10240, // 只有大小大於該值的資源會被處理 10240
            minRatio: 0.8, // 只有壓縮率小於這個值的資源纔會被處理
            deleteOriginalAssets: false // 刪除原文件
        })
    )
}
複製代碼

六、公共代碼抽離

(1) 在vue.config.js module.exports configureWebpack 裏面新增,直接放在gzip壓縮下邊便可

// 公共代碼抽離
config.optimization = {
    splitChunks: {
        cacheGroups: {
            vendor: {
                chunks: 'all',
                test: /node_modules/,
                name: 'vendor',
                minChunks: 1,
                maxInitialRequests: 5,
                minSize: 0,
                priority: 100
            },
            common: {
                chunks: 'all',
                test: /[\\/]src[\\/]js[\\/]/,
                name: 'common',
                minChunks: 2,
                maxInitialRequests: 5,
                minSize: 0,
                priority: 60
            },
            styles: {
                name: 'styles',
                test: /\.(sa|sc|c)ss$/,
                chunks: 'all',
                enforce: true
            },
            runtimeChunk: {
                name: 'manifest'
            }
        }
    }
}
複製代碼

完整的vue.config.js

// 代碼壓縮
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')

// gzip壓縮
const CompressionWebpackPlugin = require('compression-webpack-plugin')

// 是否爲生產環境
const isProduction = process.env.NODE_ENV !== 'development'

// 本地環境是否須要使用cdn
const devNeedCdn = true

// cdn連接
const cdn = {
    // cdn:模塊名稱和模塊做用域命名(對應window裏面掛載的變量名稱)
    externals: {
        vue: 'Vue',
        vuex: 'Vuex',
        'vue-router': 'VueRouter'
    },
    // cdn的css連接
    css: [],
    // cdn的js連接
    js: [
        'https://cdn.staticfile.org/vue/2.6.10/vue.min.js',
        'https://cdn.staticfile.org/vuex/3.0.1/vuex.min.js',
        'https://cdn.staticfile.org/vue-router/3.0.3/vue-router.min.js'
    ]
}

module.exports = {
    productionSourceMap: false,
    chainWebpack: config => {
        // ============壓縮圖片 start============
        config.module
            .rule('images')
            .use('image-webpack-loader')
            .loader('image-webpack-loader')
            .options({ bypassOnDebug: true })
            .end()
        // ============壓縮圖片 end============

        // ============注入cdn start============
        config.plugin('html').tap(args => {
            // 生產環境或本地須要cdn時,才注入cdn
            if (isProduction || devNeedCdn) args[0].cdn = cdn
            return args
        })
        // ============注入cdn start============
    },
    configureWebpack: config => {
        // 用cdn方式引入,則構建時要忽略相關資源
        if (isProduction || devNeedCdn) config.externals = cdn.externals

        // 生產環境相關配置
        if (isProduction) {
            // 代碼壓縮
            config.plugins.push(
                new UglifyJsPlugin({
                    uglifyOptions: {
                        //生產環境自動刪除console
                        compress: {
                            warnings: false, // 若打包錯誤,則註釋這行
                            drop_debugger: true,
                            drop_console: true,
                            pure_funcs: ['console.log']
                        }
                    },
                    sourceMap: false,
                    parallel: true
                })
            )

            // gzip壓縮
            const productionGzipExtensions = ['html', 'js', 'css']
            config.plugins.push(
                new CompressionWebpackPlugin({
                    filename: '[path].gz[query]',
                    algorithm: 'gzip',
                    test: new RegExp(
                        '\\.(' + productionGzipExtensions.join('|') + ')$'
                    ),
                    threshold: 10240, // 只有大小大於該值的資源會被處理 10240
                    minRatio: 0.8, // 只有壓縮率小於這個值的資源纔會被處理
                    deleteOriginalAssets: false // 刪除原文件
                })
            )

            // 公共代碼抽離
            config.optimization = {
                splitChunks: {
                    cacheGroups: {
                        vendor: {
                            chunks: 'all',
                            test: /node_modules/,
                            name: 'vendor',
                            minChunks: 1,
                            maxInitialRequests: 5,
                            minSize: 0,
                            priority: 100
                        },
                        common: {
                            chunks: 'all',
                            test: /[\\/]src[\\/]js[\\/]/,
                            name: 'common',
                            minChunks: 2,
                            maxInitialRequests: 5,
                            minSize: 0,
                            priority: 60
                        },
                        styles: {
                            name: 'styles',
                            test: /\.(sa|sc|c)ss$/,
                            chunks: 'all',
                            enforce: true
                        },
                        runtimeChunk: {
                            name: 'manifest'
                        }
                    }
                }
            }
        }
    }
}
複製代碼
相關文章
相關標籤/搜索