vue.config.js
7.35 KB
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// const webpack = require('webpack');
const path = require('path');
const UglifyPlugin = require('uglifyjs-webpack-plugin');
const CompressionWebpackPlugin = require("compression-webpack-plugin");
// const AddAssetHtmlPlugin = require('add-asset-html-webpack-plugin');
const openGzip = true;
const isProduction = process.env.NODE_ENV === 'production';
let { version, version_lib, library } = require('./package.json');
version = version.replace(/\./g, '_');
const CDN_DOMAIN = 'https://cdn.bootcss.com'
const cdn = {
css: [],
js: [`${CDN_DOMAIN}/vue/2.6.10/vue.min.js`, `${CDN_DOMAIN}/vue-router/3.0.6/vue-router.min.js`]
}
// version_lib = version_lib.replace(/\./g,'_');
module.exports = {
publicPath: './', // 基本路径
outputDir: 'dist', // 输出文件目录
assetsDir: "static",
lintOnSave: false, // eslint-loader 是否在保存的时候检查
// see https://github.com/vuejs/vue-cli/blob/dev/docs/webpack.md
// webpack配置
chainWebpack: (config) => {
// 修复HMR
config.resolve.symlinks(true);
// 别名配置
config.resolve.alias
.set('@', path.resolve(__dirname, './src'))
.set('@a', path.resolve(__dirname, './src/assets'))
.set('@c', path.resolve(__dirname, './src/components'))
.set('@v', path.resolve(__dirname, './src/views'));
if (isProduction) {
config.plugin('html')
.tap(args => {
args[0].cdn = cdn;
return args;
})
}
},
configureWebpack: (config) => {
if (isProduction) {
// 为生产环境修改配置...
// config.mode = 'production';
// 将每个依赖包打包成单独的js文件
let optimization = {
runtimeChunk: 'single',
splitChunks: {
chunks: 'all',
maxInitialRequests: Infinity,
minSize: 20000, // 依赖包超过20000bit将被单独打包
cacheGroups: {
vendor: {
test: /[\\/]node_modules[\\/]/,
name(module) {
// get the name. E.g. node_modules/packageName/not/this/part.js or node_modules/packageName
const packageName = module.context.match(/[\\/]node_modules[\\/](.*?)([\\/]|$)/)[1];
// npm package names are URL-safe, but some servers don't like @ symbols
return `npm.${packageName.replace('@', '')}`;
}
}
}
},
minimizer: [
new UglifyPlugin({
uglifyOptions: {
warnings: false,
compress: {
drop_console: true, // console
drop_debugger: false,
pure_funcs: ['console.log'] // 移除console
}
}
})
]
};
Object.assign(config, {
output: {
...config.output,
filename: `static/js/[name].[chunkhash].${version}.js`,
chunkFilename: `static/js/[name].[chunkhash].${version}.js`
},
externals: {
'vue': 'Vue',
// 'element-ui': 'ELEMENT',
'vue-router': 'VueRouter',
// 'vuex': 'Vuex'
}, // 防止将某些 import 的包(package)打包到 bundle 中,而是在运行时(runtime)再去从外部获取这些扩展依赖(用于cdn引入)
optimization,
// plugins:[
// ...config.plugins,
// ...Object.keys(library).map(name => {
// return new webpack.DllReferencePlugin({
// context: process.cwd(),
// manifest: require(`./libs/package/json/${name}.manifest.json`),
// })
// }),
// new AddAssetHtmlPlugin(Object.keys(library).map(name => {
// return {
// filepath: require.resolve(path.resolve(`libs/package/js/${name}.${version_lib}.dll.js`)),
// outputPath: 'static/lib/js',
// publicPath:'./static/lib/js',
// includeSourcemap: false
// }
// })),
// ]
});
if(openGzip){
// config.plugins = [
// ...config.plugins,
// new CompressionPlugin({
// test:/\.js$|\.html$|.\css/, //匹配文件名
// threshold: 10240,//对超过10k的数据压缩
// deleteOriginalAssets: false //不删除源文件
// }),
// 打包生产.gz包
config.plugins.push(new CompressionWebpackPlugin({
algorithm: 'gzip',
test: /\.js$|\.html$|.\css/,
threshold: 10240,
minRatio: 0.8
}))
// ]
}
} else {
// 为开发环境修改配置...
// config.mode = 'development';
}
// Object.assign(config, {
// 开发生产共同配置
// plugins:[
// ...config.plugins,
// new webpack.ProvidePlugin({
// jQuery: "jquery",
// $: "jquery",
// "windows.jQuery":"jquery"
// })
// ]
// });
},
productionSourceMap: false, // 生产环境是否生成 sourceMap 文件
// css相关配置
css: {
// extract: true, // 是否使用css分离插件 ExtractTextPlugin
sourceMap: false, // 开启 CSS source maps?
loaderOptions: {
css: {}, // 这里的选项会传递给 css-loader
postcss: {} // 这里的选项会传递给 postcss-loader
}, // css预设器配置项
modules: false // 启用 CSS modules for all css / pre-processor files.
},
parallel: require('os').cpus().length > 1, // 是否为 Babel 或 TypeScript 使用 thread-loader。该选项在系统的 CPU 有多于一个内核时自动启用,仅作用于生产构建。
pwa: {}, // PWA 插件相关配置 see https://github.com/vuejs/vue-cli/tree/dev/packages/%40vue/cli-plugin-pwa
// webpack-dev-server 相关配置
devServer: {
open: process.platform === 'darwin',
host: '0.0.0.0', // 允许外部ip访问
port: 8080, // 端口
https: false, // 启用https
disableHostCheck: false,
overlay: {
warnings: true,
errors: true
}, // 错误、警告在页面弹出
proxy: {
'/api': {
target: `${process.env.VUE_APP_BASE_API}/api`,
changeOrigin: true, // 允许websockets跨域
// ws: true,
pathRewrite: {
'^/api': ''
}
}
} // 代理转发配置,用于调试环境
}, // 第三方插件配置
pluginOptions: {}
};