-
Notifications
You must be signed in to change notification settings - Fork 2
/
nuxt.config.js
executable file
·122 lines (112 loc) · 2.98 KB
/
nuxt.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
const path = require('path')
const SpriteLoaderPlugin = require('svg-sprite-loader/plugin')
module.exports = {
/*
** Headers of the page
*/
head: {
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' }
]
},
/*
** Customize the progress bar color
*/
loading: { color: '#3B8070' },
/*
** Build configuration
*/
build: {
extend (config, ctx) {
config.module.rules.forEach((rule) => {
// images
if (~rule.test.source.indexOf('|svg')) {
rule.test = new RegExp(rule.test.source.replace('|svg', ''))
}
// transform urls to require calls
if (rule.loader === 'vue-loader') {
rule.options.transformToRequire = {
'img': 'src',
'image': 'xlink:href',
'use': 'xlink:href'
}
return
}
})
// svg
config.module.rules.push({
test: /\.svg$/,
loader: 'svg-sprite-loader',
options: {
symbolId: this.options.dev
? '[1].[ext]'
: (new IncrementalCache()).get,
symbolRegExp: this.options.srcDir + '/(.*)\\.svg',
esModule: false,
extract: true,
spriteFilename: this.options.dev
? spriteFilenameGeneratorDev.bind(this)()
: spriteFilenameGeneratorProd.bind(this)()
}
})
},
plugins: [
new SpriteLoaderPlugin()
]
}
}
function spriteFilenameGeneratorDev() {
return (svgPath) => {
const pathKey = getSvgSpritePathKey({ svgPath, options: this.options })
const normalizedPathKey = normalizeSvgSpritePathKey(pathKey)
return `sprites/${normalizedPathKey}.svg`
}
}
function spriteFilenameGeneratorProd(svgPath) {
const cache = new IncrementalCache()
cache.get('default') // set 0 for default
return (svgPath) => {
const pathKey = getSvgSpritePathKey({ svgPath, options: this.options })
const normalizedPathKey = normalizeSvgSpritePathKey(pathKey)
const id = cache.get(normalizedPathKey)
return `sprites/${id}.[hash:7].svg`
}
}
function normalizeSvgSpritePathKey(pathKey) {
return pathKey
.toLowerCase()
.replace(/(^\/|\/$)/g, '')
.replace(/\//g, '-')
}
function getSvgSpritePathKey({ svgPath, options }) {
if (~svgPath.indexOf(options.srcDir)) {
const relPath = svgPath.slice(options.srcDir.length + 1)
const rules = [
// components first level
/(components[\/\\][^\/\\]+)[\/\\]/
]
let m
for (rule of rules) {
m = relPath.match(rule)
if (m && m[1]) return m[1]
}
// pages
for (route of options.router.routes) {
if (~relPath.indexOf(options.dir.pages + route.path)) {
return options.dir.pages + route.path
}
}
}
return 'default'
}
function IncrementalCache() {
this.store = {}
this.lastId = 0
this.get = (key) => {
if (!this.store[key]) {
this.store[key] = (this.lastId++).toString(36)
}
return this.store[key]
}
}