mbler 0.1.1 → 0.1.3-alpna-r1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/bin/mbler.js +12 -12
- package/bin/minify.js +58 -44
- package/lib/build/build-g-config.json +11 -11
- package/lib/data/includes.json +12 -12
- package/lib/data/version.json +88 -88
- package/lib/mcx/compile-mcx/compile.js +1 -1
- package/lib/mcx/compile-mcx/compiler/bundler.js +1 -0
- package/lib/mcx/compile-mcx/compiler/compileData.js +1 -0
- package/lib/mcx/compile-mcx/compiler/compileMain.js +1 -0
- package/lib/mcx/compile-mcx/compiler/index.js +1 -0
- package/lib/mcx/compile-mcx/compiler/utils.js +1 -0
- package/lib/modules/create-game/mbler.config.json +13 -13
- package/lib/modules/gameLib/mbler.config.json +15 -15
- package/lib/modules/gutils/mbler.config.json +11 -11
- package/lib/modules/innerDef.json +4 -4
- package/lib/runTemp/config.json +3 -3
- package/lib/start/create.js +1 -1
- package/lib/start/index.js +1 -1
- package/lib/start/rechce.js +1 -1
- package/package.json +49 -1
- package/test/2x sword/behavior/scripts/index.js +24 -24
- package/test/2x sword/mbler.config.json +11 -11
- package/test/2x sword/package.json +13 -13
- package/test/README.md +7 -7
- package/test/des/index.js +19 -19
- package/test/des/mbler.config.json +4 -4
- package/test/des/package.json +6 -6
- package/test/initializer/index.js +5 -5
- package/test/initializer/mbler.config.json +4 -4
- package/test/initializer/package.json +6 -6
- package/test/mbler-int/behavior/res/items/mang_one_int.json +21 -21
- package/test/mbler-int/behavior/scripts/index.js +7 -7
- package/test/mbler-int/behavior/scripts/lib/event/index.js +19 -19
- package/test/mbler-int/behavior/scripts/lib/ui/Lore.js +18 -18
- package/test/mbler-int/behavior/scripts/lib/ui/baseUi.js +6 -6
- package/test/mbler-int/behavior/scripts/lib/ui/index.js +206 -206
- package/test/mbler-int/behavior/scripts/lib/utils/index.js +51 -51
- package/test/mbler-int/mbler.config.json +18 -18
- package/test/mbler-int/package.json +12 -12
- package/test/script-mbler/behavior/scripts/index.js +5 -5
- package/test/script-mbler/behavior/scripts/index.ts +5 -5
- package/test/script-mbler/mbler.config.json +14 -14
- package/test/script-mbler/package.json +15 -15
- package/lib/mcx/compile-mcx/_compile.js +0 -602
|
@@ -1,602 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
-
var ownKeys = function(o) {
|
|
20
|
-
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
-
var ar = [];
|
|
22
|
-
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
-
return ar;
|
|
24
|
-
};
|
|
25
|
-
return ownKeys(o);
|
|
26
|
-
};
|
|
27
|
-
return function (mod) {
|
|
28
|
-
if (mod && mod.__esModule) return mod;
|
|
29
|
-
var result = {};
|
|
30
|
-
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
-
__setModuleDefault(result, mod);
|
|
32
|
-
return result;
|
|
33
|
-
};
|
|
34
|
-
})();
|
|
35
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
-
};
|
|
38
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
exports.McxCompileData = exports.CompileData = exports.DefaultCache = void 0;
|
|
40
|
-
const context_js_1 = __importDefault(require("./context.js"));
|
|
41
|
-
const parser_1 = __importDefault(require("@babel/parser"));
|
|
42
|
-
const t = __importStar(require("@babel/types"));
|
|
43
|
-
const index_js_1 = __importDefault(require("./../ast/index.js"));
|
|
44
|
-
const lib_js_1 = require("./../compile-component/lib.js");
|
|
45
|
-
const utils_node_js_1 = __importDefault(require("./utils.node.js"));
|
|
46
|
-
const utils_js_1 = __importDefault(require("./../utils.js"));
|
|
47
|
-
const node_path_1 = __importDefault(require("node:path"));
|
|
48
|
-
const index_js_2 = __importDefault(require("../../loger/index.js"));
|
|
49
|
-
const promises_1 = require("node:fs/promises");
|
|
50
|
-
const node_crypto_1 = require("node:crypto");
|
|
51
|
-
const McxConfig = {
|
|
52
|
-
allowTag: {
|
|
53
|
-
script: null,
|
|
54
|
-
Event: null,
|
|
55
|
-
Component: {
|
|
56
|
-
items: {
|
|
57
|
-
item: null
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
};
|
|
62
|
-
const conBuild = require("./../../build/build-g-config.json");
|
|
63
|
-
exports.DefaultCache = {
|
|
64
|
-
import: [],
|
|
65
|
-
export: [],
|
|
66
|
-
call: []
|
|
67
|
-
};
|
|
68
|
-
class CompileData {
|
|
69
|
-
AST;
|
|
70
|
-
Cache;
|
|
71
|
-
type = "JsCompileData";
|
|
72
|
-
context;
|
|
73
|
-
filePath = "";
|
|
74
|
-
constructor(AST, cache = exports.DefaultCache) {
|
|
75
|
-
this.AST = AST;
|
|
76
|
-
this.Cache = cache;
|
|
77
|
-
this.context = new context_js_1.default();
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
exports.CompileData = CompileData;
|
|
81
|
-
class McxCompileData {
|
|
82
|
-
AST;
|
|
83
|
-
JSContext = new context_js_1.default();
|
|
84
|
-
type = "McxCompileData";
|
|
85
|
-
filePath = "";
|
|
86
|
-
setFilePath(filePath) {
|
|
87
|
-
this.filePath = filePath;
|
|
88
|
-
}
|
|
89
|
-
constructor(AST) {
|
|
90
|
-
this.AST = AST;
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
exports.McxCompileData = McxCompileData;
|
|
94
|
-
class Utils {
|
|
95
|
-
static async FileAST(fileDir, parserOpt) {
|
|
96
|
-
if (typeof fileDir !== "string")
|
|
97
|
-
throw new TypeError("[read file]: compile utils was passed a non-string value");
|
|
98
|
-
const file = await utils_js_1.default.readFile(fileDir);
|
|
99
|
-
if (typeof file !== "string")
|
|
100
|
-
throw new Error("[read file]: not found file " + fileDir);
|
|
101
|
-
try {
|
|
102
|
-
return parser_1.default.parse(file).program;
|
|
103
|
-
}
|
|
104
|
-
catch (err) {
|
|
105
|
-
throw new Error("[compile ast]: " + err.stack);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
static async FileContent(fileDir) {
|
|
109
|
-
const file = await utils_js_1.default.readFile(fileDir);
|
|
110
|
-
if (typeof file === "string")
|
|
111
|
-
return file;
|
|
112
|
-
throw new Error("[read file]: not found file " + fileDir);
|
|
113
|
-
}
|
|
114
|
-
static nodeStringValue(node) {
|
|
115
|
-
if (node.type == "StringLiteral") {
|
|
116
|
-
return node.value;
|
|
117
|
-
}
|
|
118
|
-
else if (node.type == "Identifier") {
|
|
119
|
-
return node.name;
|
|
120
|
-
}
|
|
121
|
-
throw new TypeError("[read id error]: no way to read string id");
|
|
122
|
-
}
|
|
123
|
-
static CheckImportNode(node, ir) {
|
|
124
|
-
const newList = Utils.ImportToCache(node);
|
|
125
|
-
// Eliminate common differences
|
|
126
|
-
if (newList.source !== ir.source)
|
|
127
|
-
return false;
|
|
128
|
-
if (newList.imported.length !== ir.imported.length)
|
|
129
|
-
return false;
|
|
130
|
-
// in this for, newList.imported and ir.imported is same length
|
|
131
|
-
for (let irIndex = 0; irIndex < newList.imported.length; irIndex++) {
|
|
132
|
-
const newItem = newList.imported[irIndex];
|
|
133
|
-
const oldItem = ir.imported[irIndex];
|
|
134
|
-
if (newItem?.import !== oldItem?.import || newItem?.as !== oldItem?.as || newItem?.isAll !== oldItem?.isAll)
|
|
135
|
-
return false;
|
|
136
|
-
}
|
|
137
|
-
return true;
|
|
138
|
-
}
|
|
139
|
-
static CacheToImportNode(ir) {
|
|
140
|
-
if (!ir)
|
|
141
|
-
throw new TypeError("plase call use right ImportList");
|
|
142
|
-
// first verify ir.raw
|
|
143
|
-
if (ir?.raw && Utils.CheckImportNode(ir?.raw, ir))
|
|
144
|
-
return ir.raw;
|
|
145
|
-
let result = [];
|
|
146
|
-
for (let ImportIt of ir.imported) {
|
|
147
|
-
if (!ImportIt)
|
|
148
|
-
continue;
|
|
149
|
-
if (ImportIt.isAll) {
|
|
150
|
-
result.push(t.importNamespaceSpecifier(t.identifier(ImportIt.as)));
|
|
151
|
-
continue;
|
|
152
|
-
}
|
|
153
|
-
if (ImportIt.import == "default") {
|
|
154
|
-
result.push(t.importDefaultSpecifier(t.identifier(ImportIt.as)));
|
|
155
|
-
continue;
|
|
156
|
-
}
|
|
157
|
-
if (!ImportIt.import)
|
|
158
|
-
throw new TypeError("[compile node]: not found imported");
|
|
159
|
-
result.push(t.importSpecifier(t.identifier(ImportIt.as), t.identifier(ImportIt.import)));
|
|
160
|
-
}
|
|
161
|
-
return t.importDeclaration(result, t.stringLiteral(ir.source));
|
|
162
|
-
}
|
|
163
|
-
static ImportToCache(node) {
|
|
164
|
-
const result = [];
|
|
165
|
-
for (let item of node.specifiers) {
|
|
166
|
-
const thisName = item.local.name;
|
|
167
|
-
if (item.type == "ImportNamespaceSpecifier") {
|
|
168
|
-
result.push({
|
|
169
|
-
isAll: true,
|
|
170
|
-
as: thisName
|
|
171
|
-
});
|
|
172
|
-
}
|
|
173
|
-
else if (item.type == "ImportDefaultSpecifier") {
|
|
174
|
-
result.push({
|
|
175
|
-
isAll: false,
|
|
176
|
-
import: "default",
|
|
177
|
-
as: thisName
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
else if (item.type == "ImportSpecifier") {
|
|
181
|
-
result.push({
|
|
182
|
-
isAll: false,
|
|
183
|
-
as: thisName,
|
|
184
|
-
import: Utils.nodeStringValue(item.imported)
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
return {
|
|
189
|
-
source: Utils.nodeStringValue(node.source),
|
|
190
|
-
imported: result,
|
|
191
|
-
raw: node
|
|
192
|
-
};
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
class ComponentExport {
|
|
196
|
-
rule;
|
|
197
|
-
ast;
|
|
198
|
-
TopConext;
|
|
199
|
-
// export: fileid
|
|
200
|
-
constructor(code, rule, BabelConfig) {
|
|
201
|
-
this.rule = rule;
|
|
202
|
-
this.ast = parser_1.default.parse(code).program;
|
|
203
|
-
this.TopConext = {};
|
|
204
|
-
this.run();
|
|
205
|
-
}
|
|
206
|
-
run() {
|
|
207
|
-
const spData = {};
|
|
208
|
-
this.atre(this.ast, spData);
|
|
209
|
-
return spData;
|
|
210
|
-
}
|
|
211
|
-
FindVar(name, ThisContext) {
|
|
212
|
-
if (typeof name == "string") {
|
|
213
|
-
if (this.TopConext[name])
|
|
214
|
-
return this.TopConext[name];
|
|
215
|
-
if (ThisContext[name])
|
|
216
|
-
return ThisContext[name];
|
|
217
|
-
}
|
|
218
|
-
throw new Error("[compile error]: can't find var : " + name);
|
|
219
|
-
}
|
|
220
|
-
writeResult(spData, declaration, id) {
|
|
221
|
-
// @ts-ignore
|
|
222
|
-
if (this.rule.getExport && this.rule.getExport.includes(id)) {
|
|
223
|
-
continue;
|
|
224
|
-
}
|
|
225
|
-
const init = declaration.init;
|
|
226
|
-
if (init?.type !== "NewExpression")
|
|
227
|
-
return;
|
|
228
|
-
const className = init.callee;
|
|
229
|
-
if (className.type !== "MemberExpression")
|
|
230
|
-
return;
|
|
231
|
-
const callee = utils_node_js_1.default.memberExpressionToStringArray(className, 2);
|
|
232
|
-
if (callee[0] !== "__MCX__")
|
|
233
|
-
return;
|
|
234
|
-
const name = callee[1];
|
|
235
|
-
if (name == "ItemComponent")
|
|
236
|
-
spData[id] = new lib_js_1.ItemComponent();
|
|
237
|
-
}
|
|
238
|
-
atre(block, spData) {
|
|
239
|
-
const isTop = block.type == "Program";
|
|
240
|
-
const ThisContext = {};
|
|
241
|
-
for (const _node of block.body) {
|
|
242
|
-
if (_node.type == "BlockStatement") {
|
|
243
|
-
this.atre(_node, spData);
|
|
244
|
-
}
|
|
245
|
-
else if (_node.type == "BreakStatement" || _node.type == "ContinueStatement" || _node.type == "EmptyStatement" || _node.type == "WithStatement")
|
|
246
|
-
continue;
|
|
247
|
-
let node = _node;
|
|
248
|
-
// 基础模块报错
|
|
249
|
-
let isExport = isTop ? Boolean(this.rule.setup) : false;
|
|
250
|
-
if (isTop) {
|
|
251
|
-
if (_node.type == "ExportDefaultDeclaration") {
|
|
252
|
-
isExport = true;
|
|
253
|
-
node = _node.declaration;
|
|
254
|
-
}
|
|
255
|
-
else if (_node.type == "ExportNamedDeclaration") {
|
|
256
|
-
if (_node.specifiers.length > 0 || !_node.declaration)
|
|
257
|
-
throw new Error("[compile component]: you shouldn't use 'export xxx from xxx' in component mcx");
|
|
258
|
-
isExport = true;
|
|
259
|
-
node = _node.declaration;
|
|
260
|
-
}
|
|
261
|
-
else if (_node.type == "ExportAllDeclaration" || _node.type == "ImportDeclaration") {
|
|
262
|
-
throw new Error("[compile component]: 'export * from ' or 'import'(you should use __MCX__ to visit mcx component class) can't use in component mcx");
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
if (node.type == "VariableDeclaration") {
|
|
266
|
-
if (node.kind !== "const")
|
|
267
|
-
continue;
|
|
268
|
-
for (const declaration of node.declarations) {
|
|
269
|
-
if (!declaration || !declaration.init)
|
|
270
|
-
continue;
|
|
271
|
-
// 理想模式
|
|
272
|
-
if (declaration.id.type == "Identifier") {
|
|
273
|
-
const id = declaration.id.name;
|
|
274
|
-
if (!id)
|
|
275
|
-
continue;
|
|
276
|
-
if (isTop) {
|
|
277
|
-
this.TopConext[id] = declaration.init;
|
|
278
|
-
}
|
|
279
|
-
else {
|
|
280
|
-
ThisContext[id] = declaration.init;
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
}
|
|
288
|
-
class CompileMain {
|
|
289
|
-
compileOpt;
|
|
290
|
-
main;
|
|
291
|
-
babelConfig;
|
|
292
|
-
constructor(compileOpt) {
|
|
293
|
-
this.compileOpt = compileOpt;
|
|
294
|
-
this.main = utils_js_1.default.AbsoluteJoin(this.compileOpt.ProjectDir, this.compileOpt.main);
|
|
295
|
-
let babelConfig = this.compileOpt.config?.babelParser;
|
|
296
|
-
if (!babelConfig)
|
|
297
|
-
babelConfig = {};
|
|
298
|
-
this.babelConfig = {
|
|
299
|
-
sourceType: "module",
|
|
300
|
-
allowImportExportEverywhere: false,
|
|
301
|
-
createParenthesizedExpressions: false,
|
|
302
|
-
attachComment: false,
|
|
303
|
-
...babelConfig
|
|
304
|
-
};
|
|
305
|
-
}
|
|
306
|
-
async compile() {
|
|
307
|
-
if (!await utils_js_1.default.FileExsit(this.main)) {
|
|
308
|
-
throw new Error("[compile error]: cannot find ");
|
|
309
|
-
}
|
|
310
|
-
let moduleCache = new Map();
|
|
311
|
-
const main__code = await Utils.FileAST(this.main, this.babelConfig);
|
|
312
|
-
const mainCompileData = this.traverse(main__code, true);
|
|
313
|
-
mainCompileData.filePath = this.main;
|
|
314
|
-
// for in import
|
|
315
|
-
const importIrs = mainCompileData.Cache.import;
|
|
316
|
-
let newBody = main__code.body;
|
|
317
|
-
if (importIrs.length > 0) {
|
|
318
|
-
// go in fn
|
|
319
|
-
this.trImport(importIrs, moduleCache, this.main);
|
|
320
|
-
}
|
|
321
|
-
if (mainCompileData.Cache.export.length > 0) {
|
|
322
|
-
newBody.push(...this.MergeExport(mainCompileData.Cache, mainCompileData.Cache.export));
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
RandomUniqueValue() {
|
|
326
|
-
return "__mbler__" + (0, node_crypto_1.randomUUID)().replace("-", "");
|
|
327
|
-
}
|
|
328
|
-
ensureExpression(node) {
|
|
329
|
-
if (node.type == "ClassDeclaration")
|
|
330
|
-
return t.classExpression(node.id, node.superClass, node.body);
|
|
331
|
-
if (node.type == "FunctionDeclaration")
|
|
332
|
-
return t.functionExpression(node.id, node.params, node.body, node.generator, node.async);
|
|
333
|
-
if (t.isExpression(node))
|
|
334
|
-
return node;
|
|
335
|
-
throw new Error("[compile error]: cannot ensure");
|
|
336
|
-
}
|
|
337
|
-
MergeExport(ir, exp) {
|
|
338
|
-
let result = [];
|
|
339
|
-
let resObj = t.objectExpression([]);
|
|
340
|
-
for (const item of exp) {
|
|
341
|
-
if (item.type == "ExportAllDeclaration") {
|
|
342
|
-
// ExportAllDeclaration
|
|
343
|
-
resObj.properties.push(t.spreadElement(t.callExpression(t.identifier("__import"), [
|
|
344
|
-
t.identifier(item.source.value)
|
|
345
|
-
])));
|
|
346
|
-
}
|
|
347
|
-
else if (item.type == "ExportDefaultDeclaration") {
|
|
348
|
-
// ExportDefaultDclaration
|
|
349
|
-
resObj.properties.push(t.objectProperty(t.identifier("default"), this.ensureExpression(item.declaration)));
|
|
350
|
-
}
|
|
351
|
-
else {
|
|
352
|
-
// ExportNamedDeclaration
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
result.push(t.returnStatement(resObj));
|
|
356
|
-
return result;
|
|
357
|
-
}
|
|
358
|
-
genaterateModuleFunction(body) {
|
|
359
|
-
return t.functionDeclaration(t.identifier(this.RandomUniqueValue()), [
|
|
360
|
-
t.identifier("__import")
|
|
361
|
-
], body, false, true);
|
|
362
|
-
}
|
|
363
|
-
async trImport(importList, cacheMap, filePath) {
|
|
364
|
-
for (const moduleData of importList) {
|
|
365
|
-
const source = moduleData.source;
|
|
366
|
-
// 判断源的类型
|
|
367
|
-
const PasreDir = node_path_1.default.parse(source);
|
|
368
|
-
// if root = /, we should warn user because compiling can leave the computer
|
|
369
|
-
if (PasreDir.root == "/") {
|
|
370
|
-
index_js_2.default.w("mcx compile", "you shouldn't use Absolute path");
|
|
371
|
-
continue;
|
|
372
|
-
}
|
|
373
|
-
let modulePath = "";
|
|
374
|
-
// Relative Path
|
|
375
|
-
if (PasreDir.dir == "./") {
|
|
376
|
-
modulePath = node_path_1.default.join(filePath, source);
|
|
377
|
-
}
|
|
378
|
-
else if (source == "mcx") {
|
|
379
|
-
// visit 'mcx'
|
|
380
|
-
// mcx 提供的导出被计划为大多都是宏,不需要处理,后续补上.d.ts即可
|
|
381
|
-
continue;
|
|
382
|
-
}
|
|
383
|
-
else {
|
|
384
|
-
// find module
|
|
385
|
-
if (this.compileOpt.moduleList.includes(source)) {
|
|
386
|
-
// is module
|
|
387
|
-
const moduleDir = node_path_1.default.join(this.compileOpt.moduleDir, source);
|
|
388
|
-
let moduleMainfest;
|
|
389
|
-
try {
|
|
390
|
-
moduleMainfest = JSON.parse(await (0, promises_1.readFile)(node_path_1.default.join(moduleDir, conBuild.packageFile), "utf-8"));
|
|
391
|
-
}
|
|
392
|
-
catch {
|
|
393
|
-
moduleMainfest = {
|
|
394
|
-
name: "",
|
|
395
|
-
mcVersion: "1.21.00",
|
|
396
|
-
description: "",
|
|
397
|
-
version: "0.0.1",
|
|
398
|
-
script: {
|
|
399
|
-
main: "./index.js"
|
|
400
|
-
}
|
|
401
|
-
};
|
|
402
|
-
}
|
|
403
|
-
const TempStr = moduleMainfest.script?.main;
|
|
404
|
-
if (typeof TempStr == "string") {
|
|
405
|
-
modulePath = TempStr;
|
|
406
|
-
}
|
|
407
|
-
modulePath = node_path_1.default.join(moduleDir, modulePath);
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
let compileData;
|
|
411
|
-
const moduleCode = await (0, promises_1.readFile)(modulePath, "utf-8");
|
|
412
|
-
const moduleExt = node_path_1.default.extname(modulePath).slice(1);
|
|
413
|
-
if (moduleExt == "js") {
|
|
414
|
-
const AST = parser_1.default.parse(moduleCode).program;
|
|
415
|
-
compileData = this.traverse(AST, true);
|
|
416
|
-
}
|
|
417
|
-
else if (moduleExt == "mcx") {
|
|
418
|
-
compileData = this.MCXCompile(moduleCode);
|
|
419
|
-
}
|
|
420
|
-
else {
|
|
421
|
-
throw new Error("[load module] load module '" + modulePath + "' : ext is not useful");
|
|
422
|
-
}
|
|
423
|
-
compileData.filePath = modulePath;
|
|
424
|
-
cacheMap.set(modulePath, compileData);
|
|
425
|
-
}
|
|
426
|
-
}
|
|
427
|
-
Tag(code) {
|
|
428
|
-
return (new index_js_1.default.tag(code)).parseAST();
|
|
429
|
-
}
|
|
430
|
-
MCXCompile(code) {
|
|
431
|
-
const parser = this.Tag(code);
|
|
432
|
-
let result = {
|
|
433
|
-
Component: {
|
|
434
|
-
item: [],
|
|
435
|
-
isLoad: false
|
|
436
|
-
},
|
|
437
|
-
Event: {
|
|
438
|
-
on: "after",
|
|
439
|
-
data: []
|
|
440
|
-
}
|
|
441
|
-
};
|
|
442
|
-
let allowTag = McxConfig.allowTag;
|
|
443
|
-
const FindTag = (node, lastTag) => {
|
|
444
|
-
const allowTagNames = Object.keys(allowTag);
|
|
445
|
-
const isTop = lastTag.length == 0;
|
|
446
|
-
for (let index = 0; index < node.length; index++) {
|
|
447
|
-
const item = node[index];
|
|
448
|
-
const hasContent = !!(typeof item?.content?.data == "string" && item.content.data.trim().length > 1);
|
|
449
|
-
if (typeof item?.name !== "string")
|
|
450
|
-
throw new Error("[compile error]: mcx: Tag name is not string");
|
|
451
|
-
if (!allowTagNames.includes(item?.name))
|
|
452
|
-
throw new Error("[compile error]: mcx: visit not allow tag: " + item?.name);
|
|
453
|
-
if (isTop) {
|
|
454
|
-
if (item.name == "script") {
|
|
455
|
-
// 验证内容
|
|
456
|
-
if (!hasContent || !item.content)
|
|
457
|
-
throw new Error("[compile error]: mcx: script tag shouldn't null");
|
|
458
|
-
// 验证是否已写
|
|
459
|
-
if (result.script)
|
|
460
|
-
throw new Error("[compile error]: mcx: a mcx cannot have two script tag");
|
|
461
|
-
result.script = this.traverse(parser_1.default.parse(item.content.data).program, true);
|
|
462
|
-
continue;
|
|
463
|
-
}
|
|
464
|
-
if (item.name == "Event") {
|
|
465
|
-
// 验证内容
|
|
466
|
-
if (!hasContent || !item.content)
|
|
467
|
-
throw new Error("[compile error]: mcx: Event shouldn't null");
|
|
468
|
-
// 验证属性是否可写
|
|
469
|
-
if (result.Event.data.length > 0)
|
|
470
|
-
throw new Error("[compile error]: mcx: a mcx can't have two event tag");
|
|
471
|
-
// 验证是否冲突
|
|
472
|
-
if (result.Component.isLoad)
|
|
473
|
-
throw new Error("[compile error]: mcx: a mcx can't have comonent and event");
|
|
474
|
-
const prop = index_js_1.default.prop(item.content.data);
|
|
475
|
-
if (prop.length < 1)
|
|
476
|
-
throw new Error("[compile error]: mcx: event tag should't have null data");
|
|
477
|
-
const TempObj = item.arr;
|
|
478
|
-
let on;
|
|
479
|
-
// 判断事件类型
|
|
480
|
-
const isAfter = Boolean(TempObj["@after"]);
|
|
481
|
-
const isBefore = Boolean(TempObj["@before"]);
|
|
482
|
-
if (isAfter && isBefore)
|
|
483
|
-
throw new Error("[compile error]: mcx: Event can't have @after and @before");
|
|
484
|
-
on = isAfter ? "after" : (isBefore ? "before" : "after");
|
|
485
|
-
result.Event = {
|
|
486
|
-
on,
|
|
487
|
-
data: prop
|
|
488
|
-
};
|
|
489
|
-
}
|
|
490
|
-
if (item.name == "Component") {
|
|
491
|
-
if (!hasContent || !item.content)
|
|
492
|
-
throw new Error("[compile error]: mcx: Component should't null");
|
|
493
|
-
// 是否冲突
|
|
494
|
-
if (result.Event.data.length > 0)
|
|
495
|
-
throw new Error("[compile error]: mcx: compoent tag can't use with event tag");
|
|
496
|
-
// 是否已写
|
|
497
|
-
if (result.Component.isLoad)
|
|
498
|
-
throw new Error("[compile error]: mcx: a mcx shouldn't use more component");
|
|
499
|
-
allowTag = McxConfig.allowTag.Component;
|
|
500
|
-
FindTag(this.Tag(item.content.data), [item.name]);
|
|
501
|
-
return;
|
|
502
|
-
}
|
|
503
|
-
}
|
|
504
|
-
const TopTag = lastTag[0];
|
|
505
|
-
// 不需要判断 script和Event,因为其只有一层
|
|
506
|
-
if (TopTag == "Component") {
|
|
507
|
-
// 遍历到第二层的时候
|
|
508
|
-
if (lastTag.length == 1) {
|
|
509
|
-
if (allowTag[item.name]) {
|
|
510
|
-
if (!hasContent || !item.content)
|
|
511
|
-
throw new Error("[compile mcx]: can't get component data");
|
|
512
|
-
FindTag(this.Tag(item.content?.data), [...lastTag, item.name]);
|
|
513
|
-
return;
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
// 遍历内部
|
|
517
|
-
if (lastTag.length > 1) {
|
|
518
|
-
if (typeof item.arr.id !== "string" || !hasContent || !item.content)
|
|
519
|
-
throw new Error("[compile mcx]: a component have to have a file id as arr");
|
|
520
|
-
if (item.name == "item") {
|
|
521
|
-
result.Component.item.push({
|
|
522
|
-
Fileid: item.arr.id.replace("../", ""),
|
|
523
|
-
UseExport: item.content.data.trim()
|
|
524
|
-
});
|
|
525
|
-
}
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
else {
|
|
529
|
-
throw new Error("[compile mcx]: TopTag is not right");
|
|
530
|
-
}
|
|
531
|
-
}
|
|
532
|
-
};
|
|
533
|
-
FindTag(parser, []);
|
|
534
|
-
const res = new McxCompileData(result);
|
|
535
|
-
return res;
|
|
536
|
-
}
|
|
537
|
-
traverse(node, isTop, data) {
|
|
538
|
-
if (isTop && node.type == "Program")
|
|
539
|
-
data = new CompileData(node);
|
|
540
|
-
if (!data)
|
|
541
|
-
throw new Error("[traverse error]: cannot init CompileData");
|
|
542
|
-
const buildCache = data.Cache;
|
|
543
|
-
// 用于
|
|
544
|
-
const tempImport;
|
|
545
|
-
for (let nodeIndex = 0; nodeIndex < node.body.length; nodeIndex++) {
|
|
546
|
-
const nodeItem = node.body[nodeIndex];
|
|
547
|
-
if (!nodeItem)
|
|
548
|
-
continue;
|
|
549
|
-
// must in global
|
|
550
|
-
if (isTop) {
|
|
551
|
-
switch (nodeItem.type) {
|
|
552
|
-
case "ImportDeclaration":
|
|
553
|
-
const newsImport = Utils.ImportToCache(nodeItem);
|
|
554
|
-
buildCache.import.push(newsImport);
|
|
555
|
-
node.body.splice(nodeIndex, 1);
|
|
556
|
-
nodeIndex -= 1;
|
|
557
|
-
break;
|
|
558
|
-
case "ExportDefaultDeclaration":
|
|
559
|
-
case "ExportAllDeclaration":
|
|
560
|
-
case "ExportNamedDeclaration":
|
|
561
|
-
buildCache.export.push(nodeItem);
|
|
562
|
-
node.body.splice(nodeIndex, 1);
|
|
563
|
-
nodeIndex -= 1;
|
|
564
|
-
break;
|
|
565
|
-
}
|
|
566
|
-
}
|
|
567
|
-
// 都有的
|
|
568
|
-
switch (nodeItem.type) {
|
|
569
|
-
case "ExpressionStatement":
|
|
570
|
-
const Exp = nodeItem.expression;
|
|
571
|
-
if (Exp.type == "CallExpression") {
|
|
572
|
-
const callee = Exp.callee;
|
|
573
|
-
let name = [];
|
|
574
|
-
if (callee.type == "Identifier")
|
|
575
|
-
name.push(callee.name);
|
|
576
|
-
if (callee.type == "MemberExpression")
|
|
577
|
-
name = utils_node_js_1.default.memberExpressionToStringArray(callee, 3);
|
|
578
|
-
if (name.length >= 1)
|
|
579
|
-
buildCache.call.push({
|
|
580
|
-
arguments: Exp.arguments,
|
|
581
|
-
remove: () => {
|
|
582
|
-
node.body.splice(nodeIndex, 1);
|
|
583
|
-
},
|
|
584
|
-
source: name
|
|
585
|
-
});
|
|
586
|
-
}
|
|
587
|
-
break;
|
|
588
|
-
case "IfStatement":
|
|
589
|
-
// handler node
|
|
590
|
-
if (nodeItem.consequent)
|
|
591
|
-
this.traverse(t.blockStatement([nodeItem.consequent]), false, data);
|
|
592
|
-
if (nodeItem.alternate)
|
|
593
|
-
this.traverse(t.blockStatement([
|
|
594
|
-
nodeItem.alternate
|
|
595
|
-
]), false, data);
|
|
596
|
-
break;
|
|
597
|
-
}
|
|
598
|
-
}
|
|
599
|
-
return data;
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
exports.default = CompileMain;
|