vue.config.js
4.09 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
const path = require('path')
const CompressionPlugin = require('compression-webpack-plugin')
const SpritesmithPlugin = require('webpack-spritesmith') // 雪碧图
const environment = process.env.VUE_APP_NODE_ENV // 定义变量 环境
const app_version = process.env.VUE_APP_VERSION // 定义变量 版本号
// cdn
const cdn = {
// 开发环境
dev: {
css: [],
js: []
},
// 生产环境
build: {
css: [],
js: [
// `https://lib.baomitu.com/vue/2.6.11/vue.min.js`,
// `https://lib.baomitu.com/vue-router/3.1.3/vue-router.min.js`,
// `https://lib.baomitu.com/vuex/3.1.2/vuex.min.js`
]
}
}
// 雪碧图的自定义模板
const templateFunction = function (data) {
var shared = '.icon-sprite { display: inline-block; background-image: url(I); background-size: Dpx Hpx; }'
.replace('I', data.sprites[0].image)
.replace('D', data.sprites[0].total_width)
.replace('H', data.sprites[0].total_height)
var perSprite = data.sprites.map(function (sprite) {
return '.icon-N { width: Wpx; height: Hpx; background-position: Xpx Ypx; }'
.replace('N', sprite.name.replace(/_/g, '-'))
.replace('W', sprite.width)
.replace('H', sprite.height)
.replace('X', sprite.offset_x)
.replace('Y', sprite.offset_y)
}).join('\n')
return shared + '\n' + perSprite
}
module.exports = {
outputDir: "dist",
assetsDir: "assets",
publicPath: "./",
devServer: {
host: '0.0.0.0',
port: "8080",
https: false,
hotOnly: false,
open: true,
overlay: {
warning: false,
errors: false
},
proxy: {
'/api': {
// 测试环境
target: process.env.VUE_APP_API, // http://192.168.0.114:8000/
changeOrigin: true, // 是否跨域
pathRewrite: {
'^/api': '' // 需要rewrite重写的, //
}
}
},
},
pages: {
index: {
entry: "./src/main.js",
template: path.join(__dirname, "public/index.html"),
filename: 'index.html',
title: 'NAFMII-H5',
chunks: ['chunk-vendors', 'chunk-common', 'index'],
// cdn: environment === 'development' ? cdn.dev : cdn.build
}
},
productionSourceMap: false,
css: {
loaderOptions: {
less: {
javascriptEnabled: true
},
}
},
configureWebpack: config => {
if (environment === "devproduction" || environment === 'test' || environment === "uat" || environment === "pro") {
//开发环境开启压缩
config.plugins.push(new CompressionPlugin({ // Gzip压缩配置
test: /\.js$|\.html$|\.less/, // 匹配文件名
threshold: 0,
deleteOriginalAssets: false // 是否删除原文件
}))
}
return {
externals: {
//moment: "moment",
//axios: "axios"
},
resolve: {
modules: ["node_modules", "spritesmith-generated"],
alias: {
// 别名
'@': path.resolve('src'),
}
},
plugins: [
new SpritesmithPlugin({
src: {
cwd: path.resolve(__dirname, './src/assets/icon'),
glob: '*.png'
},
target: { // 输出雪碧图文件及样式文件,这个是打包后,自动生成的雪碧图和样式
image: path.resolve(__dirname, './src/assets/images/sprite.png'),
css: [
[path.resolve(__dirname, './src/assets/css/sprite.less'), {
// 引用自己的模板
format: 'function_based_template'
}]
]
},
customTemplates: { // 自定义模板入口
function_based_template: templateFunction
},
apiOptions: { // 样式文件中调用雪碧图地址写法
cssImageRef: '../images/sprite.png'
},
spritesmithOptions: { // 让合成的每个图片有一定的距离
padding: 20
}
})
]
}
},
chainWebpack: config => {
const hashName = `assets/js/[name].[hash:8].${environment}.${app_version}.js`
config.output.filename(hashName).end()
config.output.chunkFilename(hashName).end()
},
lintOnSave: false
}