-
-
Notifications
You must be signed in to change notification settings - Fork 49
/
options-formatter.js
125 lines (109 loc) · 3.64 KB
/
options-formatter.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
125
const path = require('path');
const { merge } = require('webpack-merge');
// Helpers
const idify = require('./helpers/idify');
const validateOptions = require('./options-validator');
// Constants
const DEFAULT_PATTERN = '**/*.svg';
module.exports = (pattern, options) => {
// Make the first argument optional, just passing a configuration
// object should result in the default pattern being used
if ( typeof options === 'undefined' && (typeof pattern === 'object' && pattern !== null) ) {
options = pattern;
pattern = DEFAULT_PATTERN;
} else if ( typeof pattern === 'undefined' && typeof options === 'undefined' ) {
pattern = DEFAULT_PATTERN;
}
// Validate input
validateOptions(pattern, options);
// Format output (`null` values will be overwritten below the object initialization)
const output = merge({
input: {
options: {},
allowDuplicates: false
},
output: {
filename: 'spritemap.svg',
svg: {
sizes: null,
attributes: {}
},
chunk: {
name: 'spritemap',
keep: false
},
svg4everybody: false,
svgo: true
},
sprite: {
prefix: 'sprite-',
prefixStylesSelectors: false,
idify: idify,
gutter: 0,
generate: {
title: true,
symbol: true,
use: false,
view: false
}
},
styles: false
}, options || {}, {
input: {
patterns: typeof pattern === 'string' ? [pattern] : pattern
}
});
// [input.patterns] Normalize the patterns to make sure the correct slashes are used
output.input.patterns = output.input.patterns.map((pattern) => path.normalize(pattern));
// [output.svg.sizes] If no option is specified base this on the `sprite.generate.use` option
if ( output.output.svg.sizes === null ) {
output.output.svg.sizes = output.sprite.generate.use;
}
// [output.svgo] If enabled by by passing `true`, set it to an empty object
if ( output.output.svgo === true ) {
output.output.svgo = {
plugins: []
};
}
if ( output.output.svgo ) {
output.output.svgo = merge({
plugins: []
}, output.output.svgo);
}
// [output.svg4everybody] If enabled by by passing `true`, set it to an empty object
if ( output.output.svg4everybody === true ) {
output.output.svg4everybody = {};
}
// [sprite.prefix] If disabled by passing `false`, set it to an empty string
if ( output.sprite.prefix === false ) {
output.sprite.prefix = '';
}
// [sprite.idify] If disabled by passing `false`, set it to a function that returns the input
if ( output.sprite.idify === false ) {
output.sprite.idify = (value) => value;
}
// [sprite.gutter] Reformat `false` to 0
if ( output.sprite.gutter === false ) {
output.sprite.gutter = 0;
}
if ( output.styles ) {
if ( typeof output.styles === 'string' ) {
output.styles = {
filename: output.styles
};
} else if ( output.styles === true ) {
output.styles = {};
}
output.styles = merge({
filename: '~sprites.css',
format: 'data',
variables: {
sprites: 'sprites',
sizes: 'sizes',
variables: 'variables',
mixin: 'sprite'
}
}, output.styles);
}
return output;
};