-
Notifications
You must be signed in to change notification settings - Fork 18
/
vue.config.js
153 lines (153 loc) · 4.88 KB
/
vue.config.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* @Author: etongfu
* @Email: [email protected]
* @LastEditors: etongfu
* @Description: 当前项目配置文件
* @youWant: add you want info here
* @Date: 2019-04-24 17:45:34
* @LastEditTime: 2019-08-21 17:45:05
*/
const path = require('path')
const chalk = require('chalk')
const TerserPlugin = require('terser-webpack-plugin');
const { Log, StringUtil } = require('./scripts/util')
const { compressionPlugin } = require('./config/plugins.config')
const { getAllAlias } = require('./config/util')
const resolve = dir => path.join(__dirname, dir)
const cdnResource = require('./config/cdn.config')
Log.logger(chalk.blue(`当前运行环境:${process.env.NODE_ENV}`))
/**
* 全局文件变量注入
* @param {*} rule
* @param {*} filePath
*/
const addStyleResource = (rule, files) => {
rule.use('style-resource')
.loader('style-resources-loader')
.options({
patterns: files
})
}
/**
* 构建Webpack plugins 数组
*/
const buildPlugins = () => {
let plugins = []
// GZIP: typeof(process.env.VUE_APP_GZIP) === string
if (compressionPlugin.use && process.env.NODE_ENV !== "development" && StringUtil.toBoolean(process.env.VUE_APP_GZIP)) {
Log.logger('Gzip Mode opened!')
plugins.push(compressionPlugin.plugin)
}
return plugins
}
// 配置文件抛出
module.exports = {
// publicPath: '/',
// publicPath: process.env.NODE_ENV === 'development' ? '/' : '/vue-base-template/dist/',
publicPath: './', // 发布到github page上的配置路径
outputDir: 'dist',
assetsDir: 'static',
lintOnSave: process.env.NODE_ENV === 'development',
productionSourceMap: false,
devServer: {
port: process.env.port || 8080
// 接口代理需要使用的时候放开
/* proxy: {
'/api': {
target: 'http://172.16.200.156:80/fods/a/',
changeOrigin: true,
pathRewrite: {
'^/api/': ''
}
}
} */
},
// css配置
css: {
loaderOptions: {
css: {
module: true
}
// scss: {
// prependData: `@import "@/styles/variable.scss";`
// }
}
},
parallel: require('os').cpus().length > 1,
// webpack 配置
configureWebpack: {
// document.title
name: 'vue-base-template(V2)',
// entry: [resolve('src/main.js')],
// provide the app's title in webpack's name field, so that
// it can be accessed in index.html to inject the correct title.
resolve: {
// 自动扫描文件
extensions: ['.js', '.json', '.vue', '.less'],
// 别名
alias: getAllAlias({
'vue$': 'vue/dist/vue.esm.js',
'@': resolve('src')
})
},
// webpack 插件
plugins: buildPlugins()
},
// webpack chain 配置
chainWebpack: config => {
// source map形式
config.when(process.env.NODE_ENV === 'development', config => config.devtool('cheap-module-eval-source-map'))
// set preserveWhitespace
config.module
.rule('vue')
.use('vue-loader')
.loader('vue-loader')
.tap(options => {
// Whether the render function preserves spaces between all HTML tags If set to false, spaces between tags are ignored. This can slightly improve performance but may affect the layout of inline elements.
options.compilerOptions.preserveWhitespace = true
return options
})
.end()
// Auto Inject scss
const types = ['vue-modules', 'vue', 'normal-modules', 'normal']
types.forEach(type => addStyleResource(config.module.rule('scss').oneOf(type), [resolve('./src/styles/variable.scss'), resolve('./src/styles/mixins.scss')]))
// Inject cdn for cdn
const cdn = {
...cdnResource.cdn
}
config.plugin('html').tap(args => {
args[0].cdn = cdn
return args
})
// Runtime/packaging module split optimization
if (process.NODE_ENV !== 'development') {
config.optimization.splitChunks({
chunks: 'all', // global components
cacheGroups: {
libs: {
name: 'chunk-modules',
test: /[\\/]node_modules[\\/]/,
priority: 10,
chunks: 'initial' // only package third parties that are initially dependent
},
elementUI: {
name: 'chunk-element', // 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
},
// global components
commons: {
name: 'chunk-global',
test: resolve('src/components/global'), // can customize your rules
minChunks: 3, // minimum common number
priority: 5,
reuseExistingChunk: true
}
}
})
config.optimization.runtimeChunk('single')
// Multi-thread compression
config.optimization.minimizer = [new TerserPlugin({})]
}
}
}