-
Notifications
You must be signed in to change notification settings - Fork 81
/
index.js
151 lines (126 loc) · 3.36 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
'use strict';
var path = require('path'),
Vinyl = require('vinyl'),
PluginError = require('plugin-error'),
consolidate = require('consolidate'),
_ = require('lodash'),
Stream = require('stream');
var PLUGIN_NAME = 'gulp-iconfont-css';
function iconfontCSS(config) {
var glyphMap = [],
currentGlyph,
currentCodePoint,
inputFilePrefix,
stream,
outputFile,
engine,
cssClass;
// Set default values
config = _.merge({
path: 'css',
targetPath: '_icons.css',
fontPath: './',
engine: 'lodash',
firstGlyph: 0xE001,
fixedCodepoints: false,
cssClass: 'icon',
aliases: {},
cacheBuster: ''
}, config);
// Enable default stylesheet generators
if(!config.path) {
config.path = 'scss';
}
if(/^(scss|sass|less|css)$/i.test(config.path)) {
config.path = __dirname + '/templates/_icons.' + config.path;
}
// Validate config
if (!config.fontName) {
throw new PluginError(PLUGIN_NAME, 'Missing option "fontName"');
}
if (!consolidate[config.engine]) {
throw new PluginError(PLUGIN_NAME, 'Consolidate missing template engine "' + config.engine + '"');
}
try {
engine = require(config.engine);
} catch(e) {
throw new PluginError(PLUGIN_NAME, 'Template engine "' + config.engine + '" not present');
}
// Define starting point
currentGlyph = config.firstGlyph;
// Happy streaming
stream = Stream.PassThrough({
objectMode: true
});
stream._transform = function(file, unused, cb) {
var fileName;
if (file.isNull()) {
this.push(file);
return cb();
}
// Create output file
if (!outputFile) {
outputFile = new Vinyl({
base: file.base,
cwd: file.cwd,
path: path.join(file.base, config.targetPath),
contents: file.isBuffer() ? Buffer.alloc(0) : new Stream.PassThrough()
});
}
fileName = path.basename(file.path, '.svg');
if (config.fixedCodepoints && config.fixedCodepoints[fileName]) {
currentCodePoint = config.fixedCodepoints[fileName].toString(16).toUpperCase();
} else {
currentCodePoint = (currentGlyph++).toString(16).toUpperCase();
}
// Add glyph
glyphMap.push({
fileName: fileName,
codePoint: currentCodePoint
});
if (config.aliases[fileName]) {
_.each(config.aliases[fileName], function(_alias) {
glyphMap.push({
fileName: _alias,
codePoint: currentCodePoint,
originalFileName: fileName // used for less and scss
});
})
}
// Prepend codePoint to input file path for gulp-iconfont
inputFilePrefix = 'u' + currentCodePoint + '-';
file.path = path.dirname(file.path) + '/' + inputFilePrefix + path.basename(file.path);
this.push(file);
cb();
};
stream._flush = function(cb) {
var content;
if (glyphMap.length) {
consolidate[config.engine](config.path, {
glyphs: glyphMap,
fontName: config.fontName,
fontPath: config.fontPath,
cssClass: config.cssClass,
cacheBuster: config.cacheBuster,
cacheBusterQueryString: config.cacheBuster ? '?' + config.cacheBuster : ''
}, function(err, html) {
if (err) {
throw new PluginError(PLUGIN_NAME, 'Error in template: ' + err.message);
}
content = Buffer.from(html);
if (outputFile.isBuffer()) {
outputFile.contents = content;
} else {
outputFile.contents.write(content);
outputFile.contents.end();
}
stream.push(outputFile);
cb();
});
} else {
cb();
}
};
return stream;
};
module.exports = iconfontCSS;