@vue-jsx-vapor/compiler 0.0.2 → 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.js CHANGED
@@ -45,7 +45,9 @@ import {
45
45
  createSimpleExpression,
46
46
  isLiteralWhitelisted
47
47
  } from "@vue/compiler-dom";
48
- import { parseExpression } from "@babel/parser";
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
- function resolveExpression(node, context) {
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
- const source = !node ? "" : node.type === "JSXIdentifier" ? node.name : isStatic ? node.value : node.type === "JSXExpressionContainer" ? node.expression.type === "Identifier" ? node.expression.name : context.ir.source.slice(
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
- let ast = false;
73
- if (!isStatic && context.options.prefixIdentifiers) {
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, ast);
118
+ return resolveSimpleExpression(source, isStatic, location);
80
119
  }
81
- function resolveSimpleExpression(source, isStatic, location, ast) {
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 = ast ?? null;
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 = resolveSetupReference(tag, context);
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 = resolveSetupReference(tag.slice(0, dotIndex), context);
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 && resolveSetupReference(`v-${name}`, context);
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
- context.template += node.value;
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 id = context.reference();
1118
- const values = nodes.map((node) => createTextLikeExpression(node, context));
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: !values.every(isConstantExpression) && !context.inVOnce
1080
+ effect: false
1125
1081
  });
1126
1082
  }
1127
1083
  function processTextLikeContainer(children, context) {
1128
- const values = children.map(
1129
- (child) => createTextLikeExpression(child, context)
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.registerEffect(values, {
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 createTextLikeExpression(node, context) {
1143
- seen.get(context.root).add(node);
1144
- return resolveExpression(node, context);
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") && node.expression.type !== "CallExpression" || node.type === "JSXText";
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 as camelize2, extend as extend3 } from "@vue/shared";
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: camelize2(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
- if (options.prefixIdentifiers === true) {
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(prefixIdentifiers);
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(prefixIdentifiers) {
1372
+ function getBaseTransformPreset() {
1433
1373
  return [
1434
1374
  [
1435
1375
  transformTemplateRef,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vue-jsx-vapor/compiler",
3
- "version": "0.0.2",
3
+ "version": "0.1.0",
4
4
  "description": "Vue JSX Vapor Compiler",
5
5
  "type": "module",
6
6
  "keywords": [