'use strict'
const path = require('path')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
function resolve(dir) {
return path.join(__dirname, dir)
}
const webpack = require('webpack')
const px2rem = require('postcss-px2rem')
const postcss = px2rem({
// 基准大小 baseSize,需要和rem.js中相同
remUnit: 16
})
const isProduction = process.env.NODE_ENV === 'production';
// // cdn预加载使用,如果可以访问公网就可以放开
// const externals = {
// vue: 'Vue',
// 'vue-router': 'VueRouter',
// vuex: 'Vuex',
// axios: 'axios',
// "element-ui": "ELEMENT",
// 'echarts':'echarts',
// }
// If your port is set to 80,
// use administrator privileges to execute the command line.
// For example, Mac: sudo npm run
// You can change the port by the following method:
// port = 9527 npm run dev OR npm run dev --port = 9527
const port = process.env.port || process.env.npm_config_port || 9527 // dev port
// All configuration item explanations can be find in https://cli.vuejs.org/config/
module.exports = {
/**
* You will need to set publicPath if you plan to deploy your site under a sub path,
* for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
* then publicPath should be set to "/bar/".
* In most cases please use '/' !!!
* Detail: https://cli.vuejs.org/config/#publicpath
*/
publicPath: '/',
outputDir: 'dist',
assetsDir: 'static',
lintOnSave: false,
// lintOnSave: process.env.NODE_ENV === 'development',
productionSourceMap: false,
css: {
loaderOptions: {
postcss: {
plugins: [
postcss
]
},
scss:{
prependData:`@import "~@/styles/mixin.scss";`
}
}
},
devServer: {
port: port,
open: true,
overlay: {
warnings: false,
errors: true
},
proxy: {
[process.env.VUE_APP_BASE_API]: {
// target: `http://localhost:8081/ipamPortal`, //本机IP
target: `http://192.168.60.227:8080/zoneportal/`, //内网IP
// target: `http://192.168.60.149:8080/zoneportal/`, //内网IP
// target: `http://172.168.70.176:8080/ipamPortal/`,
// target: `http://172.168.70.128:8080/ipamPortal/`, //内网IP
// target: `http://192.168.60.225:8080/zoneportal/`, //测试环境IP
// target: `http://120.236.178.177:22780/zoneportal`, //外网
// target: `window.config.app_api_url`, //外网
changeOrigin: true,
pathRewrite: {
["^" + process.env.VUE_APP_BASE_API]: ""
}
}
}
// before: require('./mock/mock-server.js')
},
configureWebpack: config => {
const plugins = [];
if (isProduction) {
plugins.push(
new UglifyJsPlugin({
uglifyOptions: {
output: {
comments: false, // 去掉注释
},
warnings: false,
compress: {
drop_console: true,
drop_debugger: false,
pure_funcs: ['console.log']//移除console
}
}
})
)
// 开启分离js
config.optimization = {
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
minSize: 1000 * 60,
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name(module) {
// 排除node_modules 然后吧 @ 替换为空 ,考虑到服务器的兼容
const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1]
return `npm.${packageName.replace('@', '')}`
}
}
}
}
};
// 取消webpack警告的性能提示
config.performance = {
hints: 'warning',
//入口起点的最大体积
maxEntrypointSize: 1000 * 500,
//生成文件的最大体积
maxAssetSize: 1000 * 1000,
//只给出 js 文件的性能提示
assetFilter: function (assetFilename) {
return assetFilename.endsWith('.js');
}
}
// 打包时npm包转CDN
// config.externals = externals;
}
return { plugins }
},
chainWebpack(config) {
// CDN外链,会插入到index.html中
// const cdn = {
// // 开发环境
// dev: {
// css: ['https://unpkg.com/element-ui/lib/theme-chalk/index.css'],
// js: []
// },
// // 生产环境
// build: {
// css: ['https://unpkg.com/element-ui/lib/theme-chalk/index.css','https://cdn.bootcdn.net/ajax/libs/font-awesome/5.15.3/css/all.min.css'],
// js: [
// 'https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js',
// 'https://cdn.jsdelivr.net/npm/vue-router@3.1.5/dist/vue-router.min.js',
// 'https://cdn.jsdelivr.net/npm/axios@0.18.1/dist/axios.min.js',
// 'https://cdn.jsdelivr.net/npm/vuex@3.1.0/dist/vuex.min.js',
// 'https://unpkg.com/element-ui/lib/index.js',
// "https://cdn.staticfile.org/echarts/4.3.0/echarts.min.js"
// ]
// }
// }
// webpack 会默认给commonChunk打进chunk-vendors,所以需要对webpack的配置进行delete
config.optimization.delete('splitChunks')
// it can improve the speed of the first screen, it is recommended to turn on preload
// it can improve the speed of the first screen, it is recommended to turn on preload
config.plugin('preload').tap(() => [
{
rel: 'preload',
// to ignore runtime.js
// https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
include: 'initial'
}
])
// when there are many pages, it will cause too many meaningless requests
config.plugins.delete('prefetch')
// set svg-sprite-loader
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()
config.plugin('provide').use(webpack.ProvidePlugin, [{
$: 'jquery',
jquery: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery',
}])
// config.plugin('html').tap(args => {
// if (process.env.NODE_ENV === 'production') {
// args[0].cdn = cdn.build
// }
// if (process.env.NODE_ENV === 'development') {
// args[0].cdn = cdn.dev
// }
// return args
// })
config
.when(process.env.NODE_ENV !== 'development',
config => {
config
.plugin('ScriptExtHtmlWebpackPlugin')
.after('html')
.use('script-ext-html-webpack-plugin', [{
// `runtime` must same as runtimeChunk name. default is `runtime`
inline: /runtime\..*\.js$/
}])
.end()
/* config
.optimization.splitChunks({
chunks: 'all',
cacheGroups: {
libs: {
name: 'chunk-libs',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial' // only package third parties that are initially dependent
},
elementUI: {
name: 'chunk-elementUI', // split elementUI into a single package
priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
},
commons: {
name: 'chunk-commons',
test: resolve('src/components'), // can customize your rules
minChunks: 3, // minimum common number
priority: 5,
reuseExistingChunk: true
}
}
})*/
// https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
config.optimization.runtimeChunk('single')
}
)
}
}