加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
vue.config.js 3.25 KB
一键复制 编辑 原始数据 按行查看 历史
shaohai.li 提交于 2022-10-23 16:16 . fix: invalid Host header
const path = require('path');
const CompressionWebpackPlugin = require('compression-webpack-plugin');
function resolve(dir) {
return path.join(__dirname, dir);
}
const isProd = process.env.NODE_ENV === 'production';
const { VueCDN, AxiosCDN, VueRouterCDN, VuexCDN } = require('./src/plugins/cdn');
const cdn = {
css: [],
js: [VueCDN, AxiosCDN, VueRouterCDN, VuexCDN],
externals: {
vue: 'Vue',
'vue-router': 'VueRouter',
vuex: 'Vuex',
axios: 'axios'
}
};
module.exports = {
productionSourceMap: false,
outputDir: 'dist',
publicPath: './',
lintOnSave: !isProd,
devServer: {
port: 8999,
open: true,
overlay: {
warnings: false,
errors: true
},
before: !isProd ? require('./mock/mock-server.js') : '',
allowedHosts: ['vue-admin-all.mrlishaohai.com', 'localhost']
},
css: {
loaderOptions: {
less: {
prependData: `@import "~@/styles/variables.less";`
}
}
},
configureWebpack: {
name: process.env.VUE_APP_BASE_NAME,
resolve: {
alias: {
'@': resolve('src')
}
},
externals: isProd ? cdn.externals : {}
},
chainWebpack(config) {
config.plugins.delete('prefetch');
config.plugins.delete('preload');
// 设置svg
config.module.rule('svg').exclude.add(resolve('src/icons')).end();
config.module
.rule('icons')
.test(/\.svg$/)
.include.add(resolve('src/icons'))
.end()
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options({
symbolId: 'icon-[name]'
})
.end();
//设置开发环境sourceMap
config.when(!isProd, config => config.devtool('cheap-source-map'));
//生产环境
config.when(isProd, config => {
config.optimization.splitChunks({
chunks: 'all',
cacheGroups: {
libs: {
name: 'common-chunk-plugins',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial'
},
commons: {
name: 'commons-libs',
test: /[\\/]src[\\/]js[\\/]/,
minChunks: 2, // minimum common number
priority: 5,
reuseExistingChunk: true
}
}
});
config.plugin('html').tap(args => {
args[0].cdn = cdn;
return args;
});
config.optimization.runtimeChunk('single');
//去除生产环境debugger 和console
config.optimization.minimizer('terser').tap(args => {
args[0].terserOptions.compress.warnings = false;
args[0].terserOptions.compress.drop_console = true;
args[0].terserOptions.compress.drop_debugger = true;
args[0].terserOptions.compress.pure_funcs = ['console.*'];
return args;
});
//g-zip开启
config.plugin('CompressionWebpackPlugin').use(CompressionWebpackPlugin, [
{
filename: '[path].gz[query]',
algorithm: 'gzip',
test: /\.js$|\.css/, //匹配文件名
threshold: 10240, //对超过10k的数据压缩
minRatio: 0.8
}
]);
//打包大小分析
if (process.env.npm_config_report) {
config.plugin('webpack-bundle-analyzer').use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin);
}
});
}
};
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化