@builder.io/mitosis 0.0.71 → 0.0.72-1

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.
Files changed (33) hide show
  1. package/dist/src/generators/angular.d.ts +1 -0
  2. package/dist/src/generators/angular.js +2 -0
  3. package/dist/src/generators/context/rsc.d.ts +12 -0
  4. package/dist/src/generators/context/rsc.js +32 -0
  5. package/dist/src/generators/react/generator.d.ts +1 -0
  6. package/dist/src/generators/react/generator.js +54 -31
  7. package/dist/src/generators/react/types.d.ts +6 -1
  8. package/dist/src/generators/rsc.d.ts +4 -0
  9. package/dist/src/generators/rsc.js +90 -0
  10. package/dist/src/generators/vue/blocks.d.ts +8 -0
  11. package/dist/src/generators/vue/blocks.js +294 -0
  12. package/dist/src/generators/vue/compositionApi.d.ts +3 -0
  13. package/dist/src/generators/vue/compositionApi.js +107 -0
  14. package/dist/src/generators/vue/helpers.d.ts +16 -0
  15. package/dist/src/generators/vue/helpers.js +62 -0
  16. package/dist/src/generators/vue/index.d.ts +2 -0
  17. package/dist/src/generators/vue/index.js +18 -0
  18. package/dist/src/generators/vue/optionsApi.d.ts +3 -0
  19. package/dist/src/generators/vue/optionsApi.js +186 -0
  20. package/dist/src/generators/vue/types.d.ts +12 -0
  21. package/dist/src/generators/vue/types.js +2 -0
  22. package/dist/src/generators/vue/vue.d.ts +6 -0
  23. package/dist/src/generators/vue/vue.js +227 -0
  24. package/dist/src/helpers/render-imports.d.ts +6 -3
  25. package/dist/src/helpers/render-imports.js +12 -8
  26. package/dist/src/index.d.ts +2 -0
  27. package/dist/src/index.js +2 -0
  28. package/dist/src/targets.d.ts +1 -0
  29. package/dist/src/targets.js +2 -0
  30. package/dist/tsconfig.build.tsbuildinfo +1 -1
  31. package/package.json +3 -2
  32. package/dist/src/generators/vue.d.ts +0 -23
  33. package/dist/src/generators/vue.js +0 -778
@@ -3,6 +3,7 @@ import { BaseTranspilerOptions, TranspilerGenerator } from '../types/transpiler'
3
3
  export interface ToAngularOptions extends BaseTranspilerOptions {
4
4
  standalone?: boolean;
5
5
  preserveImports?: boolean;
6
+ preserveFileExtensions?: boolean;
6
7
  }
