@vureact/compiler-core 1.2.0 → 1.3.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/lib/{chunk-FCLIDEIZ.js → chunk-7LBUUA24.js} +986 -861
- package/lib/{chunk-UJZGDNNB.esm.js → chunk-CUCUXW56.esm.js} +887 -762
- package/lib/cli.d.cts +1 -2
- package/lib/cli.d.ts +1 -2
- package/lib/cli.esm.js +47 -2
- package/lib/cli.js +54 -9
- package/lib/compiler-core.d.cts +9 -0
- package/lib/compiler-core.d.ts +9 -0
- package/lib/compiler-core.esm.js +2 -2
- package/lib/compiler-core.js +3 -3
- package/package.json +13 -5
- package/templates/route-setup-notes.md +192 -0
- package/templates/route-setup-notes.zh.md +192 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* @vureact/compiler-core v1.
|
|
2
|
+
* @vureact/compiler-core v1.3.0
|
|
3
3
|
* (c) 2025-present Ruihong Zhong (Ryan John)
|
|
4
4
|
* @license MIT
|
|
5
5
|
*/
|
|
@@ -24,7 +24,7 @@ function executePlugins(map, result, ctx) {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
// src/core/codegen/component/jsx/syntax-processor/postprocess/build-ctx-provider.ts
|
|
27
|
-
import * as
|
|
27
|
+
import * as t11 from "@babel/types";
|
|
28
28
|
|
|
29
29
|
// src/consts/other.ts
|
|
30
30
|
var PACKAGE_NAME = {
|
|
@@ -312,11 +312,27 @@ var ADAPTER_RULES = {
|
|
|
312
312
|
package: PACKAGE_NAME.router,
|
|
313
313
|
type: "rename"
|
|
314
314
|
},
|
|
315
|
+
// =============== Methods ===============
|
|
315
316
|
createRouter: {
|
|
316
317
|
target: "createRouter",
|
|
317
318
|
package: PACKAGE_NAME.router,
|
|
318
319
|
type: "rename"
|
|
319
320
|
},
|
|
321
|
+
createWebHistory: {
|
|
322
|
+
target: "createWebHistory",
|
|
323
|
+
package: PACKAGE_NAME.router,
|
|
324
|
+
type: "rename"
|
|
325
|
+
},
|
|
326
|
+
createWebHashHistory: {
|
|
327
|
+
target: "createWebHashHistory",
|
|
328
|
+
package: PACKAGE_NAME.router,
|
|
329
|
+
type: "rename"
|
|
330
|
+
},
|
|
331
|
+
createMemoryHistory: {
|
|
332
|
+
target: "createMemoryHistory",
|
|
333
|
+
package: PACKAGE_NAME.router,
|
|
334
|
+
type: "rename"
|
|
335
|
+
},
|
|
320
336
|
// =============== Hooks ===============
|
|
321
337
|
useRoute: {
|
|
322
338
|
target: "useRoute",
|
|
@@ -369,6 +385,56 @@ var VUE_API_MAP = {
|
|
|
369
385
|
RouterLink: "RouterLink"
|
|
370
386
|
};
|
|
371
387
|
|
|
388
|
+
// src/shared/babel-utils.ts
|
|
389
|
+
import { parseExpression } from "@babel/parser";
|
|
390
|
+
function getBabelParseOptions(lang = "js", context = "script", filename) {
|
|
391
|
+
const baseOptions = {
|
|
392
|
+
sourceType: "module",
|
|
393
|
+
sourceFilename: filename ?? "anonymous",
|
|
394
|
+
errorRecovery: true
|
|
395
|
+
// 容错模式
|
|
396
|
+
};
|
|
397
|
+
const plugins = [];
|
|
398
|
+
if (lang.includes("ts")) {
|
|
399
|
+
plugins.push("typescript");
|
|
400
|
+
}
|
|
401
|
+
if (lang.endsWith("sx")) {
|
|
402
|
+
if (lang.startsWith("t")) plugins.push("typescript");
|
|
403
|
+
plugins.push("jsx");
|
|
404
|
+
}
|
|
405
|
+
if (context === "vueTemplate") {
|
|
406
|
+
plugins.push("decorators-legacy");
|
|
407
|
+
} else if (context === "expression") {
|
|
408
|
+
baseOptions.sourceType = "script";
|
|
409
|
+
baseOptions.allowReturnOutsideFunction = true;
|
|
410
|
+
baseOptions.allowSuperOutsideMethod = true;
|
|
411
|
+
}
|
|
412
|
+
plugins.push("classProperties", "objectRestSpread", "asyncGenerators");
|
|
413
|
+
return {
|
|
414
|
+
...baseOptions,
|
|
415
|
+
plugins: [...new Set(plugins)]
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
function stringToExpr(input, lang, filename = "") {
|
|
419
|
+
return parseExpression(input, getBabelParseOptions(lang, "expression", filename));
|
|
420
|
+
}
|
|
421
|
+
function atComponentOrHookRoot(path8, rootScope, inScriptFile = false) {
|
|
422
|
+
const { parentPath, scope } = path8;
|
|
423
|
+
const parentBlock = scope.block;
|
|
424
|
+
if (!parentPath) return !inScriptFile;
|
|
425
|
+
if (parentBlock === rootScope) {
|
|
426
|
+
if (inScriptFile) return false;
|
|
427
|
+
if (parentPath.isBlockStatement() && parentPath.node !== rootScope) {
|
|
428
|
+
return false;
|
|
429
|
+
}
|
|
430
|
+
if (parentPath.isCallExpression() || parentPath.isNewExpression()) {
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
return true;
|
|
434
|
+
}
|
|
435
|
+
return false;
|
|
436
|
+
}
|
|
437
|
+
|
|
372
438
|
// src/core/codegen/component/jsx/utils/jsx-element-utils.ts
|
|
373
439
|
import * as t from "@babel/types";
|
|
374
440
|
function createJsxElement(tag, props, children, selfClosing) {
|
|
@@ -382,7 +448,7 @@ function createJsxElement(tag, props, children, selfClosing) {
|
|
|
382
448
|
}
|
|
383
449
|
|
|
384
450
|
// src/core/codegen/component/jsx/syntax-processor/process/build-condition-node.ts
|
|
385
|
-
import * as
|
|
451
|
+
import * as t10 from "@babel/types";
|
|
386
452
|
|
|
387
453
|
// src/core/codegen/component/jsx/utils/jsx-expression-utils.ts
|
|
388
454
|
import * as t2 from "@babel/types";
|
|
@@ -478,7 +544,82 @@ function buildJsxChildren(children, ctx) {
|
|
|
478
544
|
}
|
|
479
545
|
|
|
480
546
|
// src/core/codegen/component/jsx/syntax-processor/process/build-loop-node.ts
|
|
547
|
+
import * as t6 from "@babel/types";
|
|
548
|
+
|
|
549
|
+
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
481
550
|
import * as t5 from "@babel/types";
|
|
551
|
+
|
|
552
|
+
// src/utils/camelCase.ts
|
|
553
|
+
var camelCase = (str) => {
|
|
554
|
+
if (str.includes("-")) {
|
|
555
|
+
return str.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
|
|
556
|
+
}
|
|
557
|
+
return str;
|
|
558
|
+
};
|
|
559
|
+
|
|
560
|
+
// src/utils/capitalize.ts
|
|
561
|
+
var capitalize = (str) => {
|
|
562
|
+
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
// src/core/transform/sfc/template/shared/resolve-string-expression/special-expressions.ts
|
|
566
|
+
function resolveSpecialExpressions(input, ctx) {
|
|
567
|
+
input = resolveEmitsCalls(input, ctx);
|
|
568
|
+
input = resolveRefVariable(input, ctx);
|
|
569
|
+
return input;
|
|
570
|
+
}
|
|
571
|
+
function resolveEmitsCalls(input, ctx) {
|
|
572
|
+
const result = matchEmitCalls(input, ctx);
|
|
573
|
+
if (!result) return input;
|
|
574
|
+
const [, , eventName, args] = result;
|
|
575
|
+
const callee = eventName.split(/[:\-]/).map((part) => capitalize(camelCase(part))).join("");
|
|
576
|
+
const event = args ? `on${callee}?.(${args})` : `on${callee}?.()`;
|
|
577
|
+
return `${ctx.propField}?.${event}`;
|
|
578
|
+
}
|
|
579
|
+
function matchEmitCalls(input, ctx) {
|
|
580
|
+
const { reactiveBindings } = ctx.templateData;
|
|
581
|
+
const macroBinding = Object.values(reactiveBindings).find((b) => b.source === "defineEmits");
|
|
582
|
+
if (!macroBinding) return null;
|
|
583
|
+
const escapedName = macroBinding.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
584
|
+
const regex = new RegExp(
|
|
585
|
+
`${escapedName}\\s*\\(\\s*(['"\`])([^\\1]*?)\\1\\s*(?:,\\s*(.*?))?\\s*\\)$`,
|
|
586
|
+
// 可选的第二个参数
|
|
587
|
+
"s"
|
|
588
|
+
// s 标志让 . 匹配换行符
|
|
589
|
+
);
|
|
590
|
+
return input.trim().match(regex);
|
|
591
|
+
}
|
|
592
|
+
function resolveRefVariable(input, ctx) {
|
|
593
|
+
const { reactiveBindings } = ctx.templateData;
|
|
594
|
+
const addValueProperty = (input2, varName) => {
|
|
595
|
+
const escapedVarName = varName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
596
|
+
const regex = new RegExp(
|
|
597
|
+
`(?<![a-zA-Z0-9_\\.])${escapedVarName}(?![a-zA-Z0-9_])(?!\\.value)`,
|
|
598
|
+
"g"
|
|
599
|
+
);
|
|
600
|
+
return input2.replace(regex, `${varName}.value`);
|
|
601
|
+
};
|
|
602
|
+
for (const name in reactiveBindings) {
|
|
603
|
+
const binding = reactiveBindings[name];
|
|
604
|
+
if (binding?.reactiveType !== "ref") continue;
|
|
605
|
+
input = addValueProperty(input, name);
|
|
606
|
+
}
|
|
607
|
+
return input;
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
611
|
+
function resolveStringExpr(input, ctx, toStrLiteral = false) {
|
|
612
|
+
if (toStrLiteral) return t5.stringLiteral(input);
|
|
613
|
+
const { filename, scriptData } = ctx;
|
|
614
|
+
const newContent = resolveSpecialExpressions(input, ctx);
|
|
615
|
+
try {
|
|
616
|
+
return stringToExpr(newContent, scriptData.lang, filename);
|
|
617
|
+
} catch {
|
|
618
|
+
return t5.identifier(newContent);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// src/core/codegen/component/jsx/syntax-processor/process/build-loop-node.ts
|
|
482
623
|
function buildLoopNode(nodeIR, ctx) {
|
|
483
624
|
const loop = nodeIR.meta.loop;
|
|
484
625
|
loop.isHandled = true;
|
|
@@ -488,15 +629,15 @@ function buildLoopNode(nodeIR, ctx) {
|
|
|
488
629
|
function buildArrayLoopNode(nodeIR, ctx) {
|
|
489
630
|
const loop = nodeIR.meta.loop;
|
|
490
631
|
const { source, value, index, key } = loop.value;
|
|
491
|
-
const
|
|
492
|
-
const params = [
|
|
632
|
+
const sourceExpression = resolveStringExpr(source, ctx);
|
|
633
|
+
const params = [t6.identifier(value)];
|
|
493
634
|
if (index ?? key) {
|
|
494
|
-
params.push(
|
|
635
|
+
params.push(t6.identifier(index ?? key));
|
|
495
636
|
}
|
|
496
|
-
const mapCallExpression =
|
|
497
|
-
|
|
637
|
+
const mapCallExpression = t6.callExpression(
|
|
638
|
+
t6.memberExpression(sourceExpression, t6.identifier("map")),
|
|
498
639
|
[
|
|
499
|
-
|
|
640
|
+
t6.arrowFunctionExpression(
|
|
500
641
|
params,
|
|
501
642
|
convertJsxChildToExpression(buildElementNode(nodeIR, ctx))
|
|
502
643
|
)
|
|
@@ -507,21 +648,21 @@ function buildArrayLoopNode(nodeIR, ctx) {
|
|
|
507
648
|
function buildObjectLoopNode(nodeIR, ctx) {
|
|
508
649
|
const loop = nodeIR.meta.loop;
|
|
509
650
|
const { source, value, key, index } = loop.value;
|
|
510
|
-
const
|
|
511
|
-
const objectEntriesCallExpression =
|
|
512
|
-
|
|
513
|
-
[
|
|
651
|
+
const sourceExpression = resolveStringExpr(source, ctx);
|
|
652
|
+
const objectEntriesCallExpression = t6.callExpression(
|
|
653
|
+
t6.memberExpression(t6.identifier("Object"), t6.identifier("entries")),
|
|
654
|
+
[sourceExpression]
|
|
514
655
|
);
|
|
515
656
|
const params = [
|
|
516
|
-
|
|
657
|
+
t6.arrayPattern([t6.identifier(key || "key"), t6.identifier(value)])
|
|
517
658
|
];
|
|
518
659
|
if (index) {
|
|
519
|
-
params.push(
|
|
660
|
+
params.push(t6.identifier(index));
|
|
520
661
|
}
|
|
521
|
-
const mapCallExpression =
|
|
522
|
-
|
|
662
|
+
const mapCallExpression = t6.callExpression(
|
|
663
|
+
t6.memberExpression(objectEntriesCallExpression, t6.identifier("map")),
|
|
523
664
|
[
|
|
524
|
-
|
|
665
|
+
t6.arrowFunctionExpression(
|
|
525
666
|
params,
|
|
526
667
|
convertJsxChildToExpression(buildElementNode(nodeIR, ctx))
|
|
527
668
|
)
|
|
@@ -531,25 +672,25 @@ function buildObjectLoopNode(nodeIR, ctx) {
|
|
|
531
672
|
}
|
|
532
673
|
|
|
533
674
|
// src/core/codegen/component/jsx/syntax-processor/process/build-memo-node.ts
|
|
534
|
-
import * as
|
|
675
|
+
import * as t7 from "@babel/types";
|
|
535
676
|
function buildMemoNode(nodeIR, ctx) {
|
|
536
677
|
const memo = nodeIR.meta.memo;
|
|
537
678
|
memo.isHandled = true;
|
|
538
679
|
const depsExpression = memo.babelExp.ast;
|
|
539
680
|
const bodyExpression = convertJsxChildToExpression(buildElementNode(nodeIR, ctx));
|
|
540
|
-
const useMemoCallExpression =
|
|
541
|
-
|
|
681
|
+
const useMemoCallExpression = t7.callExpression(t7.identifier("useMemo"), [
|
|
682
|
+
t7.arrowFunctionExpression([], bodyExpression),
|
|
542
683
|
depsExpression
|
|
543
684
|
]);
|
|
544
685
|
return buildJsxExpressionNode(useMemoCallExpression);
|
|
545
686
|
}
|
|
546
687
|
|
|
547
688
|
// src/core/codegen/component/jsx/syntax-processor/process/build-props.ts
|
|
548
|
-
import * as
|
|
689
|
+
import * as t9 from "@babel/types";
|
|
549
690
|
|
|
550
691
|
// src/core/codegen/component/jsx/syntax-processor/process/build-slot-prop.ts
|
|
551
|
-
import { parseExpression } from "@babel/parser";
|
|
552
|
-
import * as
|
|
692
|
+
import { parseExpression as parseExpression2 } from "@babel/parser";
|
|
693
|
+
import * as t8 from "@babel/types";
|
|
553
694
|
|
|
554
695
|
// src/shared/logger.ts
|
|
555
696
|
import kleur2 from "kleur";
|
|
@@ -558,6 +699,9 @@ import kleur2 from "kleur";
|
|
|
558
699
|
import { minimatch } from "minimatch";
|
|
559
700
|
import { fileURLToPath } from "url";
|
|
560
701
|
import path from "path";
|
|
702
|
+
function getDirname(metaUrl) {
|
|
703
|
+
return path.dirname(fileURLToPath(metaUrl));
|
|
704
|
+
}
|
|
561
705
|
var PathFilter = class {
|
|
562
706
|
patterns = [];
|
|
563
707
|
cwd;
|
|
@@ -798,7 +942,7 @@ var logger = new Logger();
|
|
|
798
942
|
|
|
799
943
|
// src/core/codegen/component/jsx/syntax-processor/process/build-slot-prop.ts
|
|
800
944
|
function buildSlotProp(nodeIR, ctx) {
|
|
801
|
-
const slotKey =
|
|
945
|
+
const slotKey = t8.jsxIdentifier(nodeIR.name);
|
|
802
946
|
const childrenNodeIR = !nodeIR.isScoped ? nodeIR.content : nodeIR.callback?.exp;
|
|
803
947
|
if (!childrenNodeIR?.length) {
|
|
804
948
|
return null;
|
|
@@ -807,7 +951,7 @@ function buildSlotProp(nodeIR, ctx) {
|
|
|
807
951
|
if (!jsxChild) {
|
|
808
952
|
return null;
|
|
809
953
|
}
|
|
810
|
-
const slotValue = nodeIR.isScoped ?
|
|
954
|
+
const slotValue = nodeIR.isScoped ? t8.arrowFunctionExpression(
|
|
811
955
|
buildSlotCallbackParams(nodeIR, ctx),
|
|
812
956
|
convertJsxChildToExpression(jsxChild)
|
|
813
957
|
) : jsxChild;
|
|
@@ -815,16 +959,16 @@ function buildSlotProp(nodeIR, ctx) {
|
|
|
815
959
|
return slotValue;
|
|
816
960
|
}
|
|
817
961
|
if (!nodeIR.isStatic) {
|
|
818
|
-
const dynamicSlotKey =
|
|
819
|
-
const spreadObject =
|
|
820
|
-
|
|
962
|
+
const dynamicSlotKey = parseExpression2(nodeIR.name);
|
|
963
|
+
const spreadObject = t8.objectExpression([
|
|
964
|
+
t8.objectProperty(dynamicSlotKey, convertSlotValueToExpression(slotValue), true)
|
|
821
965
|
]);
|
|
822
|
-
return
|
|
966
|
+
return t8.jsxSpreadAttribute(spreadObject);
|
|
823
967
|
}
|
|
824
|
-
return
|
|
968
|
+
return t8.jsxAttribute(slotKey, buildJsxExpressionNode(convertSlotValueToExpression(slotValue)));
|
|
825
969
|
}
|
|
826
970
|
function convertSlotValueToExpression(nodeIR) {
|
|
827
|
-
if (
|
|
971
|
+
if (t8.isArrowFunctionExpression(nodeIR)) {
|
|
828
972
|
return nodeIR;
|
|
829
973
|
}
|
|
830
974
|
return convertJsxChildToExpression(nodeIR);
|
|
@@ -836,8 +980,8 @@ function buildSlotCallbackParams(nodeIR, ctx) {
|
|
|
836
980
|
}
|
|
837
981
|
const source = rawArg.startsWith("(") ? `${rawArg} => null` : `(${rawArg}) => null`;
|
|
838
982
|
try {
|
|
839
|
-
const expression =
|
|
840
|
-
if (!
|
|
983
|
+
const expression = parseExpression2(source);
|
|
984
|
+
if (!t8.isArrowFunctionExpression(expression)) {
|
|
841
985
|
return [];
|
|
842
986
|
}
|
|
843
987
|
return expression.params;
|
|
@@ -880,13 +1024,13 @@ function buildStandardProp(nodeIR) {
|
|
|
880
1024
|
}
|
|
881
1025
|
} = nodeIR;
|
|
882
1026
|
if (!isStatic || isKeyLessVBind) {
|
|
883
|
-
return
|
|
1027
|
+
return t9.jsxSpreadAttribute(valueAST);
|
|
884
1028
|
}
|
|
885
1029
|
let value;
|
|
886
1030
|
if (content !== "true") {
|
|
887
|
-
value = isStringLiteral13 ?
|
|
1031
|
+
value = isStringLiteral13 ? t9.stringLiteral(content) : buildJsxExpressionNode(valueAST);
|
|
888
1032
|
}
|
|
889
|
-
return
|
|
1033
|
+
return t9.jsxAttribute(keyAST, value);
|
|
890
1034
|
}
|
|
891
1035
|
|
|
892
1036
|
// src/core/codegen/component/jsx/syntax-processor/process/build-element-node.ts
|
|
@@ -932,8 +1076,8 @@ function buildConditionNode(nodeIR, ctx) {
|
|
|
932
1076
|
}
|
|
933
1077
|
const testExpression = condition.babelExp.ast;
|
|
934
1078
|
const trueBranchExpression = convertJsxChildToExpression(buildCurrentNode());
|
|
935
|
-
const falseBranchExpression = nextNodeIR ? convertJsxChildToExpression(buildElementNode(nextNodeIR, ctx)) :
|
|
936
|
-
const conditionalExpression2 =
|
|
1079
|
+
const falseBranchExpression = nextNodeIR ? convertJsxChildToExpression(buildElementNode(nextNodeIR, ctx)) : t10.nullLiteral();
|
|
1080
|
+
const conditionalExpression2 = t10.conditionalExpression(
|
|
937
1081
|
testExpression,
|
|
938
1082
|
// 条件测试部分
|
|
939
1083
|
trueBranchExpression,
|
|
@@ -952,17 +1096,25 @@ function buildCtxProviderNode(nodeIR, ctx, children) {
|
|
|
952
1096
|
if (nextProvide?.isOccupied) {
|
|
953
1097
|
childNodes = [buildCtxProviderNode(nextProvide, ctx, children)];
|
|
954
1098
|
}
|
|
955
|
-
const
|
|
956
|
-
|
|
957
|
-
|
|
1099
|
+
const parseProviderExpr = (raw) => {
|
|
1100
|
+
if (!raw) return t11.stringLiteral("");
|
|
1101
|
+
try {
|
|
1102
|
+
return stringToExpr(raw, ctx.scriptData.lang, ctx.filename);
|
|
1103
|
+
} catch {
|
|
1104
|
+
return t11.stringLiteral(raw);
|
|
1105
|
+
}
|
|
1106
|
+
};
|
|
1107
|
+
const nameProp = t11.jsxAttribute(
|
|
1108
|
+
t11.jsxIdentifier("name"),
|
|
1109
|
+
buildJsxExpressionNode(parseProviderExpr(name))
|
|
958
1110
|
);
|
|
959
|
-
const valueProp =
|
|
960
|
-
|
|
961
|
-
buildJsxExpressionNode(
|
|
1111
|
+
const valueProp = t11.jsxAttribute(
|
|
1112
|
+
t11.jsxIdentifier("value"),
|
|
1113
|
+
buildJsxExpressionNode(parseProviderExpr(value))
|
|
962
1114
|
);
|
|
963
1115
|
void ctx;
|
|
964
1116
|
const adpater = ADAPTER_RULES.runtime[VUE_API_MAP.provide];
|
|
965
|
-
return createJsxElement(adpater.target, [
|
|
1117
|
+
return createJsxElement(adpater.target, [nameProp, valueProp], childNodes);
|
|
966
1118
|
}
|
|
967
1119
|
|
|
968
1120
|
// src/core/codegen/component/jsx/syntax-processor/postprocess/build-root-jsx.ts
|
|
@@ -1011,29 +1163,29 @@ function buildJSX(nodeIR, ctx) {
|
|
|
1011
1163
|
}
|
|
1012
1164
|
|
|
1013
1165
|
// src/core/codegen/component/script/syntax-processor/index.ts
|
|
1014
|
-
import * as
|
|
1166
|
+
import * as t14 from "@babel/types";
|
|
1015
1167
|
|
|
1016
1168
|
// src/core/codegen/component/script/syntax-processor/postprocess/build-program-node.ts
|
|
1017
|
-
import * as
|
|
1169
|
+
import * as t12 from "@babel/types";
|
|
1018
1170
|
function buildProgramNode(nodeIR, ctx, state) {
|
|
1019
1171
|
const statements = [...state.preambleStatements];
|
|
1020
1172
|
if (!state.component) {
|
|
1021
|
-
state.result =
|
|
1173
|
+
state.result = t12.program(statements, void 0, "module");
|
|
1022
1174
|
return;
|
|
1023
1175
|
}
|
|
1024
1176
|
statements.push(state.component);
|
|
1025
1177
|
if (state.expose) {
|
|
1026
1178
|
const [declarator] = state.component.declarations;
|
|
1027
1179
|
const { name } = declarator.id;
|
|
1028
|
-
statements.push(
|
|
1180
|
+
statements.push(t12.exportDefaultDeclaration(t12.identifier(name)));
|
|
1029
1181
|
}
|
|
1030
|
-
state.result =
|
|
1182
|
+
state.result = t12.program(statements, void 0, "module");
|
|
1031
1183
|
void nodeIR;
|
|
1032
1184
|
void ctx;
|
|
1033
1185
|
}
|
|
1034
1186
|
|
|
1035
1187
|
// src/core/codegen/component/script/syntax-processor/process/build-component-function.ts
|
|
1036
|
-
import * as
|
|
1188
|
+
import * as t13 from "@babel/types";
|
|
1037
1189
|
|
|
1038
1190
|
// src/consts/react-api-map.ts
|
|
1039
1191
|
var REACT_API_MAP = {
|
|
@@ -1047,19 +1199,6 @@ var REACT_API_MAP = {
|
|
|
1047
1199
|
useImperativeHandle: "useImperativeHandle"
|
|
1048
1200
|
};
|
|
1049
1201
|
|
|
1050
|
-
// src/utils/camelCase.ts
|
|
1051
|
-
var camelCase = (str) => {
|
|
1052
|
-
if (str.includes("-")) {
|
|
1053
|
-
return str.replace(/-([a-z])/g, (_, char) => char.toUpperCase());
|
|
1054
|
-
}
|
|
1055
|
-
return str;
|
|
1056
|
-
};
|
|
1057
|
-
|
|
1058
|
-
// src/utils/capitalize.ts
|
|
1059
|
-
var capitalize = (str) => {
|
|
1060
|
-
return str.charAt(0).toUpperCase() + str.slice(1);
|
|
1061
|
-
};
|
|
1062
|
-
|
|
1063
1202
|
// src/utils/hash.ts
|
|
1064
1203
|
import XXH from "xxhashjs";
|
|
1065
1204
|
function genHashByXXH(str) {
|
|
@@ -1083,7 +1222,7 @@ function buildComponent(scriptIR, ctx, state) {
|
|
|
1083
1222
|
} = ctx;
|
|
1084
1223
|
const hasProps = getHasProps(propsTSIface.propsTypes) || getHasProps(propsTSIface.emitTypes) || getHasProps(propsTSIface.slotTypes);
|
|
1085
1224
|
const shouldMemo = scriptIR !== null || hasProps;
|
|
1086
|
-
const jsxStatement =
|
|
1225
|
+
const jsxStatement = t13.returnStatement(state.jsx || t13.nullLiteral());
|
|
1087
1226
|
const component = !shouldMemo ? resolvePureUIComponent(jsxStatement, ctx) : resolveMemoComponent(scriptIR.statement.local, jsxStatement, ctx);
|
|
1088
1227
|
state.component = component;
|
|
1089
1228
|
}
|
|
@@ -1091,24 +1230,24 @@ function getHasProps(list) {
|
|
|
1091
1230
|
return list.length > 0;
|
|
1092
1231
|
}
|
|
1093
1232
|
function resolvePureUIComponent(jsxStmt, ctx) {
|
|
1094
|
-
return
|
|
1095
|
-
|
|
1233
|
+
return t13.variableDeclaration("const", [
|
|
1234
|
+
t13.variableDeclarator(
|
|
1096
1235
|
resolveComponentName(ctx),
|
|
1097
|
-
|
|
1236
|
+
t13.arrowFunctionExpression([], t13.blockStatement([jsxStmt]))
|
|
1098
1237
|
)
|
|
1099
1238
|
]);
|
|
1100
1239
|
}
|
|
1101
1240
|
function resolveMemoComponent(local, jsxStmt, ctx) {
|
|
1102
1241
|
const name = resolveComponentName(ctx);
|
|
1103
1242
|
const param = resolvePropsParam(ctx);
|
|
1104
|
-
const body =
|
|
1243
|
+
const body = t13.blockStatement(resolveLocalStatements(local, jsxStmt));
|
|
1105
1244
|
const component = resolveComponent(param, body, ctx);
|
|
1106
1245
|
let compFn = component;
|
|
1107
1246
|
if (ctx.scriptData.forwardRef.enabled) {
|
|
1108
1247
|
compFn = resolveForwardRef(body, ctx);
|
|
1109
1248
|
}
|
|
1110
|
-
const memoCall =
|
|
1111
|
-
return
|
|
1249
|
+
const memoCall = t13.callExpression(t13.identifier(REACT_API_MAP.memo), [compFn]);
|
|
1250
|
+
return t13.variableDeclaration("const", [t13.variableDeclarator(name, memoCall)]);
|
|
1112
1251
|
}
|
|
1113
1252
|
function resolveComponentName(ctx) {
|
|
1114
1253
|
const { filename, compName } = ctx;
|
|
@@ -1122,7 +1261,7 @@ function resolveComponentName(ctx) {
|
|
|
1122
1261
|
file: filename
|
|
1123
1262
|
});
|
|
1124
1263
|
}
|
|
1125
|
-
return
|
|
1264
|
+
return t13.identifier(name);
|
|
1126
1265
|
}
|
|
1127
1266
|
function resolveLocalStatements(local, jsx) {
|
|
1128
1267
|
const stmts = [jsx];
|
|
@@ -1142,7 +1281,7 @@ function resolveComponent(param, body, ctx) {
|
|
|
1142
1281
|
if (forwardRef.enabled) {
|
|
1143
1282
|
return resolveForwardRef(body, ctx);
|
|
1144
1283
|
}
|
|
1145
|
-
const baseComponent =
|
|
1284
|
+
const baseComponent = t13.arrowFunctionExpression(!param ? [] : [param], body);
|
|
1146
1285
|
return baseComponent;
|
|
1147
1286
|
}
|
|
1148
1287
|
function resolveForwardRef(body, ctx) {
|
|
@@ -1150,18 +1289,18 @@ function resolveForwardRef(body, ctx) {
|
|
|
1150
1289
|
propField,
|
|
1151
1290
|
scriptData: { forwardRef, lang }
|
|
1152
1291
|
} = ctx;
|
|
1153
|
-
const params = [
|
|
1292
|
+
const params = [t13.identifier(forwardRef.refField)];
|
|
1154
1293
|
const propsId = resolvePropsParam(ctx);
|
|
1155
|
-
const callExpr =
|
|
1156
|
-
|
|
1294
|
+
const callExpr = t13.callExpression(t13.identifier(REACT_API_MAP.forwardRef), [
|
|
1295
|
+
t13.arrowFunctionExpression(params, body)
|
|
1157
1296
|
]);
|
|
1158
1297
|
if (lang.startsWith("ts")) {
|
|
1159
|
-
const types = [
|
|
1298
|
+
const types = [t13.tsAnyKeyword()];
|
|
1160
1299
|
if (propsId) {
|
|
1161
1300
|
const propsType = propsId?.typeAnnotation?.typeAnnotation;
|
|
1162
|
-
types.push(propsType ||
|
|
1301
|
+
types.push(propsType || t13.tsAnyKeyword());
|
|
1163
1302
|
}
|
|
1164
|
-
callExpr.typeParameters =
|
|
1303
|
+
callExpr.typeParameters = t13.tsTypeParameterInstantiation(types);
|
|
1165
1304
|
}
|
|
1166
1305
|
if (propsId) {
|
|
1167
1306
|
propsId.typeAnnotation = null;
|
|
@@ -1172,7 +1311,7 @@ function resolveForwardRef(body, ctx) {
|
|
|
1172
1311
|
function resolvePropsParam(ctx) {
|
|
1173
1312
|
const { propField, scriptData } = ctx;
|
|
1174
1313
|
const { propsTSIface } = scriptData;
|
|
1175
|
-
const propsIdentifier =
|
|
1314
|
+
const propsIdentifier = t13.identifier(propField);
|
|
1176
1315
|
if (scriptData.lang.startsWith("js")) {
|
|
1177
1316
|
if (propsTSIface.hasPropsInJsEnv) {
|
|
1178
1317
|
return propsIdentifier;
|
|
@@ -1182,8 +1321,8 @@ function resolvePropsParam(ctx) {
|
|
|
1182
1321
|
if (!propsTSIface.name) {
|
|
1183
1322
|
return;
|
|
1184
1323
|
}
|
|
1185
|
-
const typeIdentifier =
|
|
1186
|
-
propsIdentifier.typeAnnotation =
|
|
1324
|
+
const typeIdentifier = t13.identifier(propsTSIface.name);
|
|
1325
|
+
propsIdentifier.typeAnnotation = t13.tsTypeAnnotation(t13.tsTypeReference(typeIdentifier));
|
|
1187
1326
|
return propsIdentifier;
|
|
1188
1327
|
}
|
|
1189
1328
|
|
|
@@ -1208,7 +1347,7 @@ function buildScriptSyntax(nodeIR, ctx, jsx, expose = true) {
|
|
|
1208
1347
|
process: [buildProgramPreamble, buildComponent],
|
|
1209
1348
|
postprocess: [buildProgramNode]
|
|
1210
1349
|
});
|
|
1211
|
-
return state.result ||
|
|
1350
|
+
return state.result || t14.program([], void 0, "module");
|
|
1212
1351
|
}
|
|
1213
1352
|
function scriptSyntaxProcessor(nodeIR, ctx, state, options) {
|
|
1214
1353
|
const run = (p) => {
|
|
@@ -1270,58 +1409,6 @@ function generate(ir, ctx, options) {
|
|
|
1270
1409
|
|
|
1271
1410
|
// src/core/parse/script-only.ts
|
|
1272
1411
|
import { parse as babelParse } from "@babel/parser";
|
|
1273
|
-
|
|
1274
|
-
// src/shared/babel-utils.ts
|
|
1275
|
-
import { parseExpression as parseExpression2 } from "@babel/parser";
|
|
1276
|
-
function getBabelParseOptions(lang = "js", context = "script", filename) {
|
|
1277
|
-
const baseOptions = {
|
|
1278
|
-
sourceType: "module",
|
|
1279
|
-
sourceFilename: filename ?? "anonymous",
|
|
1280
|
-
errorRecovery: true
|
|
1281
|
-
// 容错模式
|
|
1282
|
-
};
|
|
1283
|
-
const plugins = [];
|
|
1284
|
-
if (lang.includes("ts")) {
|
|
1285
|
-
plugins.push("typescript");
|
|
1286
|
-
}
|
|
1287
|
-
if (lang.endsWith("sx")) {
|
|
1288
|
-
if (lang.startsWith("t")) plugins.push("typescript");
|
|
1289
|
-
plugins.push("jsx");
|
|
1290
|
-
}
|
|
1291
|
-
if (context === "vueTemplate") {
|
|
1292
|
-
plugins.push("decorators-legacy");
|
|
1293
|
-
} else if (context === "expression") {
|
|
1294
|
-
baseOptions.sourceType = "script";
|
|
1295
|
-
baseOptions.allowReturnOutsideFunction = true;
|
|
1296
|
-
baseOptions.allowSuperOutsideMethod = true;
|
|
1297
|
-
}
|
|
1298
|
-
plugins.push("classProperties", "objectRestSpread", "asyncGenerators");
|
|
1299
|
-
return {
|
|
1300
|
-
...baseOptions,
|
|
1301
|
-
plugins: [...new Set(plugins)]
|
|
1302
|
-
};
|
|
1303
|
-
}
|
|
1304
|
-
function stringToExpr(input, lang, filename = "") {
|
|
1305
|
-
return parseExpression2(input, getBabelParseOptions(lang, "expression", filename));
|
|
1306
|
-
}
|
|
1307
|
-
function atComponentOrHookRoot(path7, rootScope, inScriptFile = false) {
|
|
1308
|
-
const { parentPath, scope } = path7;
|
|
1309
|
-
const parentBlock = scope.block;
|
|
1310
|
-
if (!parentPath) return !inScriptFile;
|
|
1311
|
-
if (parentBlock === rootScope) {
|
|
1312
|
-
if (inScriptFile) return false;
|
|
1313
|
-
if (parentPath.isBlockStatement() && parentPath.node !== rootScope) {
|
|
1314
|
-
return false;
|
|
1315
|
-
}
|
|
1316
|
-
if (parentPath.isCallExpression() || parentPath.isNewExpression()) {
|
|
1317
|
-
return false;
|
|
1318
|
-
}
|
|
1319
|
-
return true;
|
|
1320
|
-
}
|
|
1321
|
-
return false;
|
|
1322
|
-
}
|
|
1323
|
-
|
|
1324
|
-
// src/core/parse/script-only.ts
|
|
1325
1412
|
function parseOnlyScript(source, ctx, options) {
|
|
1326
1413
|
const lang = ctx.inputType.split("-")[1];
|
|
1327
1414
|
const ast = babelParse(source, getBabelParseOptions(lang, "script", ctx.filename));
|
|
@@ -1355,7 +1442,7 @@ import {
|
|
|
1355
1442
|
// src/core/parse/sfc/process/resolve-script.ts
|
|
1356
1443
|
import { generate as generate2 } from "@babel/generator";
|
|
1357
1444
|
import { parse as babelParse2 } from "@babel/parser";
|
|
1358
|
-
import * as
|
|
1445
|
+
import * as t15 from "@babel/types";
|
|
1359
1446
|
function resolveScript(descriptor, ctx, pResult) {
|
|
1360
1447
|
const scriptBlock = descriptor.scriptSetup || descriptor.script;
|
|
1361
1448
|
if (!scriptBlock) return null;
|
|
@@ -1397,41 +1484,41 @@ function extractSetupBodyToTopLevel(content, options) {
|
|
|
1397
1484
|
const otherTopLevel = [];
|
|
1398
1485
|
let setupStatements = [];
|
|
1399
1486
|
for (const node of ast.program.body) {
|
|
1400
|
-
if (
|
|
1487
|
+
if (t15.isImportDeclaration(node)) {
|
|
1401
1488
|
importNodes.push(node);
|
|
1402
1489
|
continue;
|
|
1403
1490
|
}
|
|
1404
|
-
if (
|
|
1491
|
+
if (t15.isExportDefaultDeclaration(node)) {
|
|
1405
1492
|
const decl = node.declaration;
|
|
1406
|
-
if (decl &&
|
|
1493
|
+
if (decl && t15.isObjectExpression(decl)) {
|
|
1407
1494
|
const nameProp = decl.properties.find((p) => {
|
|
1408
|
-
if (
|
|
1495
|
+
if (t15.isObjectProperty(p)) {
|
|
1409
1496
|
const key = p.key;
|
|
1410
|
-
return
|
|
1497
|
+
return t15.isIdentifier(key) && key.name === "name" || t15.isStringLiteral(key) && key.value === "name";
|
|
1411
1498
|
}
|
|
1412
1499
|
return false;
|
|
1413
1500
|
});
|
|
1414
|
-
if (nameProp &&
|
|
1415
|
-
if (
|
|
1501
|
+
if (nameProp && t15.isObjectProperty(nameProp)) {
|
|
1502
|
+
if (t15.isStringLiteral(nameProp.value)) {
|
|
1416
1503
|
name = nameProp.value.value;
|
|
1417
1504
|
}
|
|
1418
1505
|
}
|
|
1419
1506
|
const setupProp = decl.properties.find((p) => {
|
|
1420
|
-
if (
|
|
1507
|
+
if (t15.isObjectMethod(p)) {
|
|
1421
1508
|
return p.key && p.key.name === "setup";
|
|
1422
1509
|
}
|
|
1423
|
-
if (
|
|
1510
|
+
if (t15.isObjectProperty(p)) {
|
|
1424
1511
|
const key = p.key;
|
|
1425
|
-
return
|
|
1512
|
+
return t15.isIdentifier(key) && key.name === "setup" || t15.isStringLiteral(key) && key.value === "setup";
|
|
1426
1513
|
}
|
|
1427
1514
|
return false;
|
|
1428
1515
|
});
|
|
1429
1516
|
if (setupProp) {
|
|
1430
|
-
const value =
|
|
1431
|
-
if (value && (
|
|
1432
|
-
const fnBody =
|
|
1517
|
+
const value = t15.isObjectMethod(setupProp) ? setupProp : t15.isObjectProperty(setupProp) ? setupProp.value : null;
|
|
1518
|
+
if (value && (t15.isFunction(value) || t15.isObjectMethod(value))) {
|
|
1519
|
+
const fnBody = t15.isBlockStatement(value.body) ? value.body.body : [];
|
|
1433
1520
|
for (const stmt of fnBody) {
|
|
1434
|
-
if (
|
|
1521
|
+
if (t15.isReturnStatement(stmt)) continue;
|
|
1435
1522
|
setupStatements.push(stmt);
|
|
1436
1523
|
}
|
|
1437
1524
|
}
|
|
@@ -1459,7 +1546,7 @@ function extractCompName(source) {
|
|
|
1459
1546
|
|
|
1460
1547
|
// src/core/parse/sfc/process/resolve-script-meta.ts
|
|
1461
1548
|
import { traverse } from "@babel/core";
|
|
1462
|
-
import * as
|
|
1549
|
+
import * as t16 from "@babel/types";
|
|
1463
1550
|
|
|
1464
1551
|
// src/consts/html-tag-types.ts
|
|
1465
1552
|
var HTML_TAG_TYPES = {
|
|
@@ -1563,12 +1650,12 @@ function resolveScriptMeta(result, ctx) {
|
|
|
1563
1650
|
const scriptAST = result.script?.ast;
|
|
1564
1651
|
if (!scriptAST) return;
|
|
1565
1652
|
traverse(scriptAST, {
|
|
1566
|
-
VariableDeclarator(
|
|
1567
|
-
const { node } =
|
|
1568
|
-
if (!atComponentOrHookRoot(
|
|
1653
|
+
VariableDeclarator(path8) {
|
|
1654
|
+
const { node } = path8;
|
|
1655
|
+
if (!atComponentOrHookRoot(path8, scriptAST.program) || !t16.isIdentifier(node.id)) {
|
|
1569
1656
|
return;
|
|
1570
1657
|
}
|
|
1571
|
-
if (node.init &&
|
|
1658
|
+
if (node.init && t16.isCallExpression(node.init) && t16.isIdentifier(node.init.callee)) {
|
|
1572
1659
|
collectReactiveBindings(node, ctx);
|
|
1573
1660
|
collectRefBindings(node, ctx);
|
|
1574
1661
|
}
|
|
@@ -1846,7 +1933,7 @@ import { parse as babelParse3 } from "@babel/parser";
|
|
|
1846
1933
|
import { traverse as traverse3 } from "@babel/core";
|
|
1847
1934
|
|
|
1848
1935
|
// src/core/transform/sfc/script/syntax-processor/postprocess/insert-css-import.ts
|
|
1849
|
-
import * as
|
|
1936
|
+
import * as t17 from "@babel/types";
|
|
1850
1937
|
function insertCSSImport(ctx) {
|
|
1851
1938
|
const { inputType } = ctx;
|
|
1852
1939
|
if (inputType !== "sfc") return;
|
|
@@ -1855,19 +1942,22 @@ function insertCSSImport(ctx) {
|
|
|
1855
1942
|
const scriptIR = getScriptIR(ctx);
|
|
1856
1943
|
const filename = normalizePath(filePath).split("/").pop();
|
|
1857
1944
|
const importPath = `./${filename}`;
|
|
1858
|
-
const importDecl =
|
|
1859
|
-
!moduleName ? [] : [
|
|
1860
|
-
|
|
1945
|
+
const importDecl = t17.importDeclaration(
|
|
1946
|
+
!moduleName ? [] : [t17.importDefaultSpecifier(t17.identifier(moduleName))],
|
|
1947
|
+
t17.stringLiteral(importPath)
|
|
1861
1948
|
);
|
|
1862
1949
|
scriptIR.imports.push(importDecl);
|
|
1863
1950
|
}
|
|
1864
1951
|
|
|
1865
1952
|
// src/core/transform/sfc/script/syntax-processor/postprocess/insert-required-imports.ts
|
|
1866
|
-
import * as
|
|
1953
|
+
import * as t18 from "@babel/types";
|
|
1867
1954
|
|
|
1868
1955
|
// src/core/transform/shared.ts
|
|
1869
1956
|
function recordImport(ctx, pkg, name, onDemand = true) {
|
|
1870
1957
|
const { imports } = ctx;
|
|
1958
|
+
if (isTypeOnlyImport(name)) {
|
|
1959
|
+
name = `type ${name}`;
|
|
1960
|
+
}
|
|
1871
1961
|
if (imports.has(pkg)) {
|
|
1872
1962
|
const list = imports.get(pkg);
|
|
1873
1963
|
const foundItem = list.find((item) => item.name === name);
|
|
@@ -1878,6 +1968,10 @@ function recordImport(ctx, pkg, name, onDemand = true) {
|
|
|
1878
1968
|
}
|
|
1879
1969
|
imports.set(pkg, [{ name, onDemand }]);
|
|
1880
1970
|
}
|
|
1971
|
+
function isTypeOnlyImport(name) {
|
|
1972
|
+
const arr = [REACT_API_MAP.ReactNode];
|
|
1973
|
+
return arr.includes(name);
|
|
1974
|
+
}
|
|
1881
1975
|
|
|
1882
1976
|
// src/core/transform/sfc/script/shared/replace-vue-suffix.ts
|
|
1883
1977
|
function replaceVueSuffix(ctx, node) {
|
|
@@ -1892,36 +1986,36 @@ function insertRequiredImports(ctx) {
|
|
|
1892
1986
|
const processedModules = /* @__PURE__ */ new Set();
|
|
1893
1987
|
let hasProcessedImports = false;
|
|
1894
1988
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.memo);
|
|
1895
|
-
function resolveRequiredImport(
|
|
1896
|
-
const { node } =
|
|
1989
|
+
function resolveRequiredImport(path8) {
|
|
1990
|
+
const { node } = path8;
|
|
1897
1991
|
const moduleName = node.source.value.toLowerCase();
|
|
1898
1992
|
const isVueLike = isVueEcosystemPackage(moduleName);
|
|
1899
1993
|
mergeImports(node, ctx);
|
|
1900
|
-
if (processedModules.has(moduleName) && !
|
|
1901
|
-
|
|
1994
|
+
if (processedModules.has(moduleName) && !path8.removed) {
|
|
1995
|
+
path8.remove();
|
|
1902
1996
|
return;
|
|
1903
1997
|
}
|
|
1904
1998
|
processedModules.add(moduleName);
|
|
1905
1999
|
if (!hasProcessedImports) {
|
|
1906
2000
|
const required = createRequiredImports(ctx);
|
|
1907
2001
|
if (isVueLike) {
|
|
1908
|
-
|
|
2002
|
+
path8.replaceWithMultiple(required);
|
|
1909
2003
|
} else if (moduleName === PACKAGE_NAME.react) {
|
|
1910
|
-
|
|
2004
|
+
path8.insertAfter(required);
|
|
1911
2005
|
} else {
|
|
1912
|
-
|
|
2006
|
+
path8.insertBefore(required);
|
|
1913
2007
|
}
|
|
1914
2008
|
hasProcessedImports = true;
|
|
1915
2009
|
}
|
|
1916
|
-
if (isVueLike && !
|
|
1917
|
-
|
|
2010
|
+
if (isVueLike && !path8.removed) {
|
|
2011
|
+
path8.remove();
|
|
1918
2012
|
return;
|
|
1919
2013
|
}
|
|
1920
2014
|
replaceVueSuffix(ctx, node.source);
|
|
1921
2015
|
}
|
|
1922
|
-
function resolveStyleFileExt(
|
|
2016
|
+
function resolveStyleFileExt(path8) {
|
|
1923
2017
|
if (!ctx.preprocessStyles) return;
|
|
1924
|
-
const { node } =
|
|
2018
|
+
const { node } = path8;
|
|
1925
2019
|
if (!node || !node.source || !node.source.value) return;
|
|
1926
2020
|
const importSource = node.source.value;
|
|
1927
2021
|
if (typeof importSource !== "string") return;
|
|
@@ -1933,16 +2027,16 @@ function insertRequiredImports(ctx) {
|
|
|
1933
2027
|
return {
|
|
1934
2028
|
// 增加 Program.exit 兜底注入 required imports(处理无 ImportDeclaration 的 SFC)
|
|
1935
2029
|
Program: {
|
|
1936
|
-
exit(
|
|
2030
|
+
exit(path8) {
|
|
1937
2031
|
if (hasProcessedImports) return;
|
|
1938
2032
|
const required = createRequiredImports(ctx);
|
|
1939
|
-
|
|
2033
|
+
path8.unshiftContainer("body", required);
|
|
1940
2034
|
hasProcessedImports = true;
|
|
1941
2035
|
}
|
|
1942
2036
|
},
|
|
1943
|
-
ImportDeclaration(
|
|
1944
|
-
resolveRequiredImport(
|
|
1945
|
-
resolveStyleFileExt(
|
|
2037
|
+
ImportDeclaration(path8) {
|
|
2038
|
+
resolveRequiredImport(path8);
|
|
2039
|
+
resolveStyleFileExt(path8);
|
|
1946
2040
|
}
|
|
1947
2041
|
};
|
|
1948
2042
|
}
|
|
@@ -1966,17 +2060,17 @@ function mergeImports(currentNode, ctx) {
|
|
|
1966
2060
|
}
|
|
1967
2061
|
const currentImports = /* @__PURE__ */ new Set();
|
|
1968
2062
|
for (const spec of currentNode.specifiers) {
|
|
1969
|
-
if (
|
|
2063
|
+
if (t18.isImportSpecifier(spec) && t18.isIdentifier(spec.imported)) {
|
|
1970
2064
|
currentImports.add(spec.imported.name);
|
|
1971
2065
|
}
|
|
1972
|
-
if (
|
|
2066
|
+
if (t18.isImportDefaultSpecifier(spec) && t18.isIdentifier(spec.local)) {
|
|
1973
2067
|
currentImports.add(spec.local.name);
|
|
1974
2068
|
}
|
|
1975
2069
|
}
|
|
1976
2070
|
for (const item of ctxImportItems) {
|
|
1977
2071
|
if (currentImports.has(item.name)) return;
|
|
1978
|
-
const local =
|
|
1979
|
-
const newNode = !item.onDemand ?
|
|
2072
|
+
const local = t18.identifier(item.name);
|
|
2073
|
+
const newNode = !item.onDemand ? t18.importDefaultSpecifier(local) : t18.importSpecifier(local, local);
|
|
1980
2074
|
currentNode.specifiers.push(newNode);
|
|
1981
2075
|
}
|
|
1982
2076
|
ctx.imports.delete(moduleName);
|
|
@@ -1987,18 +2081,18 @@ function createRequiredImports(ctx) {
|
|
|
1987
2081
|
ctx.imports.forEach((items, moduleName) => {
|
|
1988
2082
|
const specifier = [];
|
|
1989
2083
|
for (const item of items) {
|
|
1990
|
-
const local =
|
|
2084
|
+
const local = t18.identifier(item.name);
|
|
1991
2085
|
if (!item.onDemand) {
|
|
1992
|
-
specifier.push(
|
|
2086
|
+
specifier.push(t18.importDefaultSpecifier(local));
|
|
1993
2087
|
} else {
|
|
1994
|
-
specifier.push(
|
|
2088
|
+
specifier.push(t18.importSpecifier(local, local));
|
|
1995
2089
|
}
|
|
1996
2090
|
}
|
|
1997
2091
|
importMap[moduleName] = specifier;
|
|
1998
2092
|
});
|
|
1999
2093
|
for (const name in importMap) {
|
|
2000
2094
|
const specifiers = importMap[name];
|
|
2001
|
-
const importDecl =
|
|
2095
|
+
const importDecl = t18.importDeclaration(specifiers, t18.stringLiteral(name));
|
|
2002
2096
|
if (name === PACKAGE_NAME.react) {
|
|
2003
2097
|
result.unshift(importDecl);
|
|
2004
2098
|
} else {
|
|
@@ -2009,33 +2103,33 @@ function createRequiredImports(ctx) {
|
|
|
2009
2103
|
}
|
|
2010
2104
|
|
|
2011
2105
|
// src/core/transform/sfc/script/syntax-processor/postprocess/resolve-static-hoisting.ts
|
|
2012
|
-
import * as
|
|
2106
|
+
import * as t20 from "@babel/types";
|
|
2013
2107
|
|
|
2014
2108
|
// src/core/transform/sfc/script/shared/babel-utils.ts
|
|
2015
|
-
import * as
|
|
2016
|
-
function findRootVariablePath(
|
|
2017
|
-
const rootId = findRootIdentifier(
|
|
2109
|
+
import * as t19 from "@babel/types";
|
|
2110
|
+
function findRootVariablePath(path8) {
|
|
2111
|
+
const rootId = findRootIdentifier(path8.node);
|
|
2018
2112
|
if (!rootId?.name) return null;
|
|
2019
|
-
const binding =
|
|
2113
|
+
const binding = path8.scope.getBinding(rootId.name);
|
|
2020
2114
|
if (!binding) return null;
|
|
2021
2115
|
const rootPath = getVariableDeclaratorPath(binding.path);
|
|
2022
2116
|
return rootPath;
|
|
2023
2117
|
}
|
|
2024
2118
|
function findRootIdentifier(node) {
|
|
2025
2119
|
let current = node.object;
|
|
2026
|
-
while (
|
|
2120
|
+
while (t19.isMemberExpression(current) || t19.isOptionalMemberExpression(current)) {
|
|
2027
2121
|
current = current.object;
|
|
2028
2122
|
}
|
|
2029
|
-
return
|
|
2123
|
+
return t19.isIdentifier(current) ? current : null;
|
|
2030
2124
|
}
|
|
2031
|
-
function getVariableDeclaratorPath(
|
|
2032
|
-
if (
|
|
2033
|
-
return
|
|
2125
|
+
function getVariableDeclaratorPath(path8) {
|
|
2126
|
+
if (path8.isVariableDeclarator()) {
|
|
2127
|
+
return path8;
|
|
2034
2128
|
}
|
|
2035
|
-
return
|
|
2129
|
+
return path8.findParent((p) => p.isVariableDeclarator());
|
|
2036
2130
|
}
|
|
2037
|
-
function isVariableDeclTopLevel(
|
|
2038
|
-
const variableDeclaratorPath =
|
|
2131
|
+
function isVariableDeclTopLevel(path8) {
|
|
2132
|
+
const variableDeclaratorPath = path8;
|
|
2039
2133
|
const variableDeclarationPath = variableDeclaratorPath.parentPath;
|
|
2040
2134
|
if (!variableDeclarationPath) {
|
|
2041
2135
|
return false;
|
|
@@ -2049,75 +2143,75 @@ function isVariableDeclTopLevel(path7) {
|
|
|
2049
2143
|
}
|
|
2050
2144
|
return false;
|
|
2051
2145
|
}
|
|
2052
|
-
function isRealVariableAccess(
|
|
2053
|
-
return isIdentifierAccess(
|
|
2146
|
+
function isRealVariableAccess(path8) {
|
|
2147
|
+
return isIdentifierAccess(path8) && !isPropertyName(path8);
|
|
2054
2148
|
}
|
|
2055
|
-
function isIdentifierAccess(
|
|
2056
|
-
if (isIdentifierDeclaration(
|
|
2149
|
+
function isIdentifierAccess(path8) {
|
|
2150
|
+
if (isIdentifierDeclaration(path8)) {
|
|
2057
2151
|
return false;
|
|
2058
2152
|
}
|
|
2059
|
-
const binding =
|
|
2153
|
+
const binding = path8.scope.getBinding(path8.node.name);
|
|
2060
2154
|
if (!binding) {
|
|
2061
2155
|
return true;
|
|
2062
2156
|
}
|
|
2063
|
-
return binding.identifier !==
|
|
2157
|
+
return binding.identifier !== path8.node;
|
|
2064
2158
|
}
|
|
2065
|
-
function isIdentifierDeclaration(
|
|
2066
|
-
const parent =
|
|
2159
|
+
function isIdentifierDeclaration(path8) {
|
|
2160
|
+
const parent = path8.parentPath;
|
|
2067
2161
|
if (!parent) return false;
|
|
2068
|
-
if (parent.isVariableDeclarator() && parent.node.id ===
|
|
2162
|
+
if (parent.isVariableDeclarator() && parent.node.id === path8.node) {
|
|
2069
2163
|
return true;
|
|
2070
2164
|
}
|
|
2071
|
-
if (parent.isFunctionDeclaration() && parent.node.id ===
|
|
2165
|
+
if (parent.isFunctionDeclaration() && parent.node.id === path8.node) {
|
|
2072
2166
|
return true;
|
|
2073
2167
|
}
|
|
2074
|
-
if (parent.isFunctionExpression() && parent.node.id ===
|
|
2168
|
+
if (parent.isFunctionExpression() && parent.node.id === path8.node) {
|
|
2075
2169
|
return true;
|
|
2076
2170
|
}
|
|
2077
|
-
if (parent.isClassDeclaration() && parent.node.id ===
|
|
2171
|
+
if (parent.isClassDeclaration() && parent.node.id === path8.node) {
|
|
2078
2172
|
return true;
|
|
2079
2173
|
}
|
|
2080
|
-
if (parent.isImportSpecifier() && parent.node.local ===
|
|
2174
|
+
if (parent.isImportSpecifier() && parent.node.local === path8.node) {
|
|
2081
2175
|
return true;
|
|
2082
2176
|
}
|
|
2083
|
-
if (parent.isImportDefaultSpecifier() && parent.node.local ===
|
|
2177
|
+
if (parent.isImportDefaultSpecifier() && parent.node.local === path8.node) {
|
|
2084
2178
|
return true;
|
|
2085
2179
|
}
|
|
2086
|
-
if (parent.isImportNamespaceSpecifier() && parent.node.local ===
|
|
2180
|
+
if (parent.isImportNamespaceSpecifier() && parent.node.local === path8.node) {
|
|
2087
2181
|
return true;
|
|
2088
2182
|
}
|
|
2089
|
-
if (parent.isFunction() && parent.node.params.includes(
|
|
2183
|
+
if (parent.isFunction() && parent.node.params.includes(path8.node)) {
|
|
2090
2184
|
return true;
|
|
2091
2185
|
}
|
|
2092
|
-
if (parent.isCatchClause() && parent.node.param ===
|
|
2186
|
+
if (parent.isCatchClause() && parent.node.param === path8.node) {
|
|
2093
2187
|
return true;
|
|
2094
2188
|
}
|
|
2095
2189
|
return false;
|
|
2096
2190
|
}
|
|
2097
|
-
function isPropertyName(
|
|
2098
|
-
const parent =
|
|
2191
|
+
function isPropertyName(path8) {
|
|
2192
|
+
const parent = path8.parentPath;
|
|
2099
2193
|
if (!parent) return false;
|
|
2100
|
-
if (parent.isObjectProperty() && parent.node.key ===
|
|
2194
|
+
if (parent.isObjectProperty() && parent.node.key === path8.node) {
|
|
2101
2195
|
return true;
|
|
2102
2196
|
}
|
|
2103
|
-
if (parent.isClassProperty() && parent.node.key ===
|
|
2197
|
+
if (parent.isClassProperty() && parent.node.key === path8.node) {
|
|
2104
2198
|
return true;
|
|
2105
2199
|
}
|
|
2106
|
-
if (parent.isMemberExpression() && parent.node.property ===
|
|
2200
|
+
if (parent.isMemberExpression() && parent.node.property === path8.node) {
|
|
2107
2201
|
return true;
|
|
2108
2202
|
}
|
|
2109
2203
|
return false;
|
|
2110
2204
|
}
|
|
2111
2205
|
function replaceCallName(callExp, identifierName) {
|
|
2112
2206
|
const { callee } = callExp;
|
|
2113
|
-
if (!
|
|
2207
|
+
if (!t19.isIdentifier(callee)) return;
|
|
2114
2208
|
callee.name = identifierName;
|
|
2115
2209
|
if (callee.loc) {
|
|
2116
2210
|
callee.loc.identifierName = identifierName;
|
|
2117
2211
|
}
|
|
2118
2212
|
}
|
|
2119
2213
|
function replaceIdName(id, newName) {
|
|
2120
|
-
if (!
|
|
2214
|
+
if (!t19.isIdentifier(id)) return;
|
|
2121
2215
|
id.name = newName;
|
|
2122
2216
|
if (id.loc) {
|
|
2123
2217
|
id.loc.identifierName = newName;
|
|
@@ -2127,65 +2221,65 @@ function stringValueToTSType(ctx, input, tsTypeAnnotation7) {
|
|
|
2127
2221
|
const { filename, scriptData } = ctx;
|
|
2128
2222
|
const exp = stringToExpr(input, scriptData.lang, filename);
|
|
2129
2223
|
const ts = expressionToTSType(exp);
|
|
2130
|
-
return tsTypeAnnotation7 ?
|
|
2224
|
+
return tsTypeAnnotation7 ? t19.tsTypeAnnotation(ts) : ts;
|
|
2131
2225
|
}
|
|
2132
2226
|
function expressionToTSType(exp) {
|
|
2133
|
-
if (
|
|
2134
|
-
if (
|
|
2135
|
-
if (
|
|
2136
|
-
if (
|
|
2137
|
-
if (
|
|
2227
|
+
if (t19.isStringLiteral(exp)) return t19.tsStringKeyword();
|
|
2228
|
+
if (t19.isNumericLiteral(exp)) return t19.tsNumberKeyword();
|
|
2229
|
+
if (t19.isBooleanLiteral(exp)) return t19.tsBooleanKeyword();
|
|
2230
|
+
if (t19.isArrayExpression(exp)) return t19.tsArrayType(t19.tsAnyKeyword());
|
|
2231
|
+
if (t19.isObjectExpression(exp)) {
|
|
2138
2232
|
const members = [];
|
|
2139
2233
|
for (const p of exp.properties) {
|
|
2140
|
-
if (!
|
|
2234
|
+
if (!t19.isObjectProperty(p)) continue;
|
|
2141
2235
|
let key;
|
|
2142
|
-
if (
|
|
2143
|
-
else if (
|
|
2236
|
+
if (t19.isIdentifier(p.key)) key = p.key.name;
|
|
2237
|
+
else if (t19.isStringLiteral(p.key)) key = p.key.value;
|
|
2144
2238
|
if (!key) continue;
|
|
2145
|
-
if (
|
|
2239
|
+
if (t19.isExpression(p.value)) {
|
|
2146
2240
|
members.push(
|
|
2147
|
-
|
|
2241
|
+
t19.tsPropertySignature(t19.identifier(key), t19.tsTypeAnnotation(expressionToTSType(p.value)))
|
|
2148
2242
|
);
|
|
2149
2243
|
} else {
|
|
2150
2244
|
members.push(
|
|
2151
|
-
|
|
2245
|
+
t19.tsPropertySignature(t19.identifier(key), t19.tsTypeAnnotation(t19.tsAnyKeyword()))
|
|
2152
2246
|
);
|
|
2153
2247
|
}
|
|
2154
2248
|
}
|
|
2155
|
-
return
|
|
2249
|
+
return t19.tsTypeLiteral(members);
|
|
2156
2250
|
}
|
|
2157
|
-
if (
|
|
2251
|
+
if (t19.isArrowFunctionExpression(exp) || t19.isFunctionExpression(exp)) {
|
|
2158
2252
|
const params = exp.params.map((p, i) => {
|
|
2159
|
-
const id =
|
|
2160
|
-
id.typeAnnotation =
|
|
2253
|
+
const id = t19.isIdentifier(p) ? t19.identifier(p.name) : t19.identifier(`arg${i}`);
|
|
2254
|
+
id.typeAnnotation = t19.tsTypeAnnotation(t19.tsAnyKeyword());
|
|
2161
2255
|
return id;
|
|
2162
2256
|
});
|
|
2163
|
-
let returnType =
|
|
2164
|
-
if (
|
|
2257
|
+
let returnType = t19.tsAnyKeyword();
|
|
2258
|
+
if (t19.isBlockStatement(exp.body)) {
|
|
2165
2259
|
for (const stmt of exp.body.body) {
|
|
2166
|
-
if (
|
|
2167
|
-
if (
|
|
2260
|
+
if (t19.isReturnStatement(stmt) && stmt.argument) {
|
|
2261
|
+
if (t19.isExpression(stmt.argument)) {
|
|
2168
2262
|
returnType = expressionToTSType(stmt.argument);
|
|
2169
2263
|
break;
|
|
2170
2264
|
}
|
|
2171
2265
|
}
|
|
2172
2266
|
}
|
|
2173
|
-
} else if (
|
|
2267
|
+
} else if (t19.isExpression(exp.body)) {
|
|
2174
2268
|
returnType = expressionToTSType(exp.body);
|
|
2175
2269
|
}
|
|
2176
|
-
return
|
|
2270
|
+
return t19.tsFunctionType(null, params, t19.tsTypeAnnotation(returnType));
|
|
2177
2271
|
}
|
|
2178
|
-
return
|
|
2272
|
+
return t19.tsAnyKeyword();
|
|
2179
2273
|
}
|
|
2180
2274
|
function isCalleeNamed(node, name) {
|
|
2181
|
-
if (!
|
|
2275
|
+
if (!t19.isIdentifier(node.callee)) {
|
|
2182
2276
|
return false;
|
|
2183
2277
|
}
|
|
2184
2278
|
return node.callee.name === name;
|
|
2185
2279
|
}
|
|
2186
2280
|
function isSimpleLiteral(node) {
|
|
2187
2281
|
if (!node) return false;
|
|
2188
|
-
if (
|
|
2282
|
+
if (t19.isStringLiteral(node) || t19.isNumericLiteral(node) || t19.isBooleanLiteral(node) || t19.isNullLiteral(node) || t19.isRegExpLiteral(node) || t19.isBigIntLiteral(node) || t19.isDecimalLiteral(node)) {
|
|
2189
2283
|
return true;
|
|
2190
2284
|
}
|
|
2191
2285
|
return false;
|
|
@@ -2214,26 +2308,26 @@ function resolveStaticHoisting(ctx) {
|
|
|
2214
2308
|
return {};
|
|
2215
2309
|
}
|
|
2216
2310
|
return {
|
|
2217
|
-
"ImportDeclaration|ExportDeclaration"(
|
|
2218
|
-
if (
|
|
2219
|
-
scriptIR.imports.push(
|
|
2220
|
-
} else if (
|
|
2221
|
-
scriptIR.exports.push(
|
|
2311
|
+
"ImportDeclaration|ExportDeclaration"(path8) {
|
|
2312
|
+
if (t20.isImportDeclaration(path8.node)) {
|
|
2313
|
+
scriptIR.imports.push(path8.node);
|
|
2314
|
+
} else if (t20.isExportDeclaration(path8.node)) {
|
|
2315
|
+
scriptIR.exports.push(path8.node);
|
|
2222
2316
|
}
|
|
2223
|
-
|
|
2317
|
+
path8.remove();
|
|
2224
2318
|
},
|
|
2225
|
-
"TSInterfaceDeclaration|TSTypeAliasDeclaration|TSEnumDeclaration|TSModuleDeclaration|TSModuleDeclaration"(
|
|
2226
|
-
if (
|
|
2227
|
-
scriptIR.tsTypes.push(
|
|
2228
|
-
|
|
2319
|
+
"TSInterfaceDeclaration|TSTypeAliasDeclaration|TSEnumDeclaration|TSModuleDeclaration|TSModuleDeclaration"(path8) {
|
|
2320
|
+
if (t20.isProgram(path8.parent)) {
|
|
2321
|
+
scriptIR.tsTypes.push(path8.node);
|
|
2322
|
+
path8.remove();
|
|
2229
2323
|
}
|
|
2230
2324
|
},
|
|
2231
|
-
VariableDeclarator(
|
|
2232
|
-
const { node } =
|
|
2233
|
-
if (!isVariableDeclTopLevel(
|
|
2325
|
+
VariableDeclarator(path8) {
|
|
2326
|
+
const { node } = path8;
|
|
2327
|
+
if (!isVariableDeclTopLevel(path8) || !isSimpleLiteral(node.init) || getScriptNodeMeta(node)) {
|
|
2234
2328
|
return;
|
|
2235
2329
|
}
|
|
2236
|
-
const declarationPath =
|
|
2330
|
+
const declarationPath = path8.findParent((p) => p.isVariableDeclaration());
|
|
2237
2331
|
if (!declarationPath) return;
|
|
2238
2332
|
scriptIR.statement.global.push(declarationPath.node);
|
|
2239
2333
|
declarationPath.remove();
|
|
@@ -2247,27 +2341,27 @@ function collectLocalStatements(ctx, ast) {
|
|
|
2247
2341
|
}
|
|
2248
2342
|
|
|
2249
2343
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-async-component.ts
|
|
2250
|
-
import * as
|
|
2344
|
+
import * as t21 from "@babel/types";
|
|
2251
2345
|
function resolveDefineAsyncComponent(ctx) {
|
|
2252
2346
|
return {
|
|
2253
|
-
CallExpression(
|
|
2254
|
-
const { node } =
|
|
2347
|
+
CallExpression(path8) {
|
|
2348
|
+
const { node } = path8;
|
|
2255
2349
|
if (!isCalleeNamed(node, VUE_API_MAP.defineAsyncComponent)) {
|
|
2256
2350
|
return;
|
|
2257
2351
|
}
|
|
2258
2352
|
const [arg] = node.arguments;
|
|
2259
2353
|
checkIsUnsupported(ctx, arg);
|
|
2260
|
-
pushToGlobalScope(
|
|
2354
|
+
pushToGlobalScope(path8, ctx);
|
|
2261
2355
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.lazy);
|
|
2262
2356
|
}
|
|
2263
2357
|
};
|
|
2264
2358
|
}
|
|
2265
2359
|
function checkIsUnsupported(ctx, arg) {
|
|
2266
|
-
if (
|
|
2360
|
+
if (t21.isFunction(arg)) {
|
|
2267
2361
|
checkIsDynamicImport(ctx, arg);
|
|
2268
|
-
} else if (
|
|
2362
|
+
} else if (t21.isObjectExpression(arg)) {
|
|
2269
2363
|
const { value } = arg.properties.find(
|
|
2270
|
-
(p) =>
|
|
2364
|
+
(p) => t21.isObjectProperty(p) && t21.isIdentifier(p.key) && p.key.name === "loader"
|
|
2271
2365
|
);
|
|
2272
2366
|
checkIsDynamicImport(ctx, value);
|
|
2273
2367
|
if (arg.properties.length > 1) {
|
|
@@ -2278,7 +2372,7 @@ function checkIsUnsupported(ctx, arg) {
|
|
|
2278
2372
|
function checkIsDynamicImport(ctx, node) {
|
|
2279
2373
|
const { scriptData, filename } = ctx;
|
|
2280
2374
|
const warnIsNotImport = (target) => {
|
|
2281
|
-
if (!target || !
|
|
2375
|
+
if (!target || !t21.isImport(target)) {
|
|
2282
2376
|
logger.error(
|
|
2283
2377
|
`Only ES module dynamic imports are supported. You must use and return import('...').`,
|
|
2284
2378
|
{
|
|
@@ -2289,20 +2383,20 @@ function checkIsDynamicImport(ctx, node) {
|
|
|
2289
2383
|
);
|
|
2290
2384
|
}
|
|
2291
2385
|
};
|
|
2292
|
-
if (
|
|
2386
|
+
if (t21.isFunction(node)) {
|
|
2293
2387
|
checkIsDynamicImport(ctx, node.body);
|
|
2294
2388
|
return;
|
|
2295
2389
|
}
|
|
2296
|
-
if (
|
|
2390
|
+
if (t21.isBlockStatement(node)) {
|
|
2297
2391
|
const [returnSmt] = node.body;
|
|
2298
|
-
if (
|
|
2392
|
+
if (t21.isReturnStatement(returnSmt)) {
|
|
2299
2393
|
warnIsNotImport(returnSmt.argument);
|
|
2300
2394
|
}
|
|
2301
2395
|
return;
|
|
2302
2396
|
}
|
|
2303
|
-
if (
|
|
2397
|
+
if (t21.isCallExpression(node)) {
|
|
2304
2398
|
warnIsNotImport(node.callee);
|
|
2305
|
-
if (
|
|
2399
|
+
if (t21.isStringLiteral(node.arguments[0])) {
|
|
2306
2400
|
replaceVueSuffix(ctx, node.arguments[0]);
|
|
2307
2401
|
}
|
|
2308
2402
|
return;
|
|
@@ -2320,15 +2414,15 @@ function warnMultipleOptionsUsed(ctx, node) {
|
|
|
2320
2414
|
}
|
|
2321
2415
|
);
|
|
2322
2416
|
}
|
|
2323
|
-
function pushToGlobalScope(
|
|
2324
|
-
const { node } =
|
|
2417
|
+
function pushToGlobalScope(path8, ctx) {
|
|
2418
|
+
const { node } = path8;
|
|
2325
2419
|
const callee = node.callee;
|
|
2326
2420
|
callee.name = REACT_API_MAP.lazy;
|
|
2327
2421
|
callee.loc.identifierName = REACT_API_MAP.lazy;
|
|
2328
2422
|
if (node.typeParameters) {
|
|
2329
2423
|
node.typeParameters = void 0;
|
|
2330
2424
|
}
|
|
2331
|
-
let declarationPath =
|
|
2425
|
+
let declarationPath = path8.parentPath;
|
|
2332
2426
|
while (declarationPath) {
|
|
2333
2427
|
if (declarationPath.isVariableDeclaration()) {
|
|
2334
2428
|
break;
|
|
@@ -2339,78 +2433,78 @@ function pushToGlobalScope(path7, ctx) {
|
|
|
2339
2433
|
if (declarationPath?.isVariableDeclaration()) {
|
|
2340
2434
|
fullNode = declarationPath.node;
|
|
2341
2435
|
declarationPath.remove();
|
|
2342
|
-
} else if (
|
|
2343
|
-
fullNode =
|
|
2344
|
-
|
|
2436
|
+
} else if (path8.parentPath.isVariableDeclarator()) {
|
|
2437
|
+
fullNode = path8.parent;
|
|
2438
|
+
path8.parentPath.remove();
|
|
2345
2439
|
} else {
|
|
2346
|
-
fullNode =
|
|
2347
|
-
|
|
2440
|
+
fullNode = path8.node;
|
|
2441
|
+
path8.remove();
|
|
2348
2442
|
}
|
|
2349
2443
|
const scriptIR = getScriptIR(ctx);
|
|
2350
2444
|
scriptIR.statement.global.push(fullNode);
|
|
2351
2445
|
}
|
|
2352
2446
|
|
|
2353
2447
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
2354
|
-
import * as
|
|
2448
|
+
import * as t23 from "@babel/types";
|
|
2355
2449
|
|
|
2356
2450
|
// src/core/transform/sfc/script/shared/hook-creator.ts
|
|
2357
|
-
import * as
|
|
2451
|
+
import * as t22 from "@babel/types";
|
|
2358
2452
|
function createUseCallback(body, deps) {
|
|
2359
|
-
return
|
|
2453
|
+
return t22.callExpression(t22.identifier(REACT_API_MAP.useCallback), [
|
|
2360
2454
|
body,
|
|
2361
|
-
deps ??
|
|
2455
|
+
deps ?? t22.arrayExpression([])
|
|
2362
2456
|
]);
|
|
2363
2457
|
}
|
|
2364
2458
|
function createUseMemo(body, deps) {
|
|
2365
|
-
return
|
|
2366
|
-
|
|
2367
|
-
deps ??
|
|
2459
|
+
return t22.callExpression(t22.identifier(REACT_API_MAP.useMemo), [
|
|
2460
|
+
t22.arrowFunctionExpression([], body),
|
|
2461
|
+
deps ?? t22.arrayExpression([])
|
|
2368
2462
|
]);
|
|
2369
2463
|
}
|
|
2370
2464
|
function createUseImperativeHandle(refId, init) {
|
|
2371
|
-
return
|
|
2465
|
+
return t22.callExpression(t22.identifier(REACT_API_MAP.useImperativeHandle), [refId, init]);
|
|
2372
2466
|
}
|
|
2373
2467
|
|
|
2374
2468
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-expose.ts
|
|
2375
2469
|
function resolveDefineExpose(ctx) {
|
|
2376
2470
|
if (ctx.inputType !== "sfc") return {};
|
|
2377
2471
|
return {
|
|
2378
|
-
CallExpression(
|
|
2379
|
-
const { node } =
|
|
2472
|
+
CallExpression(path8) {
|
|
2473
|
+
const { node } = path8;
|
|
2380
2474
|
const { filename, scriptData } = ctx;
|
|
2381
2475
|
if (!isCalleeNamed(node, MACRO_API_NAMES.expose)) {
|
|
2382
2476
|
return;
|
|
2383
2477
|
}
|
|
2384
2478
|
const [expose] = node.arguments;
|
|
2385
2479
|
if (!expose) {
|
|
2386
|
-
|
|
2480
|
+
path8.remove();
|
|
2387
2481
|
return;
|
|
2388
2482
|
}
|
|
2389
2483
|
const adapter = ADAPTER_RULES.react[MACRO_API_NAMES.expose];
|
|
2390
2484
|
recordImport(ctx, adapter.package, REACT_API_MAP.forwardRef);
|
|
2391
2485
|
recordImport(ctx, adapter.package, adapter.target);
|
|
2392
|
-
if (!
|
|
2486
|
+
if (!t23.isObjectExpression(expose) && !t23.isFunction(expose)) {
|
|
2393
2487
|
logger.warn("Non-deterministic object literal may cause unknown risks.", {
|
|
2394
2488
|
file: filename,
|
|
2395
2489
|
loc: expose.loc,
|
|
2396
2490
|
source: scriptData.source
|
|
2397
2491
|
});
|
|
2398
2492
|
}
|
|
2399
|
-
const init = !
|
|
2493
|
+
const init = !t23.isFunction(expose) ? t23.arrowFunctionExpression([], expose) : expose;
|
|
2400
2494
|
const { forwardRef } = scriptData;
|
|
2401
|
-
const newNode = createUseImperativeHandle(
|
|
2495
|
+
const newNode = createUseImperativeHandle(t23.identifier(forwardRef.refField), init);
|
|
2402
2496
|
forwardRef.enabled = true;
|
|
2403
|
-
|
|
2497
|
+
path8.replaceWith(newNode);
|
|
2404
2498
|
}
|
|
2405
2499
|
};
|
|
2406
2500
|
}
|
|
2407
2501
|
|
|
2408
2502
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-define-options.ts
|
|
2409
|
-
import * as
|
|
2503
|
+
import * as t24 from "@babel/types";
|
|
2410
2504
|
function resolveDefineOptions(ctx) {
|
|
2411
2505
|
return {
|
|
2412
|
-
CallExpression(
|
|
2413
|
-
const { node } =
|
|
2506
|
+
CallExpression(path8) {
|
|
2507
|
+
const { node } = path8;
|
|
2414
2508
|
if (!isCalleeNamed(node, MACRO_API_NAMES.options)) {
|
|
2415
2509
|
return;
|
|
2416
2510
|
}
|
|
@@ -2422,10 +2516,10 @@ function resolveDefineOptions(ctx) {
|
|
|
2422
2516
|
file: filename,
|
|
2423
2517
|
loc: node?.loc
|
|
2424
2518
|
});
|
|
2425
|
-
|
|
2519
|
+
path8.remove();
|
|
2426
2520
|
return;
|
|
2427
2521
|
}
|
|
2428
|
-
if (!
|
|
2522
|
+
if (!t24.isObjectExpression(options)) {
|
|
2429
2523
|
logger.warn("Argument for defineOptions must be an object expression.", {
|
|
2430
2524
|
source: scriptData.source,
|
|
2431
2525
|
file: filename,
|
|
@@ -2433,13 +2527,13 @@ function resolveDefineOptions(ctx) {
|
|
|
2433
2527
|
});
|
|
2434
2528
|
} else {
|
|
2435
2529
|
for (const prop of options.properties) {
|
|
2436
|
-
if (!
|
|
2530
|
+
if (!t24.isObjectProperty(prop) || !t24.isIdentifier(prop.key)) {
|
|
2437
2531
|
continue;
|
|
2438
2532
|
}
|
|
2439
2533
|
extractName(prop, ctx);
|
|
2440
2534
|
}
|
|
2441
2535
|
}
|
|
2442
|
-
|
|
2536
|
+
path8.remove();
|
|
2443
2537
|
}
|
|
2444
2538
|
};
|
|
2445
2539
|
}
|
|
@@ -2447,7 +2541,7 @@ function extractName(prop, ctx) {
|
|
|
2447
2541
|
if (ctx.compName) return;
|
|
2448
2542
|
const { filename, scriptData } = ctx;
|
|
2449
2543
|
if (!prop.computed && prop.key.name === "name") {
|
|
2450
|
-
if (
|
|
2544
|
+
if (t24.isStringLiteral(prop.value)) {
|
|
2451
2545
|
ctx.compName = prop.value.value;
|
|
2452
2546
|
return;
|
|
2453
2547
|
}
|
|
@@ -2460,13 +2554,21 @@ function extractName(prop, ctx) {
|
|
|
2460
2554
|
}
|
|
2461
2555
|
|
|
2462
2556
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-emit-calls.ts
|
|
2463
|
-
import * as
|
|
2557
|
+
import * as t25 from "@babel/types";
|
|
2464
2558
|
function resolveEmitCalls(ctx) {
|
|
2559
|
+
const formatEmitEventName = (raw) => {
|
|
2560
|
+
if (raw.startsWith("update:")) {
|
|
2561
|
+
const modelKey = raw.slice("update:".length);
|
|
2562
|
+
return `onUpdate${capitalize(camelCase(modelKey))}`;
|
|
2563
|
+
}
|
|
2564
|
+
const normalized = raw.includes(":") ? raw.replace(/:/g, "-") : raw;
|
|
2565
|
+
return `on${capitalize(camelCase(normalized))}`;
|
|
2566
|
+
};
|
|
2465
2567
|
return {
|
|
2466
|
-
CallExpression(
|
|
2467
|
-
const { node } =
|
|
2568
|
+
CallExpression(path8) {
|
|
2569
|
+
const { node } = path8;
|
|
2468
2570
|
const { filename, templateData, scriptData } = ctx;
|
|
2469
|
-
if (!
|
|
2571
|
+
if (!t25.isIdentifier(node.callee)) return;
|
|
2470
2572
|
const { name } = node.callee;
|
|
2471
2573
|
const checkIfFromDefineEmits = () => {
|
|
2472
2574
|
let result = false;
|
|
@@ -2475,10 +2577,10 @@ function resolveEmitCalls(ctx) {
|
|
|
2475
2577
|
result = meta.source === MACRO_API_NAMES.emits;
|
|
2476
2578
|
}
|
|
2477
2579
|
if (!result) {
|
|
2478
|
-
const binding =
|
|
2580
|
+
const binding = path8.scope.getBinding(name);
|
|
2479
2581
|
if (binding) {
|
|
2480
2582
|
const parent = binding.path.node;
|
|
2481
|
-
if (
|
|
2583
|
+
if (t25.isVariableDeclarator(parent) && t25.isCallExpression(parent.init) && t25.isIdentifier(parent.init.callee)) {
|
|
2482
2584
|
result = parent.init.callee.name === MACRO_API_NAMES.emits;
|
|
2483
2585
|
}
|
|
2484
2586
|
}
|
|
@@ -2487,55 +2589,56 @@ function resolveEmitCalls(ctx) {
|
|
|
2487
2589
|
};
|
|
2488
2590
|
if (!checkIfFromDefineEmits()) return;
|
|
2489
2591
|
const [callee, ...args] = node.arguments;
|
|
2490
|
-
const eventName =
|
|
2592
|
+
const eventName = t25.isStringLiteral(callee) ? formatEmitEventName(callee.value) : void 0;
|
|
2491
2593
|
if (!eventName) {
|
|
2492
2594
|
logger.warn(`Expected String type but got ${callee?.type}, expression will be removed`, {
|
|
2493
2595
|
file: filename,
|
|
2494
2596
|
source: scriptData.source,
|
|
2495
2597
|
loc: callee?.loc
|
|
2496
2598
|
});
|
|
2497
|
-
|
|
2599
|
+
path8.remove();
|
|
2498
2600
|
return;
|
|
2499
2601
|
}
|
|
2500
|
-
const propCall =
|
|
2501
|
-
|
|
2502
|
-
|
|
2503
|
-
|
|
2602
|
+
const propCall = t25.optionalCallExpression(
|
|
2603
|
+
t25.optionalMemberExpression(
|
|
2604
|
+
t25.identifier(ctx.propField),
|
|
2605
|
+
t25.identifier(eventName),
|
|
2504
2606
|
false,
|
|
2505
2607
|
true
|
|
2506
2608
|
),
|
|
2507
|
-
args
|
|
2609
|
+
args,
|
|
2610
|
+
true
|
|
2508
2611
|
);
|
|
2509
|
-
|
|
2612
|
+
path8.replaceWith(propCall);
|
|
2510
2613
|
}
|
|
2511
2614
|
};
|
|
2512
2615
|
}
|
|
2513
2616
|
|
|
2514
2617
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/index.ts
|
|
2515
|
-
import * as
|
|
2618
|
+
import * as t30 from "@babel/types";
|
|
2516
2619
|
|
|
2517
2620
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-emits.ts
|
|
2518
|
-
import * as
|
|
2621
|
+
import * as t27 from "@babel/types";
|
|
2519
2622
|
|
|
2520
2623
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/shared.ts
|
|
2521
|
-
import * as
|
|
2624
|
+
import * as t26 from "@babel/types";
|
|
2522
2625
|
function cloneCallableParams(params) {
|
|
2523
2626
|
const cloneCallableParam = (param, index) => {
|
|
2524
|
-
if (
|
|
2627
|
+
if (t26.isRestElement(param)) {
|
|
2525
2628
|
const arg = param.argument;
|
|
2526
|
-
const name =
|
|
2527
|
-
const rest =
|
|
2528
|
-
rest.typeAnnotation = param.typeAnnotation || (
|
|
2629
|
+
const name = t26.isIdentifier(arg) ? arg.name : `args${index}`;
|
|
2630
|
+
const rest = t26.restElement(t26.identifier(name));
|
|
2631
|
+
rest.typeAnnotation = param.typeAnnotation || (t26.isIdentifier(arg) ? arg.typeAnnotation : null) || t26.tsTypeAnnotation(t26.tsArrayType(t26.tsAnyKeyword()));
|
|
2529
2632
|
return rest;
|
|
2530
2633
|
}
|
|
2531
|
-
if (
|
|
2532
|
-
const id =
|
|
2634
|
+
if (t26.isIdentifier(param)) {
|
|
2635
|
+
const id = t26.identifier(param.name || `arg${index}`);
|
|
2533
2636
|
id.optional = param.optional;
|
|
2534
|
-
id.typeAnnotation = param.typeAnnotation ||
|
|
2637
|
+
id.typeAnnotation = param.typeAnnotation || t26.tsTypeAnnotation(t26.tsAnyKeyword());
|
|
2535
2638
|
return id;
|
|
2536
2639
|
}
|
|
2537
|
-
const fallback =
|
|
2538
|
-
fallback.typeAnnotation =
|
|
2640
|
+
const fallback = t26.identifier(`arg${index}`);
|
|
2641
|
+
fallback.typeAnnotation = t26.tsTypeAnnotation(t26.tsAnyKeyword());
|
|
2539
2642
|
return fallback;
|
|
2540
2643
|
};
|
|
2541
2644
|
return params.map(cloneCallableParam);
|
|
@@ -2544,19 +2647,19 @@ function cloneCallableParams(params) {
|
|
|
2544
2647
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-emits.ts
|
|
2545
2648
|
function resolveEmitsTopLevelTypes(ctx) {
|
|
2546
2649
|
return {
|
|
2547
|
-
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(
|
|
2548
|
-
if (!
|
|
2549
|
-
const { node } =
|
|
2550
|
-
if (
|
|
2551
|
-
const typeLiteral =
|
|
2650
|
+
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(path8) {
|
|
2651
|
+
if (!t27.isProgram(path8.parent)) return;
|
|
2652
|
+
const { node } = path8;
|
|
2653
|
+
if (t27.isTSInterfaceDeclaration(node)) {
|
|
2654
|
+
const typeLiteral = t27.tsTypeLiteral(node.body.body);
|
|
2552
2655
|
if (!hasEmitsSignatureInType(typeLiteral)) return;
|
|
2553
2656
|
const resolved = resolveTopLevelEmitType(typeLiteral);
|
|
2554
|
-
if (resolved &&
|
|
2657
|
+
if (resolved && t27.isTSTypeLiteral(resolved)) {
|
|
2555
2658
|
node.body.body = resolved.members;
|
|
2556
2659
|
}
|
|
2557
2660
|
return;
|
|
2558
2661
|
}
|
|
2559
|
-
if (
|
|
2662
|
+
if (t27.isTSTypeAliasDeclaration(node)) {
|
|
2560
2663
|
if (!hasEmitsSignatureInType(node.typeAnnotation)) return;
|
|
2561
2664
|
const resolved = resolveTopLevelEmitType(node.typeAnnotation);
|
|
2562
2665
|
if (resolved) {
|
|
@@ -2567,52 +2670,52 @@ function resolveEmitsTopLevelTypes(ctx) {
|
|
|
2567
2670
|
};
|
|
2568
2671
|
}
|
|
2569
2672
|
function resolveTopLevelEmitType(tsType) {
|
|
2570
|
-
if (
|
|
2673
|
+
if (t27.isTSParenthesizedType(tsType)) {
|
|
2571
2674
|
return resolveTopLevelEmitType(tsType.typeAnnotation);
|
|
2572
2675
|
}
|
|
2573
|
-
if (
|
|
2676
|
+
if (t27.isTSTypeReference(tsType)) {
|
|
2574
2677
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2575
2678
|
return tsType;
|
|
2576
2679
|
}
|
|
2577
2680
|
const params = tsType.typeParameters.params.map((param) => resolveTopLevelEmitType(param)).filter(Boolean);
|
|
2578
|
-
return
|
|
2681
|
+
return t27.tsTypeReference(
|
|
2579
2682
|
tsType.typeName,
|
|
2580
|
-
|
|
2683
|
+
t27.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2581
2684
|
);
|
|
2582
2685
|
}
|
|
2583
|
-
if (
|
|
2686
|
+
if (t27.isTSIntersectionType(tsType)) {
|
|
2584
2687
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
2585
2688
|
if (!types.length) return null;
|
|
2586
2689
|
if (types.length === 1) return types[0];
|
|
2587
|
-
return
|
|
2690
|
+
return t27.tsIntersectionType(types);
|
|
2588
2691
|
}
|
|
2589
|
-
if (
|
|
2692
|
+
if (t27.isTSUnionType(tsType)) {
|
|
2590
2693
|
const types = tsType.types.map(resolveTopLevelEmitType).filter(Boolean);
|
|
2591
2694
|
if (!types.length) return null;
|
|
2592
2695
|
if (types.length === 1) return types[0];
|
|
2593
|
-
return
|
|
2696
|
+
return t27.tsUnionType(types);
|
|
2594
2697
|
}
|
|
2595
|
-
if (
|
|
2698
|
+
if (t27.isTSTypeLiteral(tsType)) {
|
|
2596
2699
|
const members = [];
|
|
2597
2700
|
for (const member of tsType.members) {
|
|
2598
|
-
if (
|
|
2701
|
+
if (t27.isTSCallSignatureDeclaration(member)) {
|
|
2599
2702
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
2600
2703
|
continue;
|
|
2601
2704
|
}
|
|
2602
2705
|
members.push(member);
|
|
2603
2706
|
}
|
|
2604
2707
|
if (!members.length) return null;
|
|
2605
|
-
return
|
|
2708
|
+
return t27.tsTypeLiteral(members);
|
|
2606
2709
|
}
|
|
2607
|
-
if (
|
|
2710
|
+
if (t27.isTSFunctionType(tsType)) {
|
|
2608
2711
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
2609
2712
|
if (!props.length) return null;
|
|
2610
|
-
return
|
|
2713
|
+
return t27.tsTypeLiteral(props);
|
|
2611
2714
|
}
|
|
2612
2715
|
return tsType;
|
|
2613
2716
|
}
|
|
2614
|
-
function resolveDefineEmitsIface(
|
|
2615
|
-
const { node } =
|
|
2717
|
+
function resolveDefineEmitsIface(path8, ctx) {
|
|
2718
|
+
const { node } = path8;
|
|
2616
2719
|
const [runtimeArg] = node.arguments;
|
|
2617
2720
|
const tsParams = node.typeParameters?.params;
|
|
2618
2721
|
if (tsParams?.length) {
|
|
@@ -2628,41 +2731,41 @@ function processInferredTypes(ctx, runtimeArg) {
|
|
|
2628
2731
|
propsTSIface: { emitTypes }
|
|
2629
2732
|
} = ctx.scriptData;
|
|
2630
2733
|
const members = [];
|
|
2631
|
-
if (
|
|
2734
|
+
if (t27.isArrayExpression(runtimeArg)) {
|
|
2632
2735
|
for (const element of runtimeArg.elements) {
|
|
2633
|
-
if (!element || !
|
|
2736
|
+
if (!element || !t27.isStringLiteral(element)) continue;
|
|
2634
2737
|
const handlerName = resolveEmitHandlerName(element.value);
|
|
2635
2738
|
const key = buildKey(handlerName);
|
|
2636
|
-
const fnType =
|
|
2739
|
+
const fnType = t27.tsFunctionType(
|
|
2637
2740
|
null,
|
|
2638
2741
|
[createRestAnyParam("args")],
|
|
2639
|
-
|
|
2742
|
+
t27.tsTypeAnnotation(t27.tsAnyKeyword())
|
|
2640
2743
|
);
|
|
2641
|
-
const prop =
|
|
2744
|
+
const prop = t27.tsPropertySignature(key, t27.tsTypeAnnotation(fnType));
|
|
2642
2745
|
prop.optional = true;
|
|
2643
2746
|
members.push(prop);
|
|
2644
2747
|
}
|
|
2645
2748
|
if (members.length) {
|
|
2646
|
-
emitTypes.push(
|
|
2749
|
+
emitTypes.push(t27.tsTypeLiteral(members));
|
|
2647
2750
|
}
|
|
2648
2751
|
return;
|
|
2649
2752
|
}
|
|
2650
|
-
if (
|
|
2753
|
+
if (t27.isObjectExpression(runtimeArg)) {
|
|
2651
2754
|
for (const prop of runtimeArg.properties) {
|
|
2652
|
-
if (!
|
|
2653
|
-
if (
|
|
2755
|
+
if (!t27.isObjectProperty(prop)) continue;
|
|
2756
|
+
if (t27.isSpreadElement(prop)) continue;
|
|
2654
2757
|
const rawName = resolvePropName(prop.key);
|
|
2655
2758
|
if (!rawName) continue;
|
|
2656
2759
|
const handlerName = resolveEmitHandlerName(rawName);
|
|
2657
2760
|
const key = buildKey(handlerName);
|
|
2658
|
-
const params =
|
|
2659
|
-
const fnType =
|
|
2660
|
-
const propSig =
|
|
2761
|
+
const params = t27.isArrayExpression(prop.value) ? resolveRuntimeTupleParams(prop.value) : [createRestAnyParam("args")];
|
|
2762
|
+
const fnType = t27.tsFunctionType(null, params, t27.tsTypeAnnotation(t27.tsAnyKeyword()));
|
|
2763
|
+
const propSig = t27.tsPropertySignature(key, t27.tsTypeAnnotation(fnType));
|
|
2661
2764
|
propSig.optional = true;
|
|
2662
2765
|
members.push(propSig);
|
|
2663
2766
|
}
|
|
2664
2767
|
if (members.length) {
|
|
2665
|
-
emitTypes.push(
|
|
2768
|
+
emitTypes.push(t27.tsTypeLiteral(members));
|
|
2666
2769
|
}
|
|
2667
2770
|
}
|
|
2668
2771
|
}
|
|
@@ -2683,129 +2786,129 @@ function resolveEmitHandlerName(rawName) {
|
|
|
2683
2786
|
return `on${name}`;
|
|
2684
2787
|
}
|
|
2685
2788
|
function resolvePropName(key) {
|
|
2686
|
-
if (
|
|
2687
|
-
if (
|
|
2688
|
-
if (
|
|
2789
|
+
if (t27.isIdentifier(key)) return key.name;
|
|
2790
|
+
if (t27.isStringLiteral(key)) return key.value;
|
|
2791
|
+
if (t27.isNumericLiteral(key)) return String(key.value);
|
|
2689
2792
|
return null;
|
|
2690
2793
|
}
|
|
2691
2794
|
function buildKey(name) {
|
|
2692
|
-
return
|
|
2795
|
+
return t27.isValidIdentifier(name) ? t27.identifier(name) : t27.stringLiteral(name);
|
|
2693
2796
|
}
|
|
2694
2797
|
function createRestAnyParam(name) {
|
|
2695
|
-
const id =
|
|
2696
|
-
const rest =
|
|
2697
|
-
rest.typeAnnotation =
|
|
2798
|
+
const id = t27.identifier(name);
|
|
2799
|
+
const rest = t27.restElement(id);
|
|
2800
|
+
rest.typeAnnotation = t27.tsTypeAnnotation(t27.tsArrayType(t27.tsAnyKeyword()));
|
|
2698
2801
|
return rest;
|
|
2699
2802
|
}
|
|
2700
2803
|
function resolveRuntimeTupleParams(value) {
|
|
2701
2804
|
const params = [];
|
|
2702
2805
|
value.elements.forEach((element, index) => {
|
|
2703
2806
|
if (!element) return;
|
|
2704
|
-
if (
|
|
2807
|
+
if (t27.isSpreadElement(element)) {
|
|
2705
2808
|
params.push(createRestAnyParam(`args${index}`));
|
|
2706
2809
|
return;
|
|
2707
2810
|
}
|
|
2708
|
-
if (
|
|
2709
|
-
const id =
|
|
2710
|
-
id.typeAnnotation = element.typeAnnotation ||
|
|
2811
|
+
if (t27.isIdentifier(element)) {
|
|
2812
|
+
const id = t27.identifier(element.name);
|
|
2813
|
+
id.typeAnnotation = element.typeAnnotation || t27.tsTypeAnnotation(t27.tsAnyKeyword());
|
|
2711
2814
|
params.push(id);
|
|
2712
2815
|
return;
|
|
2713
2816
|
}
|
|
2714
|
-
if (
|
|
2715
|
-
const id =
|
|
2716
|
-
id.typeAnnotation =
|
|
2817
|
+
if (t27.isTSAsExpression(element)) {
|
|
2818
|
+
const id = t27.identifier(`arg${index}`);
|
|
2819
|
+
id.typeAnnotation = t27.tsTypeAnnotation(element.typeAnnotation);
|
|
2717
2820
|
params.push(id);
|
|
2718
2821
|
return;
|
|
2719
2822
|
}
|
|
2720
|
-
const fallback =
|
|
2721
|
-
fallback.typeAnnotation =
|
|
2823
|
+
const fallback = t27.identifier(`arg${index}`);
|
|
2824
|
+
fallback.typeAnnotation = t27.tsTypeAnnotation(t27.tsAnyKeyword());
|
|
2722
2825
|
params.push(fallback);
|
|
2723
2826
|
});
|
|
2724
2827
|
return params;
|
|
2725
2828
|
}
|
|
2726
2829
|
function resolveExplicitEmitType(tsType) {
|
|
2727
|
-
if (
|
|
2830
|
+
if (t27.isTSParenthesizedType(tsType)) {
|
|
2728
2831
|
return resolveExplicitEmitType(tsType.typeAnnotation);
|
|
2729
2832
|
}
|
|
2730
|
-
if (
|
|
2833
|
+
if (t27.isTSTypeReference(tsType)) {
|
|
2731
2834
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2732
2835
|
return tsType;
|
|
2733
2836
|
}
|
|
2734
2837
|
const params = tsType.typeParameters.params.map((param) => resolveExplicitEmitType(param)).filter(Boolean);
|
|
2735
|
-
return
|
|
2838
|
+
return t27.tsTypeReference(
|
|
2736
2839
|
tsType.typeName,
|
|
2737
|
-
|
|
2840
|
+
t27.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
2738
2841
|
);
|
|
2739
2842
|
}
|
|
2740
|
-
if (
|
|
2843
|
+
if (t27.isTSIntersectionType(tsType)) {
|
|
2741
2844
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
2742
2845
|
if (!types.length) return null;
|
|
2743
2846
|
if (types.length === 1) return types[0];
|
|
2744
|
-
return
|
|
2847
|
+
return t27.tsIntersectionType(types);
|
|
2745
2848
|
}
|
|
2746
|
-
if (
|
|
2849
|
+
if (t27.isTSUnionType(tsType)) {
|
|
2747
2850
|
const types = tsType.types.map(resolveExplicitEmitType).filter(Boolean);
|
|
2748
2851
|
if (!types.length) return null;
|
|
2749
2852
|
if (types.length === 1) return types[0];
|
|
2750
|
-
return
|
|
2853
|
+
return t27.tsUnionType(types);
|
|
2751
2854
|
}
|
|
2752
|
-
if (
|
|
2855
|
+
if (t27.isTSTypeLiteral(tsType)) {
|
|
2753
2856
|
const members = [];
|
|
2754
2857
|
for (const member of tsType.members) {
|
|
2755
|
-
if (
|
|
2858
|
+
if (t27.isTSPropertySignature(member)) {
|
|
2756
2859
|
const prop = resolveEmitPropFromPropertySignature(member);
|
|
2757
2860
|
if (prop) members.push(prop);
|
|
2758
2861
|
continue;
|
|
2759
2862
|
}
|
|
2760
|
-
if (
|
|
2863
|
+
if (t27.isTSCallSignatureDeclaration(member)) {
|
|
2761
2864
|
members.push(...resolveEmitPropsFromCallSignature(member));
|
|
2762
2865
|
continue;
|
|
2763
2866
|
}
|
|
2764
2867
|
}
|
|
2765
2868
|
if (!members.length) return null;
|
|
2766
|
-
return
|
|
2869
|
+
return t27.tsTypeLiteral(members);
|
|
2767
2870
|
}
|
|
2768
|
-
if (
|
|
2871
|
+
if (t27.isTSFunctionType(tsType)) {
|
|
2769
2872
|
const props = resolveEmitPropsFromCallable(tsType.parameters, tsType.typeAnnotation);
|
|
2770
2873
|
if (!props.length) return null;
|
|
2771
|
-
return
|
|
2874
|
+
return t27.tsTypeLiteral(props);
|
|
2772
2875
|
}
|
|
2773
2876
|
return tsType;
|
|
2774
2877
|
}
|
|
2775
2878
|
function hasEmitsSignatureInType(tsType) {
|
|
2776
|
-
if (
|
|
2879
|
+
if (t27.isTSParenthesizedType(tsType)) {
|
|
2777
2880
|
return hasEmitsSignatureInType(tsType.typeAnnotation);
|
|
2778
2881
|
}
|
|
2779
|
-
if (
|
|
2882
|
+
if (t27.isTSTypeReference(tsType)) {
|
|
2780
2883
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
2781
2884
|
return false;
|
|
2782
2885
|
}
|
|
2783
2886
|
return tsType.typeParameters.params.some(hasEmitsSignatureInType);
|
|
2784
2887
|
}
|
|
2785
|
-
if (
|
|
2888
|
+
if (t27.isTSIntersectionType(tsType) || t27.isTSUnionType(tsType)) {
|
|
2786
2889
|
return tsType.types.some(hasEmitsSignatureInType);
|
|
2787
2890
|
}
|
|
2788
|
-
if (
|
|
2891
|
+
if (t27.isTSTypeLiteral(tsType)) {
|
|
2789
2892
|
return tsType.members.some(hasEmitsSignatureInMember);
|
|
2790
2893
|
}
|
|
2791
|
-
if (
|
|
2894
|
+
if (t27.isTSFunctionType(tsType)) {
|
|
2792
2895
|
return isEmitsCallable(tsType.parameters);
|
|
2793
2896
|
}
|
|
2794
2897
|
return false;
|
|
2795
2898
|
}
|
|
2796
2899
|
function hasEmitsSignatureInMember(member) {
|
|
2797
|
-
if (
|
|
2900
|
+
if (t27.isTSCallSignatureDeclaration(member)) {
|
|
2798
2901
|
return isEmitsCallable(member.parameters);
|
|
2799
2902
|
}
|
|
2800
2903
|
return false;
|
|
2801
2904
|
}
|
|
2802
2905
|
function isEmitsCallable(parameters) {
|
|
2803
2906
|
const [eventParam] = parameters;
|
|
2804
|
-
if (!eventParam || !
|
|
2907
|
+
if (!eventParam || !t27.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
2805
2908
|
return false;
|
|
2806
2909
|
}
|
|
2807
2910
|
const { typeAnnotation } = eventParam;
|
|
2808
|
-
if (
|
|
2911
|
+
if (t27.isNoop(typeAnnotation)) return false;
|
|
2809
2912
|
return resolveEventNames(typeAnnotation.typeAnnotation).length > 0;
|
|
2810
2913
|
}
|
|
2811
2914
|
function resolveEmitPropFromPropertySignature(member) {
|
|
@@ -2815,19 +2918,19 @@ function resolveEmitPropFromPropertySignature(member) {
|
|
|
2815
2918
|
const key = buildKey(handlerName);
|
|
2816
2919
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
2817
2920
|
let params = [];
|
|
2818
|
-
let returnType =
|
|
2819
|
-
if (typeAnnotation &&
|
|
2921
|
+
let returnType = t27.tsAnyKeyword();
|
|
2922
|
+
if (typeAnnotation && t27.isTSFunctionType(typeAnnotation)) {
|
|
2820
2923
|
params = cloneCallableParams(typeAnnotation.parameters);
|
|
2821
2924
|
returnType = typeAnnotation.typeAnnotation?.typeAnnotation ?? returnType;
|
|
2822
|
-
} else if (typeAnnotation &&
|
|
2925
|
+
} else if (typeAnnotation && t27.isTSTupleType(typeAnnotation)) {
|
|
2823
2926
|
params = resolveTupleTypeParams(typeAnnotation);
|
|
2824
2927
|
} else if (typeAnnotation) {
|
|
2825
|
-
const id =
|
|
2826
|
-
id.typeAnnotation =
|
|
2928
|
+
const id = t27.identifier("value");
|
|
2929
|
+
id.typeAnnotation = t27.tsTypeAnnotation(typeAnnotation);
|
|
2827
2930
|
params = [id];
|
|
2828
2931
|
}
|
|
2829
|
-
const fnType =
|
|
2830
|
-
const prop =
|
|
2932
|
+
const fnType = t27.tsFunctionType(null, params, t27.tsTypeAnnotation(returnType));
|
|
2933
|
+
const prop = t27.tsPropertySignature(key, t27.tsTypeAnnotation(fnType));
|
|
2831
2934
|
prop.optional = !!member.optional;
|
|
2832
2935
|
return prop;
|
|
2833
2936
|
}
|
|
@@ -2836,32 +2939,32 @@ function resolveEmitPropsFromCallSignature(member) {
|
|
|
2836
2939
|
}
|
|
2837
2940
|
function resolveEmitPropsFromCallable(parameters, typeAnnotation) {
|
|
2838
2941
|
const [eventParam, ...restParams] = parameters;
|
|
2839
|
-
if (!eventParam || !
|
|
2942
|
+
if (!eventParam || !t27.isIdentifier(eventParam) || !eventParam.typeAnnotation) {
|
|
2840
2943
|
return [];
|
|
2841
2944
|
}
|
|
2842
2945
|
const { typeAnnotation: paramTypeAnnotation } = eventParam;
|
|
2843
|
-
if (
|
|
2946
|
+
if (t27.isNoop(paramTypeAnnotation)) return [];
|
|
2844
2947
|
const eventNames = resolveEventNames(paramTypeAnnotation.typeAnnotation);
|
|
2845
2948
|
if (!eventNames.length) return [];
|
|
2846
|
-
const returnType = typeAnnotation?.typeAnnotation ??
|
|
2949
|
+
const returnType = typeAnnotation?.typeAnnotation ?? t27.tsAnyKeyword();
|
|
2847
2950
|
return eventNames.map((eventName) => {
|
|
2848
2951
|
const handlerName = resolveEmitHandlerName(eventName);
|
|
2849
2952
|
const key = buildKey(handlerName);
|
|
2850
2953
|
const params = cloneCallableParams(restParams);
|
|
2851
|
-
const fnType =
|
|
2852
|
-
const prop =
|
|
2954
|
+
const fnType = t27.tsFunctionType(null, params, t27.tsTypeAnnotation(returnType));
|
|
2955
|
+
const prop = t27.tsPropertySignature(key, t27.tsTypeAnnotation(fnType));
|
|
2853
2956
|
prop.optional = true;
|
|
2854
2957
|
return prop;
|
|
2855
2958
|
});
|
|
2856
2959
|
}
|
|
2857
2960
|
function resolveEventNames(type) {
|
|
2858
|
-
if (
|
|
2961
|
+
if (t27.isTSLiteralType(type) && t27.isStringLiteral(type.literal)) {
|
|
2859
2962
|
return [type.literal.value];
|
|
2860
2963
|
}
|
|
2861
|
-
if (
|
|
2964
|
+
if (t27.isTSUnionType(type)) {
|
|
2862
2965
|
return type.types.flatMap(resolveEventNames);
|
|
2863
2966
|
}
|
|
2864
|
-
if (
|
|
2967
|
+
if (t27.isTSParenthesizedType(type)) {
|
|
2865
2968
|
return resolveEventNames(type.typeAnnotation);
|
|
2866
2969
|
}
|
|
2867
2970
|
return [];
|
|
@@ -2874,46 +2977,46 @@ function resolveTupleTypeParams(tuple) {
|
|
|
2874
2977
|
return params;
|
|
2875
2978
|
}
|
|
2876
2979
|
function resolveTupleElementParam(element, index) {
|
|
2877
|
-
const isNamedTuple = typeof
|
|
2980
|
+
const isNamedTuple = typeof t27.isTSNamedTupleMember === "function" && t27.isTSNamedTupleMember(element);
|
|
2878
2981
|
if (isNamedTuple) {
|
|
2879
2982
|
const tupleMember = element;
|
|
2880
2983
|
const name = tupleMember.label?.name || `arg${index}`;
|
|
2881
2984
|
let innerType = tupleMember.elementType;
|
|
2882
2985
|
let optional = tupleMember.optional;
|
|
2883
|
-
if (
|
|
2986
|
+
if (t27.isTSOptionalType(innerType)) {
|
|
2884
2987
|
optional = true;
|
|
2885
2988
|
innerType = innerType.typeAnnotation;
|
|
2886
2989
|
}
|
|
2887
|
-
if (
|
|
2888
|
-
const rest =
|
|
2889
|
-
rest.typeAnnotation =
|
|
2990
|
+
if (t27.isTSRestType(innerType)) {
|
|
2991
|
+
const rest = t27.restElement(t27.identifier(name));
|
|
2992
|
+
rest.typeAnnotation = t27.tsTypeAnnotation(innerType.typeAnnotation);
|
|
2890
2993
|
return rest;
|
|
2891
2994
|
}
|
|
2892
|
-
const id2 =
|
|
2995
|
+
const id2 = t27.identifier(name);
|
|
2893
2996
|
id2.optional = optional;
|
|
2894
|
-
id2.typeAnnotation =
|
|
2997
|
+
id2.typeAnnotation = t27.tsTypeAnnotation(innerType);
|
|
2895
2998
|
return id2;
|
|
2896
2999
|
}
|
|
2897
|
-
if (
|
|
2898
|
-
const rest =
|
|
2899
|
-
rest.typeAnnotation =
|
|
3000
|
+
if (t27.isTSRestType(element)) {
|
|
3001
|
+
const rest = t27.restElement(t27.identifier(`args${index}`));
|
|
3002
|
+
rest.typeAnnotation = t27.tsTypeAnnotation(element.typeAnnotation);
|
|
2900
3003
|
return rest;
|
|
2901
3004
|
}
|
|
2902
|
-
if (
|
|
2903
|
-
const id2 =
|
|
3005
|
+
if (t27.isTSOptionalType(element)) {
|
|
3006
|
+
const id2 = t27.identifier(`arg${index}`);
|
|
2904
3007
|
id2.optional = true;
|
|
2905
|
-
id2.typeAnnotation =
|
|
3008
|
+
id2.typeAnnotation = t27.tsTypeAnnotation(element.typeAnnotation);
|
|
2906
3009
|
return id2;
|
|
2907
3010
|
}
|
|
2908
|
-
const id =
|
|
2909
|
-
id.typeAnnotation =
|
|
3011
|
+
const id = t27.identifier(`arg${index}`);
|
|
3012
|
+
id.typeAnnotation = t27.tsTypeAnnotation(element);
|
|
2910
3013
|
return id;
|
|
2911
3014
|
}
|
|
2912
3015
|
|
|
2913
3016
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-props.ts
|
|
2914
|
-
import * as
|
|
2915
|
-
function resolveDefinePropsIface(
|
|
2916
|
-
const { node } =
|
|
3017
|
+
import * as t28 from "@babel/types";
|
|
3018
|
+
function resolveDefinePropsIface(path8, ctx) {
|
|
3019
|
+
const { node } = path8;
|
|
2917
3020
|
const [runtimeArg] = node.arguments;
|
|
2918
3021
|
const tsParams = node.typeParameters?.params;
|
|
2919
3022
|
if (tsParams?.length) {
|
|
@@ -2931,38 +3034,38 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
2931
3034
|
} = scriptData;
|
|
2932
3035
|
if (!runtimeArg) return;
|
|
2933
3036
|
const members = [];
|
|
2934
|
-
if (
|
|
3037
|
+
if (t28.isArrayExpression(runtimeArg)) {
|
|
2935
3038
|
for (const element of runtimeArg.elements) {
|
|
2936
|
-
if (!element || !
|
|
2937
|
-
const key =
|
|
2938
|
-
const prop =
|
|
3039
|
+
if (!element || !t28.isStringLiteral(element)) continue;
|
|
3040
|
+
const key = t28.isValidIdentifier(element.value) ? t28.identifier(element.value) : t28.stringLiteral(element.value);
|
|
3041
|
+
const prop = t28.tsPropertySignature(key, t28.tsTypeAnnotation(t28.tsAnyKeyword()));
|
|
2939
3042
|
prop.optional = true;
|
|
2940
3043
|
members.push(prop);
|
|
2941
3044
|
}
|
|
2942
3045
|
if (members.length) {
|
|
2943
|
-
propsTypes.push(
|
|
3046
|
+
propsTypes.push(t28.tsTypeLiteral(members));
|
|
2944
3047
|
}
|
|
2945
3048
|
return;
|
|
2946
3049
|
}
|
|
2947
|
-
if (
|
|
3050
|
+
if (t28.isObjectExpression(runtimeArg)) {
|
|
2948
3051
|
for (const prop of runtimeArg.properties) {
|
|
2949
|
-
if (!
|
|
2950
|
-
if (
|
|
3052
|
+
if (!t28.isObjectProperty(prop)) continue;
|
|
3053
|
+
if (t28.isSpreadElement(prop)) continue;
|
|
2951
3054
|
const key = prop.key;
|
|
2952
3055
|
let propName = null;
|
|
2953
|
-
if (
|
|
2954
|
-
if (
|
|
2955
|
-
if (
|
|
3056
|
+
if (t28.isIdentifier(key)) propName = key.name;
|
|
3057
|
+
if (t28.isStringLiteral(key)) propName = key.value;
|
|
3058
|
+
if (t28.isNumericLiteral(key)) propName = String(key.value);
|
|
2956
3059
|
if (!propName) continue;
|
|
2957
3060
|
const { type, required } = resolveRuntimePropMeta(prop.value);
|
|
2958
|
-
const tsType = type ??
|
|
2959
|
-
const tsKey =
|
|
2960
|
-
const tsProp =
|
|
3061
|
+
const tsType = type ?? t28.tsAnyKeyword();
|
|
3062
|
+
const tsKey = t28.isValidIdentifier(propName) ? t28.identifier(propName) : t28.stringLiteral(propName);
|
|
3063
|
+
const tsProp = t28.tsPropertySignature(tsKey, t28.tsTypeAnnotation(tsType));
|
|
2961
3064
|
tsProp.optional = !required;
|
|
2962
3065
|
members.push(tsProp);
|
|
2963
3066
|
}
|
|
2964
3067
|
if (members.length) {
|
|
2965
|
-
propsTypes.push(
|
|
3068
|
+
propsTypes.push(t28.tsTypeLiteral(members));
|
|
2966
3069
|
}
|
|
2967
3070
|
return;
|
|
2968
3071
|
}
|
|
@@ -2976,42 +3079,42 @@ function processInferredTypes2(ctx, runtimeArg) {
|
|
|
2976
3079
|
);
|
|
2977
3080
|
}
|
|
2978
3081
|
function resolveRuntimePropMeta(value) {
|
|
2979
|
-
if (
|
|
3082
|
+
if (t28.isIdentifier(value)) {
|
|
2980
3083
|
return {
|
|
2981
3084
|
type: mapRuntimeTypeToTSType(value),
|
|
2982
3085
|
required: false
|
|
2983
3086
|
};
|
|
2984
3087
|
}
|
|
2985
|
-
if (
|
|
3088
|
+
if (t28.isArrayExpression(value)) {
|
|
2986
3089
|
return {
|
|
2987
3090
|
type: resolveRuntimeUnionType(value),
|
|
2988
3091
|
required: false
|
|
2989
3092
|
};
|
|
2990
3093
|
}
|
|
2991
|
-
if (!
|
|
3094
|
+
if (!t28.isObjectExpression(value)) {
|
|
2992
3095
|
return { required: false };
|
|
2993
3096
|
}
|
|
2994
3097
|
let type;
|
|
2995
3098
|
let required = false;
|
|
2996
3099
|
for (const prop of value.properties) {
|
|
2997
|
-
if (!
|
|
2998
|
-
if (
|
|
3100
|
+
if (!t28.isObjectProperty(prop)) continue;
|
|
3101
|
+
if (t28.isSpreadElement(prop)) continue;
|
|
2999
3102
|
const key = prop.key;
|
|
3000
|
-
const propName =
|
|
3103
|
+
const propName = t28.isIdentifier(key) ? key.name : t28.isStringLiteral(key) ? key.value : null;
|
|
3001
3104
|
if (!propName) continue;
|
|
3002
3105
|
if (propName === "type") {
|
|
3003
3106
|
const valueNode = prop.value;
|
|
3004
|
-
if (
|
|
3107
|
+
if (t28.isArrayExpression(valueNode)) {
|
|
3005
3108
|
type = resolveRuntimeUnionType(valueNode);
|
|
3006
3109
|
continue;
|
|
3007
3110
|
}
|
|
3008
|
-
if (
|
|
3111
|
+
if (t28.isIdentifier(valueNode)) {
|
|
3009
3112
|
type = mapRuntimeTypeToTSType(valueNode);
|
|
3010
3113
|
continue;
|
|
3011
3114
|
}
|
|
3012
3115
|
}
|
|
3013
3116
|
if (propName === "required") {
|
|
3014
|
-
if (
|
|
3117
|
+
if (t28.isBooleanLiteral(prop.value)) {
|
|
3015
3118
|
required = prop.value.value;
|
|
3016
3119
|
}
|
|
3017
3120
|
}
|
|
@@ -3021,58 +3124,58 @@ function resolveRuntimePropMeta(value) {
|
|
|
3021
3124
|
function resolveRuntimeUnionType(value) {
|
|
3022
3125
|
const types = [];
|
|
3023
3126
|
for (const element of value.elements) {
|
|
3024
|
-
if (!element || !
|
|
3127
|
+
if (!element || !t28.isIdentifier(element)) continue;
|
|
3025
3128
|
const resolved = mapRuntimeTypeToTSType(element);
|
|
3026
3129
|
if (resolved) types.push(resolved);
|
|
3027
3130
|
}
|
|
3028
|
-
if (!types.length) return
|
|
3131
|
+
if (!types.length) return t28.tsAnyKeyword();
|
|
3029
3132
|
if (types.length === 1) return types[0];
|
|
3030
|
-
return
|
|
3133
|
+
return t28.tsUnionType(types);
|
|
3031
3134
|
}
|
|
3032
3135
|
function mapRuntimeTypeToTSType(value) {
|
|
3033
3136
|
switch (value.name) {
|
|
3034
3137
|
case "String":
|
|
3035
|
-
return
|
|
3138
|
+
return t28.tsStringKeyword();
|
|
3036
3139
|
case "Number":
|
|
3037
|
-
return
|
|
3140
|
+
return t28.tsNumberKeyword();
|
|
3038
3141
|
case "Boolean":
|
|
3039
|
-
return
|
|
3142
|
+
return t28.tsBooleanKeyword();
|
|
3040
3143
|
case "Object":
|
|
3041
|
-
return
|
|
3144
|
+
return t28.tsTypeLiteral([]);
|
|
3042
3145
|
case "Array":
|
|
3043
|
-
return
|
|
3146
|
+
return t28.tsArrayType(t28.tsAnyKeyword());
|
|
3044
3147
|
case "Function":
|
|
3045
|
-
return
|
|
3148
|
+
return t28.tsFunctionType(null, [], t28.tsTypeAnnotation(t28.tsAnyKeyword()));
|
|
3046
3149
|
case "Symbol":
|
|
3047
|
-
return
|
|
3150
|
+
return t28.tsSymbolKeyword();
|
|
3048
3151
|
case "BigInt":
|
|
3049
|
-
return
|
|
3152
|
+
return t28.tsBigIntKeyword();
|
|
3050
3153
|
default:
|
|
3051
|
-
return
|
|
3154
|
+
return t28.tsAnyKeyword();
|
|
3052
3155
|
}
|
|
3053
3156
|
}
|
|
3054
3157
|
|
|
3055
3158
|
// src/core/transform/sfc/script/syntax-processor/preprocess/resolve-props-interface/resolve-slot.ts
|
|
3056
|
-
import * as
|
|
3159
|
+
import * as t29 from "@babel/types";
|
|
3057
3160
|
var SLOT_DEFAULT_NAME = "default";
|
|
3058
3161
|
var SLOT_CHILDREN_NAME = "children";
|
|
3059
3162
|
var SLOT_FN_PARAM_NAME = "props";
|
|
3060
3163
|
function resolveSlotsTopLevelTypes(ctx) {
|
|
3061
3164
|
return {
|
|
3062
|
-
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(
|
|
3063
|
-
if (!
|
|
3064
|
-
const { node } =
|
|
3065
|
-
if (
|
|
3066
|
-
const typeLiteral =
|
|
3165
|
+
"TSInterfaceDeclaration|TSTypeAliasDeclaration"(path8) {
|
|
3166
|
+
if (!t29.isProgram(path8.parent)) return;
|
|
3167
|
+
const { node } = path8;
|
|
3168
|
+
if (t29.isTSInterfaceDeclaration(node)) {
|
|
3169
|
+
const typeLiteral = t29.tsTypeLiteral(node.body.body);
|
|
3067
3170
|
if (!hasSlotsSignatureInType(typeLiteral)) return;
|
|
3068
3171
|
const resolved = resolveSlotType(typeLiteral);
|
|
3069
|
-
if (resolved &&
|
|
3172
|
+
if (resolved && t29.isTSTypeLiteral(resolved)) {
|
|
3070
3173
|
node.body.body = resolved.members;
|
|
3071
3174
|
recordReactNode(ctx);
|
|
3072
3175
|
}
|
|
3073
3176
|
return;
|
|
3074
3177
|
}
|
|
3075
|
-
if (
|
|
3178
|
+
if (t29.isTSTypeAliasDeclaration(node)) {
|
|
3076
3179
|
if (!hasSlotsSignatureInType(node.typeAnnotation)) return;
|
|
3077
3180
|
const resolved = resolveSlotType(node.typeAnnotation);
|
|
3078
3181
|
if (resolved) {
|
|
@@ -3083,8 +3186,8 @@ function resolveSlotsTopLevelTypes(ctx) {
|
|
|
3083
3186
|
}
|
|
3084
3187
|
};
|
|
3085
3188
|
}
|
|
3086
|
-
function resolveDefineSlotsIface(
|
|
3087
|
-
const { node } =
|
|
3189
|
+
function resolveDefineSlotsIface(path8, ctx) {
|
|
3190
|
+
const { node } = path8;
|
|
3088
3191
|
const tsParams = node.typeParameters?.params;
|
|
3089
3192
|
if (!tsParams?.length) return;
|
|
3090
3193
|
const {
|
|
@@ -3117,7 +3220,7 @@ function resolveTemplateSlotIface(ctx) {
|
|
|
3117
3220
|
}
|
|
3118
3221
|
if (tsMembers.length) {
|
|
3119
3222
|
recordReactNode(ctx);
|
|
3120
|
-
slotTypes.push(
|
|
3223
|
+
slotTypes.push(t29.tsTypeLiteral(tsMembers));
|
|
3121
3224
|
}
|
|
3122
3225
|
}
|
|
3123
3226
|
function recordReactNode(ctx) {
|
|
@@ -3127,32 +3230,32 @@ function recordReactNode(ctx) {
|
|
|
3127
3230
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.ReactNode);
|
|
3128
3231
|
}
|
|
3129
3232
|
function resolveSlotType(tsType) {
|
|
3130
|
-
if (
|
|
3233
|
+
if (t29.isTSParenthesizedType(tsType)) {
|
|
3131
3234
|
return resolveSlotType(tsType.typeAnnotation);
|
|
3132
3235
|
}
|
|
3133
|
-
if (
|
|
3236
|
+
if (t29.isTSTypeReference(tsType)) {
|
|
3134
3237
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
3135
3238
|
return tsType;
|
|
3136
3239
|
}
|
|
3137
3240
|
const params = tsType.typeParameters.params.map((param) => resolveSlotType(param)).filter(Boolean);
|
|
3138
|
-
return
|
|
3241
|
+
return t29.tsTypeReference(
|
|
3139
3242
|
tsType.typeName,
|
|
3140
|
-
|
|
3243
|
+
t29.tsTypeParameterInstantiation(params.length ? params : tsType.typeParameters.params)
|
|
3141
3244
|
);
|
|
3142
3245
|
}
|
|
3143
|
-
if (
|
|
3246
|
+
if (t29.isTSIntersectionType(tsType)) {
|
|
3144
3247
|
const types = tsType.types.map(resolveSlotType).filter(Boolean);
|
|
3145
3248
|
if (!types.length) return null;
|
|
3146
3249
|
if (types.length === 1) return types[0];
|
|
3147
|
-
return
|
|
3250
|
+
return t29.tsIntersectionType(types);
|
|
3148
3251
|
}
|
|
3149
|
-
if (
|
|
3252
|
+
if (t29.isTSUnionType(tsType)) {
|
|
3150
3253
|
const types = tsType.types.map(resolveSlotType).filter(Boolean);
|
|
3151
3254
|
if (!types.length) return null;
|
|
3152
3255
|
if (types.length === 1) return types[0];
|
|
3153
|
-
return
|
|
3256
|
+
return t29.tsUnionType(types);
|
|
3154
3257
|
}
|
|
3155
|
-
if (
|
|
3258
|
+
if (t29.isTSTypeLiteral(tsType)) {
|
|
3156
3259
|
const members = [];
|
|
3157
3260
|
for (const member of tsType.members) {
|
|
3158
3261
|
const resolved = resolveSlotPropFromMember(member);
|
|
@@ -3163,56 +3266,56 @@ function resolveSlotType(tsType) {
|
|
|
3163
3266
|
members.push(member);
|
|
3164
3267
|
}
|
|
3165
3268
|
if (!members.length) return null;
|
|
3166
|
-
return
|
|
3269
|
+
return t29.tsTypeLiteral(members);
|
|
3167
3270
|
}
|
|
3168
|
-
if (
|
|
3271
|
+
if (t29.isTSFunctionType(tsType)) {
|
|
3169
3272
|
const props = buildSlotPropSignature(
|
|
3170
3273
|
SLOT_DEFAULT_NAME,
|
|
3171
3274
|
cloneCallableParams(tsType.parameters),
|
|
3172
3275
|
false
|
|
3173
3276
|
);
|
|
3174
|
-
return
|
|
3277
|
+
return t29.tsTypeLiteral([props]);
|
|
3175
3278
|
}
|
|
3176
3279
|
return tsType;
|
|
3177
3280
|
}
|
|
3178
3281
|
function hasSlotsSignatureInType(tsType) {
|
|
3179
|
-
if (
|
|
3282
|
+
if (t29.isTSParenthesizedType(tsType)) {
|
|
3180
3283
|
return hasSlotsSignatureInType(tsType.typeAnnotation);
|
|
3181
3284
|
}
|
|
3182
|
-
if (
|
|
3285
|
+
if (t29.isTSTypeReference(tsType)) {
|
|
3183
3286
|
if (!tsType.typeParameters || !tsType.typeParameters.params.length) {
|
|
3184
3287
|
return false;
|
|
3185
3288
|
}
|
|
3186
3289
|
return tsType.typeParameters.params.some(hasSlotsSignatureInType);
|
|
3187
3290
|
}
|
|
3188
|
-
if (
|
|
3291
|
+
if (t29.isTSIntersectionType(tsType) || t29.isTSUnionType(tsType)) {
|
|
3189
3292
|
return tsType.types.some(hasSlotsSignatureInType);
|
|
3190
3293
|
}
|
|
3191
|
-
if (
|
|
3294
|
+
if (t29.isTSTypeLiteral(tsType)) {
|
|
3192
3295
|
return tsType.members.some(hasSlotsSignatureInMember);
|
|
3193
3296
|
}
|
|
3194
|
-
if (
|
|
3297
|
+
if (t29.isTSFunctionType(tsType)) {
|
|
3195
3298
|
return true;
|
|
3196
3299
|
}
|
|
3197
3300
|
return false;
|
|
3198
3301
|
}
|
|
3199
3302
|
function hasSlotsSignatureInMember(member) {
|
|
3200
|
-
if (
|
|
3201
|
-
if (
|
|
3202
|
-
if (
|
|
3303
|
+
if (t29.isTSMethodSignature(member)) return true;
|
|
3304
|
+
if (t29.isTSCallSignatureDeclaration(member)) return true;
|
|
3305
|
+
if (t29.isTSPropertySignature(member)) {
|
|
3203
3306
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
3204
3307
|
return !!(typeAnnotation && resolveCallableType(typeAnnotation));
|
|
3205
3308
|
}
|
|
3206
3309
|
return false;
|
|
3207
3310
|
}
|
|
3208
3311
|
function resolveSlotPropFromMember(member) {
|
|
3209
|
-
if (
|
|
3312
|
+
if (t29.isTSMethodSignature(member)) {
|
|
3210
3313
|
const rawName = resolvePropName2(member.key);
|
|
3211
3314
|
if (!rawName) return null;
|
|
3212
3315
|
const params = cloneCallableParams(member.parameters);
|
|
3213
3316
|
return buildSlotPropSignature(rawName, params, !!member.optional);
|
|
3214
3317
|
}
|
|
3215
|
-
if (
|
|
3318
|
+
if (t29.isTSPropertySignature(member)) {
|
|
3216
3319
|
const rawName = resolvePropName2(member.key);
|
|
3217
3320
|
if (!rawName) return null;
|
|
3218
3321
|
const typeAnnotation = member.typeAnnotation?.typeAnnotation;
|
|
@@ -3221,52 +3324,53 @@ function resolveSlotPropFromMember(member) {
|
|
|
3221
3324
|
const params = cloneCallableParams(callable.parameters);
|
|
3222
3325
|
return buildSlotPropSignature(rawName, params, !!member.optional);
|
|
3223
3326
|
}
|
|
3224
|
-
if (
|
|
3327
|
+
if (t29.isTSCallSignatureDeclaration(member)) {
|
|
3225
3328
|
const params = cloneCallableParams(member.parameters);
|
|
3226
3329
|
return buildSlotPropSignature(SLOT_DEFAULT_NAME, params, true);
|
|
3227
3330
|
}
|
|
3228
3331
|
return null;
|
|
3229
3332
|
}
|
|
3230
3333
|
function resolveCallableType(tsType) {
|
|
3231
|
-
if (
|
|
3232
|
-
if (
|
|
3334
|
+
if (t29.isTSFunctionType(tsType)) return tsType;
|
|
3335
|
+
if (t29.isTSParenthesizedType(tsType)) return resolveCallableType(tsType.typeAnnotation);
|
|
3233
3336
|
return null;
|
|
3234
3337
|
}
|
|
3235
3338
|
function buildSlotPropSignature(rawName, params, optional) {
|
|
3236
3339
|
const propName = rawName === SLOT_DEFAULT_NAME ? SLOT_CHILDREN_NAME : rawName;
|
|
3237
|
-
const key =
|
|
3238
|
-
const reactNodeType =
|
|
3239
|
-
|
|
3340
|
+
const key = t29.isValidIdentifier(propName) ? t29.identifier(propName) : t29.stringLiteral(propName);
|
|
3341
|
+
const reactNodeType = t29.tsTypeAnnotation(
|
|
3342
|
+
t29.tsTypeReference(t29.identifier(REACT_API_MAP.ReactNode))
|
|
3240
3343
|
);
|
|
3241
3344
|
let typeAnnotation;
|
|
3242
3345
|
if (rawName === SLOT_DEFAULT_NAME && params.length === 0 || params.length === 0) {
|
|
3243
3346
|
typeAnnotation = reactNodeType;
|
|
3244
3347
|
} else {
|
|
3245
|
-
const fnType =
|
|
3246
|
-
typeAnnotation =
|
|
3348
|
+
const fnType = t29.tsFunctionType(null, params, reactNodeType);
|
|
3349
|
+
typeAnnotation = t29.tsTypeAnnotation(fnType);
|
|
3247
3350
|
}
|
|
3248
|
-
const prop =
|
|
3351
|
+
const prop = t29.tsPropertySignature(key, typeAnnotation);
|
|
3249
3352
|
prop.optional = optional;
|
|
3250
3353
|
return prop;
|
|
3251
3354
|
}
|
|
3252
3355
|
function createSlotScopeParam(props, ctx) {
|
|
3253
|
-
const paramId =
|
|
3356
|
+
const paramId = t29.identifier(SLOT_FN_PARAM_NAME);
|
|
3254
3357
|
const propsSigns = [];
|
|
3255
3358
|
const { reactiveBindings } = ctx.templateData;
|
|
3256
3359
|
props.forEach(({ prop, tsType }) => {
|
|
3257
3360
|
const foundBindingValue = reactiveBindings[prop]?.value;
|
|
3258
3361
|
const foundBindingTypes = foundBindingValue ? expressionToTSType(foundBindingValue) : null;
|
|
3259
|
-
const typeAnnotation = foundBindingTypes ?
|
|
3260
|
-
const
|
|
3362
|
+
const typeAnnotation = foundBindingTypes ? t29.tsTypeAnnotation(foundBindingTypes) : tsType;
|
|
3363
|
+
const key = t29.isValidIdentifier(prop) ? t29.identifier(prop) : t29.stringLiteral(prop);
|
|
3364
|
+
const propSign = t29.tsPropertySignature(key, typeAnnotation);
|
|
3261
3365
|
propsSigns.push(propSign);
|
|
3262
3366
|
});
|
|
3263
|
-
paramId.typeAnnotation =
|
|
3367
|
+
paramId.typeAnnotation = t29.tsTypeAnnotation(t29.tsTypeLiteral(propsSigns));
|
|
3264
3368
|
return paramId;
|
|
3265
3369
|
}
|
|
3266
3370
|
function resolvePropName2(key) {
|
|
3267
|
-
if (
|
|
3268
|
-
if (
|
|
3269
|
-
if (
|
|
3371
|
+
if (t29.isIdentifier(key)) return key.name;
|
|
3372
|
+
if (t29.isStringLiteral(key)) return key.value;
|
|
3373
|
+
if (t29.isNumericLiteral(key)) return String(key.value);
|
|
3270
3374
|
return null;
|
|
3271
3375
|
}
|
|
3272
3376
|
|
|
@@ -3274,8 +3378,8 @@ function resolvePropName2(key) {
|
|
|
3274
3378
|
function resolvePropsIface(ctx) {
|
|
3275
3379
|
const isTS = ctx.scriptData.lang.startsWith("ts");
|
|
3276
3380
|
return {
|
|
3277
|
-
CallExpression(
|
|
3278
|
-
const { node, parentPath } =
|
|
3381
|
+
CallExpression(path8) {
|
|
3382
|
+
const { node, parentPath } = path8;
|
|
3279
3383
|
const name = node.callee.name;
|
|
3280
3384
|
if (!isCalleeNamed(node, MACRO_API_NAMES.props) && !isCalleeNamed(node, MACRO_API_NAMES.emits) && !isCalleeNamed(node, MACRO_API_NAMES.slots)) {
|
|
3281
3385
|
return;
|
|
@@ -3284,7 +3388,7 @@ function resolvePropsIface(ctx) {
|
|
|
3284
3388
|
if (parentPath.isVariableDeclaration() || parentPath.isVariableDeclarator()) {
|
|
3285
3389
|
parentPath.remove();
|
|
3286
3390
|
} else {
|
|
3287
|
-
|
|
3391
|
+
path8.remove();
|
|
3288
3392
|
}
|
|
3289
3393
|
};
|
|
3290
3394
|
if (ctx.inputType !== "sfc") {
|
|
@@ -3298,11 +3402,11 @@ function resolvePropsIface(ctx) {
|
|
|
3298
3402
|
propsTSIface.hasPropsInJsEnv = true;
|
|
3299
3403
|
} else {
|
|
3300
3404
|
if (name === MACRO_API_NAMES.props) {
|
|
3301
|
-
resolveDefinePropsIface(
|
|
3405
|
+
resolveDefinePropsIface(path8, ctx);
|
|
3302
3406
|
} else if (name === MACRO_API_NAMES.emits) {
|
|
3303
|
-
resolveDefineEmitsIface(
|
|
3407
|
+
resolveDefineEmitsIface(path8, ctx);
|
|
3304
3408
|
} else if (name === MACRO_API_NAMES.slots) {
|
|
3305
|
-
resolveDefineSlotsIface(
|
|
3409
|
+
resolveDefineSlotsIface(path8, ctx);
|
|
3306
3410
|
}
|
|
3307
3411
|
}
|
|
3308
3412
|
removePath();
|
|
@@ -3318,9 +3422,9 @@ function resolveCompIProps(ctx, ast) {
|
|
|
3318
3422
|
}
|
|
3319
3423
|
const n = ctx.compName || "Comp";
|
|
3320
3424
|
const ns = `I${camelCase(capitalize(n))}Props`;
|
|
3321
|
-
const typeNode =
|
|
3322
|
-
const typeAliasDecl =
|
|
3323
|
-
const exportDecl =
|
|
3425
|
+
const typeNode = t30.tsIntersectionType(tsTypes);
|
|
3426
|
+
const typeAliasDecl = t30.tsTypeAliasDeclaration(t30.identifier(ns), null, typeNode);
|
|
3427
|
+
const exportDecl = t30.exportNamedDeclaration(typeAliasDecl);
|
|
3324
3428
|
propsTSIface.name = ns;
|
|
3325
3429
|
const scriptIR = getScriptIR(ctx);
|
|
3326
3430
|
scriptIR.exports.push(exportDecl);
|
|
@@ -3328,17 +3432,17 @@ function resolveCompIProps(ctx, ast) {
|
|
|
3328
3432
|
}
|
|
3329
3433
|
|
|
3330
3434
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
3331
|
-
import * as
|
|
3435
|
+
import * as t32 from "@babel/types";
|
|
3332
3436
|
|
|
3333
3437
|
// src/core/transform/sfc/script/shared/dependency-analyzer.ts
|
|
3334
3438
|
import { traverse as traverse2 } from "@babel/core";
|
|
3335
|
-
import * as
|
|
3439
|
+
import * as t31 from "@babel/types";
|
|
3336
3440
|
var TRACE_MAX_DEPTH = 20;
|
|
3337
3441
|
function analyzeDeps(node, ctx, parentPath) {
|
|
3338
3442
|
if (!parentPath) {
|
|
3339
|
-
return
|
|
3443
|
+
return t31.arrayExpression([]);
|
|
3340
3444
|
}
|
|
3341
|
-
const isFnExpr =
|
|
3445
|
+
const isFnExpr = t31.isArrowFunctionExpression(node) || t31.isFunctionExpression(node);
|
|
3342
3446
|
const analyzeTarget = isFnExpr ? node.body : node;
|
|
3343
3447
|
const bindingLocalBoundary = isFnExpr ? node : analyzeTarget;
|
|
3344
3448
|
const reactiveStateApis = getReactiveStateApis();
|
|
@@ -3351,11 +3455,11 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3351
3455
|
analyzeTarget,
|
|
3352
3456
|
{
|
|
3353
3457
|
"MemberExpression|OptionalMemberExpression"(memberPath) {
|
|
3354
|
-
const
|
|
3355
|
-
if (isNestedMemberObject(
|
|
3356
|
-
const rootId = findRootIdentifier(
|
|
3458
|
+
const path8 = memberPath;
|
|
3459
|
+
if (isNestedMemberObject(path8)) return;
|
|
3460
|
+
const rootId = findRootIdentifier(path8.node);
|
|
3357
3461
|
if (!rootId) return;
|
|
3358
|
-
tryAddDependency(
|
|
3462
|
+
tryAddDependency(path8, rootId.name, path8.scope);
|
|
3359
3463
|
processedIdentifiers.add(rootId);
|
|
3360
3464
|
},
|
|
3361
3465
|
Identifier(idPath) {
|
|
@@ -3390,40 +3494,41 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3390
3494
|
addDependency(sourcedExpression);
|
|
3391
3495
|
}
|
|
3392
3496
|
}
|
|
3393
|
-
function normalizeDependencyExpr(
|
|
3394
|
-
if (
|
|
3395
|
-
return
|
|
3497
|
+
function normalizeDependencyExpr(path8, rootName) {
|
|
3498
|
+
if (t31.isIdentifier(path8.node)) {
|
|
3499
|
+
return t31.identifier(path8.node.name);
|
|
3396
3500
|
}
|
|
3397
|
-
if (
|
|
3398
|
-
const normalizedExp = normalizeMemberForCallSite(
|
|
3399
|
-
|
|
3400
|
-
|
|
3501
|
+
if (t31.isMemberExpression(path8.node) || t31.isOptionalMemberExpression(path8.node)) {
|
|
3502
|
+
const normalizedExp = normalizeMemberForCallSite(path8, path8.node);
|
|
3503
|
+
const safeExp = t31.isMemberExpression(normalizedExp) || t31.isOptionalMemberExpression(normalizedExp) ? ensureOptionalForRefValue(normalizedExp) : normalizedExp;
|
|
3504
|
+
if (isReactValidDependencyExpr(safeExp)) {
|
|
3505
|
+
return t31.cloneNode(safeExp, true);
|
|
3401
3506
|
}
|
|
3402
|
-
return
|
|
3507
|
+
return t31.identifier(rootName);
|
|
3403
3508
|
}
|
|
3404
3509
|
return null;
|
|
3405
3510
|
}
|
|
3406
3511
|
function isReactValidDependencyExpr(node2) {
|
|
3407
|
-
if (
|
|
3512
|
+
if (t31.isIdentifier(node2)) {
|
|
3408
3513
|
return true;
|
|
3409
3514
|
}
|
|
3410
|
-
if (
|
|
3515
|
+
if (t31.isMemberExpression(node2) || t31.isOptionalMemberExpression(node2)) {
|
|
3411
3516
|
return isStaticMemberChain(node2);
|
|
3412
3517
|
}
|
|
3413
3518
|
return false;
|
|
3414
3519
|
}
|
|
3415
3520
|
function isStaticMemberChain(node2) {
|
|
3416
3521
|
let current = node2;
|
|
3417
|
-
while (
|
|
3418
|
-
if (!current.computed && !
|
|
3522
|
+
while (t31.isMemberExpression(current) || t31.isOptionalMemberExpression(current)) {
|
|
3523
|
+
if (!current.computed && !t31.isIdentifier(current.property)) {
|
|
3419
3524
|
return false;
|
|
3420
3525
|
}
|
|
3421
|
-
if (current.computed && !
|
|
3526
|
+
if (current.computed && !t31.isStringLiteral(current.property) && !t31.isNumericLiteral(current.property)) {
|
|
3422
3527
|
return false;
|
|
3423
3528
|
}
|
|
3424
3529
|
current = current.object;
|
|
3425
3530
|
}
|
|
3426
|
-
return
|
|
3531
|
+
return t31.isIdentifier(current);
|
|
3427
3532
|
}
|
|
3428
3533
|
function isBindingDeclaredInsideBoundary(binding, boundary) {
|
|
3429
3534
|
let current = binding.path;
|
|
@@ -3435,17 +3540,42 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3435
3540
|
}
|
|
3436
3541
|
return false;
|
|
3437
3542
|
}
|
|
3438
|
-
function normalizeMemberForCallSite(
|
|
3439
|
-
const parent =
|
|
3543
|
+
function normalizeMemberForCallSite(path8, node2) {
|
|
3544
|
+
const parent = path8.parentPath;
|
|
3440
3545
|
const isDirectCallee = !!parent && (parent.isCallExpression() && parent.node.callee === node2 || parent.isOptionalCallExpression() && parent.node.callee === node2);
|
|
3441
3546
|
if (!isDirectCallee) {
|
|
3442
3547
|
return node2;
|
|
3443
3548
|
}
|
|
3444
|
-
if (!
|
|
3549
|
+
if (!t31.isExpression(node2.object)) {
|
|
3445
3550
|
return node2;
|
|
3446
3551
|
}
|
|
3447
3552
|
return node2.object;
|
|
3448
3553
|
}
|
|
3554
|
+
function ensureOptionalForRefValue(node2) {
|
|
3555
|
+
if (!hasRefValueAccess(node2)) {
|
|
3556
|
+
return node2;
|
|
3557
|
+
}
|
|
3558
|
+
if (t31.isOptionalMemberExpression(node2) && node2.optional) {
|
|
3559
|
+
return node2;
|
|
3560
|
+
}
|
|
3561
|
+
const object = t31.cloneNode(node2.object, true);
|
|
3562
|
+
const property = t31.cloneNode(node2.property, true);
|
|
3563
|
+
return t31.optionalMemberExpression(object, property, node2.computed, true);
|
|
3564
|
+
}
|
|
3565
|
+
function hasRefValueAccess(node2) {
|
|
3566
|
+
let current = node2;
|
|
3567
|
+
while (t31.isMemberExpression(current) || t31.isOptionalMemberExpression(current)) {
|
|
3568
|
+
if (current.computed) {
|
|
3569
|
+
if (t31.isStringLiteral(current.property) && current.property.value === "value") {
|
|
3570
|
+
return true;
|
|
3571
|
+
}
|
|
3572
|
+
} else if (t31.isIdentifier(current.property) && current.property.name === "value") {
|
|
3573
|
+
return true;
|
|
3574
|
+
}
|
|
3575
|
+
current = current.object;
|
|
3576
|
+
}
|
|
3577
|
+
return false;
|
|
3578
|
+
}
|
|
3449
3579
|
function isEligibleBindingSource(binding) {
|
|
3450
3580
|
if (binding.kind === "param") {
|
|
3451
3581
|
return false;
|
|
@@ -3455,19 +3585,19 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3455
3585
|
const isImportBinding = bindingPath.isImportSpecifier() || bindingPath.isImportDefaultSpecifier() || bindingPath.isImportNamespaceSpecifier();
|
|
3456
3586
|
const isReactiveVarBinding = !!declaratorPath && isReactiveBinding(declaratorPath.node);
|
|
3457
3587
|
const nodeInit = declaratorPath?.node.init;
|
|
3458
|
-
const isReactiveApiCallVarBinding = !!declaratorPath &&
|
|
3459
|
-
const isHookCallVarBinding = !!declaratorPath &&
|
|
3460
|
-
const isFunctionBinding = bindingPath.isFunctionDeclaration() || !!declaratorPath && !!nodeInit && (
|
|
3461
|
-
if (declaratorPath && nodeInit && (
|
|
3588
|
+
const isReactiveApiCallVarBinding = !!declaratorPath && t31.isCallExpression(nodeInit) && t31.isIdentifier(nodeInit.callee) && reactiveStateApis.has(nodeInit.callee.name);
|
|
3589
|
+
const isHookCallVarBinding = !!declaratorPath && t31.isCallExpression(nodeInit) && isHookLikeCallee(nodeInit.callee);
|
|
3590
|
+
const isFunctionBinding = bindingPath.isFunctionDeclaration() || !!declaratorPath && !!nodeInit && (t31.isArrowFunctionExpression(nodeInit) || t31.isFunctionExpression(nodeInit));
|
|
3591
|
+
if (declaratorPath && nodeInit && (t31.isArrowFunctionExpression(nodeInit) || t31.isFunctionExpression(nodeInit))) {
|
|
3462
3592
|
markAsAnalyzed(nodeInit, false);
|
|
3463
3593
|
}
|
|
3464
3594
|
return isImportBinding || isReactiveVarBinding || isReactiveApiCallVarBinding || isHookCallVarBinding || isFunctionBinding;
|
|
3465
3595
|
}
|
|
3466
3596
|
function isHookLikeCallee(callee) {
|
|
3467
|
-
if (
|
|
3597
|
+
if (t31.isIdentifier(callee)) {
|
|
3468
3598
|
return callee.name.startsWith("use");
|
|
3469
3599
|
}
|
|
3470
|
-
if (
|
|
3600
|
+
if (t31.isMemberExpression(callee) && !callee.computed && t31.isIdentifier(callee.property)) {
|
|
3471
3601
|
return callee.property.name.startsWith("use");
|
|
3472
3602
|
}
|
|
3473
3603
|
return false;
|
|
@@ -3483,7 +3613,7 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3483
3613
|
}
|
|
3484
3614
|
function isExpressionSourcedFromEligibleBinding(exp, scope, seen, depth) {
|
|
3485
3615
|
if (depth <= 0) return null;
|
|
3486
|
-
if (
|
|
3616
|
+
if (t31.isIdentifier(exp)) {
|
|
3487
3617
|
const sourceBinding = scope.getBinding(exp.name);
|
|
3488
3618
|
if (!sourceBinding) return null;
|
|
3489
3619
|
if (isEligibleBindingSource(sourceBinding)) {
|
|
@@ -3491,13 +3621,13 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3491
3621
|
}
|
|
3492
3622
|
return traceBindingSource(sourceBinding, seen, depth - 1);
|
|
3493
3623
|
}
|
|
3494
|
-
if (
|
|
3624
|
+
if (t31.isMemberExpression(exp) || t31.isOptionalMemberExpression(exp)) {
|
|
3495
3625
|
const root = findRootIdentifier(exp);
|
|
3496
3626
|
if (!root) return null;
|
|
3497
3627
|
const sourceBinding = scope.getBinding(root.name);
|
|
3498
3628
|
if (!sourceBinding) return null;
|
|
3499
3629
|
if (isEligibleBindingSource(sourceBinding)) {
|
|
3500
|
-
return
|
|
3630
|
+
return t31.cloneNode(exp);
|
|
3501
3631
|
}
|
|
3502
3632
|
const sourcedRoot = traceBindingSource(sourceBinding, seen, depth - 1);
|
|
3503
3633
|
if (sourcedRoot) {
|
|
@@ -3505,17 +3635,17 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3505
3635
|
if (rebuilt) {
|
|
3506
3636
|
return rebuilt;
|
|
3507
3637
|
}
|
|
3508
|
-
return
|
|
3638
|
+
return t31.cloneNode(sourcedRoot, true);
|
|
3509
3639
|
}
|
|
3510
3640
|
}
|
|
3511
3641
|
return null;
|
|
3512
3642
|
}
|
|
3513
3643
|
function rebuildMemberWithNewRoot(node2, nextRoot) {
|
|
3514
3644
|
const replacedObject = (() => {
|
|
3515
|
-
if (
|
|
3516
|
-
return
|
|
3645
|
+
if (t31.isIdentifier(node2.object)) {
|
|
3646
|
+
return t31.cloneNode(nextRoot, true);
|
|
3517
3647
|
}
|
|
3518
|
-
if (
|
|
3648
|
+
if (t31.isMemberExpression(node2.object) || t31.isOptionalMemberExpression(node2.object)) {
|
|
3519
3649
|
return rebuildMemberWithNewRoot(node2.object, nextRoot);
|
|
3520
3650
|
}
|
|
3521
3651
|
return null;
|
|
@@ -3523,45 +3653,45 @@ function analyzeDeps(node, ctx, parentPath) {
|
|
|
3523
3653
|
if (!replacedObject) {
|
|
3524
3654
|
return null;
|
|
3525
3655
|
}
|
|
3526
|
-
const property =
|
|
3527
|
-
if (
|
|
3528
|
-
return
|
|
3656
|
+
const property = t31.cloneNode(node2.property, true);
|
|
3657
|
+
if (t31.isMemberExpression(node2)) {
|
|
3658
|
+
return t31.memberExpression(
|
|
3529
3659
|
replacedObject,
|
|
3530
3660
|
property,
|
|
3531
3661
|
node2.computed
|
|
3532
3662
|
);
|
|
3533
3663
|
}
|
|
3534
|
-
return
|
|
3664
|
+
return t31.optionalMemberExpression(
|
|
3535
3665
|
replacedObject,
|
|
3536
3666
|
property,
|
|
3537
3667
|
node2.computed,
|
|
3538
3668
|
node2.optional
|
|
3539
3669
|
);
|
|
3540
3670
|
}
|
|
3541
|
-
return
|
|
3671
|
+
return t31.arrayExpression(Array.from(deps.values()));
|
|
3542
3672
|
}
|
|
3543
3673
|
function getDependencyKey(exp) {
|
|
3544
|
-
if (
|
|
3674
|
+
if (t31.isIdentifier(exp)) {
|
|
3545
3675
|
return exp.name;
|
|
3546
3676
|
}
|
|
3547
|
-
if (
|
|
3677
|
+
if (t31.isMemberExpression(exp) || t31.isOptionalMemberExpression(exp)) {
|
|
3548
3678
|
const objectKey = getDependencyKey(exp.object);
|
|
3549
3679
|
const opt = exp.optional ? "?" : "";
|
|
3550
|
-
if (!exp.computed &&
|
|
3680
|
+
if (!exp.computed && t31.isIdentifier(exp.property)) {
|
|
3551
3681
|
return `${objectKey}${opt}.${exp.property.name}`;
|
|
3552
3682
|
}
|
|
3553
|
-
if (
|
|
3683
|
+
if (t31.isStringLiteral(exp.property) || t31.isNumericLiteral(exp.property)) {
|
|
3554
3684
|
return `${objectKey}${opt}[${JSON.stringify(exp.property.value)}]`;
|
|
3555
3685
|
}
|
|
3556
3686
|
return `${objectKey}${opt}[*]`;
|
|
3557
3687
|
}
|
|
3558
3688
|
return exp.type;
|
|
3559
3689
|
}
|
|
3560
|
-
function isNestedMemberObject(
|
|
3561
|
-
const parent =
|
|
3690
|
+
function isNestedMemberObject(path8) {
|
|
3691
|
+
const parent = path8.parentPath;
|
|
3562
3692
|
if (!parent) return false;
|
|
3563
3693
|
if (parent.isMemberExpression() || parent.isOptionalMemberExpression()) {
|
|
3564
|
-
return parent.node.object ===
|
|
3694
|
+
return parent.node.object === path8.node;
|
|
3565
3695
|
}
|
|
3566
3696
|
return false;
|
|
3567
3697
|
}
|
|
@@ -3581,15 +3711,15 @@ function getIsAnalyzed(node) {
|
|
|
3581
3711
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-analysis-only-adapter.ts
|
|
3582
3712
|
function resolveAnalysisOnlyAdapter(ctx) {
|
|
3583
3713
|
return {
|
|
3584
|
-
"CallExpression|Identifier"(
|
|
3585
|
-
const node =
|
|
3714
|
+
"CallExpression|Identifier"(path8) {
|
|
3715
|
+
const node = path8.node;
|
|
3586
3716
|
const apiName = getApiName(node);
|
|
3587
3717
|
const adapter = ADAPTER_RULES.runtime[apiName];
|
|
3588
3718
|
if (!adapter || adapter.type !== "analyzed-deps") {
|
|
3589
3719
|
return;
|
|
3590
3720
|
}
|
|
3591
|
-
if (
|
|
3592
|
-
resolveCallNode(
|
|
3721
|
+
if (t32.isCallExpression(node)) {
|
|
3722
|
+
resolveCallNode(path8, adapter, ctx);
|
|
3593
3723
|
} else {
|
|
3594
3724
|
replaceIdName(node, adapter.target);
|
|
3595
3725
|
}
|
|
@@ -3598,24 +3728,24 @@ function resolveAnalysisOnlyAdapter(ctx) {
|
|
|
3598
3728
|
};
|
|
3599
3729
|
}
|
|
3600
3730
|
function getApiName(node) {
|
|
3601
|
-
const isCallNode =
|
|
3731
|
+
const isCallNode = t32.isCallExpression(node);
|
|
3602
3732
|
let apiName = "";
|
|
3603
|
-
if (
|
|
3733
|
+
if (t32.isIdentifier(node)) {
|
|
3604
3734
|
apiName = node.name;
|
|
3605
|
-
} else if (isCallNode &&
|
|
3735
|
+
} else if (isCallNode && t32.isIdentifier(node.callee)) {
|
|
3606
3736
|
apiName = node.callee.name;
|
|
3607
3737
|
}
|
|
3608
3738
|
return apiName;
|
|
3609
3739
|
}
|
|
3610
|
-
function resolveCallNode(
|
|
3611
|
-
const { node } =
|
|
3740
|
+
function resolveCallNode(path8, adapter, ctx) {
|
|
3741
|
+
const { node } = path8;
|
|
3612
3742
|
const { arguments: args } = node;
|
|
3613
3743
|
if (!args.length) return;
|
|
3614
3744
|
const fn = args[0];
|
|
3615
|
-
if (!
|
|
3745
|
+
if (!t32.isArrowFunctionExpression(fn) && !t32.isFunctionExpression(fn)) {
|
|
3616
3746
|
return;
|
|
3617
3747
|
}
|
|
3618
|
-
const fnPath =
|
|
3748
|
+
const fnPath = path8.get("arguments")[0];
|
|
3619
3749
|
const deps = analyzeDeps(fn, ctx, fnPath);
|
|
3620
3750
|
args.push(deps);
|
|
3621
3751
|
replaceCallName(node, adapter.target);
|
|
@@ -3625,41 +3755,41 @@ function resolveCallNode(path7, adapter, ctx) {
|
|
|
3625
3755
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-arrow-deps.ts
|
|
3626
3756
|
function resolveArrowFnDeps(ctx, ast) {
|
|
3627
3757
|
return {
|
|
3628
|
-
ArrowFunctionExpression(
|
|
3629
|
-
const { node, parentPath } =
|
|
3630
|
-
if (isSkip(
|
|
3758
|
+
ArrowFunctionExpression(path8) {
|
|
3759
|
+
const { node, parentPath } = path8;
|
|
3760
|
+
if (isSkip(path8) || !atComponentOrHookRoot(parentPath, ast.program)) {
|
|
3631
3761
|
return;
|
|
3632
3762
|
}
|
|
3633
|
-
const deps = analyzeDeps(node, ctx,
|
|
3763
|
+
const deps = analyzeDeps(node, ctx, path8);
|
|
3634
3764
|
if (!deps.elements.length) return;
|
|
3635
3765
|
const newNode = createUseCallback(node, deps);
|
|
3636
|
-
const declaratorPath = getVariableDeclaratorPath(
|
|
3766
|
+
const declaratorPath = getVariableDeclaratorPath(path8);
|
|
3637
3767
|
markAsAnalyzed(newNode.arguments[0]);
|
|
3638
3768
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useCallback);
|
|
3639
3769
|
setScriptNodeMeta(declaratorPath?.node, { is_reactive: true, reactive_type: "indirect" });
|
|
3640
|
-
|
|
3770
|
+
path8.replaceWith(newNode);
|
|
3641
3771
|
}
|
|
3642
3772
|
};
|
|
3643
3773
|
}
|
|
3644
3774
|
function resolveUnanalyzedArrow(ctx) {
|
|
3645
3775
|
return {
|
|
3646
|
-
ArrowFunctionExpression(
|
|
3647
|
-
const { node } =
|
|
3776
|
+
ArrowFunctionExpression(path8) {
|
|
3777
|
+
const { node } = path8;
|
|
3648
3778
|
const analyzed = getIsAnalyzed(node);
|
|
3649
3779
|
if (typeof analyzed === "undefined" || analyzed) return;
|
|
3650
3780
|
const newNode = createUseCallback(node);
|
|
3651
|
-
const declaratorPath = getVariableDeclaratorPath(
|
|
3781
|
+
const declaratorPath = getVariableDeclaratorPath(path8);
|
|
3652
3782
|
if (declaratorPath?.node) {
|
|
3653
3783
|
setScriptNodeMeta(declaratorPath.node, { is_reactive: true, reactive_type: "indirect" });
|
|
3654
3784
|
}
|
|
3655
3785
|
markAsAnalyzed(newNode.arguments[0]);
|
|
3656
|
-
|
|
3786
|
+
path8.replaceWith(newNode);
|
|
3657
3787
|
}
|
|
3658
3788
|
};
|
|
3659
3789
|
}
|
|
3660
|
-
function isSkip(
|
|
3661
|
-
const { parentPath } =
|
|
3662
|
-
const isVariableDecl = () => getVariableDeclaratorPath(
|
|
3790
|
+
function isSkip(path8) {
|
|
3791
|
+
const { parentPath } = path8;
|
|
3792
|
+
const isVariableDecl = () => getVariableDeclaratorPath(path8) !== null;
|
|
3663
3793
|
const isReturnFunc = () => !isVariableDecl() && parentPath.isReturnStatement();
|
|
3664
3794
|
const isCallback = () => {
|
|
3665
3795
|
if (!parentPath) {
|
|
@@ -3668,12 +3798,12 @@ function isSkip(path7) {
|
|
|
3668
3798
|
if (parentPath.isCallExpression()) {
|
|
3669
3799
|
const callExpressionPath = parentPath;
|
|
3670
3800
|
const args = callExpressionPath.node.arguments;
|
|
3671
|
-
return args.some((arg) => arg ===
|
|
3801
|
+
return args.some((arg) => arg === path8.node);
|
|
3672
3802
|
}
|
|
3673
3803
|
if (parentPath.isArrayExpression()) {
|
|
3674
3804
|
const arrayExpressionPath = parentPath;
|
|
3675
3805
|
const elements = arrayExpressionPath.node.elements;
|
|
3676
|
-
return elements.some((element) => element ===
|
|
3806
|
+
return elements.some((element) => element === path8.node);
|
|
3677
3807
|
}
|
|
3678
3808
|
return false;
|
|
3679
3809
|
};
|
|
@@ -3694,16 +3824,16 @@ function isSkip(path7) {
|
|
|
3694
3824
|
}
|
|
3695
3825
|
|
|
3696
3826
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-element-ref.ts
|
|
3697
|
-
import * as
|
|
3827
|
+
import * as t33 from "@babel/types";
|
|
3698
3828
|
function resolveElementRef(ctx) {
|
|
3699
3829
|
return {
|
|
3700
|
-
CallExpression(
|
|
3830
|
+
CallExpression(path8) {
|
|
3701
3831
|
const {
|
|
3702
3832
|
inputType,
|
|
3703
3833
|
templateData: { refBindings }
|
|
3704
3834
|
} = ctx;
|
|
3705
3835
|
if (inputType !== "sfc") return;
|
|
3706
|
-
const { node } =
|
|
3836
|
+
const { node } = path8;
|
|
3707
3837
|
const isUseTemplateRef = isCalleeNamed(node, VUE_API_MAP.useTemplateRef);
|
|
3708
3838
|
const isCompRefBindings = Object.keys(refBindings.componentRefs).length > 0 && isCalleeNamed(node, VUE_API_MAP.ref);
|
|
3709
3839
|
const shouldProcess = isUseTemplateRef || isCompRefBindings;
|
|
@@ -3711,31 +3841,31 @@ function resolveElementRef(ctx) {
|
|
|
3711
3841
|
return;
|
|
3712
3842
|
}
|
|
3713
3843
|
if (isCompRefBindings) {
|
|
3714
|
-
const varDeclaratorPath = getVariableDeclaratorPath(
|
|
3715
|
-
if (!
|
|
3844
|
+
const varDeclaratorPath = getVariableDeclaratorPath(path8)?.node;
|
|
3845
|
+
if (!t33.isIdentifier(varDeclaratorPath?.id)) {
|
|
3716
3846
|
return;
|
|
3717
3847
|
}
|
|
3718
3848
|
const varName = varDeclaratorPath.id.name;
|
|
3719
3849
|
const compRef = refBindings.componentRefs[varName];
|
|
3720
3850
|
if (!compRef) return;
|
|
3721
3851
|
}
|
|
3722
|
-
node.arguments[0] =
|
|
3723
|
-
resolveTypeParameters(ctx,
|
|
3852
|
+
node.arguments[0] = t33.identifier("null");
|
|
3853
|
+
resolveTypeParameters(ctx, path8);
|
|
3724
3854
|
replaceCallName(node, REACT_API_MAP.useRef);
|
|
3725
3855
|
recordImport(ctx, PACKAGE_NAME.react, REACT_API_MAP.useRef);
|
|
3726
3856
|
},
|
|
3727
|
-
MemberExpression(
|
|
3728
|
-
resolveRefValueToCurrent(
|
|
3857
|
+
MemberExpression(path8) {
|
|
3858
|
+
resolveRefValueToCurrent(path8);
|
|
3729
3859
|
}
|
|
3730
3860
|
};
|
|
3731
3861
|
}
|
|
3732
|
-
function resolveTypeParameters(ctx,
|
|
3862
|
+
function resolveTypeParameters(ctx, path8) {
|
|
3733
3863
|
const {
|
|
3734
3864
|
templateData: { refBindings },
|
|
3735
3865
|
scriptData
|
|
3736
3866
|
} = ctx;
|
|
3737
|
-
const { node } =
|
|
3738
|
-
const varDeclaratorNode = getVariableDeclaratorPath(
|
|
3867
|
+
const { node } = path8;
|
|
3868
|
+
const varDeclaratorNode = getVariableDeclaratorPath(path8)?.node;
|
|
3739
3869
|
if (!scriptData.lang.startsWith("ts") || !varDeclaratorNode) {
|
|
3740
3870
|
return;
|
|
3741
3871
|
}
|
|
@@ -3744,45 +3874,45 @@ function resolveTypeParameters(ctx, path7) {
|
|
|
3744
3874
|
const compBindingMeta = refBindings.componentRefs[idName];
|
|
3745
3875
|
if (!node.typeParameters && (domBindingMeta || compBindingMeta)) {
|
|
3746
3876
|
const type = compBindingMeta ? "any" : domBindingMeta.htmlType;
|
|
3747
|
-
node.typeParameters =
|
|
3877
|
+
node.typeParameters = t33.tsTypeParameterInstantiation([t33.tsTypeReference(t33.identifier(type))]);
|
|
3748
3878
|
}
|
|
3749
3879
|
}
|
|
3750
|
-
function resolveRefValueToCurrent(
|
|
3751
|
-
const { node } =
|
|
3752
|
-
if (node.computed || !
|
|
3880
|
+
function resolveRefValueToCurrent(path8) {
|
|
3881
|
+
const { node } = path8;
|
|
3882
|
+
if (node.computed || !t33.isIdentifier(node.property) || node.property.name !== "value") {
|
|
3753
3883
|
return;
|
|
3754
3884
|
}
|
|
3755
|
-
const rootPath = findRootVariablePath(
|
|
3756
|
-
if (!rootPath?.node || !
|
|
3885
|
+
const rootPath = findRootVariablePath(path8);
|
|
3886
|
+
if (!rootPath?.node || !t33.isCallExpression(rootPath.node.init) || !isCalleeNamed(rootPath.node.init, REACT_API_MAP.useRef)) {
|
|
3757
3887
|
return;
|
|
3758
3888
|
}
|
|
3759
3889
|
const rootId = findRootIdentifier(node);
|
|
3760
|
-
if (!
|
|
3890
|
+
if (!t33.isIdentifier(node.object) || node.object.name !== rootId?.name) {
|
|
3761
3891
|
return;
|
|
3762
3892
|
}
|
|
3763
3893
|
node.property.name = "current";
|
|
3764
3894
|
}
|
|
3765
3895
|
|
|
3766
3896
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-expression-memo.ts
|
|
3767
|
-
import * as
|
|
3897
|
+
import * as t34 from "@babel/types";
|
|
3768
3898
|
function resolveExprMemo(ctx, ast) {
|
|
3769
3899
|
const isScriptFile = ctx.inputType !== "sfc";
|
|
3770
3900
|
return {
|
|
3771
|
-
VariableDeclarator(
|
|
3772
|
-
const { node } =
|
|
3901
|
+
VariableDeclarator(path8) {
|
|
3902
|
+
const { node } = path8;
|
|
3773
3903
|
const { init } = node;
|
|
3774
3904
|
if (!init) return;
|
|
3775
3905
|
const shouldMemo = () => {
|
|
3776
|
-
if (!atComponentOrHookRoot(
|
|
3906
|
+
if (!atComponentOrHookRoot(path8, ast.program, isScriptFile)) {
|
|
3777
3907
|
return false;
|
|
3778
3908
|
}
|
|
3779
|
-
if (
|
|
3909
|
+
if (t34.isCallExpression(init) && t34.isIdentifier(init.callee) && init.callee.name.startsWith("use")) {
|
|
3780
3910
|
return false;
|
|
3781
3911
|
}
|
|
3782
3912
|
return true;
|
|
3783
3913
|
};
|
|
3784
3914
|
if (!shouldMemo()) return;
|
|
3785
|
-
const initPath =
|
|
3915
|
+
const initPath = path8.get("init");
|
|
3786
3916
|
if (!initPath.isExpression()) return;
|
|
3787
3917
|
const deps = analyzeDeps(initPath.node, ctx, initPath);
|
|
3788
3918
|
if (!deps.elements.length) return;
|
|
@@ -3794,13 +3924,13 @@ function resolveExprMemo(ctx, ast) {
|
|
|
3794
3924
|
}
|
|
3795
3925
|
|
|
3796
3926
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-lint-rules.ts
|
|
3797
|
-
import * as
|
|
3927
|
+
import * as t35 from "@babel/types";
|
|
3798
3928
|
function resolveLintRules(ctx, ast) {
|
|
3799
3929
|
const inScriptFile = ctx.inputType !== "sfc";
|
|
3800
3930
|
return {
|
|
3801
|
-
CallExpression(
|
|
3802
|
-
const { node, parentPath } =
|
|
3803
|
-
if (!
|
|
3931
|
+
CallExpression(path8) {
|
|
3932
|
+
const { node, parentPath } = path8;
|
|
3933
|
+
if (!t35.isIdentifier(node.callee)) return;
|
|
3804
3934
|
const { name: callName } = node.callee;
|
|
3805
3935
|
const addLog = (t41) => {
|
|
3806
3936
|
logger.error(t41, {
|
|
@@ -3818,7 +3948,7 @@ function resolveLintRules(ctx, ast) {
|
|
|
3818
3948
|
);
|
|
3819
3949
|
return;
|
|
3820
3950
|
}
|
|
3821
|
-
if (!atComponentOrHookRoot(
|
|
3951
|
+
if (!atComponentOrHookRoot(path8, ast.program)) {
|
|
3822
3952
|
addLog(
|
|
3823
3953
|
`The ${macro} must be defined at the top level of the component, not inside blocks or functions.`
|
|
3824
3954
|
);
|
|
@@ -3834,7 +3964,7 @@ function resolveLintRules(ctx, ast) {
|
|
|
3834
3964
|
};
|
|
3835
3965
|
const lintHooks = () => {
|
|
3836
3966
|
if (!callName.startsWith("use")) return;
|
|
3837
|
-
if (!atComponentOrHookRoot(
|
|
3967
|
+
if (!atComponentOrHookRoot(path8, ast.program, inScriptFile)) {
|
|
3838
3968
|
addLog(
|
|
3839
3969
|
`The ${callName} hook must be called at the top level, not inside loops, conditions, or nested functions.`
|
|
3840
3970
|
);
|
|
@@ -3848,20 +3978,22 @@ function resolveLintRules(ctx, ast) {
|
|
|
3848
3978
|
|
|
3849
3979
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-provide.ts
|
|
3850
3980
|
import { generate as generate3 } from "@babel/generator";
|
|
3851
|
-
import * as
|
|
3981
|
+
import * as t36 from "@babel/types";
|
|
3852
3982
|
function resolveProvide(ctx) {
|
|
3853
|
-
if (ctx.inputType
|
|
3983
|
+
if (ctx.inputType === "style") return {};
|
|
3854
3984
|
return {
|
|
3855
|
-
CallExpression(
|
|
3856
|
-
const { node } =
|
|
3857
|
-
|
|
3985
|
+
CallExpression(path8) {
|
|
3986
|
+
const { node } = path8;
|
|
3987
|
+
const providerTarget = ADAPTER_RULES.runtime[VUE_API_MAP.provide]?.target;
|
|
3988
|
+
const isProvideCall = isCalleeNamed(node, VUE_API_MAP.provide) || providerTarget && isCalleeNamed(node, providerTarget);
|
|
3989
|
+
if (!isProvideCall) return;
|
|
3858
3990
|
const { provide } = ctx.scriptData;
|
|
3859
3991
|
const [key, value] = node.arguments;
|
|
3860
3992
|
const target = findOrCreateCtxProvider(provide);
|
|
3861
3993
|
const adapter = ADAPTER_RULES.runtime[VUE_API_MAP.provide];
|
|
3862
3994
|
assignProviderValue(target, key, value);
|
|
3863
3995
|
recordImport(ctx, adapter.package, adapter.target);
|
|
3864
|
-
|
|
3996
|
+
path8.parentPath.remove();
|
|
3865
3997
|
}
|
|
3866
3998
|
};
|
|
3867
3999
|
}
|
|
@@ -3878,13 +4010,13 @@ function findOrCreateCtxProvider(root) {
|
|
|
3878
4010
|
function assignProviderValue(target, key, value) {
|
|
3879
4011
|
const getRawExp = (exp) => {
|
|
3880
4012
|
if (!exp) return "''";
|
|
3881
|
-
if (
|
|
3882
|
-
return
|
|
4013
|
+
if (t36.isStringLiteral(exp)) {
|
|
4014
|
+
return JSON.stringify(exp.value);
|
|
3883
4015
|
}
|
|
3884
|
-
if (
|
|
4016
|
+
if (t36.isNumericLiteral(exp)) {
|
|
3885
4017
|
return exp.value.toString();
|
|
3886
4018
|
}
|
|
3887
|
-
if (
|
|
4019
|
+
if (t36.isIdentifier(exp)) {
|
|
3888
4020
|
return exp.name;
|
|
3889
4021
|
}
|
|
3890
4022
|
try {
|
|
@@ -3900,16 +4032,16 @@ function assignProviderValue(target, key, value) {
|
|
|
3900
4032
|
}
|
|
3901
4033
|
|
|
3902
4034
|
// src/core/transform/sfc/script/syntax-processor/process/resolve-rename-adapter.ts
|
|
3903
|
-
import * as
|
|
4035
|
+
import * as t37 from "@babel/types";
|
|
3904
4036
|
function resolveRenameAdapter(ctx) {
|
|
3905
4037
|
return {
|
|
3906
|
-
"CallExpression|Identifier"(
|
|
3907
|
-
const node =
|
|
3908
|
-
const isCallNode =
|
|
4038
|
+
"CallExpression|Identifier"(path8) {
|
|
4039
|
+
const node = path8.node;
|
|
4040
|
+
const isCallNode = t37.isCallExpression(node);
|
|
3909
4041
|
let apiName = "";
|
|
3910
|
-
if (
|
|
4042
|
+
if (t37.isIdentifier(node)) {
|
|
3911
4043
|
apiName = node.name;
|
|
3912
|
-
} else if (isCallNode &&
|
|
4044
|
+
} else if (isCallNode && t37.isIdentifier(node.callee)) {
|
|
3913
4045
|
apiName = node.callee.name;
|
|
3914
4046
|
}
|
|
3915
4047
|
const runtimeAdapter = ADAPTER_RULES.runtime[apiName];
|
|
@@ -3920,7 +4052,7 @@ function resolveRenameAdapter(ctx) {
|
|
|
3920
4052
|
}
|
|
3921
4053
|
if (adapter.isTrackable) {
|
|
3922
4054
|
const reactiveType = getReactiveType(apiName);
|
|
3923
|
-
const declaratorPath = getVariableDeclaratorPath(
|
|
4055
|
+
const declaratorPath = getVariableDeclaratorPath(path8);
|
|
3924
4056
|
setScriptNodeMeta(declaratorPath?.node, {
|
|
3925
4057
|
is_reactive: true,
|
|
3926
4058
|
reactive_type: reactiveType
|
|
@@ -3956,11 +4088,12 @@ function processVueSyntax2(ast, ctx) {
|
|
|
3956
4088
|
process: {
|
|
3957
4089
|
applyBabel: [
|
|
3958
4090
|
resolveElementRef,
|
|
4091
|
+
// provide 需要在 rename 之前收集并移除原始调用,避免被重命名后失配
|
|
4092
|
+
resolveProvide,
|
|
3959
4093
|
resolveRenameAdapter,
|
|
3960
4094
|
resolveArrowFnDeps,
|
|
3961
4095
|
resolveUnanalyzedArrow,
|
|
3962
4096
|
resolveAnalysisOnlyAdapter,
|
|
3963
|
-
resolveProvide,
|
|
3964
4097
|
resolveExprMemo,
|
|
3965
4098
|
resolveLintRules
|
|
3966
4099
|
],
|
|
@@ -4064,7 +4197,7 @@ import * as t39 from "@babel/types";
|
|
|
4064
4197
|
|
|
4065
4198
|
// src/shared/string-code-types.ts
|
|
4066
4199
|
import { parseExpression as parseExpression3 } from "@babel/parser";
|
|
4067
|
-
import * as
|
|
4200
|
+
import * as t38 from "@babel/types";
|
|
4068
4201
|
var strCodeTypes = {
|
|
4069
4202
|
isIdentifier: isIdentifier20,
|
|
4070
4203
|
isSimpleExpression,
|
|
@@ -4077,22 +4210,22 @@ function isSimpleExpression(code, excludeVar = false) {
|
|
|
4077
4210
|
} catch {
|
|
4078
4211
|
return false;
|
|
4079
4212
|
}
|
|
4080
|
-
if (
|
|
4213
|
+
if (t38.isLiteral(node)) {
|
|
4081
4214
|
return true;
|
|
4082
4215
|
}
|
|
4083
|
-
if (!excludeVar &&
|
|
4216
|
+
if (!excludeVar && t38.isIdentifier(node)) {
|
|
4084
4217
|
return true;
|
|
4085
4218
|
}
|
|
4086
|
-
if (
|
|
4087
|
-
return isSimpleExpression(node.object) &&
|
|
4219
|
+
if (t38.isMemberExpression(node)) {
|
|
4220
|
+
return isSimpleExpression(node.object) && t38.isIdentifier(node.property);
|
|
4088
4221
|
}
|
|
4089
|
-
if (
|
|
4222
|
+
if (t38.isObjectExpression(node) || t38.isArrayExpression(node)) {
|
|
4090
4223
|
return false;
|
|
4091
4224
|
}
|
|
4092
|
-
if (
|
|
4225
|
+
if (t38.isCallExpression(node) || t38.isAssignmentExpression(node)) {
|
|
4093
4226
|
return false;
|
|
4094
4227
|
}
|
|
4095
|
-
if (
|
|
4228
|
+
if (t38.isBinaryExpression(node) || t38.isUnaryExpression(node)) {
|
|
4096
4229
|
return true;
|
|
4097
4230
|
}
|
|
4098
4231
|
return false;
|
|
@@ -4100,7 +4233,7 @@ function isSimpleExpression(code, excludeVar = false) {
|
|
|
4100
4233
|
function isIdentifier20(code) {
|
|
4101
4234
|
try {
|
|
4102
4235
|
const node = parseExpression3(code);
|
|
4103
|
-
return
|
|
4236
|
+
return t38.isIdentifier(node);
|
|
4104
4237
|
} catch {
|
|
4105
4238
|
return false;
|
|
4106
4239
|
}
|
|
@@ -4108,69 +4241,12 @@ function isIdentifier20(code) {
|
|
|
4108
4241
|
function isStringLiteral12(code) {
|
|
4109
4242
|
try {
|
|
4110
4243
|
const node = parseExpression3(code);
|
|
4111
|
-
return
|
|
4244
|
+
return t38.isStringLiteral(node) || t38.isTemplateLiteral(node);
|
|
4112
4245
|
} catch {
|
|
4113
4246
|
return false;
|
|
4114
4247
|
}
|
|
4115
4248
|
}
|
|
4116
4249
|
|
|
4117
|
-
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
4118
|
-
import * as t38 from "@babel/types";
|
|
4119
|
-
|
|
4120
|
-
// src/core/transform/sfc/template/shared/resolve-string-expression/special-expressions.ts
|
|
4121
|
-
function resolveSpecialExpressions(input, ctx) {
|
|
4122
|
-
input = resolveEmitsCalls(input, ctx);
|
|
4123
|
-
input = resolveRefVariable(input, ctx);
|
|
4124
|
-
return input;
|
|
4125
|
-
}
|
|
4126
|
-
function resolveEmitsCalls(input, ctx) {
|
|
4127
|
-
const result = matchEmitCalls(input, ctx);
|
|
4128
|
-
if (!result) return input;
|
|
4129
|
-
const [, , eventName, args] = result;
|
|
4130
|
-
const callee = eventName.split(/[:\-]/).map((part) => capitalize(camelCase(part))).join("");
|
|
4131
|
-
const event = args ? `on${callee}(${args})` : `on${callee}()`;
|
|
4132
|
-
return `${ctx.propField}?.${event}`;
|
|
4133
|
-
}
|
|
4134
|
-
function matchEmitCalls(input, ctx) {
|
|
4135
|
-
const { reactiveBindings } = ctx.templateData;
|
|
4136
|
-
const macroBinding = Object.values(reactiveBindings).find((b) => b.source === "defineEmits");
|
|
4137
|
-
if (!macroBinding) return null;
|
|
4138
|
-
const escapedName = macroBinding.name.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4139
|
-
const regex = new RegExp(
|
|
4140
|
-
`${escapedName}\\s*\\(\\s*(['"\`])([^\\1]*?)\\1\\s*(?:,\\s*(.*?))?\\s*\\)$`,
|
|
4141
|
-
// 可选的第二个参数
|
|
4142
|
-
"s"
|
|
4143
|
-
// s 标志让 . 匹配换行符
|
|
4144
|
-
);
|
|
4145
|
-
return input.trim().match(regex);
|
|
4146
|
-
}
|
|
4147
|
-
function resolveRefVariable(input, ctx) {
|
|
4148
|
-
const { reactiveBindings } = ctx.templateData;
|
|
4149
|
-
const addValueProperty = (input2, varName) => {
|
|
4150
|
-
const escapedVarName = varName.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
4151
|
-
const regex = new RegExp(`(?<![a-zA-Z0-9_])${escapedVarName}(?![a-zA-Z0-9_])(?!\\.value)`, "g");
|
|
4152
|
-
return input2.replace(regex, `${varName}.value`);
|
|
4153
|
-
};
|
|
4154
|
-
for (const name in reactiveBindings) {
|
|
4155
|
-
const binding = reactiveBindings[name];
|
|
4156
|
-
if (binding?.reactiveType !== "ref") continue;
|
|
4157
|
-
input = addValueProperty(input, name);
|
|
4158
|
-
}
|
|
4159
|
-
return input;
|
|
4160
|
-
}
|
|
4161
|
-
|
|
4162
|
-
// src/core/transform/sfc/template/shared/resolve-string-expression/index.ts
|
|
4163
|
-
function resolveStringExpr(input, ctx, toStrLiteral = false) {
|
|
4164
|
-
if (toStrLiteral) return t38.stringLiteral(input);
|
|
4165
|
-
const { filename, scriptData } = ctx;
|
|
4166
|
-
const newContent = resolveSpecialExpressions(input, ctx);
|
|
4167
|
-
try {
|
|
4168
|
-
return stringToExpr(newContent, scriptData.lang, filename);
|
|
4169
|
-
} catch {
|
|
4170
|
-
return t38.identifier(newContent);
|
|
4171
|
-
}
|
|
4172
|
-
}
|
|
4173
|
-
|
|
4174
4250
|
// src/core/transform/sfc/template/shared/style-utils.ts
|
|
4175
4251
|
function parseStyleString(styleStr) {
|
|
4176
4252
|
if (isSimpleStyle(styleStr) || strCodeTypes.isIdentifier(styleStr)) {
|
|
@@ -4449,6 +4525,8 @@ function resolveDefaultStyleModuleName(node) {
|
|
|
4449
4525
|
// src/core/transform/sfc/template/syntax-processor/preprocess/resolve-style-scope-attribute.ts
|
|
4450
4526
|
import {
|
|
4451
4527
|
ElementTypes,
|
|
4528
|
+
isSlotOutlet,
|
|
4529
|
+
isTemplateNode,
|
|
4452
4530
|
NodeTypes as NodeTypes3
|
|
4453
4531
|
} from "@vue/compiler-core";
|
|
4454
4532
|
function resolveStyleScopeAttribute(node, _ir, ctx) {
|
|
@@ -4470,7 +4548,7 @@ function walkElementNodes2(node, onElement) {
|
|
|
4470
4548
|
}
|
|
4471
4549
|
function injectStyleScopeAttribute(node, ctx) {
|
|
4472
4550
|
const { scopeId } = ctx.styleData;
|
|
4473
|
-
if (!scopeId || isComponentElement(node)) {
|
|
4551
|
+
if (!scopeId || isComponentElement(node) || isSlotOutlet(node) || isTemplateNode(node)) {
|
|
4474
4552
|
return;
|
|
4475
4553
|
}
|
|
4476
4554
|
const hasDynamicIs = node.props.some((prop) => {
|
|
@@ -4853,9 +4931,8 @@ function resolveVModel(node, _ir, ctx, elementNode, nodeIR) {
|
|
|
4853
4931
|
let eventPropIR;
|
|
4854
4932
|
if (isComponent) {
|
|
4855
4933
|
valuePropIR = createPropsIR("v-model", propName, getterName);
|
|
4856
|
-
const
|
|
4857
|
-
const
|
|
4858
|
-
const eventVueName = `update:${getterNamespace}`;
|
|
4934
|
+
const eventReactName = `onUpdate${capitalize(camelCase(propName))}`;
|
|
4935
|
+
const eventVueName = `update:${propName}`;
|
|
4859
4936
|
const isTS = ctx.scriptData?.lang?.startsWith("ts");
|
|
4860
4937
|
const valueArg = isTS ? "value: any" : "value";
|
|
4861
4938
|
const processedValue = applyValueModifiers("value", modifiers);
|
|
@@ -4916,12 +4993,6 @@ function applyValueModifiers(valueExp, modifiers) {
|
|
|
4916
4993
|
}
|
|
4917
4994
|
return result;
|
|
4918
4995
|
}
|
|
4919
|
-
function getRootIdName(expr) {
|
|
4920
|
-
if (typeof expr !== "string") return;
|
|
4921
|
-
const pattern = /^([a-zA-Z_$][a-zA-Z0-9_$]*)(?:[\.\?\.\[\(].*)?$/;
|
|
4922
|
-
const match = pattern.exec(expr.trim());
|
|
4923
|
-
return match?.[1];
|
|
4924
|
-
}
|
|
4925
4996
|
|
|
4926
4997
|
// src/core/transform/sfc/template/syntax-processor/process/props/resolve-v-on.ts
|
|
4927
4998
|
import * as t40 from "@babel/types";
|
|
@@ -5187,7 +5258,7 @@ import { NodeTypes as NodeTypes9 } from "@vue/compiler-core";
|
|
|
5187
5258
|
|
|
5188
5259
|
// src/core/transform/sfc/template/syntax-processor/process/resolve-template-children.ts
|
|
5189
5260
|
import {
|
|
5190
|
-
isSlotOutlet,
|
|
5261
|
+
isSlotOutlet as isSlotOutlet2,
|
|
5191
5262
|
NodeTypes as VueNodeTypes
|
|
5192
5263
|
} from "@vue/compiler-core";
|
|
5193
5264
|
|
|
@@ -5223,7 +5294,7 @@ function resolveTemplateChildren(node, nodeIR, ctx) {
|
|
|
5223
5294
|
function resolveChildNodes(node, nodeIR, ctx, parentIR, childrenIR) {
|
|
5224
5295
|
for (const child of node.children) {
|
|
5225
5296
|
if (child.type === VueNodeTypes.ELEMENT) {
|
|
5226
|
-
if (
|
|
5297
|
+
if (isSlotOutlet2(child)) {
|
|
5227
5298
|
resolveSlotOutletNode(child, nodeIR, ctx, parentIR, childrenIR);
|
|
5228
5299
|
continue;
|
|
5229
5300
|
}
|
|
@@ -5389,7 +5460,7 @@ function transform(ast, ctx, options) {
|
|
|
5389
5460
|
}
|
|
5390
5461
|
|
|
5391
5462
|
// package.json
|
|
5392
|
-
var version = "1.
|
|
5463
|
+
var version = "1.3.0";
|
|
5393
5464
|
var bin = {
|
|
5394
5465
|
vureact: "./bin/vureact.js"
|
|
5395
5466
|
};
|
|
@@ -5701,9 +5772,9 @@ var Helper = class {
|
|
|
5701
5772
|
};
|
|
5702
5773
|
}
|
|
5703
5774
|
async removeOutputFile(filePath, resolveOutputPath) {
|
|
5704
|
-
const
|
|
5705
|
-
if (!fs.existsSync(
|
|
5706
|
-
await fs.promises.unlink(
|
|
5775
|
+
const path8 = resolveOutputPath ? this.resolveOutputPath(filePath) : filePath;
|
|
5776
|
+
if (!fs.existsSync(path8)) return;
|
|
5777
|
+
await fs.promises.unlink(path8);
|
|
5707
5778
|
}
|
|
5708
5779
|
updateCache(targetFile, newData, cache) {
|
|
5709
5780
|
const index = cache.target.findIndex((c) => c.file === targetFile);
|
|
@@ -5768,12 +5839,12 @@ var Helper = class {
|
|
|
5768
5839
|
/**
|
|
5769
5840
|
* 读取 package.json 文件内容,并处理成对象返回
|
|
5770
5841
|
*/
|
|
5771
|
-
async resolvePackageFile(
|
|
5842
|
+
async resolvePackageFile(path8) {
|
|
5772
5843
|
try {
|
|
5773
|
-
if (!fs.existsSync(
|
|
5844
|
+
if (!fs.existsSync(path8)) {
|
|
5774
5845
|
return {};
|
|
5775
5846
|
}
|
|
5776
|
-
return JSON.parse(await fs.promises.readFile(
|
|
5847
|
+
return JSON.parse(await fs.promises.readFile(path8, "utf-8"));
|
|
5777
5848
|
} catch (error) {
|
|
5778
5849
|
console.error(error);
|
|
5779
5850
|
return {};
|
|
@@ -6294,10 +6365,12 @@ var CacheManager = class {
|
|
|
6294
6365
|
const meta = { ...unit };
|
|
6295
6366
|
delete meta.source;
|
|
6296
6367
|
if (key === "sfc" /* SFC */) {
|
|
6297
|
-
delete meta.output
|
|
6298
|
-
delete meta.output
|
|
6368
|
+
delete meta.output?.jsx.code;
|
|
6369
|
+
delete meta.output?.css.code;
|
|
6299
6370
|
} else if (key === "script" /* SCRIPT */) {
|
|
6300
|
-
delete meta.output
|
|
6371
|
+
delete meta.output?.script.code;
|
|
6372
|
+
} else if (key === "style" /* STYLE */) {
|
|
6373
|
+
delete meta.output?.style.code;
|
|
6301
6374
|
}
|
|
6302
6375
|
this.updateCache(unit.file, meta, cache);
|
|
6303
6376
|
await this.fileCompiler.saveCache(cache);
|
|
@@ -6458,6 +6531,7 @@ var CompilationUnitProcessor = class {
|
|
|
6458
6531
|
|
|
6459
6532
|
// src/compiler/shared/file-compiler/file-processor.ts
|
|
6460
6533
|
import fs3 from "fs";
|
|
6534
|
+
import path6 from "path";
|
|
6461
6535
|
var FileProcessor = class {
|
|
6462
6536
|
constructor(fileCompiler, compilationUnitProcessor, cacheManager) {
|
|
6463
6537
|
this.fileCompiler = fileCompiler;
|
|
@@ -6516,6 +6590,7 @@ var FileProcessor = class {
|
|
|
6516
6590
|
if (key === "sfc" /* SFC */ || key === "script" /* SCRIPT */) {
|
|
6517
6591
|
if (processed?.hasRoute) {
|
|
6518
6592
|
await this.injectVuReactRouteDep();
|
|
6593
|
+
await this.copyRouteSetupNotes();
|
|
6519
6594
|
}
|
|
6520
6595
|
}
|
|
6521
6596
|
await this.cacheManager.updateCacheIncrementally(processed, key);
|
|
@@ -6526,13 +6601,34 @@ var FileProcessor = class {
|
|
|
6526
6601
|
const pkgPath = this.fileCompiler.getOutputPkgPath();
|
|
6527
6602
|
const pkg = await this.fileCompiler.resolvePackageFile(pkgPath);
|
|
6528
6603
|
const { router } = this.pkgs;
|
|
6604
|
+
if (!pkg["dependencies"]) {
|
|
6605
|
+
pkg["dependencies"] = {};
|
|
6606
|
+
}
|
|
6529
6607
|
pkg["dependencies"][router.name] = router.version;
|
|
6530
6608
|
await fs3.promises.writeFile(pkgPath, JSON.stringify(pkg, null, 2), "utf-8");
|
|
6531
6609
|
}
|
|
6610
|
+
/**
|
|
6611
|
+
* 如果使用了路由,则拷贝路由配置说明文档到输出目录根部。
|
|
6612
|
+
*/
|
|
6613
|
+
async copyRouteSetupNotes() {
|
|
6614
|
+
const outputDir = this.fileCompiler.getOuputPath();
|
|
6615
|
+
const packageRoot = path6.resolve(getDirname(import.meta.url), "../");
|
|
6616
|
+
const templateDir = path6.join(packageRoot, "templates");
|
|
6617
|
+
if (!fs3.existsSync(templateDir)) {
|
|
6618
|
+
return;
|
|
6619
|
+
}
|
|
6620
|
+
const files = ["route-setup-notes.md", "route-setup-notes.zh.md"];
|
|
6621
|
+
for (const file of files) {
|
|
6622
|
+
const srcPath = path6.join(templateDir, file);
|
|
6623
|
+
if (!fs3.existsSync(srcPath)) continue;
|
|
6624
|
+
const destPath = path6.join(outputDir, file);
|
|
6625
|
+
await fs3.promises.copyFile(srcPath, destPath);
|
|
6626
|
+
}
|
|
6627
|
+
}
|
|
6532
6628
|
};
|
|
6533
6629
|
|
|
6534
6630
|
// src/compiler/shared/file-compiler/pipeline-manager.ts
|
|
6535
|
-
import
|
|
6631
|
+
import path7 from "path";
|
|
6536
6632
|
var PipelineManager = class {
|
|
6537
6633
|
constructor(fileCompiler, fileProcessor) {
|
|
6538
6634
|
this.fileCompiler = fileCompiler;
|
|
@@ -6567,7 +6663,7 @@ var PipelineManager = class {
|
|
|
6567
6663
|
const scriptExtRegex = /\.(js|ts)$/i;
|
|
6568
6664
|
const styleExtRegex = /\.(css|less|sass|scss)$/i;
|
|
6569
6665
|
const files = this.fileCompiler.scanFiles(inputPath, (p) => {
|
|
6570
|
-
const ext =
|
|
6666
|
+
const ext = path7.extname(p);
|
|
6571
6667
|
if (key === "sfc" /* SFC */) {
|
|
6572
6668
|
return ext === ".vue";
|
|
6573
6669
|
}
|
|
@@ -6780,27 +6876,55 @@ var FileCompiler = class extends BaseCompiler {
|
|
|
6780
6876
|
}
|
|
6781
6877
|
await this.viteBootstrapper.bootstrapIfNeeded();
|
|
6782
6878
|
try {
|
|
6783
|
-
this.
|
|
6784
|
-
const
|
|
6785
|
-
this.
|
|
6786
|
-
this.
|
|
6787
|
-
const scriptCount = await this.pipelineManager.runScriptPipeline();
|
|
6788
|
-
this.spinner.stop();
|
|
6789
|
-
this.spinner.start("Compiling style files...");
|
|
6790
|
-
const styleCount = await this.pipelineManager.runStylePipeline();
|
|
6791
|
-
this.spinner.stop();
|
|
6792
|
-
this.spinner.start("Copying assets...");
|
|
6793
|
-
const assetCount = await this.assetManager.runAssetPipeline();
|
|
6794
|
-
this.spinner.stop();
|
|
6879
|
+
const sfcCount = await this.runPipelineWithSpinner("sfc" /* SFC */);
|
|
6880
|
+
const scriptCount = await this.runPipelineWithSpinner("script" /* SCRIPT */);
|
|
6881
|
+
const styleCount = await this.runPipelineWithSpinner("style" /* STYLE */);
|
|
6882
|
+
const assetCount = await this.runPipelineWithSpinner("copied" /* ASSET */);
|
|
6795
6883
|
await this.options.onSuccess?.();
|
|
6796
6884
|
const endTime = calcElapsedTime(startTime);
|
|
6797
6885
|
this.printCoreLogs();
|
|
6798
6886
|
this.showCompileStats(endTime, sfcCount, scriptCount, styleCount, assetCount);
|
|
6799
6887
|
} catch (error) {
|
|
6800
|
-
this.spinner.stop();
|
|
6801
6888
|
const endTime = calcElapsedTime(startTime);
|
|
6802
|
-
console.error(kleur6.red("\u2716"), `Build failed in ${endTime}
|
|
6803
|
-
|
|
6889
|
+
console.error(kleur6.red("\u2716"), `Build failed in ${endTime}`);
|
|
6890
|
+
console.error(error);
|
|
6891
|
+
}
|
|
6892
|
+
}
|
|
6893
|
+
/**
|
|
6894
|
+
* 运行管线并显示加载动画
|
|
6895
|
+
*
|
|
6896
|
+
* @private
|
|
6897
|
+
* @param text - 加载动画显示的文本
|
|
6898
|
+
* @param pipelineFn - 要执行的管线函数
|
|
6899
|
+
* @returns 返回的处理的文件数
|
|
6900
|
+
*/
|
|
6901
|
+
async runPipelineWithSpinner(name) {
|
|
6902
|
+
const options = {
|
|
6903
|
+
["sfc" /* SFC */]: {
|
|
6904
|
+
text: "Compiling Vue files...",
|
|
6905
|
+
pipeline: () => this.pipelineManager.runSfcPipeline()
|
|
6906
|
+
},
|
|
6907
|
+
["script" /* SCRIPT */]: {
|
|
6908
|
+
text: "Compiling script files...",
|
|
6909
|
+
pipeline: () => this.pipelineManager.runScriptPipeline()
|
|
6910
|
+
},
|
|
6911
|
+
["style" /* STYLE */]: {
|
|
6912
|
+
text: "Compiling style files...",
|
|
6913
|
+
pipeline: () => this.pipelineManager.runStylePipeline()
|
|
6914
|
+
},
|
|
6915
|
+
["copied" /* ASSET */]: {
|
|
6916
|
+
text: "Copying assets...",
|
|
6917
|
+
pipeline: () => this.assetManager.runAssetPipeline()
|
|
6918
|
+
}
|
|
6919
|
+
};
|
|
6920
|
+
const { text, pipeline } = options[name];
|
|
6921
|
+
try {
|
|
6922
|
+
this.spinner.start(text);
|
|
6923
|
+
return await pipeline();
|
|
6924
|
+
} catch (err) {
|
|
6925
|
+
throw err;
|
|
6926
|
+
} finally {
|
|
6927
|
+
this.spinner.stop();
|
|
6804
6928
|
}
|
|
6805
6929
|
}
|
|
6806
6930
|
/**
|
|
@@ -6970,6 +7094,7 @@ var VuReact = class extends FileCompiler {
|
|
|
6970
7094
|
};
|
|
6971
7095
|
|
|
6972
7096
|
export {
|
|
7097
|
+
getDirname,
|
|
6973
7098
|
normalizePath,
|
|
6974
7099
|
calcElapsedTime,
|
|
6975
7100
|
generateComponent,
|