-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathIconsPlugin.js
121 lines (96 loc) · 3.79 KB
/
IconsPlugin.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
var path = require("path");
var crypto = require("crypto");
var Vinyl = require("vinyl");
var iconfont = require("gulp-iconfont");
var merge = require('merge');
var LoaderUtils = require("loader-utils");
var RawSource = require("webpack-core/lib/RawSource");
var ModuleAliasPlugin = require("enhanced-resolve/lib/ModuleAliasPlugin");
var NullFactory = require("webpack/lib/NullFactory");
var ConstDependency = require("webpack/lib/dependencies/ConstDependency");
function jsonDependency (objectFactory) {
return function (expr) {
var dep = new ConstDependency("(" + JSON.stringify(objectFactory()) + ")", expr.range);
dep.loc = expr.loc;
this.state.current.addDependency(dep);
return true;
};
}
function IconsPlugin (options) {
options = options || {};
this.options = merge.recursive(true, options, {
fontName: options.fontName || "icons",
filenameTemplate: options.filenameTemplate || {
name: "[name]-[hash].[ext]",
}
});
this.glyphs = [];
this.styles = {};
}
IconsPlugin.prototype.apply = function (compiler) {
var plugin = this;
compiler.resolvers.normal.apply(new ModuleAliasPlugin({
"icons-loader": path.join(__dirname, "template.js"),
}));
var cache = {};
compiler.plugin("compilation", function (compilation) {
var cacheInvalid = false;
compilation.dependencyFactories.set(ConstDependency, new NullFactory());
compilation.dependencyTemplates.set(ConstDependency, new ConstDependency.Template());
compilation.__iconsPlugin = { addIcon: function (content) {
cacheInvalid = true;
var id = "i" + crypto.createHash("sha1").update(new Buffer(content)).digest("hex");
cache[id] = content;
return id;
} };
compilation.plugin("optimize-tree", function (chunks, modules, callback) {
if ( !cacheInvalid ) {
return callback();
}
var stream = iconfont(plugin.options);
plugin.glyphs = [];
plugin.styles = {
fontName: plugin.options.fontName,
};
stream.on("data", function (vinyl) {
var assetType = path.extname(vinyl.path).substr(1);
var assetName = LoaderUtils.interpolateName({
resourcePath: vinyl.path,
}, plugin.options.filenameTemplate.name, {
content: vinyl.contents,
regExp: plugin.options.filenameTemplate.regExp,
});
plugin.styles[assetType] = assetName;
compilation.assets[assetName] = new RawSource(vinyl.contents);
});
stream.on('glyphs', function (_glyphs) {
plugin.glyphs = _glyphs;
});
stream.on("error", callback);
stream.on("end", function () {
var module = modules.filter(function (module) {
return module.rawRequest === "icons-loader";
})[0];
compilation.rebuildModule(module, callback);
});
Object.keys(cache).map(function (id) {
return new Vinyl({
path: id + ".svg",
contents: new Buffer(cache[id]),
});
}).forEach(function (vinyl) {
stream.write(vinyl);
});
process.nextTick(function () {
stream.end();
});
});
});
compiler.parser.plugin("expression __ICONS_PLUGIN_GLYPHS__", jsonDependency(function () {
return plugin.glyphs;
}));
compiler.parser.plugin("expression __ICONS_PLUGIN_STYLES__", jsonDependency(function () {
return plugin.styles;
}));
};
module.exports = IconsPlugin;