-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathwebpack-plugin.js
57 lines (43 loc) · 1.38 KB
/
webpack-plugin.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
const fs = require('fs');
const path = require('path');
const webpack = require('webpack');
const getContent = function (...paths) {
return paths.flat(2).reduce((contents, fileName) => {
return contents.concat(fs.readFileSync(fileName, 'utf8'));
}, '');
};
const isResourceFaIcon = function (resource) {
return (new RegExp('(.*)(/fa)(.*)(.js)$')).test(resource);
}
const isNotResourceFaIcon = function (resource) {
return !isResourceFaIcon(resource);
}
const getCheckResource = function (paths) {
const content = getContent(paths);
function contentContains(str) {
return content.indexOf(str) !== -1;
}
return function (resource, dir) {
const doIgnore = true;
const doNotIgnore = false;
if (isNotResourceFaIcon(resource)) {
return doNotIgnore;
}
const resourcePath = path.join(dir, resource);
const icon = require(resourcePath);
if (contentContains(`${icon.prefix} fa-${icon.iconName}`)) {
return doNotIgnore;
}
if (contentContains('\\' + icon.unicode)) {
return doNotIgnore;
}
return doIgnore;
}
}
class PurgeFontawesomePlugin extends webpack.IgnorePlugin {
constructor(options) {
const { paths } = options;
super({ checkResource: getCheckResource(paths) });
}
}
module.exports = PurgeFontawesomePlugin;