1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const path = require("path");
|
4 | const class_1 = require("../class");
|
5 | const util_1 = require("../util");
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | class XcxNode {
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | constructor(request, root) {
|
20 | |
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | this.children = [];
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | this.useRequests = [];
|
34 | |
35 |
|
36 |
|
37 |
|
38 |
|
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 |
|
52 |
|
53 | }
|
54 | if (this.isAvailable) {
|
55 |
|
56 | util_1.xcxNext.removeLack(this.request.srcRelative);
|
57 | }
|
58 | else {
|
59 |
|
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 |
|
71 |
|
72 |
|
73 |
|
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 |
|
97 |
|
98 | compile() {
|
99 | if (!this.isAvailable)
|
100 | return;
|
101 | this.wxFile.updateDepends(this.useRequests);
|
102 | this.wxFile.save();
|
103 | }
|
104 | |
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | cached() {
|
111 | util_1.xcxNodeCache.set(this.request.src, this);
|
112 | }
|
113 | |
114 |
|
115 |
|
116 |
|
117 |
|
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 |
|
193 |
|
194 |
|
195 | lack() {
|
196 | if (this.lackRequests.length > 0) {
|
197 |
|
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 | }
|
210 | exports.XcxNode = XcxNode;
|