- gzip打包压缩优化
- cdn加载外部资源优化
- 请求代理
- 引入全局scss的
mixin和变量
// npm install --save-dev compression-webpack-plugin 压缩优化
const CompressionWebpackPlugin = require("compression-webpack-plugin");
const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i;
// vue.config.js
module.exports = {
publicPath: process.env.NODE_ENV === 'production'? '/production/' : '/', // 部署应用包时的基本 URL
outputDir: 'dist', // 打包文件放置的目录
assetsDir: '', //放置生成的静态资源 (js、css、img、fonts) 的 (相对于 outputDir 的) 目录。
indexPath: 'index.html', // 指定生成的 index.html 的输出路径 (相对于 outputDir)。也可以是一个绝对路径。
filenameHashing: true, // 静态资源文件名是否哈希
devServer: { // 运行配置
host: 'localhost', // target host
post: '8080' // target post
proxy: { // 请求代理
'/api': {
target: 'http://192.128.1.30:80', // 代理地址,这里设置的地址会代替axios中设置的baseURL
changeOrigin: true, // 如果接口跨域,需要进行这个参数配置
ws: true, // proxy webscokets
pathRewrite: {
'^/api': '/',
pathRewrite: {'^/api': '/'}, // 重写之后url为 http://192.128.1.16:80/xxxx
pathRewrite: {'^/api': '/api'} // 重写之后url为 http://192.128.1.16:80/api/xxxx
}
}
}
},
configureWebpack: config => {
if (process.env.NODE_ENV === 'production') {
// 为生产环境修改配置
// Begin 生成 gzip 压缩文件
plugins.push(
new CompressionWebpackPlugin({ // 压缩优化
filename: "[path].gz[query]",
algorithm: "gzip",
test: productionGzipExtensions,
threshold: 10240,
minRatio: 0.8
})
);
// End 生成 gzip 压缩文件
config.plugins = [...config.plugins, ...plugins];
config.externals = { // 打包cdn优化
"vue": "Vue",
"vuex": "Vuex",
"vue-router": "VueRouter",
"element-ui": "ELEMENTUI"
};
} else {
// 为开发环境修改配置
}
},
pluginOptions: {
'style-resources-loader': {
preProcessor: 'scss',
patterns: [
// 路径根据具体需求更改
path.resolve(__dirname, 'src/styles/_variables.scss'),
path.resolve(__dirname, 'src/styles/_mixins.scss')
]
}
},
pages: { // 构建多页面
index: {
// page 的入口
entry: 'src/index/main.js',
// 模板来源
template: 'public/index.html',
// 在 dist/index.html 的输出
filename: 'index.html',
// 当使用 title 选项时,
// template 中的 title 标签需要是 <title><%= htmlWebpackPlugin.options.title %></title>
title: 'Index Page',
// 在这个页面中包含的块,默认情况下会包含
// 提取出来的通用 chunk 和 vendor chunk。
chunks: ['chunk-vendors', 'chunk-common', 'index']
},
// 当使用只有入口的字符串格式时,
// 模板会被推导为 `public/subpage.html`
// 并且如果找不到的话,就回退到 `public/index.html`。
// 输出文件名会被推导为 `subpage.html`。
subpage: 'src/subpage/main.js'
}
}