This repository has been archived by the owner on Mar 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
95 lines (84 loc) · 2.32 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
const path = require('path')
const SpriteLoaderPlugin = require('svg-sprite-loader/plugin')
function excludePaths(config, paths) {
const {exclude} = config.module.rule('svg')
for (const path of paths) {
exclude.add(path)
}
}
function register(config, options, isProduction) {
excludePaths(config, options.include)
const rule = config.module.rule('svg-sprite')
const {include} = rule.test(/\.(svg)(\?.*)?$/)
for (const path of options.include) {
include.add(path)
}
rule
.use('svg-sprite-loader')
.loader('svg-sprite-loader')
.options(options.svgSpriteOptions)
.end()
if (isProduction) {
rule
.use('svgo-loader')
.loader('svgo-loader')
.options(options.svgoOptions)
.end()
}
config
.plugin('svg-sprite')
.use(SpriteLoaderPlugin, [options.svgSpritePluginOptions])
// Config.plugins.delete('module-concatenation')
}
/**
* Add svg-sprite support in Poi.
* @name svgSpritePreset
* @param {Object} options
* @param {String|String[]} [options.include] - Specific directory for svg files
*
* @param {Object} [options.svgSpriteOptions={
* extract: true
* }] - svg-sprite-loader options
* See {@link https://github.com/kisenka/svg-sprite-loader#configuration svg-sprite-loader options}
*
* @param {Object} [options.svgSpritePluginOptions={}] - svg-sprite-loader-plugin options
* See {@link https://github.com/kisenka/svg-sprite-loader#plain-sprite svg-sprite-loader-plugin options}
*
* @param {Object} [options.svgoOptions={
* plugins: []
* }] - svgo-loader options
* See {@link https://github.com/rpominov/svgo-loader#usage svgo-loader options}
*
*/
exports.apply = (api, {
include,
svgSpriteOptions = {
extract: true,
},
svgSpritePluginOptions = {},
svgoOptions = {
plugins: [],
},
}) => {
if (!include) {
throw new Error('Missing required parameter: include')
}
if (typeof include === 'string') {
include = [include]
}
if (!Array.isArray(include)) {
throw new TypeError(`Parameter include type(${typeof include}) error`)
}
include = include.map(inc => {
return path.resolve(process.cwd(), inc)
})
const options = {
include,
svgSpriteOptions,
svgSpritePluginOptions,
svgoOptions,
}
api.hook('createWebpackChain', config => {
register(config, options, api.mode === 'production')
})
}