forked from pguimera/webfonts-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
209 lines (179 loc) · 6.01 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
var loaderUtils = require('loader-utils');
var webfontsGenerator = require('webfonts-generator');
var path = require('path');
var glob = require('glob');
var url = require('url');
var hashFiles = require('./utils').hashFiles;
var mimeTypes = {
'eot': 'application/vnd.ms-fontobject',
'svg': 'image/svg+xml',
'ttf': 'application/x-font-ttf',
'woff': 'application/font-woff',
'woff2': 'font/woff2'
};
function getFilesAndDeps (patterns, context) {
var files = [];
var filesDeps = [];
var directoryDeps = [];
function addFile (file) {
filesDeps.push(file);
files.push(path.resolve(context, file));
}
function addByGlob (globExp) {
var globOptions = {cwd: context};
var foundFiles = glob.sync(globExp, globOptions);
files = files.concat(foundFiles.map(file => {
return path.resolve(context, file);
}));
var globDirs = glob.sync(path.dirname(globExp) + '/', globOptions);
directoryDeps = directoryDeps.concat(globDirs.map(file => {
return path.resolve(context, file);
}));
}
// 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
}
};
}
// Futureproof webpack option parsing
function wpGetOptions (context) {
if (typeof context.query === 'string') return loaderUtils.getOptions(context);
return context.query;
}
module.exports = function (content) {
this.cacheable();
var webpackOptions = this.options || {}; // only makes sense in Webpack 1.x, or when LoaderOptionsPlugin is used
var options = wpGetOptions(this) || {};
var rawFontConfig;
try {
rawFontConfig = JSON.parse(content);
} catch (ex) {
rawFontConfig = this.exec(content, this.resourcePath);
}
var fontConfig = Object.assign({}, options, rawFontConfig);
var filesAndDeps = getFilesAndDeps(fontConfig.files, this.context);
filesAndDeps.dependencies.files.forEach(this.addDependency.bind(this));
filesAndDeps.dependencies.directories.forEach(this.addContextDependency.bind(this));
fontConfig.files = filesAndDeps.files;
// With everything set up, let's make an ACTUAL config.
var formats = fontConfig.types || ['eot', 'woff', 'woff2', 'ttf', 'svg'];
if (formats.constructor !== Array) {
formats = [formats];
}
var generatorOptions = {
files: fontConfig.files,
fontName: fontConfig.fontName,
types: formats,
order: formats,
fontHeight: fontConfig.fontHeight || 1000, // Fixes conversion issues with small svgs,
codepoints: fontConfig.codepoints || {},
templateOptions: {
baseSelector: fontConfig.baseSelector || '.icon',
classPrefix: 'classPrefix' in fontConfig ? fontConfig.classPrefix : 'icon-'
},
dest: fontConfig.dest || '',
html: fontConfig.html || false,
htmlDest: fontConfig.htmlDest || undefined,
writeFiles: fontConfig.writeFiles || false,
cssFontsUrl: fontConfig.cssFontsUrl || '',
embed: fontConfig.embed || false,
formatOptions: fontConfig.formatOptions || {}
};
// Add key only if it exists in config object to avoid fs errors
if ('htmlTemplate' in fontConfig) {
generatorOptions.htmlTemplate = fontConfig.htmlTemplate;
}
// This originally was in the object notation itself.
// Unfortunately that actually broke my editor's syntax-highlighting...
// ... what a shame.
if (typeof fontConfig.rename === 'function') {
generatorOptions.rename = fontConfig.rename;
} else {
generatorOptions.rename = function (f) {
return path.basename(f, '.svg');
};
}
if (fontConfig.cssTemplate) {
generatorOptions.cssTemplate = path.resolve(this.context, fontConfig.cssTemplate);
}
if (fontConfig.cssFontsPath) {
generatorOptions.cssFontsPath = path.resolve(this.context, fontConfig.cssFontsPath);
}
if (fontConfig.htmlTemplate) {
generatorOptions.htmlTemplate = path.resolve(this.context, fontConfig.htmlTemplate);
}
if (fontConfig.htmlDest) {
generatorOptions.htmlDest = path.resolve(this.context, fontConfig.htmlDest);
}
if (fontConfig.dest) {
generatorOptions.dest = path.resolve(this.context, fontConfig.dest);
}
// svgicons2svgfont stuff
var keys = [
'fixedWidth',
'centerHorizontally',
'normalize',
'fontHeight',
'round',
'descent'
];
for (var x in keys) {
if (typeof fontConfig[keys[x]] !== 'undefined') {
generatorOptions[keys[x]] = fontConfig[keys[x]];
}
}
var cb = this.async();
var publicPath = options.publicPath || (webpackOptions.output && webpackOptions.output.publicPath) || '/';
var embed = !!generatorOptions.embed;
if (generatorOptions.cssTemplate) {
this.addDependency(generatorOptions.cssTemplate);
}
if (generatorOptions.cssFontsPath) {
this.addDependency(generatorOptions.cssFontsPath);
}
webfontsGenerator(generatorOptions, (err, res) => {
if (err) {
return cb(err);
}
var urls = {};
for (var i in formats) {
var format = formats[i];
var filename = fontConfig.fileName || options.fileName || '[chunkhash]-[fontname].[ext]';
var chunkHash = filename.indexOf('[chunkhash]') !== -1
? hashFiles(generatorOptions.files, options.hashLength)
: '';
filename = filename
.replace('[chunkhash]', chunkHash)
.replace('[fontname]', generatorOptions.fontName)
.replace('[ext]', format);
if (!embed) {
var formatFilename = loaderUtils.interpolateName(this,
filename,
{
context: this.rootContext || this.options.context || this.context,
content: res[format]
}
);
urls[format] = url.resolve(publicPath, formatFilename.replace(/\\/g, '/'));
this.emitFile(formatFilename, res[format]);
} else {
urls[format] = 'data:' +
mimeTypes[format] +
';charset=utf-8;base64,' +
(Buffer.from(res[format]).toString('base64'));
}
}
cb(null, res.generateCss(urls));
});
};