@vue-jsx-vapor/compiler 0.0.3 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +85 -145
- package/dist/index.js +84 -144
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/index.ts
|
2
2
|
var _compilervapor = require('@vue/compiler-vapor');
|
3
3
|
|
4
4
|
// src/compile.ts
|
@@ -46,6 +46,8 @@ var _types = require('@babel/types');
|
|
46
46
|
|
47
47
|
|
48
48
|
|
49
|
+
|
50
|
+
|
49
51
|
var __BROWSER__ = false;
|
50
52
|
function isConstantExpression(exp) {
|
51
53
|
return _compilerdom.isLiteralWhitelisted.call(void 0, exp.content) || _shared.isGloballyAllowed.call(void 0, exp.content) || getLiteralExpressionValue(exp) !== null;
|
@@ -62,29 +64,66 @@ function getLiteralExpressionValue(exp) {
|
|
62
64
|
}
|
63
65
|
return exp.isStatic ? exp.content : null;
|
64
66
|
}
|
65
|
-
|
67
|
+
var isConstant = (node) => {
|
68
|
+
if (!node) return false;
|
69
|
+
if (node.type === "Identifier") {
|
70
|
+
return node.name === "undefined";
|
71
|
+
}
|
72
|
+
if (node.type === "ArrayExpression") {
|
73
|
+
const { elements } = node;
|
74
|
+
return elements.every((element) => element && isConstant(element));
|
75
|
+
}
|
76
|
+
if (node.type === "ObjectExpression") {
|
77
|
+
return node.properties.every(
|
78
|
+
(property) => isConstant(property.value)
|
79
|
+
);
|
80
|
+
}
|
81
|
+
if (node.type === "TemplateLiteral" ? !node.expressions.length : _types.isLiteral.call(void 0, node)) {
|
82
|
+
return true;
|
83
|
+
}
|
84
|
+
return false;
|
85
|
+
};
|
86
|
+
function isLiteralExpressionContainer(node) {
|
87
|
+
return _optionalChain([node, 'optionalAccess', _2 => _2.type]) === "JSXExpressionContainer" && node.expression.type !== "JSXEmptyExpression" && isConstant(node.expression);
|
88
|
+
}
|
89
|
+
var EMPTY_TEXT_REGEX = /^\s*[\n\r]\s*$/;
|
90
|
+
var START_EMPTY_TEXT_REGEX = /^\s*[\n\r]/;
|
91
|
+
var END_EMPTY_TEXT_REGEX = /[\n\r]\s*$/;
|
92
|
+
function resolveJSXText(node) {
|
93
|
+
if (EMPTY_TEXT_REGEX.test(`${_optionalChain([node, 'access', _3 => _3.extra, 'optionalAccess', _4 => _4.raw])}`)) {
|
94
|
+
return "";
|
95
|
+
}
|
96
|
+
let value = node.value;
|
97
|
+
if (START_EMPTY_TEXT_REGEX.test(value)) {
|
98
|
+
value = value.trimStart();
|
99
|
+
}
|
100
|
+
if (END_EMPTY_TEXT_REGEX.test(value)) {
|
101
|
+
value = value.trimEnd();
|
102
|
+
}
|
103
|
+
return value;
|
104
|
+
}
|
105
|
+
function isEmptyText(node) {
|
106
|
+
return node.type === "JSXText" && EMPTY_TEXT_REGEX.test(`${_optionalChain([node, 'access', _5 => _5.extra, 'optionalAccess', _6 => _6.raw])}`) || node.type === "JSXExpressionContainer" && node.expression.type === "JSXEmptyExpression";
|
107
|
+
}
|
108
|
+
function resolveExpression(node, context, effect = false) {
|
66
109
|
const isStatic = !!node && (node.type === "StringLiteral" || node.type === "JSXText" || node.type === "JSXIdentifier");
|
67
|
-
|
110
|
+
let source = !node ? "" : node.type === "JSXIdentifier" ? node.name : node.type === "StringLiteral" ? node.value : node.type === "JSXText" ? resolveJSXText(node) : node.type === "JSXExpressionContainer" ? node.expression.type === "JSXEmptyExpression" ? "" : node.expression.type === "Identifier" ? node.expression.name : context.ir.source.slice(
|
68
111
|
node.expression.start,
|
69
112
|
node.expression.end
|
70
113
|
) : context.ir.source.slice(node.start, node.end);
|
71
114
|
const location = node ? node.loc : null;
|
72
|
-
|
73
|
-
|
74
|
-
ast = _parser.parseExpression.call(void 0, ` ${source}`, {
|
75
|
-
sourceType: "module",
|
76
|
-
plugins: context.options.expressionPlugins
|
77
|
-
});
|
115
|
+
if (source && !isStatic && effect && !isLiteralExpressionContainer(node)) {
|
116
|
+
source = `() => (${source})`;
|
78
117
|
}
|
79
|
-
return resolveSimpleExpression(source, isStatic, location
|
118
|
+
return resolveSimpleExpression(source, isStatic, location);
|
80
119
|
}
|
81
|
-
function resolveSimpleExpression(source, isStatic, location
|
120
|
+
function resolveSimpleExpression(source, isStatic, location) {
|
82
121
|
const result = _compilerdom.createSimpleExpression.call(void 0,
|
83
122
|
source,
|
84
123
|
isStatic,
|
85
124
|
resolveLocation(location, source)
|
86
125
|
);
|
87
|
-
result.ast =
|
126
|
+
result.ast = null;
|
88
127
|
return result;
|
89
128
|
}
|
90
129
|
function resolveLocation(location, context) {
|
@@ -184,11 +223,8 @@ function isJSXComponent(node) {
|
|
184
223
|
return openingElement.name.type === "JSXMemberExpression";
|
185
224
|
}
|
186
225
|
}
|
187
|
-
function isMapCallExpression(node) {
|
188
|
-
return !!node && node.type === "CallExpression" && node.callee.type === "MemberExpression" && node.callee.property.type === "Identifier" && node.callee.property.name === "map";
|
189
|
-
}
|
190
226
|
function findProp(expression, key) {
|
191
|
-
if (_optionalChain([expression, 'optionalAccess',
|
227
|
+
if (_optionalChain([expression, 'optionalAccess', _7 => _7.type]) === "JSXElement") {
|
192
228
|
for (const attr of expression.openingElement.attributes) {
|
193
229
|
if (attr.type === "JSXAttribute" && attr.name.name === key) {
|
194
230
|
return attr;
|
@@ -196,18 +232,6 @@ function findProp(expression, key) {
|
|
196
232
|
}
|
197
233
|
}
|
198
234
|
}
|
199
|
-
function getReturnExpression(node) {
|
200
|
-
if (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
|
201
|
-
if (node.body.type !== "BlockStatement") {
|
202
|
-
return node.body;
|
203
|
-
} else {
|
204
|
-
for (const statement of node.body.body) {
|
205
|
-
if (statement.type === "ReturnStatement" && statement.argument)
|
206
|
-
return statement.argument;
|
207
|
-
}
|
208
|
-
}
|
209
|
-
}
|
210
|
-
}
|
211
235
|
function isJSXElement(node) {
|
212
236
|
return !!node && (node.type === "JSXElement" || node.type === "JSXFragment");
|
213
237
|
}
|
@@ -479,13 +503,6 @@ function extractIdentifiers(ids, node) {
|
|
479
503
|
// src/transforms/transformElement.ts
|
480
504
|
|
481
505
|
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
506
|
// src/html-nesting.ts
|
490
507
|
function isValidHTMLNesting(parent, child) {
|
491
508
|
if (parent in onlyValidChildren) {
|
@@ -687,14 +704,14 @@ var transformElement = (node, context) => {
|
|
687
704
|
function transformComponentElement(tag, propsResult, singleRoot, context) {
|
688
705
|
let asset = true;
|
689
706
|
if (!__BROWSER__2) {
|
690
|
-
const fromSetup =
|
707
|
+
const fromSetup = tag;
|
691
708
|
if (fromSetup) {
|
692
709
|
tag = fromSetup;
|
693
710
|
asset = false;
|
694
711
|
}
|
695
712
|
const dotIndex = tag.indexOf(".");
|
696
713
|
if (dotIndex > 0) {
|
697
|
-
const ns =
|
714
|
+
const ns = tag.slice(0, dotIndex);
|
698
715
|
if (ns) {
|
699
716
|
tag = ns + tag.slice(dotIndex);
|
700
717
|
asset = false;
|
@@ -717,16 +734,6 @@ function transformComponentElement(tag, propsResult, singleRoot, context) {
|
|
717
734
|
});
|
718
735
|
context.slots = [];
|
719
736
|
}
|
720
|
-
function resolveSetupReference(name, context) {
|
721
|
-
const bindings = context.options.bindingMetadata;
|
722
|
-
if (!context.options.prefixIdentifiers) return name;
|
723
|
-
if (!bindings || bindings.__isScriptSetup === false) {
|
724
|
-
return;
|
725
|
-
}
|
726
|
-
const camelName = _shared.camelize.call(void 0, name);
|
727
|
-
const PascalName = _shared.capitalize.call(void 0, camelName);
|
728
|
-
return bindings[name] ? name : bindings[camelName] ? camelName : bindings[PascalName] ? PascalName : void 0;
|
729
|
-
}
|
730
737
|
function transformNativeElement(tag, propsResult, singleRoot, context) {
|
731
738
|
const { scopeId } = context.options;
|
732
739
|
let template = "";
|
@@ -841,7 +848,7 @@ function transformProp(prop, node, context) {
|
|
841
848
|
return directiveTransform(prop, node, context);
|
842
849
|
}
|
843
850
|
if (!_shared.isBuiltInDirective.call(void 0, name)) {
|
844
|
-
const fromSetup = !__BROWSER__2 &&
|
851
|
+
const fromSetup = !__BROWSER__2 && `v-${name}`;
|
845
852
|
if (fromSetup) {
|
846
853
|
name = fromSetup;
|
847
854
|
} else {
|
@@ -886,16 +893,6 @@ function mergePropValues(existing, incoming) {
|
|
886
893
|
var transformChildren = (node, context) => {
|
887
894
|
const isFragment = node.type === 0 /* ROOT */ || node.type === "JSXFragment" || isJSXComponent(node);
|
888
895
|
if (node.type !== "JSXElement" && !isFragment) return;
|
889
|
-
Array.from(node.children).forEach((child, index) => {
|
890
|
-
if (child.type === "JSXText" && !child.value.trim()) {
|
891
|
-
child.value = " ";
|
892
|
-
if (!index) {
|
893
|
-
node.children.splice(0, 1);
|
894
|
-
} else if (index === node.children.length) {
|
895
|
-
node.children.splice(-1, 1);
|
896
|
-
}
|
897
|
-
}
|
898
|
-
});
|
899
896
|
for (const [i, child] of node.children.entries()) {
|
900
897
|
const childContext = context.create(child, i);
|
901
898
|
transformNode(childContext);
|
@@ -959,7 +956,7 @@ function processDynamicChildren(context) {
|
|
959
956
|
var transformTemplateRef = (node, context) => {
|
960
957
|
if (node.type !== "JSXElement") return;
|
961
958
|
const dir = findProp(node, "ref");
|
962
|
-
if (!_optionalChain([dir, 'optionalAccess',
|
959
|
+
if (!_optionalChain([dir, 'optionalAccess', _8 => _8.value])) return;
|
963
960
|
const value = resolveExpression(dir.value, context);
|
964
961
|
return () => {
|
965
962
|
const id = context.reference();
|
@@ -1039,46 +1036,6 @@ function processLogicalExpression(node, context) {
|
|
1039
1036
|
];
|
1040
1037
|
}
|
1041
1038
|
|
1042
|
-
// src/transforms/vFor.ts
|
1043
|
-
function processMapCallExpression(node, context) {
|
1044
|
-
const {
|
1045
|
-
callee,
|
1046
|
-
arguments: [argument]
|
1047
|
-
} = node;
|
1048
|
-
if (!(argument.type === "FunctionExpression" || argument.type === "ArrowFunctionExpression") || _optionalChain([callee, 'optionalAccess', _4 => _4.type]) !== "MemberExpression")
|
1049
|
-
return;
|
1050
|
-
context.dynamic.flags |= 2 /* NON_TEMPLATE */ | 4 /* INSERT */;
|
1051
|
-
const id = context.reference();
|
1052
|
-
const [render, exitBlock] = createBranch(argument, context, true);
|
1053
|
-
const source = resolveExpression(callee.object, context);
|
1054
|
-
const value = argument.params[0] && resolveExpression(argument.params[0], context);
|
1055
|
-
const key = argument.params[1] && resolveExpression(argument.params[1], context);
|
1056
|
-
const index = argument.params[2] && resolveExpression(argument.params[2], context);
|
1057
|
-
const returnExpression = getReturnExpression(argument);
|
1058
|
-
const keyProp = findProp(returnExpression, "key");
|
1059
|
-
const keyProperty = keyProp && resolveExpression(keyProp.value, context);
|
1060
|
-
return () => {
|
1061
|
-
exitBlock();
|
1062
|
-
const { parent } = context;
|
1063
|
-
let container;
|
1064
|
-
if (parent && parent.block.node !== parent.node && parent.node.children.length === 1) {
|
1065
|
-
container = parent.reference();
|
1066
|
-
}
|
1067
|
-
context.registerOperation({
|
1068
|
-
type: 19 /* FOR */,
|
1069
|
-
id,
|
1070
|
-
source,
|
1071
|
-
value,
|
1072
|
-
key,
|
1073
|
-
index,
|
1074
|
-
keyProp: keyProperty,
|
1075
|
-
render,
|
1076
|
-
once: context.inVOnce,
|
1077
|
-
container
|
1078
|
-
});
|
1079
|
-
};
|
1080
|
-
}
|
1081
|
-
|
1082
1039
|
// src/transforms/transformText.ts
|
1083
1040
|
var seen = /* @__PURE__ */ new WeakMap();
|
1084
1041
|
var transformText = (node, context) => {
|
@@ -1097,73 +1054,61 @@ var transformText = (node, context) => {
|
|
1097
1054
|
return processConditionalExpression(node.expression, context);
|
1098
1055
|
} else if (node.expression.type === "LogicalExpression") {
|
1099
1056
|
return processLogicalExpression(node.expression, context);
|
1100
|
-
} else if (node.expression.type === "CallExpression") {
|
1101
|
-
if (isMapCallExpression(node.expression)) {
|
1102
|
-
return processMapCallExpression(node.expression, context);
|
1103
|
-
} else {
|
1104
|
-
processCallExpression(node.expression, context);
|
1105
|
-
}
|
1106
1057
|
} else {
|
1107
1058
|
processTextLike(context);
|
1108
1059
|
}
|
1109
1060
|
} else if (node.type === "JSXText") {
|
1110
|
-
|
1061
|
+
const value = resolveJSXText(node);
|
1062
|
+
if (value) {
|
1063
|
+
context.template += value;
|
1064
|
+
} else {
|
1065
|
+
context.dynamic.flags |= 2 /* NON_TEMPLATE */;
|
1066
|
+
}
|
1111
1067
|
}
|
1112
1068
|
};
|
1113
1069
|
function processTextLike(context) {
|
1114
|
-
const nexts = _optionalChain([context, 'access',
|
1070
|
+
const nexts = _optionalChain([context, 'access', _9 => _9.parent, 'access', _10 => _10.node, 'access', _11 => _11.children, 'optionalAccess', _12 => _12.slice, 'call', _13 => _13(context.index)]);
|
1115
1071
|
const idx = nexts.findIndex((n) => !isTextLike(n));
|
1116
1072
|
const nodes = idx > -1 ? nexts.slice(0, idx) : nexts;
|
1117
|
-
const
|
1118
|
-
|
1073
|
+
const values = createTextLikeExpressions(nodes, context);
|
1074
|
+
if (!values.length) return;
|
1119
1075
|
context.dynamic.flags |= 4 /* INSERT */ | 2 /* NON_TEMPLATE */;
|
1120
1076
|
context.registerOperation({
|
1121
1077
|
type: 13 /* CREATE_TEXT_NODE */,
|
1122
|
-
id,
|
1078
|
+
id: context.reference(),
|
1123
1079
|
values,
|
1124
|
-
effect:
|
1080
|
+
effect: false
|
1125
1081
|
});
|
1126
1082
|
}
|
1127
1083
|
function processTextLikeContainer(children, context) {
|
1128
|
-
const values = children
|
1129
|
-
|
1130
|
-
);
|
1084
|
+
const values = createTextLikeExpressions(children, context);
|
1085
|
+
if (!values.length) return;
|
1131
1086
|
const literals = values.map(getLiteralExpressionValue);
|
1132
1087
|
if (literals.every((l) => l != null)) {
|
1133
1088
|
context.childrenTemplate = literals.map((l) => String(l));
|
1134
1089
|
} else {
|
1135
|
-
context.
|
1090
|
+
context.registerOperation({
|
1136
1091
|
type: 4 /* SET_TEXT */,
|
1137
1092
|
element: context.reference(),
|
1138
1093
|
values
|
1139
1094
|
});
|
1140
1095
|
}
|
1141
1096
|
}
|
1142
|
-
function
|
1143
|
-
|
1144
|
-
|
1097
|
+
function createTextLikeExpressions(nodes, context) {
|
1098
|
+
const values = [];
|
1099
|
+
for (const node of nodes) {
|
1100
|
+
if (isEmptyText(node)) continue;
|
1101
|
+
seen.get(context.root).add(node);
|
1102
|
+
values.push(resolveExpression(node, context, true));
|
1103
|
+
}
|
1104
|
+
return values;
|
1145
1105
|
}
|
1146
1106
|
function isAllTextLike(children) {
|
1147
1107
|
return !!children.length && children.every(isTextLike) && // at least one an interpolation
|
1148
1108
|
children.some((n) => n.type === "JSXExpressionContainer");
|
1149
1109
|
}
|
1150
1110
|
function isTextLike(node) {
|
1151
|
-
return node.type === "JSXExpressionContainer" && !(node.expression.type === "ConditionalExpression" || node.expression.type === "LogicalExpression")
|
1152
|
-
}
|
1153
|
-
function processCallExpression(node, context) {
|
1154
|
-
context.dynamic.flags |= 2 /* NON_TEMPLATE */ | 4 /* INSERT */;
|
1155
|
-
const root = context.root === context.parent && context.parent.node.children.length === 1;
|
1156
|
-
const tag = `() => ${context.ir.source.slice(node.start, node.end)}`;
|
1157
|
-
context.registerOperation({
|
1158
|
-
type: 14 /* CREATE_COMPONENT_NODE */,
|
1159
|
-
id: context.reference(),
|
1160
|
-
tag,
|
1161
|
-
props: [],
|
1162
|
-
asset: false,
|
1163
|
-
root,
|
1164
|
-
slots: context.slots,
|
1165
|
-
once: context.inVOnce
|
1166
|
-
});
|
1111
|
+
return node.type === "JSXExpressionContainer" && !(node.expression.type === "ConditionalExpression" || node.expression.type === "LogicalExpression") || node.type === "JSXText";
|
1167
1112
|
}
|
1168
1113
|
|
1169
1114
|
// src/transforms/vBind.ts
|
@@ -1277,7 +1222,7 @@ var transformVSlot = (node, context) => {
|
|
1277
1222
|
(attr) => attr.type === "JSXAttribute" && ["v-slots", "vSlots"].includes(attr.name.name.toString())
|
1278
1223
|
);
|
1279
1224
|
const vSlotsDir = openingElement.attributes[vSlotsIndex];
|
1280
|
-
if (vSlotsDir && _optionalChain([vSlotsDir, 'access',
|
1225
|
+
if (vSlotsDir && _optionalChain([vSlotsDir, 'access', _14 => _14.value, 'optionalAccess', _15 => _15.type]) === "JSXExpressionContainer") {
|
1281
1226
|
node.openingElement.attributes.splice(vSlotsIndex, 1);
|
1282
1227
|
context.slots = [
|
1283
1228
|
{
|
@@ -1366,19 +1311,14 @@ function compile(source, options = {}) {
|
|
1366
1311
|
const onError = options.onError || _compilerdom.defaultOnError;
|
1367
1312
|
const isModuleMode = options.mode === "module";
|
1368
1313
|
const __BROWSER__3 = false;
|
1369
|
-
if (__BROWSER__3) {
|
1370
|
-
|
1371
|
-
onError(_compilerdom.createCompilerError.call(void 0, _compilerdom.ErrorCodes.X_PREFIX_ID_NOT_SUPPORTED));
|
1372
|
-
} else if (isModuleMode) {
|
1373
|
-
onError(_compilerdom.createCompilerError.call(void 0, _compilerdom.ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED));
|
1374
|
-
}
|
1314
|
+
if (__BROWSER__3 && isModuleMode) {
|
1315
|
+
onError(_compilerdom.createCompilerError.call(void 0, _compilerdom.ErrorCodes.X_MODULE_MODE_NOT_SUPPORTED));
|
1375
1316
|
}
|
1376
|
-
const prefixIdentifiers = !__BROWSER__3 && options.prefixIdentifiers === true;
|
1377
1317
|
if (options.scopeId && !isModuleMode) {
|
1378
1318
|
onError(_compilerdom.createCompilerError.call(void 0, _compilerdom.ErrorCodes.X_SCOPE_ID_NOT_SUPPORTED));
|
1379
1319
|
}
|
1380
1320
|
const resolvedOptions = _shared.extend.call(void 0, {}, options, {
|
1381
|
-
prefixIdentifiers,
|
1321
|
+
prefixIdentifiers: false,
|
1382
1322
|
expressionPlugins: options.expressionPlugins || ["jsx"]
|
1383
1323
|
});
|
1384
1324
|
if (!__BROWSER__3 && options.isTS) {
|
@@ -1410,7 +1350,7 @@ function compile(source, options = {}) {
|
|
1410
1350
|
helpers: /* @__PURE__ */ new Set(),
|
1411
1351
|
temps: 0
|
1412
1352
|
};
|
1413
|
-
const [nodeTransforms, directiveTransforms] = getBaseTransformPreset(
|
1353
|
+
const [nodeTransforms, directiveTransforms] = getBaseTransformPreset();
|
1414
1354
|
const ir = transform(
|
1415
1355
|
ast,
|
1416
1356
|
_shared.extend.call(void 0, {}, resolvedOptions, {
|
@@ -1429,7 +1369,7 @@ function compile(source, options = {}) {
|
|
1429
1369
|
);
|
1430
1370
|
return _compilervapor.generate.call(void 0, ir, resolvedOptions);
|
1431
1371
|
}
|
1432
|
-
function getBaseTransformPreset(
|
1372
|
+
function getBaseTransformPreset() {
|
1433
1373
|
return [
|
1434
1374
|
[
|
1435
1375
|
transformTemplateRef,
|
package/dist/index.js
CHANGED
@@ -45,7 +45,9 @@ import {
|
|
45
45
|
createSimpleExpression,
|
46
46
|
isLiteralWhitelisted
|
47
47
|
} from "@vue/compiler-dom";
|
48
|
-
import {
|
48
|
+
import {
|
49
|
+
isLiteral
|
50
|
+
} from "@babel/types";
|
49
51
|
var __BROWSER__ = false;
|
50
52
|
function isConstantExpression(exp) {
|
51
53
|
return isLiteralWhitelisted(exp.content) || isGloballyAllowed(exp.content) || getLiteralExpressionValue(exp) !== null;
|
@@ -62,29 +64,66 @@ function getLiteralExpressionValue(exp) {
|
|
62
64
|
}
|
63
65
|
return exp.isStatic ? exp.content : null;
|
64
66
|
}
|
65
|
-
|
67
|
+
var isConstant = (node) => {
|
68
|
+
if (!node) return false;
|
69
|
+
if (node.type === "Identifier") {
|
70
|
+
return node.name === "undefined";
|
71
|
+
}
|
72
|
+
if (node.type === "ArrayExpression") {
|
73
|
+
const { elements } = node;
|
74
|
+
return elements.every((element) => element && isConstant(element));
|
75
|
+
}
|
76
|
+
if (node.type === "ObjectExpression") {
|
77
|
+
return node.properties.every(
|
78
|
+
(property) => isConstant(property.value)
|
79
|
+
);
|
80
|
+
}
|
81
|
+
if (node.type === "TemplateLiteral" ? !node.expressions.length : isLiteral(node)) {
|
82
|
+
return true;
|
83
|
+
}
|
84
|
+
return false;
|
85
|
+
};
|
86
|
+
function isLiteralExpressionContainer(node) {
|
87
|
+
return node?.type === "JSXExpressionContainer" && node.expression.type !== "JSXEmptyExpression" && isConstant(node.expression);
|
88
|
+
}
|
89
|
+
var EMPTY_TEXT_REGEX = /^\s*[\n\r]\s*$/;
|
90
|
+
var START_EMPTY_TEXT_REGEX = /^\s*[\n\r]/;
|
91
|
+
var END_EMPTY_TEXT_REGEX = /[\n\r]\s*$/;
|
92
|
+
function resolveJSXText(node) {
|
93
|
+
if (EMPTY_TEXT_REGEX.test(`${node.extra?.raw}`)) {
|
94
|
+
return "";
|
95
|
+
}
|
96
|
+
let value = node.value;
|
97
|
+
if (START_EMPTY_TEXT_REGEX.test(value)) {
|
98
|
+
value = value.trimStart();
|
99
|
+
}
|
100
|
+
if (END_EMPTY_TEXT_REGEX.test(value)) {
|
101
|
+
value = value.trimEnd();
|
102
|
+
}
|
103
|
+
return value;
|
104
|
+
}
|
105
|
+
function isEmptyText(node) {
|
106
|
+
return node.type === "JSXText" && EMPTY_TEXT_REGEX.test(`${node.extra?.raw}`) || node.type === "JSXExpressionContainer" && node.expression.type === "JSXEmptyExpression";
|
107
|
+
}
|
108
|
+
function resolveExpression(node, context, effect = false) {
|
66
109
|
const isStatic = !!node && (node.type === "StringLiteral" || node.type === "JSXText" || node.type === "JSXIdentifier");
|
67
|
-
|
110
|
+
let source = !node ? "" : node.type === "JSXIdentifier" ? node.name : node.type === "StringLiteral" ? node.value : node.type === "JSXText" ? resolveJSXText(node) : node.type === "JSXExpressionContainer" ? node.expression.type === "JSXEmptyExpression" ? "" : node.expression.type === "Identifier" ? node.expression.name : context.ir.source.slice(
|
68
111
|
node.expression.start,
|
69
112
|
node.expression.end
|
70
113
|
) : context.ir.source.slice(node.start, node.end);
|
71
114
|
const location = node ? node.loc : null;
|
72
|
-
|
73
|
-
|
74
|
-
ast = parseExpression(` ${source}`, {
|
75
|
-
sourceType: "module",
|
76
|
-
plugins: context.options.expressionPlugins
|
77
|
-
});
|
115
|
+
if (source && !isStatic && effect && !isLiteralExpressionContainer(node)) {
|
116
|
+
source = `() => (${source})`;
|
78
117
|
}
|
79
|
-
return resolveSimpleExpression(source, isStatic, location
|
118
|
+
return resolveSimpleExpression(source, isStatic, location);
|
80
119
|
}
|
81
|
-
function resolveSimpleExpression(source, isStatic, location
|
120
|
+
function resolveSimpleExpression(source, isStatic, location) {
|
82
121
|
const result = createSimpleExpression(
|
83
122
|
source,
|
84
123
|
isStatic,
|
85
124
|
resolveLocation(location, source)
|
86
125
|
);
|
87
|
-
result.ast =
|
126
|
+
result.ast = null;
|
88
127
|
return result;
|
89
128
|
}
|
90
129
|
function resolveLocation(location, context) {
|
@@ -184,9 +223,6 @@ function isJSXComponent(node) {
|
|
184
223
|
return openingElement.name.type === "JSXMemberExpression";
|
185
224
|
}
|
186
225
|
}
|
187
|
-
function isMapCallExpression(node) {
|
188
|
-
return !!node && node.type === "CallExpression" && node.callee.type === "MemberExpression" && node.callee.property.type === "Identifier" && node.callee.property.name === "map";
|
189
|
-
}
|
190
226
|
function findProp(expression, key) {
|
191
227
|
if (expression?.type === "JSXElement") {
|
192
228
|
for (const attr of expression.openingElement.attributes) {
|
@@ -196,18 +232,6 @@ function findProp(expression, key) {
|
|
196
232
|
}
|
197
233
|
}
|
198
234
|
}
|
199
|
-
function getReturnExpression(node) {
|
200
|
-
if (node.type === "FunctionExpression" || node.type === "ArrowFunctionExpression") {
|
201
|
-
if (node.body.type !== "BlockStatement") {
|
202
|
-
return node.body;
|
203
|
-
} else {
|
204
|
-
for (const statement of node.body.body) {
|
205
|
-
if (statement.type === "ReturnStatement" && statement.argument)
|
206
|
-
return statement.argument;
|
207
|
-
}
|
208
|
-
}
|
209
|
-
}
|
210
|
-
}
|
211
235
|
function isJSXElement(node) {
|
212
236
|
return !!node && (node.type === "JSXElement" || node.type === "JSXFragment");
|
213
237
|
}
|
@@ -477,14 +501,7 @@ function extractIdentifiers(ids, node) {
|
|
477
501
|
}
|
478
502
|
|
479
503
|
// src/transforms/transformElement.ts
|
480
|
-
import {
|
481
|
-
camelize,
|
482
|
-
capitalize,
|
483
|
-
extend as extend2,
|
484
|
-
isBuiltInDirective,
|
485
|
-
isVoidTag,
|
486
|
-
makeMap as makeMap2
|
487
|
-
} from "@vue/shared";
|
504
|
+
import { extend as extend2, isBuiltInDirective, isVoidTag, makeMap as makeMap2 } from "@vue/shared";
|
488
505
|
|
489
506
|
// src/html-nesting.ts
|
490
507
|
function isValidHTMLNesting(parent, child) {
|
@@ -687,14 +704,14 @@ var transformElement = (node, context) => {
|
|
687
704
|
function transformComponentElement(tag, propsResult, singleRoot, context) {
|
688
705
|
let asset = true;
|
689
706
|
if (!__BROWSER__2) {
|
690
|
-
const fromSetup =
|
707
|
+
const fromSetup = tag;
|
691
708
|
if (fromSetup) {
|
692
709
|
tag = fromSetup;
|
693
710
|
asset = false;
|
694
711
|
}
|
695
712
|
const dotIndex = tag.indexOf(".");
|
696
713
|
if (dotIndex > 0) {
|
697
|
-
const ns =
|
714
|
+
const ns = tag.slice(0, dotIndex);
|
698
715
|
if (ns) {
|
699
716
|
tag = ns + tag.slice(dotIndex);
|
700
717
|
asset = false;
|
@@ -717,16 +734,6 @@ function transformComponentElement(tag, propsResult, singleRoot, context) {
|
|
717
734
|
});
|
718
735
|
context.slots = [];
|
719
736
|
}
|
720
|
-
function resolveSetupReference(name, context) {
|
721
|
-
const bindings = context.options.bindingMetadata;
|
722
|
-
if (!context.options.prefixIdentifiers) return name;
|
723
|
-
if (!bindings || bindings.__isScriptSetup === false) {
|
724
|
-
return;
|
725
|
-
}
|
726
|
-
const camelName = camelize(name);
|
727
|
-
const PascalName = capitalize(camelName);
|
728
|
-
return bindings[name] ? name : bindings[camelName] ? camelName : bindings[PascalName] ? PascalName : void 0;
|
729
|
-
}
|
730
737
|
function transformNativeElement(tag, propsResult, singleRoot, context) {
|
731
738
|
const { scopeId } = context.options;
|
732
739
|
let template = "";
|
@@ -841,7 +848,7 @@ function transformProp(prop, node, context) {
|
|
841
848
|
return directiveTransform(prop, node, context);
|
842
849
|
}
|
843
850
|
if (!isBuiltInDirective(name)) {
|
844
|
-
const fromSetup = !__BROWSER__2 &&
|
851
|
+
const fromSetup = !__BROWSER__2 && `v-${name}`;
|
845
852
|
if (fromSetup) {
|
846
853
|
name = fromSetup;
|
847
854
|
} else {
|
@@ -886,16 +893,6 @@ function mergePropValues(existing, incoming) {
|
|
886
893
|
var transformChildren = (node, context) => {
|
887
894
|
const isFragment = node.type === 0 /* ROOT */ || node.type === "JSXFragment" || isJSXComponent(node);
|
888
895
|
if (node.type !== "JSXElement" && !isFragment) return;
|
889
|
-
Array.from(node.children).forEach((child, index) => {
|
890
|
-
if (child.type === "JSXText" && !child.value.trim()) {
|
891
|
-
child.value = " ";
|
892
|
-
if (!index) {
|
893
|
-
node.children.splice(0, 1);
|
894
|
-
} else if (index === node.children.length) {
|
895
|
-
node.children.splice(-1, 1);
|
896
|
-
}
|
897
|
-
}
|
898
|
-
});
|
899
896
|
for (const [i, child] of node.children.entries()) {
|
900
897
|
const childContext = context.create(child, i);
|
901
898
|
transformNode(childContext);
|
@@ -1039,46 +1036,6 @@ function processLogicalExpression(node, context) {
|
|
1039
1036
|
];
|
1040
1037
|
}
|
1041
1038
|
|
1042
|
-
// src/transforms/vFor.ts
|
1043
|
-
function processMapCallExpression(node, context) {
|
1044
|
-
const {
|
1045
|
-
callee,
|
1046
|
-
arguments: [argument]
|
1047
|
-
} = node;
|
1048
|
-
if (!(argument.type === "FunctionExpression" || argument.type === "ArrowFunctionExpression") || callee?.type !== "MemberExpression")
|
1049
|
-
return;
|
1050
|
-
context.dynamic.flags |= 2 /* NON_TEMPLATE */ | 4 /* INSERT */;
|
1051
|
-
const id = context.reference();
|
1052
|
-
const [render, exitBlock] = createBranch(argument, context, true);
|
1053
|
-
const source = resolveExpression(callee.object, context);
|
1054
|
-
const value = argument.params[0] && resolveExpression(argument.params[0], context);
|
1055
|
-
const key = argument.params[1] && resolveExpression(argument.params[1], context);
|
1056
|
-
const index = argument.params[2] && resolveExpression(argument.params[2], context);
|
1057
|
-
const returnExpression = getReturnExpression(argument);
|
1058
|
-
const keyProp = findProp(returnExpression, "key");
|
1059
|
-
const keyProperty = keyProp && resolveExpression(keyProp.value, context);
|
1060
|
-
return () => {
|
1061
|
-
exitBlock();
|
1062
|
-
const { parent } = context;
|
1063
|
-
let container;
|
1064
|
-
if (parent && parent.block.node !== parent.node && parent.node.children.length === 1) {
|
1065
|
-
container = parent.reference();
|
1066
|
-
}
|
1067
|
-
context.registerOperation({
|
1068
|
-
type: 19 /* FOR */,
|
1069
|
-
id,
|
1070
|
-
source,
|
1071
|
-
value,
|
1072
|
-
key,
|
1073
|
-
index,
|
1074
|
-
keyProp: keyProperty,
|
1075
|
-
render,
|
1076
|
-
once: context.inVOnce,
|
1077
|
-
container
|
1078
|
-
});
|
1079
|
-
};
|
1080
|
-
}
|
1081
|
-
|
1082
1039
|
// src/transforms/transformText.ts
|
1083
1040
|
var seen = /* @__PURE__ */ new WeakMap();
|
1084
1041
|
var transformText = (node, context) => {
|
@@ -1097,77 +1054,65 @@ var transformText = (node, context) => {
|
|
1097
1054
|
return processConditionalExpression(node.expression, context);
|
1098
1055
|
} else if (node.expression.type === "LogicalExpression") {
|
1099
1056
|
return processLogicalExpression(node.expression, context);
|
1100
|
-
} else if (node.expression.type === "CallExpression") {
|
1101
|
-
if (isMapCallExpression(node.expression)) {
|
1102
|
-
return processMapCallExpression(node.expression, context);
|
1103
|
-
} else {
|
1104
|
-
processCallExpression(node.expression, context);
|
1105
|
-
}
|
1106
1057
|
} else {
|
1107
1058
|
processTextLike(context);
|
1108
1059
|
}
|
1109
1060
|
} else if (node.type === "JSXText") {
|
1110
|
-
|
1061
|
+
const value = resolveJSXText(node);
|
1062
|
+
if (value) {
|
1063
|
+
context.template += value;
|
1064
|
+
} else {
|
1065
|
+
context.dynamic.flags |= 2 /* NON_TEMPLATE */;
|
1066
|
+
}
|
1111
1067
|
}
|
1112
1068
|
};
|
1113
1069
|
function processTextLike(context) {
|
1114
1070
|
const nexts = context.parent.node.children?.slice(context.index);
|
1115
1071
|
const idx = nexts.findIndex((n) => !isTextLike(n));
|
1116
1072
|
const nodes = idx > -1 ? nexts.slice(0, idx) : nexts;
|
1117
|
-
const
|
1118
|
-
|
1073
|
+
const values = createTextLikeExpressions(nodes, context);
|
1074
|
+
if (!values.length) return;
|
1119
1075
|
context.dynamic.flags |= 4 /* INSERT */ | 2 /* NON_TEMPLATE */;
|
1120
1076
|
context.registerOperation({
|
1121
1077
|
type: 13 /* CREATE_TEXT_NODE */,
|
1122
|
-
id,
|
1078
|
+
id: context.reference(),
|
1123
1079
|
values,
|
1124
|
-
effect:
|
1080
|
+
effect: false
|
1125
1081
|
});
|
1126
1082
|
}
|
1127
1083
|
function processTextLikeContainer(children, context) {
|
1128
|
-
const values = children
|
1129
|
-
|
1130
|
-
);
|
1084
|
+
const values = createTextLikeExpressions(children, context);
|
1085
|
+
if (!values.length) return;
|
1131
1086
|
const literals = values.map(getLiteralExpressionValue);
|
1132
1087
|
if (literals.every((l) => l != null)) {
|
1133
1088
|
context.childrenTemplate = literals.map((l) => String(l));
|
1134
1089
|
} else {
|
1135
|
-
context.
|
1090
|
+
context.registerOperation({
|
1136
1091
|
type: 4 /* SET_TEXT */,
|
1137
1092
|
element: context.reference(),
|
1138
1093
|
values
|
1139
1094
|
});
|
1140
1095
|
}
|
1141
1096
|
}
|
1142
|
-
function
|
1143
|
-
|
1144
|
-
|
1097
|
+
function createTextLikeExpressions(nodes, context) {
|
1098
|
+
const values = [];
|
1099
|
+
for (const node of nodes) {
|
1100
|
+
if (isEmptyText(node)) continue;
|
1101
|
+
seen.get(context.root).add(node);
|
1102
|
+
values.push(resolveExpression(node, context, true));
|
1103
|
+
}
|
1104
|
+
return values;
|
1145
1105
|
}
|
1146
1106
|
function isAllTextLike(children) {
|
1147
1107
|
return !!children.length && children.every(isTextLike) && // at least one an interpolation
|
1148
1108
|
children.some((n) => n.type === "JSXExpressionContainer");
|
1149
1109
|
}
|
1150
1110
|
function isTextLike(node) {
|
1151
|
-
return node.type === "JSXExpressionContainer" && !(node.expression.type === "ConditionalExpression" || node.expression.type === "LogicalExpression")
|
1152
|
-
}
|
1153
|
-
function processCallExpression(node, context) {
|
1154
|
-
context.dynamic.flags |= 2 /* NON_TEMPLATE */ | 4 /* INSERT */;
|
1155
|
-
const root = context.root === context.parent && context.parent.node.children.length === 1;
|
1156
|
-
const tag = `() => ${context.ir.source.slice(node.start, node.end)}`;
|
1157
|
-
context.registerOperation({
|
1158
|
-
type: 14 /* CREATE_COMPONENT_NODE */,
|
1159
|
-
id: context.reference(),
|
1160
|
-
tag,
|
1161
|
-
props: [],
|
1162
|
-
asset: false,
|
1163
|
-
root,
|
1164
|
-
slots: context.slots,
|
1165
|
-
once: context.inVOnce
|
1166
|
-
});
|
1111
|
+
return node.type === "JSXExpressionContainer" && !(node.expression.type === "ConditionalExpression" || node.expression.type === "LogicalExpression") || node.type === "JSXText";
|
1167
1112
|
}
|
1168
1113
|
|
1169
1114
|
// src/transforms/vBind.ts
|
1170
|
-
import { camelize
|
1115
|
+
import { camelize, extend as extend3 } from "@vue/shared";
|
1171
1116
|
var transformVBind = (dir, node, context) => {
|
1172
1117
|
const { name, value, loc } = dir;
|
1173
1118
|
if (!loc || name.type === "JSXNamespacedName") return;
|
@@ -1178,7 +1123,7 @@ var transformVBind = (dir, node, context) => {
|
|
1178
1123
|
let camel = false;
|
1179
1124
|
if (modifiers.includes("camel")) {
|
1180
1125
|
if (arg.isStatic) {
|
1181
|
-
arg = extend3({}, arg, { content:
|
1126
|
+
arg = extend3({}, arg, { content: camelize(arg.content) });
|
1182
1127
|
} else {
|
1183
1128
|
camel = true;
|
1184
1129
|
}
|
@@ -1366,19 +1311,14 @@ function compile(source, options = {}) {
|
|
1366
1311
|
const onError = options.onError || defaultOnError2;
|
1367
1312
|
const isModuleMode = options.mode === "module";
|
1368
1313
|
const __BROWSER__3 = false;
|
1369
|
-
if (__BROWSER__3) {
|
1370
|
-
|
1371
|
-
onError(createCompilerError2(ErrorCodes2.X_PREFIX_ID_NOT_SUPPORTED));
|
1372
|
-
} else if (isModuleMode) {
|
1373
|
-
onError(createCompilerError2(ErrorCodes2.X_MODULE_MODE_NOT_SUPPORTED));
|
1374
|
-
}
|
1314
|
+
if (__BROWSER__3 && isModuleMode) {
|
1315
|
+
onError(createCompilerError2(ErrorCodes2.X_MODULE_MODE_NOT_SUPPORTED));
|
1375
1316
|
}
|
1376
|
-
const prefixIdentifiers = !__BROWSER__3 && options.prefixIdentifiers === true;
|
1377
1317
|
if (options.scopeId && !isModuleMode) {
|
1378
1318
|
onError(createCompilerError2(ErrorCodes2.X_SCOPE_ID_NOT_SUPPORTED));
|
1379
1319
|
}
|
1380
1320
|
const resolvedOptions = extend5({}, options, {
|
1381
|
-
prefixIdentifiers,
|
1321
|
+
prefixIdentifiers: false,
|
1382
1322
|
expressionPlugins: options.expressionPlugins || ["jsx"]
|
1383
1323
|
});
|
1384
1324
|
if (!__BROWSER__3 && options.isTS) {
|
@@ -1410,7 +1350,7 @@ function compile(source, options = {}) {
|
|
1410
1350
|
helpers: /* @__PURE__ */ new Set(),
|
1411
1351
|
temps: 0
|
1412
1352
|
};
|
1413
|
-
const [nodeTransforms, directiveTransforms] = getBaseTransformPreset(
|
1353
|
+
const [nodeTransforms, directiveTransforms] = getBaseTransformPreset();
|
1414
1354
|
const ir = transform(
|
1415
1355
|
ast,
|
1416
1356
|
extend5({}, resolvedOptions, {
|
@@ -1429,7 +1369,7 @@ function compile(source, options = {}) {
|
|
1429
1369
|
);
|
1430
1370
|
return generate(ir, resolvedOptions);
|
1431
1371
|
}
|
1432
|
-
function getBaseTransformPreset(
|
1372
|
+
function getBaseTransformPreset() {
|
1433
1373
|
return [
|
1434
1374
|
[
|
1435
1375
|
transformTemplateRef,
|