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 | function uniqList(arr) {
|
46 | var ret = [],
|
47 | tmpl = {},
|
48 | item;
|
49 | for(var k=0,len=arr.length; k<len; k++){
|
50 | item = arr[k];
|
51 | if(!tmpl[item]){
|
52 | tmpl[item] = 1;
|
53 | ret.push(item);
|
54 | }
|
55 | }
|
56 | return ret;
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 | function getIconMatches (content, iconReg, cleanIconReg) {
|
63 | var matches = content.match(iconReg);
|
64 | if(matches){
|
65 | for(var i=0; i<matches.length; i++){
|
66 | matches[i] = matches[i].replace(cleanIconReg, '');
|
67 | }
|
68 | }
|
69 | return matches || [];
|
70 | }
|
71 |
|
72 |
|
73 |
|
74 | module.exports = function (ret, conf, settings, opt) {
|
75 |
|
76 | var files = ret.src,
|
77 | res = ret.map.res;
|
78 |
|
79 | var projectPath = fis.project.getProjectPath(),
|
80 | iconPrefix = settings.classPrefix || 'i-',
|
81 | iconReg = new RegExp('icon-font\\\s' + iconPrefix + '([a-zA-Z0-9\\\-_]*)', 'mg'),
|
82 | cleanIconReg = new RegExp('icon-font\\\s' + iconPrefix, 'g');
|
83 |
|
84 |
|
85 |
|
86 | var configSvgPath = settings.svgPath ? path.join(projectPath, settings.svgPath) : '';
|
87 |
|
88 | settings._svgPath = configSvgPath;
|
89 |
|
90 |
|
91 | if(!fs.existsSync(configSvgPath)) {
|
92 | fis.log.error('目录 --' + configSvgPath + '-- 不存在, 请配置正确的 fis-postpackager-iconfont 插件的 "svgPath" 属性');
|
93 | }
|
94 |
|
95 |
|
96 | var ignoreLibList = settings.ignore || ['zepto', 'badjs', 'mod', 'bj-report', 'tools', 'db'];
|
97 |
|
98 | var pages = [],
|
99 | ext,
|
100 | content,
|
101 | eachFileIconList = [],
|
102 | allIconList = [];
|
103 |
|
104 | _.map(files, function(subpath, file) {
|
105 | ext = _.ext(file.toString());
|
106 | content = file.getContent();
|
107 |
|
108 |
|
109 |
|
110 | if(file.isHtmlLike || ext.ext === '.tpl') {
|
111 |
|
112 | eachFileIconList = getIconMatches(content, iconReg, cleanIconReg);
|
113 | if(eachFileIconList.length > 0) {
|
114 | allIconList = allIconList.concat(eachFileIconList);
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 | if(ext.dirname === projectPath ) {
|
120 | pages.push(file);
|
121 | }
|
122 | } else if(file.isJsLike && ~~ignoreLibList.indexOf(ext.filename)) {
|
123 |
|
124 |
|
125 | var matches = content.match(/addClass\([\'\"]([^\'\"\s]*\s)?i-([^\'\"\s]*)/mg);
|
126 | if(matches){
|
127 |
|
128 |
|
129 |
|
130 | matches.forEach(function(match, i) {
|
131 | matches[i] = matches[i].replace(/addClass\([\'\"]([^\'\"\s]*\s)?i-/, '');
|
132 | });
|
133 | allIconList = allIconList.concat(matches);
|
134 | }
|
135 |
|
136 | }
|
137 | });
|
138 |
|
139 |
|
140 |
|
141 | |
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 | allIconList = uniqList(allIconList);
|
148 |
|
149 | if(isFis3) {
|
150 | fis.emit('iconfont:allIcons', allIconList);
|
151 | }
|
152 |
|
153 | |
154 |
|
155 |
|
156 |
|
157 |
|
158 | var fontObj = icon.genarateFonts(settings, allIconList),
|
159 | outFontsContent = fontObj.content;
|
160 |
|
161 | settings._fontCdn = {};
|
162 |
|
163 |
|
164 | for(var type in outFontsContent){
|
165 | if(outFontsContent.hasOwnProperty(type)) {
|
166 | var fontPkg = fis.file(projectPath, fontObj.subpath + '.' + type);
|
167 | fontPkg.setContent(outFontsContent[type]);
|
168 | if(!isFis3 && opt.dest === 'dev') {
|
169 |
|
170 | fontPkg.useHash = false;
|
171 | }
|
172 | ret.pkg[fontPkg.subpath] = fontPkg;
|
173 |
|
174 | settings._fontCdn[type] = fontPkg.getUrl(fontPkg.useHash, fontPkg.useDomain);
|
175 | }
|
176 | }
|
177 |
|
178 | var cssContent = icon.generateCss(settings, allIconList);
|
179 |
|
180 | var cssFileHash = 'font.css';
|
181 |
|
182 | cssFileHash = path.join(settings.output, cssFileHash);
|
183 |
|
184 |
|
185 | var cssFile = fis.file(projectPath, cssFileHash);
|
186 | cssFile.setContent(cssContent);
|
187 | if(!isFis3 && opt.dest === 'dev') {
|
188 |
|
189 | cssFile.useHash = false;
|
190 | }
|
191 | ret.pkg[cssFile.subpath] = cssFile;
|
192 |
|
193 |
|
194 | var inlineCss = '<link rel="stylesheet" type="text/css" href="' + cssFile.getUrl(cssFile.useHash, cssFile.useDomain) + '" />\r\n';
|
195 |
|
196 | if(settings.cssInline) {
|
197 |
|
198 | inlineCss = '<style>\r\n' + cssContent + '\r\n</style>';
|
199 | }
|
200 |
|
201 |
|
202 | |
203 |
|
204 |
|
205 | pages.forEach(function(page) {
|
206 | var content = page.getContent();
|
207 | if(iconfontTag.test(content)) {
|
208 | content = content.replace(iconfontTag, inlineCss);
|
209 | } else {
|
210 | content = content.replace('</head>', '\t' + inlineCss + '$&');
|
211 | }
|
212 | page.setContent(content);
|
213 | });
|
214 | };
|