forked from jeerbl/webfonts-loader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emit-codepoints.js
60 lines (55 loc) · 2.22 KB
/
emit-codepoints.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
var loaderUtils = require('loader-utils');
var hashFiles = require('./utils').hashFiles;
module.exports = {
createArrayCodepointFiles (codepointFiles, elem) {
const defaultElem = { fileName: '[fontname].codepoints.js', type: 'web' };
if (typeof (elem) === 'boolean') {
codepointFiles.push(Object.assign({}, defaultElem));
} else if (typeof (elem) === 'string') {
codepointFiles.push(Object.assign({}, defaultElem, { fileName: elem }));
} else if (Array.isArray(elem)) {
elem.forEach(e => this.createArrayCodepointFiles(codepointFiles, e));
} else if (typeof (elem) === 'object') {
codepointFiles.push(Object.assign({}, defaultElem, elem));
}
},
emitFiles (loaderContext, emitCodepointsOptions, generatorOptions, options) {
var codepointFiles = [];
this.createArrayCodepointFiles(codepointFiles, emitCodepointsOptions);
codepointFiles.forEach(emitOption => {
var codepointsContent = JSON.stringify(generatorOptions.codepoints);
switch (emitOption.type) {
case 'commonjs': {
codepointsContent = 'module.exports = ' + codepointsContent + ';';
break;
}
case 'web': {
codepointsContent = [
'if (typeof webfontIconCodepoints === \'undefined\') {',
' webfontIconCodepoints = {};',
'}',
'webfontIconCodepoints[' + JSON.stringify(generatorOptions.fontName) + '] = ' + codepointsContent + ';'
].join('\n');
break;
}
case 'json': {
break;
}
}
var codepointsFilename = emitOption.fileName;
var chunkHash = codepointsFilename.indexOf('[chunkhash]') !== -1
? hashFiles(generatorOptions.files, options.hashLength) : '';
codepointsFilename = codepointsFilename
.replace('[chunkhash]', chunkHash)
.replace('[fontname]', generatorOptions.fontName);
codepointsFilename = loaderUtils.interpolateName(loaderContext,
codepointsFilename,
{
context: loaderContext.rootContext || loaderContext.options.context || loaderContext.context,
content: codepointsContent
}
);
loaderContext.emitFile(codepointsFilename, codepointsContent);
});
}
};