-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
IconThemePlugin.js
40 lines (32 loc) · 1.21 KB
/
IconThemePlugin.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
const path = require('path');
class IconThemePlugin {
constructor(options = {}) {
this.metadataPath = options.metadataPath;
}
apply(compiler) {
const pluginName = IconThemePlugin.name;
const {webpack: {source: {RawSource}}} = compiler;
compiler.hooks.emit.tap(pluginName, (compilation) => {
const icons = compilation.getAssets().map(
(asset) => asset.name.split('/').pop()
);
const metadataFile = `${this.metadataPath || compiler.context}/metadata.json`;
const metadata = require(metadataFile);
const iconEntries = icons.map((file) => [
file.substr(0, file.lastIndexOf('.')),
file.substr(file.lastIndexOf('.') + 1, file.length)
]);
iconEntries.sort(([a], [b]) => a.localeCompare(b));
const imageTypes = ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp'];
metadata.icons = Object.fromEntries(
iconEntries.filter(
([name, ext]) => imageTypes.includes(ext)
)
);
const json = JSON.stringify(metadata, null, 2);
const relativePath = path.relative(compiler.outputPath, metadataFile);
compilation.emitAsset(relativePath, new RawSource(json));
});
}
}
module.exports = IconThemePlugin;