Files
2026-07-10 10:52:30 +08:00

222 lines
6.8 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use strict'
const path = require('path')
const defaultSettings = require('./src/settings.js')
const webpack = require("webpack")
function resolve(dir) {
return path.join(__dirname, dir)
}
const name = defaultSettings.title || '数之道' // page title
// 需要排除的包对象
let externals = {}
// 需要配置的 CDN 链接
let CDN = { css: [], js: [] }
// 判断是否是生产环境
if (/*process.env.NODE_ENV === 'production'*/true) {
externals = {
vue: 'Vue',
'element-ui': 'ELEMENT',
'vue-router': 'VueRouter',
axios: 'axios',
vuex: 'vuex',
}
CDN = {
css: [
'https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.9/theme-chalk/index.min.css'
],
js: [
// vue cdn包必须在第一个
'https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.min.js',
'https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.9/index.min.js',
'https://cdn.bootcdn.net/ajax/libs/vue-router/3.0.2/vue-router.min.js',
'https://cdn.bootcdn.net/ajax/libs/axios/0.18.1/axios.min.js',
'https://cdnjs.cloudflare.com/ajax/libs/vuex/3.1.0/vuex.min.js'
]
}
}
// 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 || 80 // dev port
const CompressionPlugin = require('compression-webpack-plugin')
const zlib = require('zlib')
// 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, //关闭eslint
productionSourceMap: false,
devServer: {
host: 'localhost',
port: port,
open: true,
proxy: {
[process.env.VUE_APP_BASE_API]: {
// target: 'http://192.168.44.130:10080', // 后端接口
target: 'http://localhost:10080',
changeOrigin: true, // 是否跨域
pathRewrite: {
["^" + process.env.VUE_APP_BASE_API]: "",
},
},
'/Api': {
target: 'http://localhost:10080/Api',
changeOrigin: true, // 是否跨域
pathRewrite: {
["^/Api"]: "",
},
},
'/Api/': {
target: 'http://localhost:10080/Api/',
changeOrigin: true, // 是否跨域
pathRewrite: {
["^/Api"]: "",
},
},
'/upgrade':{
target: 'E:/datao',
changeOrigin: true, // 是否跨域
}
},
overlay: {
warnings: false,
errors: true
},
disableHostCheck: true,
},
configureWebpack: {
// provide the app's title in webpack's name field, so that
// it can be accessed in index.html to inject the correct title.
name: name,
resolve: {
alias: {
'@': resolve('src')
}
},
// devtool: "source-map"
plugins: [
// 压缩成 .gz 文件
new CompressionPlugin({
filename: '[path][base].gz',
algorithm: 'gzip',
test: /\.js$|\.css$|\.html$/,
threshold: 10240,
minRatio: 0.8
}),
// 压缩成 .br 文件,如果 zlib 报错无法解决,可以注释这段使用代码,一般本地没问题,需要注意线上服务器会可能发生找不到 zlib 的情况。
// new CompressionPlugin({ //仅https模式下有效
// filename: '[path][base].br',
// algorithm: 'brotliCompress',
// test: /\.(js|css|html|svg)$/,
// compressionOptions: {
// params: {
// [zlib.constants.BROTLI_PARAM_QUALITY]: 11
// }
// },
// threshold: 10240,
// minRatio: 0.8
// }),
new webpack.optimize.MinChunkSizePlugin({
minChunkSize: 10240 // Minimum number of characters
})
],
externals
},
chainWebpack(config) {
// 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'
}
])
// 注入cdn变量 (打包时会执行)
config.plugin('html').tap(args => {
args[0].cdn = CDN // 配置 CDN 给插件
return args
})
// 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
.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')
}
)
}
}