UNPKG

6.06 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const path = require("path");
4const class_1 = require("../class");
5const util_1 = require("../util");
6/**
7 * 小程序节点类,用于创建生成节点树
8 *
9 * @export
10 * @class XcxNode
11 */
12class XcxNode {
13 /**
14 * Creates an instance of XcxNode.
15 * @param {Request} request
16 * @param {XcxNode} [root]
17 * @memberof XcxNode
18 */
19 constructor(request, root) {
20 /**
21 * 子节点列表
22 *
23 * @type {XcxNode[]}
24 * @memberof XcxNode
25 */
26 this.children = [];
27 /**
28 * 可用的请求列表
29 *
30 * @type {Request.Core[]}
31 * @memberof XcxNode
32 */
33 this.useRequests = [];
34 /**
35 * 缺失的请求列表
36 *
37 * @type {Request.Default[]}
38 * @memberof XcxNode
39 */
40 this.lackRequests = [];
41 if (root) {
42 root.children.push(this);
43 }
44 this.request = request;
45 try {
46 this.wxFile = new class_1.WxFile(this.request);
47 this.isAvailable = true;
48 }
49 catch (err) {
50 util_1.log.error(err);
51 // TODO
52 // Add log to app.js
53 }
54 if (this.isAvailable) {
55 // 从下一次的编译里移除
56 util_1.xcxNext.removeLack(this.request.srcRelative);
57 }
58 else {
59 // 将当前的请求地址记录到Next,用于下一次编译
60 util_1.xcxNext.addLack(this.request.srcRelative);
61 return;
62 }
63 this.cached();
64 this.recursive();
65 this.lack();
66 }
67 /**
68 * 创建一个小程序节点树
69 *
70 * @static
71 * @param {XcxNode.Options} options
72 * @returns {(XcxNode | null)}
73 * @memberof XcxNode
74 */
75 static create(options) {
76 let { isMain, isForce, root } = options;
77 if (isMain && root) {
78 util_1.log.debug(`XcxNode.create 不能同时设定'option.parent' 和 'root'`);
79 }
80 let request = new class_1.Request(options);
81 if (!request.src) {
82 if (isMain) {
83 util_1.log.error(`找不到入口:${request.request}`);
84 }
85 return null;
86 }
87 let xcxNode = util_1.xcxNodeCache.get(request.src);
88 if (isForce || !xcxNode) {
89 xcxNode = new XcxNode(request, root);
90 }
91 return xcxNode;
92 }
93 /**
94 * 编译,更新依赖列表和保存文件
95 *
96 * @memberof XcxNode
97 */
98 compile() {
99 if (!this.isAvailable)
100 return;
101 this.wxFile.updateDepends(this.useRequests);
102 this.wxFile.save();
103 }
104 /**
105 * 增加缓存
106 *
107 * @private
108 * @memberof XcxNode
109 */
110 cached() {
111 util_1.xcxNodeCache.set(this.request.src, this);
112 }
113 /**
114 * 递归依赖
115 *
116 * @private
117 * @memberof XcxNode
118 */
119 recursive() {
120 if (!this.isAvailable)
121 return;
122 let depends = this.wxFile.getDepends();
123 for (let i = 0; i < depends.length; i++) {
124 let depend = depends[i];
125 let { request, requestType, isVirtual } = depend;
126 if (isVirtual) {
127 this.resolveVirtual(depend);
128 return;
129 }
130 // 创建一个节点
131 let xcxNode = XcxNode.create({
132 request,
133 requestType,
134 parent: this.request.src,
135 isMain: false,
136 root: this,
137 isThreeNpm: this.request.isThreeNpm
138 });
139 if (!xcxNode) {
140 // 增加缺失的请求
141 this.lackRequests.push({
142 request,
143 requestType
144 });
145 }
146 else if (xcxNode.isAvailable) {
147 // 添加可用的请求
148 this.useRequests.push({
149 request,
150 requestType,
151 src: xcxNode.request.src,
152 srcRelative: xcxNode.request.srcRelative,
153 ext: xcxNode.request.ext,
154 dest: xcxNode.request.dest,
155 destRelative: xcxNode.request.destRelative,
156 isThreeNpm: xcxNode.request.isThreeNpm
157 });
158 }
159 }
160 }
161 resolveVirtual(depend) {
162 let { request, requestType } = depend;
163 let virtualPath = util_1.config.resolveVirtual[request];
164 if (!virtualPath) {
165 return;
166 }
167 if (!path.isAbsolute(virtualPath)) {
168 virtualPath = path.join(util_1.config.cwd, virtualPath);
169 }
170 if (!path.extname(virtualPath)) {
171 virtualPath += util_1.config.ext.js;
172 }
173 let src = virtualPath;
174 let srcRelative = path.relative(util_1.config.cwd, src);
175 let destRelative = util_1.src2destRelative(srcRelative);
176 let dest = path.join(util_1.config.cwd, destRelative);
177 this.useRequests.push({
178 request,
179 requestType,
180 src,
181 srcRelative,
182 ext: util_1.config.ext.js,
183 dest,
184 destRelative,
185 isThreeNpm: true,
186 isVirtual: true
187 });
188 }
189 /**
190 * 处理缺失依赖列表
191 *
192 * @private
193 * @memberof XcxNode
194 */
195 lack() {
196 if (this.lackRequests.length > 0) {
197 // 将当前的请求地址记录到Next,用于下一次编译
198 util_1.xcxNext.addLack(this.request.srcRelative);
199 // 打印缺失库的日志信息
200 this.lackRequests.forEach(lackRequest => {
201 util_1.log.error(`找不到模块:${lackRequest.request} in ${this.request.srcRelative}`);
202 });
203 }
204 else {
205 // 从下一次的编译里移除
206 util_1.xcxNext.removeLack(this.request.srcRelative);
207 }
208 }
209}
210exports.XcxNode = XcxNode;