vue.config.js完整代碼如下: 'use strict'; // Template version: 1.3.1 // see http://vuejs-templates./webpack for documentation. const path = require('path'); const resolve = (dir) => path.resolve(__dirname, dir); // 是否為生產環(huán)境 const isProduction = process.env.NODE_ENV !== 'development'; // gzip壓縮 const CompressionWebpackPlugin = require('compression-webpack-plugin'); const cdn = { // 忽略打包的第三方庫 externals: { vue: 'Vue', vuex: 'Vuex', 'vue-router': 'VueRouter', axios: 'axios', }, // 通過cdn方式使用 js: [ 'https://cdn./vue/2.6.11/vue.runtime.min.js', 'https://cdn./vue-router/3.1.2/vue-router.min.js', 'https://cdn./vuex/3.1.2/vuex.min.js', 'https://cdn./axios/0.19.2/axios.min.js', 'https://cdn./moment.js/2.24.0/moment.min.js', 'https://cdn./echarts/4.6.0/echarts.min.js', ], css: [], }; module.exports = { publicPath: '/', // 基本路徑 outputDir: 'dist', // 輸出文件目錄 assetsDir: 'static', indexPath: 'index.html', productionSourceMap: false, // 關閉生產環(huán)境的 source map chainWebpack: (config) => { config.module .rule('swf') .test(/\.swf$/) .use('url-loader') .loader('url-loader') .tap((options) => { return { limit: 10000, }; }); // 添加別名 config.resolve.alias .set('@', resolve('src')) .set('assets', resolve('src/assets')) .set('components', resolve('src/components')) .set('router', resolve('src/router')) .set('store', resolve('src/store')) .set('views', resolve('src/views')); config.plugin('copy').tap((args) => { args[0][0].to = 'resource'; return args; }); // 移除 prefetch 插件 config.plugins.delete('prefetch-index'); // 移除 preload 插件,避免加載多余的資源 config.plugins.delete('preload-index'); // 配置cdn引入 config.plugin('html').tap((args) => { args[0].cdn = cdn; return args; }); }, // 這個值是一個對象,則會通過 webpack-merge 合并到最終的配置中。 configureWebpack: (config) => { // 忽略打包配置 config.externals = cdn.externals; // 生產環(huán)境相關配置 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, // 刪除原文件 }) ); // 公共代碼抽離 config.optimization = { // 分割代碼塊 splitChunks: { cacheGroups: { //公用模塊抽離 common: { chunks: 'initial', minSize: 0, //大于0個字節(jié) minChunks: 2, //抽離公共代碼時,這個代碼塊最小被引用的次數(shù) }, //第三方庫抽離 vendor: { priority: 1, //權重 test: /node_modules/, chunks: 'initial', minSize: 0, //大于0個字節(jié) minChunks: 2, //在分割之前,這個代碼塊最小應該被引用的次數(shù) }, }, }, }; } }, devServer: { overlay: { warnings: false, errors: false, }, open: true, // host: 'localhost', host: '0.0.0.0', port: 8081, https: false, hotOnly: true, proxy: { '/api': { // 代理地址 target: process.env.VUE_APP_API, changeOrigin: true, // 是否跨域 secure: false, pathRewrite: { '^/api': '/api/ctrl', //測試環(huán)境 // '/api': '', //需要rewrite重寫的, //本地聯(lián)調 }, }, }, }, css: { extract: true, sourceMap: false, loaderOptions: { sass: { prependData: `@import "~@/assets/scss/variables.scss";`, }, }, }, }; index.html頁面引入cdn文件: <% for (var i in htmlWebpackPlugin.options.cdn && htmlWebpackPlugin.options.cdn.js) { %> <script src="<%= htmlWebpackPlugin.options.cdn.js[i] %>" crossorigin="anonymous" ></script> <% } %>
|
|