forked from DragonsInn/fontgen-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
237 lines (209 loc) · 6.98 KB
/
index.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
var loaderUtils = require("loader-utils");
var fontgen = require("webfonts-generator");
var path = require("path");
var glob = require('glob');
var assign = require('object-assign');
var handlebars = require('handlebars');
var fs = require('fs');
var mimeTypes = {
'eot': 'application/vnd.ms-fontobject',
'svg': 'image/svg+xml',
'ttf': 'application/x-font-ttf',
'woff': 'application/font-woff'
};
function absolute(from, to) {
if (arguments.length < 2) {
return function (to) {
return path.resolve(from, to);
};
}
return path.resolve(from, to);
}
function getFilesAndDeps(patterns, context) {
var files = [];
var filesDeps = [];
var directoryDeps = [];
function addFile(file) {
filesDeps.push(file);
files.push(absolute(context, file));
}
function addByGlob(globExp) {
var globOptions = {cwd: context};
var foundFiles = glob.sync(globExp, globOptions);
files = files.concat(foundFiles.map(absolute(context)));
var globDirs = glob.sync(path.dirname(globExp) + '/', globOptions);
directoryDeps = directoryDeps.concat(globDirs.map(absolute(context)));
}
// Re-work the files array.
patterns.forEach(function (pattern) {
if (glob.hasMagic(pattern)) {
addByGlob(pattern);
}
else {
addFile(pattern);
}
});
return {
files: files,
dependencies: {
directories: directoryDeps,
files: filesDeps
}
};
}
module.exports = function (content) {
this.cacheable();
var params = loaderUtils.parseQuery(this.query);
var config;
try {
config = JSON.parse(content);
}
catch (ex) {
config = this.exec(content, this.resourcePath);
}
config.__dirname = path.dirname(this.resourcePath);
// Sanity check
/*
if(typeof config.fontName != "string" || typeof config.files != "array") {
this.reportError("Typemismatch in your config. Verify your config for correct types.");
return false;
}
*/
var filesAndDeps = getFilesAndDeps(config.files, this.context);
filesAndDeps.dependencies.files.forEach(this.addDependency.bind(this));
filesAndDeps.dependencies.directories.forEach(this.addContextDependency.bind(this));
config.files = filesAndDeps.files;
// With everything set up, let's make an ACTUAL config.
var formats = params.types || config.types || ['eot', 'woff', 'ttf', 'svg'];
if (formats.constructor !== Array) {
formats = [formats];
}
var fontconf = {
files: config.files,
fontName: config.fontName,
types: formats,
order: formats,
fontHeight: config.fontHeight || 1000, // Fixes conversion issues with small svgs
templateOptions: {
baseClass: config.baseClass || "icon",
classPrefix: "classPrefix" in config ? config.classPrefix : "icon-"
},
dest: "",
writeFiles: false,
formatOptions: config.formatOptions || {}
};
// This originally was in the object notation itself.
// Unfortunately that actually broke my editor's syntax-highlighting...
// ... what a shame.
if(typeof config.rename == "function") {
fontconf.rename = config.rename;
} else {
fontconf.rename = function(f) {
return path.basename(f, ".svg");
}
}
if (config.cssTemplate) {
fontconf.cssTemplate = absolute(this.context, config.cssTemplate);
}
for(var option in config.templateOptions) {
if(config.templateOptions.hasOwnProperty(option)) {
fontconf.templateOptions[option] = config.templateOptions[option];
}
}
// svgicons2svgfont stuff
var keys = [
"fixedWidth",
"centerHorizontally",
"normalize",
"fontHeight",
"round",
"descent"
];
for (var x in keys) {
if (typeof config[keys[x]] != "undefined") {
fontconf[keys[x]] = config[keys[x]];
}
}
var cb = this.async();
var self = this;
var opts = this.options;
var pub = (
opts.output.publicPath || "/"
);
var embed = !!params.embed;
var html = !!params.html || config.html;
var exportModule = config.exportModule;
if (fontconf.cssTemplate) {
this.addDependency(fontconf.cssTemplate)
}
fontgen(fontconf, function (err, res) {
if (err) {
return cb(err);
}
var urls = {};
var hasSvg = !!~formats.indexOf('svg');
var names = fontconf.files.map(fontconf.rename);
var reFontName = /\[fontname]/gi;
var reExt = /\[ext]/gi;
for (var i in formats) {
var format = formats[i];
var url;
if (!embed) {
var filename = config.fileName || params.fileName || "[hash]-[fontname][ext]";
filename = filename
.replace(reFontName, fontconf.fontName)
.replace(reExt, format);
if (hasSvg) {
url = loaderUtils.interpolateName(this,
filename,
{
context: self.options.context || this.context,
content: res['svg']
}
);
} else {
url = loaderUtils.interpolateName(this,
filename,
{
context: self.options.context || this.context,
content: res[format]
}
);
}
urls[format] = (pub + url).replace(/\\/g, '/');
self.emitFile(url, res[format]);
} else {
urls[format] = 'data:'
+ mimeTypes[format]
+ ';charset=utf-8;base64,'
+ (new Buffer(res[format]).toString('base64'));
}
}
var styles = res.generateCss(urls);
if (html) {
var source = fs.readFileSync(fontgen.templates.html, 'utf8');
var template = handlebars.compile(source);
var ctx = assign({
names: names,
fontName: fontconf.fontName,
styles: styles
}, fontconf.templateOptions);
var content = template(ctx);
var htmlFileName = config.htmlFileName
.replace(reFontName, fontconf.fontName)
.replace(reExt, format);
var htmlFileUrl = loaderUtils.interpolateName(this,
htmlFileName,
{
context: self.options.context || this.context,
content: content
}
);
self.emitFile(htmlFileUrl, content);
}
if (exportModule && typeof exportModule === 'function') {
exportModule(names);
}
cb(null, styles);
});
};