1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const fs = require("fs-extra");
|
4 | const path = require("path");
|
5 | const glob = require("glob");
|
6 | const _ = require("lodash");
|
7 | const chokidar = require("chokidar");
|
8 | const class_1 = require("../class");
|
9 | const util_1 = require("../util");
|
10 | const MINI_PROGRAM_CONFIG_FILE_NAME = 'project.config.json';
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | class Xcx {
|
18 | |
19 |
|
20 |
|
21 |
|
22 |
|
23 | constructor(options) {
|
24 | this.options = options;
|
25 | }
|
26 | |
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | parser(entry) {
|
34 | let xcxNodes = [];
|
35 | if (_.isArray(entry)) {
|
36 | entry.forEach(item => {
|
37 | xcxNodes = [...xcxNodes, ...this.parser(item)];
|
38 | });
|
39 | }
|
40 | else {
|
41 | if (entry.isGlob) {
|
42 |
|
43 | let requests = glob.sync(entry.request, {
|
44 | cwd: entry.parent || util_1.config.cwd
|
45 | });
|
46 |
|
47 | let list = requests.map(request => {
|
48 | return Object.assign({}, _.omit(entry, 'isGlob'), { request });
|
49 | });
|
50 |
|
51 | xcxNodes = [...xcxNodes, ...this.parser(list)];
|
52 | }
|
53 | else {
|
54 |
|
55 | let xcxNode = class_1.XcxNode.create(entry);
|
56 | if (xcxNode) {
|
57 | xcxNodes.push(xcxNode);
|
58 | }
|
59 | }
|
60 | }
|
61 | return xcxNodes;
|
62 | }
|
63 | |
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | transfromFromEntry(entry) {
|
70 | let xcxNodes = this.parser(entry);
|
71 | this.transfrom(xcxNodes);
|
72 | }
|
73 | |
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | transfrom(xcxNode) {
|
80 | class_1.XcxTraverse.traverse(xcxNode, this.options.traverse);
|
81 | }
|
82 | |
83 |
|
84 |
|
85 |
|
86 |
|
87 | compile(isFromWatch) {
|
88 | util_1.log.newline();
|
89 | this.clear(isFromWatch);
|
90 |
|
91 | this.appCompile();
|
92 | this.pagesCompile();
|
93 | this.imagesCompile();
|
94 | }
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 | clearPackages() {
|
101 | let { isClear, packageNames = [] } = this.options;
|
102 | if (!isClear)
|
103 | return;
|
104 | packageNames.forEach(packageName => {
|
105 | fs.emptyDirSync(util_1.config.getPath('packages', packageName, util_1.config.package.dest));
|
106 | });
|
107 | }
|
108 | |
109 |
|
110 |
|
111 |
|
112 |
|
113 | compilePackages() {
|
114 | this.clearPackages();
|
115 | let { packageNames = [] } = this.options;
|
116 | let glob = '';
|
117 | if (packageNames.length === 0) {
|
118 | glob = '**';
|
119 | }
|
120 | else if (packageNames.length === 1) {
|
121 | glob = packageNames[0];
|
122 | }
|
123 | else {
|
124 | glob = `{${packageNames.join(',')}}`;
|
125 | }
|
126 |
|
127 | glob = `./${glob}/${util_1.config.package.src}/*${util_1.config.ext.wxc}`;
|
128 | let xcxEntry = {
|
129 | request: glob,
|
130 | parent: util_1.config.getPath('packages'),
|
131 | isMain: true,
|
132 | isGlob: true,
|
133 | isPublish: true
|
134 | };
|
135 | this.transfromFromEntry(xcxEntry);
|
136 | }
|
137 | |
138 |
|
139 |
|
140 |
|
141 |
|
142 | watch() {
|
143 | let watcher = chokidar.watch([util_1.config.src, util_1.config.packages, util_1.config.filename, MINI_PROGRAM_CONFIG_FILE_NAME], {
|
144 | cwd: util_1.config.cwd,
|
145 | ignored: /node_modules|\.git|\.txt|\.log|\.DS_Store|\.npmignore|package\.json/i,
|
146 | persistent: true,
|
147 | ignoreInitial: true
|
148 | });
|
149 | watcher
|
150 | .on('add', this.watchAdd.bind(this))
|
151 | .on('change', this.watchChange.bind(this))
|
152 | .on('unlink', this.watchDelete.bind(this))
|
153 | .on('error', (err) => {
|
154 | util_1.log.fatal(err);
|
155 | })
|
156 | .on('ready', () => {
|
157 | if (!this.isWatched) {
|
158 | this.isWatched = true;
|
159 | util_1.log.msg(util_1.LogType.WATCH, '开始监听文件改动。');
|
160 | }
|
161 | });
|
162 | return watcher;
|
163 | }
|
164 | |
165 |
|
166 |
|
167 |
|
168 |
|
169 | next() {
|
170 | let requests = util_1.xcxNext.get();
|
171 | if (!requests.length) {
|
172 | return;
|
173 | }
|
174 | let xcxEntry = requests.map(request => {
|
175 | return {
|
176 | request,
|
177 | parent: util_1.config.cwd,
|
178 | isMain: true,
|
179 | isForce: true
|
180 | };
|
181 | });
|
182 | util_1.log.newline();
|
183 | this.transfromFromEntry(xcxEntry);
|
184 | util_1.xcxNext.reset();
|
185 | }
|
186 | |
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 | |
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 | |
218 |
|
219 |
|
220 |
|
221 |
|
222 | appCompile() {
|
223 | let { app = {} } = this.options;
|
224 | let { isSFC } = app;
|
225 | let xcxEntry = {
|
226 | request: isSFC ? `./app${util_1.config.ext.wxa}` : './app.{js,wxss}',
|
227 | parent: util_1.config.getPath('src'),
|
228 | isMain: true,
|
229 | isGlob: isSFC ? false : true
|
230 | };
|
231 | this.transfromFromEntry(xcxEntry);
|
232 | }
|
233 | |
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 | pagesCompile() {
|
241 | const tabBarList = util_1.Global.appTabBarList;
|
242 | |
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | let pages = this.options.pages || [];
|
249 | if (pages.length === 0) {
|
250 | pages = util_1.Global.appPages || [];
|
251 | }
|
252 | let xcxEntry = [];
|
253 | if (pages.length > 0) {
|
254 | let pageFiles = [];
|
255 | |
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 | pageFiles = [...pageFiles, ...pages.map(page => `${page}${util_1.config.ext.wxp}`)];
|
262 |
|
263 | pageFiles = [...pageFiles, ...tabBarList.map(tabBarItem => `${tabBarItem.pagePath}${util_1.config.ext.wxp}`)];
|
264 |
|
265 | pageFiles = _.uniq(pageFiles);
|
266 | xcxEntry = [...xcxEntry, ...pageFiles.map(pageFile => {
|
267 | return {
|
268 | request: pageFile,
|
269 | parent: util_1.config.getPath('src'),
|
270 | isMain: true
|
271 | };
|
272 | })];
|
273 | }
|
274 | else {
|
275 | xcxEntry = [...xcxEntry, {
|
276 | request: `**/*${util_1.config.ext.wxp}`,
|
277 | parent: util_1.config.getPath('pages'),
|
278 | isMain: true,
|
279 | isGlob: true
|
280 | }];
|
281 | }
|
282 | this.transfromFromEntry(xcxEntry);
|
283 | }
|
284 | |
285 |
|
286 |
|
287 |
|
288 |
|
289 | imagesCompile() {
|
290 | const tabBarList = util_1.Global.appTabBarList;
|
291 |
|
292 | const images = Array.prototype.concat.apply([], tabBarList.map(tabBarItem => {
|
293 | let map = [];
|
294 | if (tabBarItem.iconPath) {
|
295 | map.push({
|
296 | origin: util_1.config.getPath('src', tabBarItem.iconPath),
|
297 | target: util_1.config.getPath('dest', tabBarItem.iconPath)
|
298 | });
|
299 | }
|
300 | if (tabBarItem.selectedIconPath) {
|
301 | map.push({
|
302 | origin: util_1.config.getPath('src', tabBarItem.selectedIconPath),
|
303 | target: util_1.config.getPath('dest', tabBarItem.selectedIconPath)
|
304 | });
|
305 | }
|
306 | return map;
|
307 | }));
|
308 | images.forEach(image => {
|
309 | if (!fs.existsSync(image.origin)) {
|
310 | util_1.log.fatal(`找不到文件:${image.origin}`);
|
311 | return;
|
312 | }
|
313 | fs.copySync(image.origin, image.target);
|
314 | });
|
315 | }
|
316 | |
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 | watchAdd(file) {
|
324 | let isProjectConfig = file === MINI_PROGRAM_CONFIG_FILE_NAME;
|
325 |
|
326 |
|
327 |
|
328 |
|
329 | util_1.xcxNext.watchNewFile(file);
|
330 | this.next();
|
331 | }
|
332 | |
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 | watchChange(file) {
|
340 | let isApp = file === path.join(util_1.config.src, `app${util_1.config.ext.wxa}`);
|
341 | let isMinConfig = file === util_1.config.filename;
|
342 | let isProjectConfig = file === MINI_PROGRAM_CONFIG_FILE_NAME;
|
343 |
|
344 |
|
345 |
|
346 | if (isApp || isMinConfig) {
|
347 | this.compile(true);
|
348 | }
|
349 | else {
|
350 | util_1.xcxNext.watchChangeFile(file);
|
351 | this.next();
|
352 | }
|
353 | }
|
354 | |
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 | watchDelete(file) {
|
362 | let isMinConfig = file === util_1.config.filename;
|
363 | let isProjectConfig = file === MINI_PROGRAM_CONFIG_FILE_NAME;
|
364 |
|
365 |
|
366 |
|
367 | if (isMinConfig) {
|
368 | this.compile(true);
|
369 | }
|
370 | else {
|
371 | util_1.xcxNext.watchDeleteFile(file);
|
372 | this.next();
|
373 | }
|
374 | }
|
375 | |
376 |
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 | clear(isFromWatch) {
|
383 | this.clearCache();
|
384 | if (isFromWatch) {
|
385 | return;
|
386 | }
|
387 | this.clearDest();
|
388 | }
|
389 | |
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 | clearCache() {
|
396 | let { isClear } = this.options;
|
397 | if (!isClear)
|
398 | return;
|
399 | util_1.Global.clear();
|
400 | util_1.xcxNext.clear();
|
401 | util_1.xcxNodeCache.clear();
|
402 | }
|
403 | |
404 |
|
405 |
|
406 |
|
407 |
|
408 |
|
409 | clearDest() {
|
410 | let { isClear } = this.options;
|
411 | if (!isClear)
|
412 | return;
|
413 | fs.emptyDirSync(util_1.config.getPath('dest'));
|
414 | }
|
415 | }
|
416 | exports.Xcx = Xcx;
|