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 | 'use strict';
|
33 |
|
34 | var path = require('path'),
|
35 | fs = require('fs'),
|
36 | _ = fis.util,
|
37 | icon = require('./iconfont.js');
|
38 |
|
39 | var iconfontTag = new RegExp('<!--ICONFONT_PLACEHOLDER-->');
|
40 |
|
41 | var fisVersion = fis.version.split('.')[0],
|
42 | isFis3 = fisVersion === 3;
|
43 |
|
44 |
|
45 |
|
46 | var DEF_CONF = {
|
47 | classPrefix: 'i-',
|
48 | svgPath: '../svgs',
|
49 | output: 'fonts/iconfont',
|
50 | cssInline: false,
|
51 | pseClass: 'before',
|
52 | base64: false,
|
53 | recursive: 0,
|
54 | ignoreSrc: []
|
55 | };
|
56 |
|
57 |
|
58 | function getIncludeAndExclude(conf) {
|
59 | var ret = {
|
60 | include: [],
|
61 | exclude: []
|
62 | };
|
63 |
|
64 | conf.forEach(function(glob) {
|
65 | if (/^!/.test(glob)) {
|
66 | ret.exclude.push(glob.replace(/^!/, ''));
|
67 | } else {
|
68 | ret.include.push(glob);
|
69 | }
|
70 | });
|
71 | return ret;
|
72 | }
|
73 |
|
74 |
|
75 | function uniqList(arr) {
|
76 | var ret = [],
|
77 | tmpl = {},
|
78 | item;
|
79 | for (var k = 0, len = arr.length; k < len; k++) {
|
80 | item = arr[k];
|
81 | if (item && !tmpl[item]) {
|
82 | tmpl[item] = 1;
|
83 | ret.push(item);
|
84 | }
|
85 | }
|
86 | return ret;
|
87 | }
|
88 |
|
89 |
|
90 |
|
91 |
|
92 | function getIconMatches(content, iconReg, cleanIconReg) {
|
93 | var matches = content.match(iconReg);
|
94 | if (matches) {
|
95 | for (var i = 0; i < matches.length; i++) {
|
96 | matches[i] = matches[i].replace(cleanIconReg, '');
|
97 | }
|
98 | }
|
99 | return matches || [];
|
100 | }
|
101 |
|
102 | function isMatchPath(source, parentPath, recursive) {
|
103 |
|
104 | if (!recursive) {
|
105 | return source === parentPath;
|
106 | }
|
107 |
|
108 |
|
109 | var index = source.lastIndexOf('/');
|
110 | if (index > -1) {
|
111 | if (source.substring(0, index) === parentPath) {
|
112 | return true;
|
113 | }
|
114 | }
|
115 | return false;
|
116 | }
|
117 |
|
118 |
|
119 | module.exports = function(ret, conf, settings, opt) {
|
120 |
|
121 | var files = ret.src,
|
122 | res = ret.map.res;
|
123 |
|
124 | settings = _.assign({}, DEF_CONF, settings);
|
125 |
|
126 | var projectPath = fis.project.getProjectPath(),
|
127 | iconPrefix = settings.classPrefix || 'i-',
|
128 | iconReg = new RegExp('icon-font\\\s' + iconPrefix + '([a-zA-Z0-9\\\-_]*)', 'mg'),
|
129 | cleanIconReg = new RegExp('icon-font\\\s' + iconPrefix, 'g');
|
130 |
|
131 |
|
132 |
|
133 | var configSvgPath = settings.svgPath ? path.join(projectPath, settings.svgPath) : '';
|
134 |
|
135 | var recursive = parseInt(settings.recursive, 10);
|
136 |
|
137 | settings._svgPath = configSvgPath;
|
138 |
|
139 |
|
140 | if (!fs.existsSync(configSvgPath)) {
|
141 | fis.log.error('目录 --' + configSvgPath + '-- 不存在, 请配置正确的 fis-postpackager-iconfont 插件的 "svgPath" 属性');
|
142 | }
|
143 |
|
144 |
|
145 | var ignoreLibList = settings.ignore || ['zepto', 'badjs', 'mod', 'bj-report', 'tools', 'db'];
|
146 |
|
147 | var pages = [],
|
148 | ext,
|
149 | content,
|
150 | eachFileIconList = [],
|
151 | allIconList = [];
|
152 |
|
153 | var inAndEx = getIncludeAndExclude(settings.ignoreSrc);
|
154 |
|
155 | _.map(files, function(subpath, file) {
|
156 | ext = _.ext(file.toString());
|
157 | content = file.getContent();
|
158 |
|
159 |
|
160 | if (settings.ignoreSrc.length > 0 && !_.filter(file.subpath, '**', inAndEx.exclude)) {
|
161 | return;
|
162 | }
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 | if (file.isHtmlLike || ext.ext === '.tpl') {
|
169 |
|
170 | eachFileIconList = getIconMatches(content, iconReg, cleanIconReg);
|
171 | if (eachFileIconList.length > 0) {
|
172 | allIconList = allIconList.concat(eachFileIconList);
|
173 | }
|
174 |
|
175 |
|
176 |
|
177 |
|
178 | if (isMatchPath(ext.dirname, projectPath, recursive)) {
|
179 | pages.push(file);
|
180 | }
|
181 |
|
182 |
|
183 |
|
184 | } else if (file.isJsLike && ~~ignoreLibList.indexOf(ext.filename)) {
|
185 |
|
186 |
|
187 | var matches = content.match(/addClass\([\'\"]([^\'\"\s]*\s)?i-([^\'\"\s]*)/mg);
|
188 | if (matches) {
|
189 |
|
190 |
|
191 |
|
192 | matches.forEach(function(match, i) {
|
193 | matches[i] = matches[i].replace(/addClass\([\'\"]([^\'\"\s]*\s)?i-/, '');
|
194 | });
|
195 | allIconList = allIconList.concat(matches);
|
196 | }
|
197 |
|
198 | }
|
199 | });
|
200 |
|
201 |
|
202 |
|
203 | |
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | allIconList = uniqList(allIconList);
|
210 |
|
211 | if (isFis3) {
|
212 | fis.emit('iconfont:allIcons', allIconList);
|
213 | }
|
214 |
|
215 | |
216 |
|
217 |
|
218 |
|
219 |
|
220 | var fontObj = icon.genarateFonts(settings, allIconList),
|
221 | outFontsContent = fontObj.content;
|
222 |
|
223 |
|
224 | if (!settings.base64) {
|
225 | settings._fontCdn = {};
|
226 |
|
227 |
|
228 | for (var type in outFontsContent) {
|
229 | if (outFontsContent.hasOwnProperty(type)) {
|
230 | var fontPkg = fis.file(projectPath, fontObj.subpath + '.' + type);
|
231 | fontPkg.setContent(outFontsContent[type]);
|
232 | if (!isFis3 && opt.dest === 'dev') {
|
233 |
|
234 | fontPkg.useHash = false;
|
235 | }
|
236 | ret.pkg[fontPkg.subpath] = fontPkg;
|
237 |
|
238 | settings._fontCdn[type] = fontPkg.getUrl(fontPkg.useHash, fontPkg.useDomain);
|
239 | }
|
240 | }
|
241 | }
|
242 |
|
243 |
|
244 | var cssContent;
|
245 | cssContent = settings.base64 ? icon.generateBase64Css(settings, allIconList, outFontsContent.ttf) : icon.generateCss(settings, allIconList);
|
246 |
|
247 | var cssFileHash = 'font.css';
|
248 |
|
249 | cssFileHash = path.join(settings.output, cssFileHash);
|
250 |
|
251 |
|
252 | var cssFile = fis.file(projectPath, cssFileHash);
|
253 | cssFile.setContent(cssContent);
|
254 | if (!isFis3 && opt.dest === 'dev') {
|
255 |
|
256 | cssFile.useHash = false;
|
257 | }
|
258 | ret.pkg[cssFile.subpath] = cssFile;
|
259 |
|
260 |
|
261 | var inlineCss = '<link rel="stylesheet" type="text/css" href="' + cssFile.getUrl(cssFile.useHash, cssFile.useDomain) + '" />\r\n';
|
262 |
|
263 | if (settings.cssInline) {
|
264 |
|
265 | inlineCss = '<style>\r\n' + cssContent + '\r\n</style>';
|
266 | }
|
267 |
|
268 |
|
269 | |
270 |
|
271 |
|
272 | pages.forEach(function(page) {
|
273 | var content = page.getContent();
|
274 | if (iconfontTag.test(content)) {
|
275 | content = content.replace(iconfontTag, inlineCss);
|
276 | } else {
|
277 | content = content.replace('</head>', '\t' + inlineCss + '$&');
|
278 | }
|
279 | page.setContent(content);
|
280 | });
|
281 | };
|