-
Notifications
You must be signed in to change notification settings - Fork 24
/
SvgStorePlugin.js
272 lines (221 loc) · 8.8 KB
/
SvgStorePlugin.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
'use strict';
const Chunk = require('webpack/lib/Chunk');
const MissingDimensionsException = require('./exceptions/MissingDimensionsException');
const { DEFAULT_LOADER_OPTIONS } = require('./loader');
const SvgSprite = require('./SvgSprite');
/**
* @property {boolean} emit - determines if sprites must be emitted.
* @property {string} filename
* @property {{ startX: number, startY: number, deltaX: number, deltaY: number, iconHeight: number, rowWidth: number }} sprite - positioning and sizing options for the symbols in a sprite.
* @type {Readonly<{emit: boolean, filename: string, sprite: Readonly<{}>}>}
*/
const DEFAULT_PLUGIN_OPTIONS = Object.freeze({
emit: true,
sprite: Object.freeze({}),
});
/**
* SVG Store Plugin
* - Manages all sprites data
* - Generates the sprites during optimization phase
* - Plugin for webpack
*/
class SvgStorePlugin {
/**
* Initializes options.
* @param {Object} options
*/
constructor(options) {
/** @member {Object} */
this.options = Object.assign({}, DEFAULT_PLUGIN_OPTIONS, options);
/** @member {SvgSprite[]} */
this.sprites = [];
}
/**
* Injects a sprite for each loader in the configuration so that they can add icons to a sprite.
* Attaches the compilation process to the current compilation.
* @param {webpack.Compiler} compiler
*/
apply(compiler) {
/** @type {{ thisCompilation: Tapable }} */
const { hooks } = compiler;
const { rules } = compiler.options.module;
// Iterates through the given list of rules and injects a sprite for each rule that uses our loader.
this.injectSpritesIntoRules(rules);
hooks.thisCompilation.tap(this.constructor.name, this.exec.bind(this));
}
/**
* Attaches the several compilation steps to their respective hook,
* so that everything is done in the right order.
* - Generates every registered sprite during optimization phase.
* - Replaces the sprite URL with the hashed URL during chunks optimization phase.
* - Adds the sprites to the compilation assets during the additional assets phase.
* @param {webpack.Compilation} compilation
*/
exec(compilation) {
const { options } = this;
/** @type {{ optimize: Tapable, optimizeModules: Tapable, additionalAssets: Tapable }} */
const { hooks } = compilation;
// Generate sprites during the optimization phase
hooks.optimize.tap(this.constructor.name, this.generateSprites.bind(this, compilation));
// Replace the sprites URL with the hashed URL during the chunks optimization phase
hooks.optimizeChunks.tap(this.constructor.name, this.fixSpritePathsInChunks.bind(this));
// Add sprites to the compilation assets
if (options.emit) {
hooks.additionalAssets.tapAsync(this.constructor.name, this.registerSprites.bind(this, compilation));
}
}
/**
* Looks for sprite URLs in the modules in the given chunks and replaces them with the URL containing the sprite hash.
* @param {Chunk[]} chunks
*/
fixSpritePathsInChunks(chunks) {
const spritesWithInterpolatedName = this.getSpritesWithInterpolateName();
for (const sprite of spritesWithInterpolatedName) {
for (const chunk of chunks) {
for (const module of chunk.modulesIterable) {
this.replaceSpritePathsInModuleWithInterpolatedPaths(module, sprite);
}
}
}
}
/**
* Generates the content for every sprite.
*/
generateSprites(compilation) {
const { options, sprites } = this;
for (const sprite of sprites) {
try {
sprite.generate(options.sprite);
} catch (error) {
if (error instanceof MissingDimensionsException) {
compilation.warnings.push(error);
} else {
throw error;
}
}
}
}
/**
* Gets sprites which name have an hash.
* @returns {SvgSprite[]}
*/
getSpritesWithInterpolateName() {
const { sprites } = this;
const spritesWithInterpolatedName = [];
for (const sprite of sprites) {
const { originalResourcePath, resourcePath } = sprite;
if (originalResourcePath !== resourcePath) {
spritesWithInterpolatedName.push(sprite);
}
}
return spritesWithInterpolatedName;
}
/**
* Injects a sprite into the given rule options so that the loader can add icons to the sprite.
* @param {Object} rule
*/
injectSpriteIntoRule(rule) {
const { sprites } = this;
if (typeof rule.options !== 'object') {
rule.options = {};
}
// Get the sprite resource path either from the rule options or the default options
const resourcePath = rule.options.name || DEFAULT_LOADER_OPTIONS.name;
// Initialize the sprite
const sprite = new SvgSprite(resourcePath);
// Inject sprite into loader options
rule.options.sprite = sprite;
// Add sprite to the list of sprites
sprites.push(sprite);
}
/**
* Iterates through the given list of rules and injects a sprite for each rule that uses our loader.
* @param {Object[]} rules
* @see https://webpack.js.org/configuration/module/#rule-loader
* @see https://webpack.js.org/configuration/module/#rule-oneof
* @see https://webpack.js.org/configuration/module/#rule-rules
* @see https://webpack.js.org/configuration/module/#rule-use
*/
injectSpritesIntoRules(rules) {
for (const rule of rules) {
const { oneOf: oneOfRules, rules: subRules, use: ruleUse } = rule;
const loaders = ruleUse || [rule];
for (const subRule of loaders) {
if (subRule.loader === SvgStorePlugin.loader) {
this.injectSpriteIntoRule(subRule);
}
}
if (subRules) {
this.injectSpritesIntoRules(subRules);
}
if (oneOfRules) {
this.injectSpritesIntoRules(oneOfRules);
}
}
}
/**
* Replaces the given sprite URL with the hashed URL in the given module.
* @param {Module} module - the module where the URL needs to be replaced.
* @param {SvgSprite} sprite - the sprite for the module.
*/
replaceSpritePathsInModuleWithInterpolatedPaths(module, sprite) {
switch (module.constructor.name) {
case 'CssModule':
module.content = sprite.replacePathsWithInterpolatedPaths(module.content);
break;
case 'NormalModule': {
// Skip it if it wasn't changed
if (!sprite.changed) {
return;
}
const source = module._source;
if (typeof source === 'string') {
module._source = sprite.replacePathsWithInterpolatedPaths(source);
} else if (typeof source === 'object') {
if (typeof source._name === 'string') {
source._name = sprite.replacePathsWithInterpolatedPaths(source._name);
}
if (typeof source._value === 'string') {
source._value = sprite.replacePathsWithInterpolatedPaths(source._value);
}
}
break;
}
}
}
/**
* Registers the sprites so that they are part of the final output.
* @param compilation
* @param {Function} callback
*/
registerSprites(compilation, callback) {
const { sprites } = this;
for (const sprite of sprites) {
const { changed, content, name, resourcePath } = sprite;
// If the sprite wasn't changed since the last compilation
// then skip this step because the assets were already generated before
if (!changed) {
continue;
}
// Create a chunk for the sprite
const chunk = new Chunk(name);
chunk.ids = [];
chunk.files.push(resourcePath);
// Add the sprite to the compilation assets
compilation.assets[resourcePath] = {
source() {
return content;
},
size() {
return content.length;
},
};
// Add chunk to the compilation
// NOTE: This step is only to allow other plugins to detect the existence of this asset
compilation.chunks.push(chunk);
}
callback();
}
}
SvgStorePlugin.loader = require.resolve('./loader');
module.exports = SvgStorePlugin;