1 |
|
2 | "use strict";
|
3 |
|
4 | const path = require('path');
|
5 | const os = require('os');
|
6 | const fs = require('fs');
|
7 | const logger = require('jdf-log');
|
8 | const cheerio = require('cheerio');
|
9 |
|
10 |
|
11 | const jdfUtils = require('jdf-utils');
|
12 | const $ = jdfUtils.base;
|
13 | const f = jdfUtils.file;
|
14 | const jdf = require('./jdf');
|
15 | const VFS = require('./VFS/VirtualFileSystem');
|
16 | const widgetParser = require('./buildWidget');
|
17 | const buildHTML = require('./buildHTML');
|
18 | const jsAst = require('./jsAst');
|
19 | const urlReplace = require('./urlReplace');
|
20 |
|
21 |
|
22 | let bow = module.exports = {};
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | bow.init = function () {
|
31 | let globalOutputNameIsInvoked = false;
|
32 | return VFS.go()
|
33 | .then(() => {
|
34 | return bow.initGlobalOutputName();
|
35 | })
|
36 | .then(() => {
|
37 | return VFS.travel((vfile, done) => {
|
38 |
|
39 | if (!$.is.html(vfile.originPath)) {
|
40 | return;
|
41 | }
|
42 |
|
43 | logger.verbose(`parse widgetOutputName:${vfile.originPath}`);
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 | let outputInfo = widgetParser.parseOutputName(vfile.targetContent);
|
53 |
|
54 |
|
55 | if (!outputInfo.name) {
|
56 | return;
|
57 | }
|
58 |
|
59 |
|
60 | if (outputInfo.name !== jdf.config.widgetOutputName) {
|
61 |
|
62 | let contents = this.concatOutputContent(outputInfo, vfile);
|
63 |
|
64 |
|
65 | this.addToVFS(outputInfo, contents);
|
66 | } else {
|
67 | globalOutputNameIsInvoked = true;
|
68 | }
|
69 |
|
70 |
|
71 |
|
72 | this.delWidgetTagInHTML(outputInfo, vfile);
|
73 |
|
74 |
|
75 | let jsPath = path.join(VFS.originDir, jdf.config.jsDir, outputInfo.name + '.js');
|
76 | let cssPath = path.join(VFS.originDir, jdf.config.cssDir, outputInfo.name + '.css');
|
77 | vfile.targetContent = vfile.targetContent.replace(outputInfo.text, '');
|
78 | if (outputInfo.concatTag.js) {
|
79 | let jsVfile = VFS.queryFile(jsPath);
|
80 | buildHTML.insertJS(vfile, jsVfile, jdf.config.build.jsPlace);
|
81 | }
|
82 | if (outputInfo.concatTag.css) {
|
83 | let cssVfile = VFS.queryFile(cssPath);
|
84 | buildHTML.insertCSS(vfile, cssVfile);
|
85 | }
|
86 | });
|
87 | })
|
88 | .then(() => {
|
89 |
|
90 | if (!globalOutputNameIsInvoked) {
|
91 | let jsPath = path.join(jdf.outputDir, jdf.config.jsDir, jdf.config.widgetOutputName + '.js');
|
92 | let cssPath = path.join(jdf.outputDir, jdf.config.cssDir, jdf.config.widgetOutputName + '.css');
|
93 | VFS.deleteFile(jsPath, 'target');
|
94 | VFS.deleteFile(cssPath, 'target');
|
95 | }
|
96 |
|
97 | })
|
98 | .catch((err) => {
|
99 | if (err && err.message) {
|
100 | logger.error(err.message);
|
101 | }
|
102 | });
|
103 | }
|
104 |
|
105 | bow.initGlobalOutputName = function () {
|
106 |
|
107 | let mode = jdf.config.widgetOutputMode;
|
108 | let widgetList = [],
|
109 | targetList = [];
|
110 | let widgetPath = path.join(jdf.currentDir, jdf.config.widgetDir);
|
111 |
|
112 | try {
|
113 | widgetList = fs.readdirSync(widgetPath);
|
114 | } catch (e) {
|
115 | logger.warn('can\'t find widget dir in your jdf project, skip combine your widget js/css file.')
|
116 | return Promise.reject();
|
117 | }
|
118 |
|
119 |
|
120 | if (mode === 1) {
|
121 | targetList = widgetList;
|
122 | } else if (mode === 2) {
|
123 | targetList = jdf.config.widgetWhiteList;
|
124 | } else if (mode === 3) {
|
125 | let blacklist = jdf.config.widgetBlackList;
|
126 | targetList = widgetList.filter(function (item) {
|
127 | return !blacklist.some(function (item1) {
|
128 | return item1 === item;
|
129 | });
|
130 | });
|
131 | }
|
132 |
|
133 |
|
134 | this.genWidgetOutputNameJS(targetList);
|
135 | this.genWidgetOutputNameCSS(targetList);
|
136 | }
|
137 |
|
138 | bow.concatOutputContent = function (info, htmlVfile) {
|
139 | let $$ = cheerio.load(htmlVfile.targetContent, {
|
140 | decodeEntities: false
|
141 | });
|
142 |
|
143 | let jsSet = new Set();
|
144 | let cssSet = new Set();
|
145 |
|
146 | if (info.concatTag.js) {
|
147 | $$('script[source=widget]').each(function (idx, el) {
|
148 | let src = $$(el).attr('src');
|
149 | src = src.split('/');
|
150 | if (src) {
|
151 | jsSet.add(src[src.length - 1].replace('.js', ''));
|
152 | }
|
153 | })
|
154 |
|
155 | }
|
156 | if (info.concatTag.css) {
|
157 | $$('link[source=widget]').each(function (idx, el) {
|
158 | let href = $$(el).attr('href');
|
159 | href = href.split('/');
|
160 | if (href) {
|
161 | cssSet.add(href[href.length - 1].replace('.css', ''))
|
162 | }
|
163 | })
|
164 | }
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 | let jsContent = '';
|
178 | for (let name of jsSet.keys()) {
|
179 | let tPath = path.join(this.getWidgetTargetDirByName(name), name + '.js');
|
180 | let widgetVfile = VFS.queryFile(tPath, 'target');
|
181 | if (widgetVfile) {
|
182 | let transferTargetContent = this.jsPathRelative(widgetVfile, path.resolve(process.cwd(), jdf.config.jsDir));
|
183 | jsContent += transferTargetContent + ';' + os.EOL;
|
184 | }
|
185 | }
|
186 |
|
187 | let cssContent = '';
|
188 | for (let name of cssSet.keys()) {
|
189 | let tPath = path.join(this.getWidgetTargetDirByName(name), name + '.css');
|
190 | let widgetVfile = VFS.queryFile(tPath, 'target');
|
191 | if (widgetVfile) {
|
192 | let transferTargetContent = this.cssPathRelative(widgetVfile, path.resolve(process.cwd(), jdf.config.cssDir));
|
193 | cssContent += transferTargetContent + os.EOL;
|
194 | }
|
195 | }
|
196 |
|
197 | return {
|
198 | css: cssContent,
|
199 | js: jsContent
|
200 | }
|
201 | }
|
202 |
|
203 | bow.getWidgetTargetDirByName = function (name) {
|
204 | let widgetDir = path.join(VFS.targetDir, jdf.config.widgetDir, name);
|
205 | return widgetDir;
|
206 | }
|
207 |
|
208 | bow.addToVFS = function (info, contents) {
|
209 | let jsPath = path.join(VFS.originDir, jdf.config.jsDir, info.name + '.js');
|
210 | let cssPath = path.join(VFS.originDir, jdf.config.cssDir, info.name + '.css');
|
211 | if (info.concatTag.js) {
|
212 | VFS.updateFile(jsPath, contents.js);
|
213 | }
|
214 | if (info.concatTag.css) {
|
215 | VFS.updateFile(cssPath, contents.css);
|
216 | }
|
217 | }
|
218 |
|
219 | bow.delWidgetTagInHTML = function (info, vfile) {
|
220 | let $$ = cheerio.load(vfile.targetContent, {
|
221 | decodeEntities: false
|
222 | });
|
223 | if (info.concatTag.js) {
|
224 | $$('script[source=widget]') && $$('script[source=widget]').remove();
|
225 | }
|
226 | if (info.concatTag.css) {
|
227 | $$('link[source=widget]') && $$('link[source=widget]').remove();
|
228 | }
|
229 | vfile.targetContent = $$.html();
|
230 | }
|
231 |
|
232 | bow.genWidgetOutputNameJS = function (widgetList) {
|
233 | let jsPathList = widgetList.map(widgetname => {
|
234 | return path.join(jdf.outputDir,
|
235 | jdf.config.widgetDir,
|
236 | widgetname,
|
237 | widgetname + '.js');
|
238 | });
|
239 | let jsContent = '';
|
240 | for (let i = 0; i < jsPathList.length; i++) {
|
241 | let jsVfile = VFS.queryFile(jsPathList[i], 'target');
|
242 | if (jsVfile) {
|
243 | let transferTargetContent = this.jsPathRelative(jsVfile, path.resolve(process.cwd(), jdf.config.jsDir));
|
244 | jsContent += transferTargetContent + ';' + os.EOL;
|
245 | }
|
246 | }
|
247 | let outputJsPath = path.join(jdf.currentDir, jdf.config.jsDir, jdf.config.widgetOutputName + '.js');
|
248 | VFS.updateFile(outputJsPath, jsContent);
|
249 |
|
250 | }
|
251 |
|
252 | bow.genWidgetOutputNameCSS = function (widgetList) {
|
253 | let cssPathList = widgetList.map(function (widgetname) {
|
254 | return path.join(jdf.outputDir,
|
255 | jdf.config.widgetDir,
|
256 | widgetname,
|
257 | widgetname + '.css');
|
258 | });
|
259 | let cssContent = '';
|
260 | for (let i = 0; i < cssPathList.length; i++) {
|
261 | let cssVfile = VFS.queryFile(cssPathList[i], 'target');
|
262 | if (cssVfile) {
|
263 | let transferTargetContent = this.cssPathRelative(cssVfile, path.resolve(process.cwd(), jdf.config.cssDir));
|
264 | cssContent += transferTargetContent + os.EOL;
|
265 | }
|
266 | }
|
267 | let outputCssPath = path.join(jdf.currentDir, jdf.config.cssDir, jdf.config.widgetOutputName + '.css');
|
268 | VFS.updateFile(outputCssPath, cssContent);
|
269 | }
|
270 |
|
271 | bow.jsPathRelative = function (vfile, targetDir) {
|
272 | var content = vfile.targetContent;
|
273 | return jsAst.main(content, {filepath: vfile.originPath}, function(nodeObj) {
|
274 | var str = nodeObj.str,
|
275 | fullpathstr;
|
276 |
|
277 | |
278 |
|
279 |
|
280 |
|
281 | if(!str ||
|
282 | $.is.httpLink(str) ||
|
283 | /^jdf\
|
284 | /^virtuals\
|
285 | /^seajs\
|
286 | /^felibs\
|
287 | path.isAbsolute(str)){
|
288 | return nodeObj;
|
289 | }
|
290 |
|
291 | fullpathstr = path.resolve(path.dirname(vfile.originPath), nodeObj.str);
|
292 |
|
293 | str = path.relative(targetDir, fullpathstr);
|
294 |
|
295 | str = f.pathFormat(str);
|
296 |
|
297 | nodeObj.str = str;
|
298 |
|
299 | return nodeObj;
|
300 | });
|
301 | }
|
302 |
|
303 | bow.cssPathRelative = function (vfile, targetDir) {
|
304 | var content = vfile.targetContent;
|
305 | return urlReplace.cssImagesUrlReplace(content, function (url) {
|
306 | var str = url,
|
307 | fullpathstr;
|
308 | |
309 |
|
310 |
|
311 |
|
312 | if($.is.httpLink(str) ||
|
313 | /^jdf\
|
314 | /^felibs\
|
315 | path.isAbsolute(str)){
|
316 | return url;
|
317 | }
|
318 |
|
319 | fullpathstr = path.resolve(path.dirname(vfile.originPath), str);
|
320 |
|
321 | str = path.relative(targetDir, fullpathstr);
|
322 | str = f.pathFormat(str);
|
323 | return str;
|
324 | });
|
325 | }
|