7
8
  interface AngularBlockOptions {
8
9
  contextVars?: string[];
@@ -220,6 +220,7 @@ var componentToAngular = function (userOptions) {
220
220
  var _component = _a.component;
221
221
  var DEFAULT_OPTIONS = {
222
222
  preserveImports: false,
223
+ preserveFileExtensions: false,
223
224
  };
224
225
  var options = __assign(__assign({}, DEFAULT_OPTIONS), userOptions);
225
226
  // Make a copy we can safely mutate, similar to babel's toolchain
@@ -337,6 +338,7 @@ var componentToAngular = function (userOptions) {
337
338
  component: json,
338
339
  target: 'angular',
339
340
  excludeMitosisComponents: !options.standalone && !options.preserveImports,
341
+ preserveFileExtensions: options.preserveFileExtensions,
340
342
  }), Object.entries(componentMetadata)
341
343
  .map(function (_a) {
342
344
  var k = _a[0], v = _a[1];
@@ -0,0 +1,12 @@
1
+ import { MitosisContext } from '../../types/mitosis-context';
2
+ declare type ContextToRscOptions = {
3
+ format?: boolean;
4
+ };
5
+ /**
6
+ * React Server Components currently do not support context, so we use
7
+ * plain objects and prop drilling instead.
8
+ */
9
+ export declare const contextToRsc: (options?: ContextToRscOptions) => ({ context }: {
10
+ context: MitosisContext;
11
+ }) => string;
12
+ export {};
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.contextToRsc = void 0;
4
+ var standalone_1 = require("prettier/standalone");
5
+ var get_state_object_string_1 = require("../../helpers/get-state-object-string");
6
+ /**
7
+ * React Server Components currently do not support context, so we use
8
+ * plain objects and prop drilling instead.
9
+ */
10
+ var contextToRsc = function (options) {
11
+ if (options === void 0) { options = {}; }
12
+ return function (_a) {
13
+ var context = _a.context;
14
+ var str = "\n export default ".concat((0, get_state_object_string_1.stringifyContextValue)(context.value), "\n ");
15
+ if (options.format !== false) {
16
+ try {
17
+ str = (0, standalone_1.format)(str, {
18
+ parser: 'typescript',
19
+ plugins: [
20
+ require('prettier/parser-typescript'), // To support running in browsers
21
+ ],
22
+ });
23
+ }
24
+ catch (err) {
25
+ console.error('Format error for file:', str);
26
+ throw err;
27
+ }
28
+ }
29
+ return str;
30
+ };
31
+ };
32
+ exports.contextToRsc = contextToRsc;
@@ -1,6 +1,7 @@
1
1
  import { TranspilerGenerator } from '../../types/transpiler';
2
2
  import { MitosisNode } from '../../types/mitosis-node';
3
3
  import { ToReactOptions } from './types';
4
+ export declare const contextPropDrillingKey = "_context";
4
5
  export declare const blockToReact: (json: MitosisNode, options: ToReactOptions, parentSlots?: any[]) => string;
5
6
  export declare const componentToPreact: TranspilerGenerator<ToReactOptions>;
6
7
  export declare const componentToReact: TranspilerGenerator<ToReactOptions>;
@@ -18,7 +18,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
18
18
  return (mod && mod.__esModule) ? mod : { "default": mod };
19
19
  };
20
20
  Object.defineProperty(exports, "__esModule", { value: true });
21
- exports.componentToReact = exports.componentToPreact = exports.blockToReact = void 0;
21
+ exports.componentToReact = exports.componentToPreact = exports.blockToReact = exports.contextPropDrillingKey = void 0;
22
22
  var dedent_1 = __importDefault(require("dedent"));
23
23
  var json5_1 = __importDefault(require("json5"));
24
24
  var lodash_1 = require("lodash");
@@ -50,6 +50,7 @@ var state_2 = require("./state");
50
50
  var helpers_2 = require("./helpers");
51
51
  var hash_sum_1 = __importDefault(require("hash-sum"));
52
52
  var for_1 = require("../../helpers/nodes/for");
53
+ exports.contextPropDrillingKey = '_context';
53
54
  var openFrag = function (options) { return getFragment('open', options); };
54
55
  var closeFrag = function (options) { return getFragment('close', options); };
55
56
  function getFragment(type, options) {
@@ -251,37 +252,55 @@ var getRefsString = function (json, refs, options) {
251
252
  }
252
253
  return [hasStateArgument, code];
253
254
  };
254
- function addProviderComponents(json, options) {
255
- for (var key in json.context.set) {
256
- var _a = json.context.set[key], name_1 = _a.name, ref = _a.ref, value = _a.value;
257
- if (value) {
258
- json.children = [
259
- (0, create_mitosis_node_1.createMitosisNode)(__assign({ name: "".concat(name_1, ".Provider"), children: json.children }, (value && {
260
- bindings: {
261
- value: {
262
- code: (0, get_state_object_string_1.stringifyContextValue)(value),
263
- },
264
- },
265
- }))),
266
- ];
255
+ function provideContext(json, options) {
256
+ if (options.contextType === 'prop-drill') {
257
+ var str = '';
258
+ for (var key in json.context.set) {
259
+ var _a = json.context.set[key], name_1 = _a.name, ref = _a.ref, value = _a.value;
260
+ if (value) {
261
+ str += "\n ".concat(exports.contextPropDrillingKey, ".").concat(name_1, " = {\n ...").concat(exports.contextPropDrillingKey, ".").concat(name_1, ",\n ...").concat((0, get_state_object_string_1.stringifyContextValue)(value), "\n }\n ");
262
+ }
263
+ // TODO: support refs. I'm not sure what those are so unclear how to support them
267
264
  }
268
- else if (ref) {
269
- json.children = [
270
- (0, create_mitosis_node_1.createMitosisNode)(__assign({ name: 'Context.Provider', children: json.children }, (ref && {
271
- bindings: {
272
- value: {
273
- code: ref,
265
+ return str;
266
+ }
267
+ else {
268
+ for (var key in json.context.set) {
269
+ var _b = json.context.set[key], name_2 = _b.name, ref = _b.ref, value = _b.value;
270
+ if (value) {
271
+ json.children = [
272
+ (0, create_mitosis_node_1.createMitosisNode)(__assign({ name: "".concat(name_2, ".Provider"), children: json.children }, (value && {
273
+ bindings: {
274
+ value: {
275
+ code: (0, get_state_object_string_1.stringifyContextValue)(value),
276
+ },
277
+ },
278
+ }))),
279
+ ];
280
+ }
281
+ else if (ref) {
282
+ json.children = [
283
+ (0, create_mitosis_node_1.createMitosisNode)(__assign({ name: 'Context.Provider', children: json.children }, (ref && {
284
+ bindings: {
285
+ value: {
286
+ code: ref,
287
+ },
274
288
  },
275
- },
276
- }))),
277
- ];
289
+ }))),
290
+ ];
291
+ }
278
292
  }
279
293
  }
280
294
  }
281
295
  function getContextString(component, options) {
282
296
  var str = '';
283
297
  for (var key in component.context.get) {
284
- str += "\n const ".concat(key, " = useContext(").concat(component.context.get[key].name, ");\n ");
298
+ if (options.contextType === 'prop-drill') {
299
+ str += "\n const ".concat(key, " = ").concat(exports.contextPropDrillingKey, "['").concat(component.context.get[key].name, "'];\n ");
300
+ }
301
+ else {
302
+ str += "\n const ".concat(key, " = useContext(").concat(component.context.get[key].name, ");\n ");
303
+ }
285
304
  }
286
305
  return str;
287
306
  }
@@ -344,7 +363,7 @@ var _componentToReact = function (json, options, isSubComponent) {
344
363
  (0, process_http_requests_1.processHttpRequests)(json);
345
364
  (0, handle_missing_state_1.handleMissingState)(json);
346
365
  (0, process_tag_references_1.processTagReferences)(json);
347
- addProviderComponents(json, options);
366
+ var contextStr = provideContext(json, options);
348
367
  var componentHasStyles = (0, helpers_1.hasCss)(json);
349
368
  if (options.stateType === 'useState') {
350
369
  (0, getters_to_functions_1.gettersToFunctions)(json);
@@ -388,7 +407,7 @@ var _componentToReact = function (json, options, isSubComponent) {
388
407
  if (useStateCode && useStateCode.includes('useState')) {
389
408
  reactLibImports.add('useState');
390
409
  }
391
- if ((0, context_1.hasContext)(json)) {
410
+ if ((0, context_1.hasContext)(json) && options.contextType !== 'prop-drill') {
392
411
  reactLibImports.add('useContext');
393
412
  }
394
413
  if (allRefs.length) {
@@ -409,7 +428,7 @@ var _componentToReact = function (json, options, isSubComponent) {
409
428
  var _o = getRefsString(json, allRefs, options), hasStateArgument = _o[0], refsString = _o[1];
410
429
  var nativeStyles = stylesType === 'react-native' && componentHasStyles && (0, react_native_1.collectReactNativeStyles)(json);
411
430
  var propsArgs = "props: ".concat(json.propsTypeRef || 'any');
412
- var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "", "function ", "(", "", ") {\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n );\n }", "\n\n ", "\n\n ", "\n\n ", "\n "], ["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "", "function ", "(", "", ") {\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n );\n }", "\n\n ", "\n\n ", "\n\n ", "\n "])), options.preact
431
+ var str = (0, dedent_1.default)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "", "function ", "(", "", ") {\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n );\n }", "\n\n ", "\n\n ", "\n\n ", "\n "], ["\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "", "function ", "(", "", ") {\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n ", "\n\n ", "\n ", "\n\n ", "\n\n ", "\n\n return (\n ", "\n ", "\n ", "\n ", "\n );\n }", "\n\n ", "\n\n ", "\n\n ", "\n "])), options.preact
413
432
  ? "\n /** @jsx h */\n import { h, Fragment } from 'preact';\n "
414
433
  : options.type !== 'native'
415
434
  ? "import * as React from 'react';"
@@ -422,7 +441,9 @@ var _componentToReact = function (json, options, isSubComponent) {
422
441
  : '', json.types ? json.types.join('\n') : '', (0, render_imports_1.renderPreComponent)({
423
442
  component: json,
424
443
  target: options.type === 'native' ? 'reactNative' : 'react',
425
- }), isSubComponent ? '' : 'export default ', isForwardRef ? "forwardRef".concat(forwardRefType ? "<".concat(forwardRefType, ">") : '', "(") : '', json.name || 'MyComponent', propsArgs, isForwardRef ? ", ".concat(options.forwardRef) : '', hasStateArgument ? '' : refsString, hasState
444
+ }), isSubComponent ? '' : 'export default ', isForwardRef ? "forwardRef".concat(forwardRefType ? "<".concat(forwardRefType, ">") : '', "(") : '', json.name || 'MyComponent', propsArgs, isForwardRef ? ", ".concat(options.forwardRef) : '', options.contextType === 'prop-drill'
445
+ ? "const ".concat(exports.contextPropDrillingKey, " = { ...props['").concat(exports.contextPropDrillingKey, "'] };")
446
+ : '', hasStateArgument ? '' : refsString, hasState
426
447
  ? stateType === 'mobx'
427
448
  ? "const state = useLocalObservable(() => (".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), "));")
428
449
  : stateType === 'useState'
@@ -430,9 +451,11 @@ var _componentToReact = function (json, options, isSubComponent) {
430
451
  : stateType === 'solid'
431
452
  ? "const state = useMutable(".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ");")
432
453
  : stateType === 'builder'
433
- ? "var state = useBuilderState(".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ");")
434
- : "const state = useLocalProxy(".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ");")
435
- : '', hasStateArgument ? refsString : '', getContextString(json, options), getInitCode(json, options), ((_g = json.hooks.onInit) === null || _g === void 0 ? void 0 : _g.code)
454
+ ? "const state = useBuilderState(".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ");")
455
+ : stateType === 'variables'
456
+ ? "const state = ".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ";")
457
+ : "const state = useLocalProxy(".concat((0, get_state_object_string_1.getStateObjectStringFromComponent)(json), ");")
458
+ : '', hasStateArgument ? refsString : '', getContextString(json, options), getInitCode(json, options), contextStr || '', ((_g = json.hooks.onInit) === null || _g === void 0 ? void 0 : _g.code)
436
459
  ? "\n useEffect(() => {\n ".concat((0, state_2.processHookCode)({
437
460
  str: json.hooks.onInit.code,
438
461
  options: options,
@@ -1,10 +1,15 @@
1
1
  import { BaseTranspilerOptions } from '../../types/transpiler';
2
2
  export interface ToReactOptions extends BaseTranspilerOptions {
3
3
  stylesType?: 'emotion' | 'styled-components' | 'styled-jsx' | 'react-native' | 'style-tag';
4
- stateType?: 'useState' | 'mobx' | 'valtio' | 'solid' | 'builder';
4
+ stateType?: 'useState' | 'mobx' | 'valtio' | 'solid' | 'builder' | 'variables';
5
5
  format?: 'lite' | 'safe';
6
6
  type?: 'dom' | 'native';
7
7
  preact?: boolean;
8
8
  forwardRef?: string;
9
9
  experimental?: any;
10
+ /**
11
+ * For RSC, normal React context is currently not supported, so provide a prop drilling
12
+ * option
13
+ */
14
+ contextType?: 'context' | 'prop-drill';
10
15
  }
@@ -0,0 +1,4 @@
1
+ import { BaseTranspilerOptions, TranspilerGenerator } from '../types/transpiler';
2
+ export interface ToRscOptions extends BaseTranspilerOptions {
3
+ }
4
+ export declare const componentToRsc: TranspilerGenerator<ToRscOptions>;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
14
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
15
+ if (ar || !(i in from)) {
16
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
17
+ ar[i] = from[i];
18
+ }
19
+ }
20
+ return to.concat(ar || Array.prototype.slice.call(from));
21
+ };
22
+ var __importDefault = (this && this.__importDefault) || function (mod) {
23
+ return (mod && mod.__esModule) ? mod : { "default": mod };
24
+ };
25
+ Object.defineProperty(exports, "__esModule", { value: true });
26
+ exports.componentToRsc = void 0;
27
+ var traverse_1 = __importDefault(require("traverse"));
28
+ var fast_clone_1 = require("../helpers/fast-clone");
29
+ var is_mitosis_node_1 = require("../helpers/is-mitosis-node");
30
+ var is_upper_case_1 = require("../helpers/is-upper-case");
31
+ var react_1 = require("./react");
32
+ /**
33
+ * Transform react to be RSC compatible, such as
34
+ * - remove event listeners
35
+ * - remove lifecycle hooks
36
+ * - remove refs
37
+ * - transform context to prop drilling
38
+ */
39
+ var RSC_TRANSFORM_PLUGIN = function () { return ({
40
+ json: {
41
+ pre: function (json) {
42
+ if (json.hooks.onMount) {
43
+ delete json.hooks.onMount;
44
+ }
45
+ if (json.hooks.onUnMount) {
46
+ delete json.hooks.onUnMount;
47
+ }
48
+ if (json.hooks.onUpdate) {
49
+ delete json.hooks.onUpdate;
50
+ }
51
+ if (json.refs) {
52
+ json.refs = {};
53
+ }
54
+ (0, traverse_1.default)(json).forEach(function (node) {
55
+ if ((0, is_mitosis_node_1.isMitosisNode)(node)) {
56
+ if ((0, is_upper_case_1.isUpperCase)(node.name[0])) {
57
+ // Drill context down, aka
58
+ // function (props) { return <Component _context{props._context} /> }
59
+ if (!node.bindings[react_1.contextPropDrillingKey]) {
60
+ node.bindings[react_1.contextPropDrillingKey] = {
61
+ code: react_1.contextPropDrillingKey,
62
+ };
63
+ }
64
+ }
65
+ if (node.bindings.ref) {
66
+ delete node.bindings.ref;
67
+ }
68
+ for (var key in node.bindings) {
69
+ if (key.match(/^on[A-Z]/)) {
70
+ delete node.bindings[key];
71
+ }
72
+ }
73
+ }
74
+ });
75
+ },
76
+ },
77
+ }); };
78
+ var DEFAULT_OPTIONS = {
79
+ plugins: [RSC_TRANSFORM_PLUGIN],
80
+ };
81
+ var componentToRsc = function (_options) {
82
+ if (_options === void 0) { _options = {}; }
83
+ return function (_a) {
84
+ var component = _a.component, path = _a.path;
85
+ var json = (0, fast_clone_1.fastClone)(component);
86
+ var options = __assign(__assign(__assign({}, DEFAULT_OPTIONS), _options), { plugins: __spreadArray(__spreadArray([], (DEFAULT_OPTIONS.plugins || []), true), (_options.plugins || []), true), stylesType: 'style-tag', stateType: 'variables', contextType: 'prop-drill' });
87
+ return (0, react_1.componentToReact)(options)({ component: json, path: path });
88
+ };
89
+ };
90
+ exports.componentToRsc = componentToRsc;
@@ -0,0 +1,8 @@
1
+ import { MitosisNode } from '../../types/mitosis-node';
2
+ import { ToVueOptions } from './types';
3
+ declare type BlockRenderer = (json: MitosisNode, options: ToVueOptions, scope?: Scope) => string;
4
+ interface Scope {
5
+ isRootNode?: boolean;
6
+ }
7
+ export declare const blockToVue: BlockRenderer;
8
+ export {};