vue.config.js
2.99 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
const path = require('path')
let styleVariables = require('./src/assets/style/variables.scss.js')
const resolve = dir => path.join(__dirname, dir)
const SpritesmithPlugin = require('webpack-spritesmith')
const CDN_DOMAIN = 'https://img02.glsx.com.cn/ajax/libs'
const cdn = {
css: [`${CDN_DOMAIN}/element-ui/2.11.1/theme-chalk/index.css`, 'https://img02.glsx.com.cn/gl-iconfont/iconfont.css'],
js: [`${CDN_DOMAIN}/rxjs/7.3.0/rxjs.umd.min.js`]
}
module.exports = {
outputDir: `dist`,
assetsDir: 'static',
devServer: {
hot: true,
disableHostCheck: true,
port: process.env.VUE_APP_PORT,
overlay: {
warnings: false,
errors: true
},
headers: {
'Access-Control-Allow-Origin': '*'
}
},
css: {
loaderOptions: {
sass: {
prependData: Object.keys(styleVariables)
.map(k => `\$${k.replace('_', '-')}: ${styleVariables[k]};`)
.join('\n')
// prependData: `@import '~@/assets/style/variables.scss';`
}
}
},
configureWebpack: {
resolve: {
alias: {
'@': resolve('src')
}
},
externals: {
rxjs: 'rxjs'
}
},
chainWebpack: config => {
config.plugin('html').tap(args => {
args[0].cdn = cdn
args[0].title = ''
return args
})
config.module
.rule('fonts')
.test(/.(ttf|otf|eot|woff|woff2)$/)
.use('url-loader')
.loader('url-loader')
.tap(options => {
options = {
// limit: 10000,
name: '/static/fonts/[name].[ext]'
}
return options
})
.end()
config.plugin('webpack-spritesmith').use(SpritesmithPlugin, [
{
src: {
cwd: './src/assets/images/icons-v2/',
glob: '*.png'
},
target: {
image: './src/assets/images/sprite/sprite.png',
css: [
[
path.resolve(__dirname, './src/assets/images/sprite/sprite.css'),
{
// 引用自己的模板
format: 'function_based_template'
}
]
]
},
apiOptions: {
cssImageRef: './sprite.png'
},
customTemplates: {
function_based_template: templateFunction
},
spritesmithOptions: {
algorithm: 'binary-tree',
padding: 10
}
}
])
// process.env.NODE_ENV === 'development' && config.plugin('webpack-bundle-analyzer').use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
}
}
const templateFunction = function (data) {
var shared = '.i { display: inline-block; vertical-align: middle; background-image: url(I) }'.replace('I', data.sprites[0].image)
var perSprite = data.sprites
.map(function (sprite) {
return '.i-N { width: Wpx; height: Hpx; background-position: Xpx Ypx; }'.replace('N', sprite.name).replace('W', sprite.width).replace('H', sprite.height).replace('X', sprite.offset_x).replace('Y', sprite.offset_y)
})
.join('\n')
return shared + '\n' + perSprite
}