transone-cli 0.1.2 → 0.2.0
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/dist/{build-mp-skmdzgyc.js → build-mp-kcnx989m.js} +263 -42
- package/dist/build-mp-kcnx989m.js.map +23 -0
- package/dist/cli.js +1 -1
- package/dist/create.d.ts +2 -2
- package/dist/{index-m62p34v6.js → index-5bdznb52.js} +30 -5
- package/dist/{index-m62p34v6.js.map → index-5bdznb52.js.map} +4 -4
- package/dist/index.js +1 -1
- package/dist/mp/inline.d.ts +3 -0
- package/dist/mp/jsgen.d.ts +0 -1
- package/dist/mp/types.d.ts +2 -0
- package/dist/mp/wxml.d.ts +2 -0
- package/package.json +2 -2
- package/dist/build-mp-skmdzgyc.js.map +0 -22
|
@@ -6,7 +6,7 @@ import {
|
|
|
6
6
|
// src/mp/build-mp.ts
|
|
7
7
|
import { cp, mkdir, readdir, rm, writeFile } from "fs/promises";
|
|
8
8
|
import { existsSync as existsSync2 } from "fs";
|
|
9
|
-
import { basename, dirname as
|
|
9
|
+
import { basename, dirname as dirname3, join as join3, relative, resolve as resolve2 } from "path";
|
|
10
10
|
|
|
11
11
|
// src/mp/ts-loader.ts
|
|
12
12
|
var tsModule = null;
|
|
@@ -371,43 +371,116 @@ function isComponentSubclass(context, classSource) {
|
|
|
371
371
|
return false;
|
|
372
372
|
}
|
|
373
373
|
function resolveImportedClass(context, from, localName, binding) {
|
|
374
|
-
if (
|
|
375
|
-
|
|
374
|
+
if (binding.specifier.startsWith(".")) {
|
|
375
|
+
const targetPath = resolveImportPath(from.filePath, binding.specifier);
|
|
376
|
+
if (!targetPath) {
|
|
377
|
+
throw mpError(from.filePath, `\u627E\u4E0D\u5230\u5BFC\u5165\u6587\u4EF6 "${binding.specifier}"\uFF08\u76F8\u5BF9 ${from.filePath}\uFF09`);
|
|
378
|
+
}
|
|
379
|
+
const source = readSource(context, targetPath);
|
|
380
|
+
const importedName = binding.importedName;
|
|
381
|
+
const found = findExportedClass(context, source, importedName, new Set);
|
|
382
|
+
if (!found) {
|
|
383
|
+
throw mpError(targetPath, `\u627E\u4E0D\u5230\u5BFC\u51FA\u7684\u7EC4\u4EF6\u7C7B "${importedName}"\uFF08\u4ECE ${localName} \u5BFC\u5165\uFF09`);
|
|
384
|
+
}
|
|
385
|
+
return {
|
|
386
|
+
filePath: found.filePath,
|
|
387
|
+
source: readSource(context, found.filePath),
|
|
388
|
+
declaration: found.declaration,
|
|
389
|
+
className: importedName
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
return resolvePackageClass(context, from, localName, binding);
|
|
393
|
+
}
|
|
394
|
+
function resolvePackageClass(context, from, localName, binding) {
|
|
395
|
+
const packageInfo = findPackageInfo(from.filePath, binding.specifier);
|
|
396
|
+
if (!packageInfo) {
|
|
397
|
+
throw mpError(from.filePath, `\u65E0\u6CD5\u89E3\u6790\u7EC4\u4EF6\u6765\u6E90 "${binding.specifier}"\uFF1A\u627E\u4E0D\u5230 node_modules \u5305\uFF08\u4EC5\u652F\u6301\u76F8\u5BF9\u8DEF\u5F84\u6216\u5DF2\u5B89\u88C5\u7684\u5305\uFF09`);
|
|
398
|
+
}
|
|
399
|
+
const sourceField = packageInfo.source;
|
|
400
|
+
if (typeof sourceField !== "string" || sourceField.length === 0) {
|
|
401
|
+
throw mpError(from.filePath, `\u5305 "${binding.specifier}" \u7F3A\u5C11 "source" \u5B57\u6BB5\uFF1A\u5C0F\u7A0B\u5E8F\u7F16\u8BD1\u9700\u8981\u53EF\u5206\u6790\u7684 TS \u6E90\u7801\u5165\u53E3\uFF08\u5728 package.json \u58F0\u660E "source": "./lib/index.ts"\uFF09`);
|
|
376
402
|
}
|
|
377
|
-
const targetPath =
|
|
403
|
+
const targetPath = resolvePackageSource(packageInfo.dir, sourceField);
|
|
378
404
|
if (!targetPath) {
|
|
379
|
-
throw mpError(from.filePath, `\
|
|
405
|
+
throw mpError(from.filePath, `\u5305 "${binding.specifier}" \u7684 "source" \u6307\u5411\u7684\u6587\u4EF6\u4E0D\u5B58\u5728: ${sourceField}`);
|
|
380
406
|
}
|
|
381
407
|
const source = readSource(context, targetPath);
|
|
382
408
|
const importedName = binding.importedName;
|
|
383
|
-
const
|
|
384
|
-
if (!
|
|
385
|
-
throw mpError(targetPath, `\u627E\u4E0D\u5230\u5BFC\u51FA\u7684\u7EC4\u4EF6\u7C7B "${importedName}"\uFF08\u4ECE ${localName} \u5BFC\u5165\uFF09`);
|
|
409
|
+
const found = findExportedClass(context, source, importedName, new Set);
|
|
410
|
+
if (!found) {
|
|
411
|
+
throw mpError(targetPath, `\u627E\u4E0D\u5230\u5BFC\u51FA\u7684\u7EC4\u4EF6\u7C7B "${importedName}"\uFF08\u4ECE ${localName} \u5BFC\u5165\uFF0C\u5305 "${binding.specifier}"\uFF09`);
|
|
386
412
|
}
|
|
387
413
|
return {
|
|
388
|
-
filePath:
|
|
389
|
-
source,
|
|
390
|
-
declaration,
|
|
414
|
+
filePath: found.filePath,
|
|
415
|
+
source: readSource(context, found.filePath),
|
|
416
|
+
declaration: found.declaration,
|
|
391
417
|
className: importedName
|
|
392
418
|
};
|
|
393
419
|
}
|
|
394
|
-
function
|
|
420
|
+
function findPackageInfo(fromFile, specifier) {
|
|
421
|
+
const segments = specifier.split("/");
|
|
422
|
+
const packageJsonRelative = join("node_modules", ...segments, "package.json");
|
|
423
|
+
let current = dirname(fromFile);
|
|
424
|
+
for (;; ) {
|
|
425
|
+
const candidate = join(current, packageJsonRelative);
|
|
426
|
+
if (existsSync(candidate)) {
|
|
427
|
+
try {
|
|
428
|
+
const json = JSON.parse(readFileSync(candidate, "utf8"));
|
|
429
|
+
return { dir: join(current, "node_modules", ...segments), source: json.source };
|
|
430
|
+
} catch {
|
|
431
|
+
return;
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
const parent = dirname(current);
|
|
435
|
+
if (parent === current) {
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
current = parent;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
function resolvePackageSource(packageDir, sourceField) {
|
|
442
|
+
const base = resolve(packageDir, sourceField);
|
|
443
|
+
const candidates = [base, `${base}.ts`, `${base}.tsx`];
|
|
444
|
+
return candidates.find((candidate) => existsSync(candidate));
|
|
445
|
+
}
|
|
446
|
+
function findExportedClass(context, source, name, seen) {
|
|
395
447
|
const { ts } = context;
|
|
448
|
+
if (seen.has(source.fileName)) {
|
|
449
|
+
return;
|
|
450
|
+
}
|
|
451
|
+
seen.add(source.fileName);
|
|
396
452
|
for (const statement of source.statements) {
|
|
397
453
|
if (ts.isClassDeclaration(statement) && statement.name?.text === name) {
|
|
398
454
|
if (isExported(ts, statement)) {
|
|
399
|
-
return statement;
|
|
455
|
+
return { filePath: source.fileName, declaration: statement };
|
|
400
456
|
}
|
|
401
457
|
}
|
|
402
458
|
}
|
|
403
459
|
for (const statement of source.statements) {
|
|
404
460
|
if (ts.isExportDeclaration(statement) && statement.exportClause && ts.isNamedExports(statement.exportClause)) {
|
|
405
461
|
for (const element of statement.exportClause.elements) {
|
|
406
|
-
if (element.name.text
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
462
|
+
if (element.name.text !== name) {
|
|
463
|
+
continue;
|
|
464
|
+
}
|
|
465
|
+
const local = collectLocalClasses(ts, source).get(name);
|
|
466
|
+
if (local) {
|
|
467
|
+
return { filePath: source.fileName, declaration: local };
|
|
468
|
+
}
|
|
469
|
+
if (!statement.moduleSpecifier) {
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
const specifier = statement.moduleSpecifier.getText().replace(/['"]/g, "");
|
|
473
|
+
if (!specifier.startsWith(".")) {
|
|
474
|
+
continue;
|
|
475
|
+
}
|
|
476
|
+
const targetPath = resolveImportPath(source.fileName, specifier);
|
|
477
|
+
if (!targetPath) {
|
|
478
|
+
continue;
|
|
479
|
+
}
|
|
480
|
+
const targetSource = readSource(context, targetPath);
|
|
481
|
+
const reExported = findExportedClass(context, targetSource, element.propertyName?.text ?? element.name.text, seen);
|
|
482
|
+
if (reExported) {
|
|
483
|
+
return reExported;
|
|
411
484
|
}
|
|
412
485
|
}
|
|
413
486
|
}
|
|
@@ -865,6 +938,9 @@ var TAG_KEYS = new Set([
|
|
|
865
938
|
"key",
|
|
866
939
|
"directions"
|
|
867
940
|
]);
|
|
941
|
+
function notUndefined(ts, expression) {
|
|
942
|
+
return expression !== undefined && !(ts.isIdentifier(expression) && expression.text === "undefined");
|
|
943
|
+
}
|
|
868
944
|
function isEventProp(key) {
|
|
869
945
|
return /^on[A-Z]/.test(key) || /^on[a-z]/.test(key);
|
|
870
946
|
}
|
|
@@ -1027,8 +1103,8 @@ function compileElementCall(context, classSource, node, env, depth) {
|
|
|
1027
1103
|
} else {
|
|
1028
1104
|
propsExpr = args[1];
|
|
1029
1105
|
childrenExpr = args[2];
|
|
1030
|
-
listenersExpr = args[3];
|
|
1031
|
-
directionsExpr = args[5];
|
|
1106
|
+
listenersExpr = notUndefined(ts, args[3]) ? args[3] : undefined;
|
|
1107
|
+
directionsExpr = notUndefined(ts, args[5]) ? args[5] : undefined;
|
|
1032
1108
|
}
|
|
1033
1109
|
} else {
|
|
1034
1110
|
tagExpr = ts.factory.createStringLiteral(kebabCase(name));
|
|
@@ -1327,16 +1403,22 @@ function compileComponent(context, classSource, classExpr, propsExpr, childrenEx
|
|
|
1327
1403
|
throw mpError(classSource.filePath, "emitters \u5FC5\u987B\u662F\u5BF9\u8C61\u5B57\u9762\u91CF");
|
|
1328
1404
|
}
|
|
1329
1405
|
for (const property of emittersExpr.properties) {
|
|
1330
|
-
if (!ts.isPropertyAssignment(property)
|
|
1406
|
+
if (!ts.isPropertyAssignment(property)) {
|
|
1407
|
+
throw mpError(classSource.filePath, "emitters \u53EA\u652F\u6301\u9759\u6001\u4E8B\u4EF6\u540D");
|
|
1408
|
+
}
|
|
1409
|
+
const eventName = ts.isIdentifier(property.name) || ts.isStringLiteral(property.name) ? property.name.text : undefined;
|
|
1410
|
+
if (eventName === undefined) {
|
|
1331
1411
|
throw mpError(classSource.filePath, "emitters \u53EA\u652F\u6301\u9759\u6001\u4E8B\u4EF6\u540D");
|
|
1332
1412
|
}
|
|
1333
1413
|
const usage = compileEmitterHandler(context, classSource, property.initializer);
|
|
1334
1414
|
if (!usage) {
|
|
1335
1415
|
throw mpError(classSource.filePath, "emitters \u5904\u7406\u5668\u53EA\u652F\u6301 () => this.method() \u6216 (value) => this.method(value)");
|
|
1336
1416
|
}
|
|
1337
|
-
usage.event =
|
|
1417
|
+
usage.event = eventName;
|
|
1418
|
+
const key = `__transone_emitter_${eventName}_${env.builder.emitters.length}`;
|
|
1419
|
+
usage.key = key;
|
|
1338
1420
|
env.builder.emitters.push(usage);
|
|
1339
|
-
attrs.push(`bind:${
|
|
1421
|
+
attrs.push(`bind:${eventName}="${key}"`);
|
|
1340
1422
|
}
|
|
1341
1423
|
}
|
|
1342
1424
|
if (directionsExpr !== undefined) {
|
|
@@ -1403,7 +1485,7 @@ function compileEmitterHandler(context, _classSource, handlerExpr) {
|
|
|
1403
1485
|
const { ts } = context;
|
|
1404
1486
|
handlerExpr = unwrapParentheses(ts, handlerExpr);
|
|
1405
1487
|
if (ts.isPropertyAccessExpression(handlerExpr) && isThisExpression(ts, handlerExpr.expression)) {
|
|
1406
|
-
return { event: "pending", method: handlerExpr.name.text, passArgs: true };
|
|
1488
|
+
return { event: "pending", method: handlerExpr.name.text, passArgs: true, key: "" };
|
|
1407
1489
|
}
|
|
1408
1490
|
if (!ts.isArrowFunction(handlerExpr)) {
|
|
1409
1491
|
return;
|
|
@@ -1413,11 +1495,11 @@ function compileEmitterHandler(context, _classSource, handlerExpr) {
|
|
|
1413
1495
|
return;
|
|
1414
1496
|
}
|
|
1415
1497
|
if (call.args.length === 0) {
|
|
1416
|
-
return { event: "pending", method: call.method, passArgs: false };
|
|
1498
|
+
return { event: "pending", method: call.method, passArgs: false, key: "" };
|
|
1417
1499
|
}
|
|
1418
1500
|
const arg = call.args.length === 1 ? unwrapTypeAssertions(ts, call.args[0]) : undefined;
|
|
1419
1501
|
if (call.args.length === 1 && handlerExpr.parameters.length === 1 && ts.isIdentifier(handlerExpr.parameters[0].name) && arg !== undefined && ts.isIdentifier(arg) && handlerExpr.parameters[0].name.text === arg.text) {
|
|
1420
|
-
return { event: "pending", method: call.method, passArgs: true };
|
|
1502
|
+
return { event: "pending", method: call.method, passArgs: true, key: "" };
|
|
1421
1503
|
}
|
|
1422
1504
|
return;
|
|
1423
1505
|
}
|
|
@@ -1503,13 +1585,37 @@ function compileStyleAttr(context, classSource, value, env) {
|
|
|
1503
1585
|
}
|
|
1504
1586
|
const cssKey = property.name.text.replace(/([A-Z])/g, "-$1").toLowerCase();
|
|
1505
1587
|
const folded = foldExpression(ts, property.initializer, env.scope.consts);
|
|
1506
|
-
if (typeof folded
|
|
1507
|
-
|
|
1588
|
+
if (typeof folded === "string" || typeof folded === "number") {
|
|
1589
|
+
declarations.push(`${cssKey}: ${String(folded)};`);
|
|
1590
|
+
} else {
|
|
1591
|
+
const unitSplit = splitUnitExpression(ts, property.initializer);
|
|
1592
|
+
if (unitSplit) {
|
|
1593
|
+
declarations.push(`${cssKey}: {{${toBinding(bindingContext(context, classSource, env), unitSplit.expr)}}}${unitSplit.unit};`);
|
|
1594
|
+
} else {
|
|
1595
|
+
declarations.push(`${cssKey}: {{${toBinding(bindingContext(context, classSource, env), property.initializer)}}};`);
|
|
1596
|
+
}
|
|
1508
1597
|
}
|
|
1509
|
-
declarations.push(`${cssKey}: ${String(folded)};`);
|
|
1510
1598
|
}
|
|
1511
1599
|
return `style="${escapeAttr(declarations.join(" "))}"`;
|
|
1512
1600
|
}
|
|
1601
|
+
function splitUnitExpression(ts, expr) {
|
|
1602
|
+
expr = unwrapParentheses(ts, expr);
|
|
1603
|
+
if (!ts.isBinaryExpression(expr)) {
|
|
1604
|
+
return;
|
|
1605
|
+
}
|
|
1606
|
+
if (expr.operatorToken.kind !== ts.SyntaxKind.PlusToken) {
|
|
1607
|
+
return;
|
|
1608
|
+
}
|
|
1609
|
+
const right = unwrapParentheses(ts, expr.right);
|
|
1610
|
+
if (!ts.isStringLiteral(right)) {
|
|
1611
|
+
return;
|
|
1612
|
+
}
|
|
1613
|
+
const unit = right.text;
|
|
1614
|
+
if (!/^[a-z%]+$/i.test(unit)) {
|
|
1615
|
+
return;
|
|
1616
|
+
}
|
|
1617
|
+
return { expr: expr.left, unit };
|
|
1618
|
+
}
|
|
1513
1619
|
function compileGenericAttr(context, classSource, key, value, env) {
|
|
1514
1620
|
const { ts } = context;
|
|
1515
1621
|
const attrName = wxmlAttrName(key);
|
|
@@ -2291,6 +2397,104 @@ function collectModuleConsts4(context, classSource) {
|
|
|
2291
2397
|
return consts;
|
|
2292
2398
|
}
|
|
2293
2399
|
|
|
2400
|
+
// src/mp/inline.ts
|
|
2401
|
+
import { dirname as dirname2, join as join2 } from "path";
|
|
2402
|
+
function collectInlineModules(context, classSource) {
|
|
2403
|
+
const { ts } = context;
|
|
2404
|
+
const output = [];
|
|
2405
|
+
const seen = new Set;
|
|
2406
|
+
const stack = new Set;
|
|
2407
|
+
const walk = (source) => {
|
|
2408
|
+
const filePath = source.fileName;
|
|
2409
|
+
if (seen.has(filePath)) {
|
|
2410
|
+
return;
|
|
2411
|
+
}
|
|
2412
|
+
for (const statement of source.statements) {
|
|
2413
|
+
if (!ts.isImportDeclaration(statement)) {
|
|
2414
|
+
continue;
|
|
2415
|
+
}
|
|
2416
|
+
const specifier = statement.moduleSpecifier;
|
|
2417
|
+
if (!ts.isStringLiteral(specifier)) {
|
|
2418
|
+
continue;
|
|
2419
|
+
}
|
|
2420
|
+
const target = resolveRelativeModule(context, filePath, specifier.text);
|
|
2421
|
+
if (!target) {
|
|
2422
|
+
continue;
|
|
2423
|
+
}
|
|
2424
|
+
if (stack.has(target)) {
|
|
2425
|
+
warn(context, `\u6A21\u5757\u5FAA\u73AF\u5F15\u7528\u5DF2\u8DF3\u8FC7: ${target}`);
|
|
2426
|
+
continue;
|
|
2427
|
+
}
|
|
2428
|
+
stack.add(target);
|
|
2429
|
+
walk(readSource(context, target));
|
|
2430
|
+
stack.delete(target);
|
|
2431
|
+
}
|
|
2432
|
+
seen.add(filePath);
|
|
2433
|
+
output.push(compileInlineModule(context, source));
|
|
2434
|
+
};
|
|
2435
|
+
walk(classSource.source);
|
|
2436
|
+
return output;
|
|
2437
|
+
}
|
|
2438
|
+
function resolveRelativeModule(context, fromFile, specifier) {
|
|
2439
|
+
if (!specifier.startsWith("./") && !specifier.startsWith("../")) {
|
|
2440
|
+
return null;
|
|
2441
|
+
}
|
|
2442
|
+
const base = specifier.endsWith(".ts") ? specifier : `${specifier}.ts`;
|
|
2443
|
+
const resolved = join2(dirname2(fromFile), base);
|
|
2444
|
+
if (isComponentFile(context, resolved)) {
|
|
2445
|
+
return null;
|
|
2446
|
+
}
|
|
2447
|
+
return resolved;
|
|
2448
|
+
}
|
|
2449
|
+
function isComponentFile(context, filePath) {
|
|
2450
|
+
const { ts } = context;
|
|
2451
|
+
const source = readSource(context, filePath);
|
|
2452
|
+
for (const statement of source.statements) {
|
|
2453
|
+
if (!ts.isClassDeclaration(statement)) {
|
|
2454
|
+
continue;
|
|
2455
|
+
}
|
|
2456
|
+
const name = statement.name?.text;
|
|
2457
|
+
if (!name) {
|
|
2458
|
+
continue;
|
|
2459
|
+
}
|
|
2460
|
+
const isExported2 = statement.modifiers?.some((m) => m.kind === ts.SyntaxKind.ExportKeyword);
|
|
2461
|
+
if (!isExported2) {
|
|
2462
|
+
continue;
|
|
2463
|
+
}
|
|
2464
|
+
const heritage = statement.heritageClauses?.some((clause) => clause.types.some((type) => {
|
|
2465
|
+
const expr = type.expression;
|
|
2466
|
+
if (!ts.isIdentifier(expr)) {
|
|
2467
|
+
return false;
|
|
2468
|
+
}
|
|
2469
|
+
return expr.text === "Component" || expr.text.endsWith("Component");
|
|
2470
|
+
}));
|
|
2471
|
+
if (heritage) {
|
|
2472
|
+
return true;
|
|
2473
|
+
}
|
|
2474
|
+
}
|
|
2475
|
+
return false;
|
|
2476
|
+
}
|
|
2477
|
+
function compileInlineModule(context, source) {
|
|
2478
|
+
const { ts } = context;
|
|
2479
|
+
const kept = [];
|
|
2480
|
+
for (const statement of source.statements) {
|
|
2481
|
+
if (ts.isImportDeclaration(statement) || ts.isExportDeclaration(statement) || ts.isExportAssignment(statement) || ts.isInterfaceDeclaration(statement) || ts.isTypeAliasDeclaration(statement)) {
|
|
2482
|
+
continue;
|
|
2483
|
+
}
|
|
2484
|
+
kept.push(statement);
|
|
2485
|
+
}
|
|
2486
|
+
const printer = ts.createPrinter({ newLine: ts.NewLineKind.LineFeed });
|
|
2487
|
+
const printed = printer.printList(ts.ListFormat.MultiLine, ts.factory.createNodeArray(kept), source);
|
|
2488
|
+
const transpiled = ts.transpileModule(printed, {
|
|
2489
|
+
compilerOptions: {
|
|
2490
|
+
target: ts.ScriptTarget.ESNext,
|
|
2491
|
+
module: ts.ModuleKind.ES2015
|
|
2492
|
+
},
|
|
2493
|
+
reportDiagnostics: false
|
|
2494
|
+
});
|
|
2495
|
+
return transpiled.outputText.replace(/^export\s+/gm, "").trim();
|
|
2496
|
+
}
|
|
2497
|
+
|
|
2294
2498
|
// src/mp/compile.ts
|
|
2295
2499
|
function compileUnit(context, classSource, options) {
|
|
2296
2500
|
const { kind, name } = options;
|
|
@@ -2313,10 +2517,11 @@ function compileUnit(context, classSource, options) {
|
|
|
2313
2517
|
usedNames.add(emitter.method);
|
|
2314
2518
|
}
|
|
2315
2519
|
const customEvents = new Set;
|
|
2520
|
+
const inlineModules = collectInlineModules(context, classSource);
|
|
2316
2521
|
const compiledMethods = compileMethods(context, classSource, usedNames, kind, customEvents);
|
|
2317
2522
|
const methods = [...compiledMethods.methods];
|
|
2318
2523
|
for (const emitter of builder.emitters) {
|
|
2319
|
-
const key = `__transone_emitter_${emitter.event}`;
|
|
2524
|
+
const key = emitter.key ?? `__transone_emitter_${emitter.event}`;
|
|
2320
2525
|
const fn = emitter.passArgs ? `function(e) { this.${emitter.method}(...e.detail.args); }` : `function() { this.${emitter.method}(); }`;
|
|
2321
2526
|
methods.push({ key, fn });
|
|
2322
2527
|
}
|
|
@@ -2337,7 +2542,8 @@ function compileUnit(context, classSource, options) {
|
|
|
2337
2542
|
methods,
|
|
2338
2543
|
lifecycle: compiledMethods.lifecycle,
|
|
2339
2544
|
customEvents: [...customEvents],
|
|
2340
|
-
usingComponents
|
|
2545
|
+
usingComponents,
|
|
2546
|
+
inlineModules
|
|
2341
2547
|
};
|
|
2342
2548
|
if (kind === "component") {
|
|
2343
2549
|
context.components.set(name, unit);
|
|
@@ -2370,14 +2576,25 @@ function finalizeComponentProperties(context) {
|
|
|
2370
2576
|
|
|
2371
2577
|
// src/mp/jsgen.ts
|
|
2372
2578
|
var GENERATED_HEADER = (dialectId) => `// \u7531 transone build --target ${dialectId} \u751F\u6210\uFF0C\u8BF7\u52FF\u624B\u52A8\u7F16\u8F91`;
|
|
2579
|
+
function inlinePreamble(unit) {
|
|
2580
|
+
if (unit.inlineModules.length === 0) {
|
|
2581
|
+
return "";
|
|
2582
|
+
}
|
|
2583
|
+
return unit.inlineModules.join(`
|
|
2584
|
+
|
|
2585
|
+
`) + `
|
|
2586
|
+
|
|
2587
|
+
`;
|
|
2588
|
+
}
|
|
2373
2589
|
function generatePageJs(unit, dialect) {
|
|
2374
2590
|
const parts = [
|
|
2375
2591
|
GENERATED_HEADER(dialect.id),
|
|
2592
|
+
inlinePreamble(unit),
|
|
2376
2593
|
"Page({",
|
|
2377
2594
|
` data: ${embedData(unit.data)},`
|
|
2378
2595
|
];
|
|
2379
2596
|
for (const entry of [...unit.methods, ...unit.lifecycle]) {
|
|
2380
|
-
parts.push(` ${entry.key}: ${entry.fn},`);
|
|
2597
|
+
parts.push(` ${JSON.stringify(entry.key)}: ${entry.fn},`);
|
|
2381
2598
|
}
|
|
2382
2599
|
parts.push("});");
|
|
2383
2600
|
return parts.join(`
|
|
@@ -2385,18 +2602,22 @@ function generatePageJs(unit, dialect) {
|
|
|
2385
2602
|
`;
|
|
2386
2603
|
}
|
|
2387
2604
|
function generateComponentJs(unit, dialect) {
|
|
2388
|
-
const parts = [
|
|
2605
|
+
const parts = [
|
|
2606
|
+
GENERATED_HEADER(dialect.id),
|
|
2607
|
+
inlinePreamble(unit),
|
|
2608
|
+
"Component({"
|
|
2609
|
+
];
|
|
2389
2610
|
parts.push(` properties: ${generatePropertiesSource(unit.properties)},`);
|
|
2390
2611
|
parts.push(` data: ${embedData(unit.data)},`);
|
|
2391
2612
|
parts.push(" methods: {");
|
|
2392
2613
|
for (const entry of unit.methods) {
|
|
2393
|
-
parts.push(` ${entry.key}: ${entry.fn},`);
|
|
2614
|
+
parts.push(` ${JSON.stringify(entry.key)}: ${entry.fn},`);
|
|
2394
2615
|
}
|
|
2395
2616
|
parts.push(" },");
|
|
2396
2617
|
if (unit.lifecycle.length > 0) {
|
|
2397
2618
|
parts.push(" lifetimes: {");
|
|
2398
2619
|
for (const entry of unit.lifecycle) {
|
|
2399
|
-
parts.push(` ${entry.key}: ${entry.fn},`);
|
|
2620
|
+
parts.push(` ${JSON.stringify(entry.key)}: ${entry.fn},`);
|
|
2400
2621
|
}
|
|
2401
2622
|
parts.push(" },");
|
|
2402
2623
|
}
|
|
@@ -2462,8 +2683,8 @@ async function buildMiniProgram(config, dialect) {
|
|
|
2462
2683
|
await mkdir(outDir, { recursive: true });
|
|
2463
2684
|
const assetsBuilt = [];
|
|
2464
2685
|
const write = async (path, content) => {
|
|
2465
|
-
const target =
|
|
2466
|
-
await mkdir(
|
|
2686
|
+
const target = join3(outDir, path);
|
|
2687
|
+
await mkdir(dirname3(target), { recursive: true });
|
|
2467
2688
|
await writeFile(target, content, "utf8");
|
|
2468
2689
|
assetsBuilt.push(target);
|
|
2469
2690
|
};
|
|
@@ -2520,9 +2741,9 @@ async function buildMiniProgram(config, dialect) {
|
|
|
2520
2741
|
}
|
|
2521
2742
|
if (existsSync2(mp.publicDir)) {
|
|
2522
2743
|
for (const file of await listFiles(mp.publicDir)) {
|
|
2523
|
-
const target =
|
|
2744
|
+
const target = join3(outDir, file);
|
|
2524
2745
|
if (!existsSync2(target)) {
|
|
2525
|
-
await cp(
|
|
2746
|
+
await cp(join3(mp.publicDir, file), target, {
|
|
2526
2747
|
recursive: true,
|
|
2527
2748
|
force: true,
|
|
2528
2749
|
errorOnExist: false
|
|
@@ -2557,7 +2778,7 @@ async function listFiles(root, dir = root) {
|
|
|
2557
2778
|
const out = [];
|
|
2558
2779
|
const entries = await readdir(dir, { withFileTypes: true });
|
|
2559
2780
|
for (const entry of entries) {
|
|
2560
|
-
const abs =
|
|
2781
|
+
const abs = join3(dir, entry.name);
|
|
2561
2782
|
if (entry.isDirectory()) {
|
|
2562
2783
|
out.push(...await listFiles(root, abs));
|
|
2563
2784
|
} else {
|
|
@@ -2570,5 +2791,5 @@ export {
|
|
|
2570
2791
|
buildMiniProgram
|
|
2571
2792
|
};
|
|
2572
2793
|
|
|
2573
|
-
//# debugId=
|
|
2574
|
-
//# sourceMappingURL=build-mp-
|
|
2794
|
+
//# debugId=1B46276CC3CBABAA64756E2164756E21
|
|
2795
|
+
//# sourceMappingURL=build-mp-kcnx989m.js.map
|