-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
124 lines (101 loc) · 3.16 KB
/
index.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
let mix = require('laravel-mix');
let path = require('path');
const { extendDefaultPlugins } = require('svgo');
class SvgVue {
name() {
return 'svgVue';
}
dependencies() {
return ['svgo-loader', 'raw-loader', 'fs', 'svg-vue', 'svg-vue3'];
}
register(options) {
this.options = Object.assign({
svgPath: 'resources/svg',
extract: false,
svgoSettings: [
{ removeTitle: true },
{ removeViewBox: false },
{ removeDimensions: true }
]
}, options);
this.includePath = path.resolve(__dirname, process.cwd() + '/' + this.options.svgPath);
}
boot() {
Mix.listen('configReady', config => {
config.module.rules.map(r => {
if (this._isSvgRegExp(r.test) && ! this._isSvgVueRule(r)) {
r.exclude = path.resolve(__dirname, process.cwd() + '/' + this.options.svgPath);
}
});
});
}
webpackRules() {
return {
test: /\.svg$/,
include: [
this.includePath
],
rules: [
{
loader: 'raw-loader'
},
{
loader: 'svgo-loader',
options: {
plugins: extendDefaultPlugins(this._convertSvgoOptions(this.options.svgoSettings))
}
}
]
}
}
webpackConfig(webpackConfig) {
let fs = require('fs');
fs.mkdir(this.includePath, error => {
if (error && error.code === 'EEXIST') return null;
});
webpackConfig.resolve.alias['svg-files-path'] = this.includePath;
if (this.options.extract) {
let svgAssetsObj = {
test: this.includePath,
name: '/js/svg',
chunks: 'all',
enforce: true
}
if (webpackConfig.optimization.hasOwnProperty('splitChunks')) {
webpackConfig.optimization.splitChunks.cacheGroups['svgAssets'] = svgAssetsObj;
} else {
webpackConfig.optimization = {
splitChunks: {
cacheGroups: {
svgAssets: svgAssetsObj
}
}
}
}
}
}
_isSvgRegExp(pattern) {
let regExCheck = new RegExp(pattern);
return regExCheck.test('.svg') || regExCheck.test('font.svg');
}
_isSvgVueRule(rule) {
if (rule.hasOwnProperty('include')) {
return rule.include.includes(this.includePath);
}
return false;
}
_convertSvgoOptions(options) {
let converted = [];
options.forEach(option => {
let settings = Object.keys(option);
settings.forEach(setting => {
converted.push({
name: setting,
active: option[setting]
});
});
});
return converted;
}
}
mix.extend('svgVue', new SvgVue());