@builder.io/mitosis 0.0.56-15 → 0.0.56-16

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 (82) hide show
  1. package/dist/src/__tests__/data/styles/class-and-className.raw.jsx +1 -3
  2. package/dist/src/__tests__/data/styles/className.raw.jsx +1 -3
  3. package/dist/src/__tests__/data/{state/state-and-store.raw.d.ts → styles/classState.raw.d.ts} +0 -0
  4. package/dist/src/__tests__/data/styles/classState.raw.jsx +12 -0
  5. package/dist/src/__tests__/qwik/convertMethodToFunction.test.d.ts +1 -0
  6. package/dist/src/__tests__/qwik/convertMethodToFunction.test.js +34 -0
  7. package/dist/src/__tests__/qwik/src-generator.test.d.ts +1 -0
  8. package/dist/src/__tests__/qwik/src-generator.test.js +65 -0
  9. package/dist/src/__tests__/qwik.test.js +4 -16
  10. package/dist/src/__tests__/shared.js +14 -0
  11. package/dist/src/__tests__/styles.test.js +9 -1
  12. package/dist/src/generators/angular.js +2 -2
  13. package/dist/src/generators/context/{react copy.d.ts → qwik.d.ts} +2 -2
  14. package/dist/src/generators/context/{react copy.js → qwik.js} +4 -5
  15. package/dist/src/generators/html.js +4 -4
  16. package/dist/src/generators/liquid.js +2 -2
  17. package/dist/src/generators/qwik/component-generator.d.ts +4 -0
  18. package/dist/src/generators/qwik/component-generator.js +271 -0
  19. package/dist/src/generators/qwik/component.js +2 -1
  20. package/dist/src/generators/qwik/convertMethodToFunction.d.ts +1 -0
  21. package/dist/src/generators/qwik/convertMethodToFunction.js +161 -0
  22. package/dist/src/generators/qwik/directives.js +18 -12
  23. package/dist/src/generators/qwik/index.d.ts +1 -0
  24. package/dist/src/generators/qwik/index.js +3 -1
  25. package/dist/src/generators/qwik/jsx.js +9 -4
  26. package/dist/src/generators/qwik/src-generator.d.ts +18 -6
  27. package/dist/src/generators/qwik/src-generator.js +134 -28
  28. package/dist/src/generators/react-native.d.ts +1 -1
  29. package/dist/src/generators/react.js +6 -4
  30. package/dist/src/generators/solid.js +2 -2
  31. package/dist/src/generators/stencil/generate.js +2 -2
  32. package/dist/src/generators/svelte.js +2 -2
  33. package/dist/src/generators/template.js +2 -2
  34. package/dist/src/generators/vue.js +2 -2
  35. package/dist/src/helpers/styles/collect-css.d.ts +6 -0
  36. package/dist/src/helpers/styles/collect-css.js +87 -0
  37. package/dist/src/helpers/styles/collect-css.test.d.ts +1 -0
  38. package/dist/src/helpers/styles/collect-css.test.js +18 -0
  39. package/dist/src/helpers/styles/collect-styled-components.d.ts +2 -0
  40. package/dist/src/helpers/styles/collect-styled-components.js +61 -0
  41. package/dist/src/helpers/styles/helpers.d.ts +30 -0
  42. package/dist/src/helpers/styles/helpers.js +61 -0
  43. package/dist/src/index.d.ts +1 -0
  44. package/dist/src/index.js +1 -0
  45. package/dist/src/jsx-types.d.ts +1 -0
  46. package/dist/src/targets.d.ts +1 -0
  47. package/dist/src/targets.js +2 -0
  48. package/dist/src/types/config.d.ts +2 -0
  49. package/dist/tsconfig.build.tsbuildinfo +1 -1
  50. package/dist/tsconfig.tsbuildinfo +1 -1
  51. package/package.json +2 -2
  52. package/dist/src/__tests__/data/basic-onUpdate-deps.raw.d.ts +0 -4
  53. package/dist/src/__tests__/data/basic-onUpdate-deps.raw.jsx +0 -25
  54. package/dist/src/__tests__/data/blocks/content-slot.raw.d.ts +0 -6
  55. package/dist/src/__tests__/data/blocks/content-slot.raw.jsx +0 -13
  56. package/dist/src/__tests__/data/blocks/onUpdate.raw copy.d.ts +0 -1
  57. package/dist/src/__tests__/data/blocks/onUpdate.raw copy.jsx +0 -10
  58. package/dist/src/__tests__/data/blocks/slot.raw.d.ts +0 -5
  59. package/dist/src/__tests__/data/blocks/slot.raw.jsx +0 -6
  60. package/dist/src/__tests__/data/blocks/styles.raw.d.ts +0 -1
  61. package/dist/src/__tests__/data/blocks/styles.raw.jsx +0 -10
  62. package/dist/src/__tests__/data/state/component-props-interface.raw.d.ts +0 -6
  63. package/dist/src/__tests__/data/state/component-props-interface.raw.jsx +0 -6
  64. package/dist/src/__tests__/data/state/component-props-type.raw.d.ts +0 -6
  65. package/dist/src/__tests__/data/state/component-props-type.raw.jsx +0 -6
  66. package/dist/src/__tests__/data/state/preserve-typing.raw.d.ts +0 -8
  67. package/dist/src/__tests__/data/state/preserve-typing.raw.jsx +0 -6
  68. package/dist/src/__tests__/data/state/state-and-store.raw.jsx +0 -14
  69. package/dist/src/__tests__/data/types copy/component-props-interface.raw.d.ts +0 -6
  70. package/dist/src/__tests__/data/types copy/component-props-interface.raw.jsx +0 -6
  71. package/dist/src/__tests__/data/types copy/component-props-type.raw.d.ts +0 -6
  72. package/dist/src/__tests__/data/types copy/component-props-type.raw.jsx +0 -6
  73. package/dist/src/__tests__/data/types copy/preserve-typing.raw.d.ts +0 -8
  74. package/dist/src/__tests__/data/types copy/preserve-typing.raw.jsx +0 -6
  75. package/dist/src/__tests__/webcomponent.test copy.d.ts +0 -1
  76. package/dist/src/__tests__/webcomponent.test copy.js +0 -224
  77. package/dist/src/helpers/babel-transform copy.d.ts +0 -8
  78. package/dist/src/helpers/babel-transform copy.js +0 -138
  79. package/dist/src/helpers/collect-styles.d.ts +0 -29
  80. package/dist/src/helpers/collect-styles.js +0 -175
  81. package/dist/src/helpers/render-imports copy.d.ts +0 -4
  82. package/dist/src/helpers/render-imports copy.js +0 -112
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function MyBasicComponent() {
4
- return (<div
5
- // @ts-ignore
6
- className="test" class="test2" css={{
4
+ return (<div className="test" class="test2" css={{
7
5
  padding: '10px',
8
6
  }}>
9
7
  Hello! I can run in React, Vue, Solid, or Liquid!
@@ -1,9 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  function MyBasicComponent() {
4
- return (<div
5
- // @ts-ignore
6
- className="test" css={{
4
+ return (<div className="test" css={{
7
5
  padding: '10px',
8
6
  }}>
9
7
  Hello! I can run in React, Vue, Solid, or Liquid!
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var mitosis_1 = require("@builder.io/mitosis");
4
+ function MyBasicComponent() {
5
+ var _a = (0, mitosis_1.useState)('testClassName'), classState = _a[0], setClassState = _a[1];
6
+ return (<div class={classState} css={{
7
+ padding: '10px',
8
+ }}>
9
+ Hello! I can run in React, Vue, Solid, or Liquid!
10
+ </div>);
11
+ }
12
+ exports.default = MyBasicComponent;
@@ -0,0 +1,34 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var convertMethodToFunction_1 = require("../../generators/qwik/convertMethodToFunction");
4
+ describe('convertMethodToFunction', function () {
5
+ var methodMap = {
6
+ methodA: 'method',
7
+ getterB: 'getter',
8
+ getCssFromFont: 'method',
9
+ };
10
+ var lexicalArgs = ['props', 'state'];
11
+ describe('rewrite', function () {
12
+ test('method', function () {
13
+ expect((0, convertMethodToFunction_1.convertMethodToFunction)('this.methodA(123)', methodMap, lexicalArgs)).toEqual('methodA(props,state,123)');
14
+ });
15
+ test('getter', function () {
16
+ expect((0, convertMethodToFunction_1.convertMethodToFunction)('this.getterB', methodMap, lexicalArgs)).toEqual('getterB(props,state)');
17
+ });
18
+ test('handle comments', function () {
19
+ expect((0, convertMethodToFunction_1.convertMethodToFunction)('//\nreturn this.getterB;', methodMap, lexicalArgs)).toEqual('//\nreturn getterB(props,state);');
20
+ });
21
+ test('braces', function () {
22
+ var code = 'getFontCss({}: {}) { this.getCssFromFont(font) }';
23
+ expect((0, convertMethodToFunction_1.convertMethodToFunction)(code, methodMap, lexicalArgs)).toEqual('getFontCss({}: {}) { getCssFromFont(props,state,font) }');
24
+ });
25
+ });
26
+ describe('string', function () {
27
+ test('should not rewrite string', function () {
28
+ expect((0, convertMethodToFunction_1.convertMethodToFunction)('"this.getterB"', methodMap, lexicalArgs)).toEqual('"this.getterB"');
29
+ });
30
+ test('should rewrite template string', function () {
31
+ expect((0, convertMethodToFunction_1.convertMethodToFunction)('`${this.getterB}this.getterB`', methodMap, lexicalArgs)).toEqual('`${getterB(props,state)}this.getterB`');
32
+ });
33
+ });
34
+ });
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,65 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var src_generator_1 = require("../../generators/qwik/src-generator");
4
+ describe('src-generator', function () {
5
+ describe('isStatement', function () {
6
+ test('is an expression', function () {
7
+ expect((0, src_generator_1.isStatement)('a.b')).toBe(false);
8
+ expect((0, src_generator_1.isStatement)('1?2:"bar"')).toBe(false);
9
+ expect((0, src_generator_1.isStatement)('"var x; return foo + \'\\"\';"')).toBe(false);
10
+ expect((0, src_generator_1.isStatement)('"foo" + `bar\nbaz`')).toBe(false);
11
+ expect((0, src_generator_1.isStatement)('(...)()')).toBe(false);
12
+ });
13
+ test('regression', function () {
14
+ expect((0, src_generator_1.isStatement)('props.attributes?.class || props.attributes?.className')).toBe(false);
15
+ });
16
+ test('is a statement', function () {
17
+ expect((0, src_generator_1.isStatement)('var x; return x;')).toBe(true);
18
+ expect((0, src_generator_1.isStatement)('var x')).toBe(true);
19
+ });
20
+ });
21
+ describe('import', function () {
22
+ var options;
23
+ var src;
24
+ describe('module', function () {
25
+ beforeEach(function () {
26
+ options = {
27
+ isJSX: true,
28
+ isPretty: false,
29
+ isTypeScript: false,
30
+ isModule: true,
31
+ isBuilder: false,
32
+ };
33
+ src = new src_generator_1.SrcBuilder(new src_generator_1.File('test', options, '', ''), options);
34
+ });
35
+ test('import to string', function () {
36
+ src.import('module', [new src_generator_1.Symbol('importName', 'asLocalName')]);
37
+ expect(src.toString()).toEqual('import{importName as asLocalName}from"module";');
38
+ });
39
+ test('import from default', function () {
40
+ src.import('module', [new src_generator_1.Symbol('default', 'asLocalName')]);
41
+ expect(src.toString()).toEqual('import asLocalName from"module";');
42
+ });
43
+ });
44
+ describe('require', function () {
45
+ beforeEach(function () {
46
+ options = {
47
+ isJSX: true,
48
+ isPretty: false,
49
+ isTypeScript: false,
50
+ isModule: false,
51
+ isBuilder: false,
52
+ };
53
+ src = new src_generator_1.SrcBuilder(new src_generator_1.File('test', options, '', ''), options);
54
+ });
55
+ test('import to string', function () {
56
+ src.import('module', [new src_generator_1.Symbol('importName', 'asLocalName')]);
57
+ expect(src.toString()).toEqual('const asLocalName=require("module").importName;');
58
+ });
59
+ test('import from default', function () {
60
+ src.import('module', [new src_generator_1.Symbol('default', 'asLocalName')]);
61
+ expect(src.toString()).toEqual('const asLocalName=require("module");');
62
+ });
63
+ });
64
+ });
65
+ });
@@ -38,12 +38,13 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  var fs_extra_promise_1 = require("fs-extra-promise");
40
40
  var qwik_1 = require("../generators/qwik");
41
+ var component_generator_1 = require("../generators/qwik/component-generator");
41
42
  var index_1 = require("../generators/qwik/index");
42
- var src_generator_1 = require("../generators/qwik/src-generator");
43
43
  var builder_1 = require("../parsers/builder");
44
44
  var jsx_1 = require("../parsers/jsx");
45
45
  var compile_away_builder_components_1 = require("../plugins/compile-away-builder-components");
46
46
  var symbol_processor_1 = require("../symbols/symbol-processor");
47
+ var shared_1 = require("./shared");
47
48
  var todo = require('../../../../examples/todo/src/components/todo.lite');
48
49
  var todos = require('../../../../examples/todo/src/components/todos.lite');
49
50
  var debugFiles = true;
@@ -76,6 +77,7 @@ var debugOutput = function (fileSet) { return __awaiter(void 0, void 0, void 0,
76
77
  });
77
78
  }); };
78
79
  describe('qwik', function () {
80
+ (0, shared_1.runTestsForTarget)('qwik', (0, component_generator_1.componentToQwik)());
79
81
  describe('todo', function () {
80
82
  test('Todo.tsx', function () { return __awaiter(void 0, void 0, void 0, function () {
81
83
  var json, fileSet;
@@ -437,6 +439,7 @@ describe('qwik', function () {
437
439
  isTypeScript: false,
438
440
  isJSX: true,
439
441
  isModule: true,
442
+ isBuilder: true,
440
443
  }, '', ''));
441
444
  });
442
445
  test('should format code', function () {
@@ -449,21 +452,6 @@ describe('qwik', function () {
449
452
  expect(file.toString()).toEqual('<Image></Image>;\n');
450
453
  });
451
454
  });
452
- describe('helper functions', function () {
453
- describe('isStatement', function () {
454
- test('is an expression', function () {
455
- expect((0, src_generator_1.isStatement)('a.b')).toBe(false);
456
- expect((0, src_generator_1.isStatement)('1?2:"bar"')).toBe(false);
457
- expect((0, src_generator_1.isStatement)('"var x; return foo + \'\\"\';"')).toBe(false);
458
- expect((0, src_generator_1.isStatement)('"foo" + `bar\nbaz`')).toBe(false);
459
- expect((0, src_generator_1.isStatement)('(...)()')).toBe(false);
460
- });
461
- test('is a statement', function () {
462
- expect((0, src_generator_1.isStatement)('var x; return x;')).toBe(true);
463
- expect((0, src_generator_1.isStatement)('var x')).toBe(true);
464
- });
465
- });
466
- });
467
455
  });
468
456
  function toObj(fileSet) {
469
457
  var obj = {};
@@ -51,6 +51,9 @@ var propsInterface = require('./data/types/component-props-interface.raw');
51
51
  var preserveTyping = require('./data/types/preserve-typing.raw');
52
52
  var propsDestructure = require('./data/basic-props-destructure.raw');
53
53
  var preserveExportOrLocalStatement = require('./data/basic-preserve-export-or-local-statement.raw');
54
+ var classRaw = require('./data/styles/class.raw');
55
+ var className = require('./data/styles/className.raw');
56
+ var classAndClassName = require('./data/styles/class-and-className.raw');
54
57
  var path = 'test-path';
55
58
  var BASIC_TESTS = {
56
59
  ContentSlotJSX: contentSlotJsx,
@@ -94,6 +97,9 @@ var BASIC_TESTS = {
94
97
  'Image State': imageState,
95
98
  'Basic OnMount Update': basicOnMountUpdate,
96
99
  preserveExportOrLocalStatement: preserveExportOrLocalStatement,
100
+ 'class + css': classRaw,
101
+ 'className + css': className,
102
+ 'class + ClassName + css': classAndClassName,
97
103
  };
98
104
  var MULTI_ON_UPDATE_TESTS = {
99
105
  multipleOnUpdate: multipleOnUpdate,
@@ -185,6 +191,14 @@ var TESTS_FOR_TARGET = {
185
191
  MULTI_ON_UPDATE_TESTS,
186
192
  FORM_BLOCK_TESTS,
187
193
  ],
194
+ qwik: [
195
+ BASIC_TESTS,
196
+ ROOT_SHOW_TESTS,
197
+ FORWARD_REF_TESTS,
198
+ MULTI_ON_UPDATE_TESTS,
199
+ FORM_BLOCK_TESTS,
200
+ FOR_SHOW_TESTS,
201
+ ],
188
202
  };
189
203
  var runTestsForTarget = function (target, generator) {
190
204
  var testsArray = TESTS_FOR_TARGET[target];
@@ -6,7 +6,15 @@ var className = require('./data/styles/className.raw');
6
6
  var classAndClassName = require('./data/styles/class-and-className.raw');
7
7
  describe('Styles', function () {
8
8
  test('class and className are equivalent', function () {
9
- expect((0, jsx_1.parseJsx)(classRaw)).toMatchSnapshot(className);
9
+ expect((0, jsx_1.parseJsx)(classRaw)).toEqual((0, jsx_1.parseJsx)(className));
10
+ });
11
+ test('class and CSS are merged', function () {
12
+ var component = (0, jsx_1.parseJsx)(classRaw);
13
+ expect(component).toMatchSnapshot();
14
+ });
15
+ test('className and CSS are merged', function () {
16
+ var component = (0, jsx_1.parseJsx)(className);
17
+ expect(component).toMatchSnapshot();
10
18
  });
11
19
  test('class and className are merged', function () {
12
20
  var component = (0, jsx_1.parseJsx)(classAndClassName);
@@ -19,7 +19,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
19
19
  exports.componentToAngular = exports.blockToAngular = void 0;
20
20
  var dedent_1 = __importDefault(require("dedent"));
21
21
  var standalone_1 = require("prettier/standalone");
22
- var collect_styles_1 = require("../helpers/collect-styles");
22
+ var collect_css_1 = require("../helpers/styles/collect-css");
23
23
  var fast_clone_1 = require("../helpers/fast-clone");
24
24
  var get_refs_1 = require("../helpers/get-refs");
25
25
  var get_state_object_string_1 = require("../helpers/get-state-object-string");
@@ -246,7 +246,7 @@ var componentToAngular = function (options) {
246
246
  if (options.plugins) {
247
247
  json = (0, plugins_1.runPostJsonPlugins)(json, options.plugins);
248
248
  }
249
- var css = (0, collect_styles_1.collectCss)(json);
249
+ var css = (0, collect_css_1.collectCss)(json);
250
250
  if (options.prettier !== false) {
251
251
  css = tryFormat(css, 'css');
252
252
  }
@@ -1,8 +1,8 @@
1
1
  import { MitosisContext } from '../../types/mitosis-context';
2
- declare type ContextToReactOptions = {
2
+ declare type ContextToQwikOptions = {
3
3
  format?: boolean;
4
4
  };
5
- export declare const contextToReact: (options?: ContextToReactOptions) => ({ context }: {
5
+ export declare const contextToQwik: (options?: ContextToQwikOptions) => ({ context }: {
6
6
  context: MitosisContext;
7
7
  }) => string;
8
8
  export {};
@@ -1,13 +1,12 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.contextToReact = void 0;
3
+ exports.contextToQwik = void 0;
4
4
  var standalone_1 = require("prettier/standalone");
5
- var get_state_object_string_1 = require("../../helpers/get-state-object-string");
6
- var contextToReact = function (options) {
5
+ var contextToQwik = function (options) {
7
6
  if (options === void 0) { options = {}; }
8
7
  return function (_a) {
9
8
  var context = _a.context;
10
- var str = "\n import { createContext } from 'react';\n\n export default createContext(".concat((0, get_state_object_string_1.getMemberObjectString)(context.value), ")\n ");
9
+ var str = "\n import { createContext } from '@builder.io/qwik';\n\n export default createContext(\"".concat(context.name, "\")\n ");
11
10
  if (options.format !== false) {
12
11
  try {
13
12
  str = (0, standalone_1.format)(str, {
@@ -25,4 +24,4 @@ var contextToReact = function (options) {
25
24
  return str;
26
25
  };
27
26
  };
28
- exports.contextToReact = contextToReact;
27
+ exports.contextToQwik = contextToQwik;
@@ -34,7 +34,7 @@ var get_refs_1 = require("../helpers/get-refs");
34
34
  var map_refs_1 = require("../helpers/map-refs");
35
35
  var traverse_1 = __importDefault(require("traverse"));
36
36
  var babel_transform_1 = require("../helpers/babel-transform");
37
- var collect_styles_1 = require("../helpers/collect-styles");
37
+ var collect_css_1 = require("../helpers/styles/collect-css");
38
38
  var dash_case_1 = require("../helpers/dash-case");
39
39
  var fast_clone_1 = require("../helpers/fast-clone");
40
40
  var get_state_object_string_1 = require("../helpers/get-state-object-string");
@@ -411,7 +411,7 @@ var componentToHtml = function (options) {
411
411
  if (options.plugins) {
412
412
  json = (0, plugins_1.runPostJsonPlugins)(json, options.plugins);
413
413
  }
414
- var css = (0, collect_styles_1.collectCss)(json, {
414
+ var css = (0, collect_css_1.collectCss)(json, {
415
415
  prefix: options.prefix,
416
416
  });
417
417
  var str = json.children.map(function (item) { return blockToHtml(item, useOptions); }).join('\n');
@@ -532,12 +532,12 @@ var componentToCustomElement = function (options) {
532
532
  var css = '';
533
533
  if ((_c = useOptions === null || useOptions === void 0 ? void 0 : useOptions.experimental) === null || _c === void 0 ? void 0 : _c.css) {
534
534
  css = (_d = useOptions === null || useOptions === void 0 ? void 0 : useOptions.experimental) === null || _d === void 0 ? void 0 : _d.css(json, useOptions, {
535
- collectCss: collect_styles_1.collectCss,
535
+ collectCss: collect_css_1.collectCss,
536
536
  prefix: options.prefix,
537
537
  });
538
538
  }
539
539
  else {
540
- css = (0, collect_styles_1.collectCss)(json, {
540
+ css = (0, collect_css_1.collectCss)(json, {
541
541
  prefix: options.prefix,
542
542
  });
543
543
  }
@@ -2,7 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.componentToLiquid = exports.isValidLiquidBinding = void 0;
4
4
  var standalone_1 = require("prettier/standalone");
5
- var collect_styles_1 = require("../helpers/collect-styles");
5
+ var collect_css_1 = require("../helpers/styles/collect-css");
6
6
  var fast_clone_1 = require("../helpers/fast-clone");
7
7
  var strip_state_and_props_refs_1 = require("../helpers/strip-state-and-props-refs");
8
8
  var jsx_1 = require("../parsers/jsx");
@@ -114,7 +114,7 @@ var componentToLiquid = function (options) {
114
114
  if (options.plugins) {
115
115
  json = (0, plugins_1.runPreJsonPlugins)(json, options.plugins);
116
116
  }
117
- var css = (0, collect_styles_1.collectCss)(json);
117
+ var css = (0, collect_css_1.collectCss)(json);
118
118
  (0, strip_meta_properties_1.stripMetaProperties)(json);
119
119
  if (options.plugins) {
120
120
  json = (0, plugins_1.runPostJsonPlugins)(json, options.plugins);
@@ -0,0 +1,4 @@
1
+ import { BaseTranspilerOptions, Transpiler } from '../../types/transpiler';
2
+ export interface ToQwikOptions extends BaseTranspilerOptions {
3
+ }
4
+ export declare const componentToQwik: (userOptions?: ToQwikOptions) => Transpiler;
@@ -0,0 +1,271 @@
1
+ "use strict";
2
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
3
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
4
+ if (ar || !(i in from)) {
5
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
6
+ ar[i] = from[i];
7
+ }
8
+ }
9
+ return to.concat(ar || Array.prototype.slice.call(from));
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.componentToQwik = void 0;
13
+ var collect_css_1 = require("../../helpers/styles/collect-css");
14
+ var convertMethodToFunction_1 = require("./convertMethodToFunction");
15
+ var jsx_1 = require("./jsx");
16
+ var src_generator_1 = require("./src-generator");
17
+ var babel_transform_1 = require("../../helpers/babel-transform");
18
+ Error.stackTraceLimit = 9999;
19
+ var componentToQwik = function (userOptions) {
20
+ if (userOptions === void 0) { userOptions = {}; }
21
+ return function (_a) {
22
+ var component = _a.component, path = _a.path;
23
+ var file = new src_generator_1.File(component.name + '.js', {
24
+ isPretty: true,
25
+ isJSX: true,
26
+ isTypeScript: false,
27
+ isModule: true,
28
+ isBuilder: false,
29
+ }, '@builder.io/qwik', '');
30
+ try {
31
+ // file.exportConst('COMPONENT', JSON.stringify(component, null, 2));
32
+ emitImports(file, component);
33
+ emitTypes(file, component);
34
+ var state_1 = emitStateMethodsAndRewriteBindings(file, component);
35
+ var hasState_1 = Boolean(Object.keys(component.state).length);
36
+ var css_1 = null;
37
+ file.src.const(component.name, (0, src_generator_1.invoke)(file.import(file.qwikModule, 'component$'), [
38
+ (0, src_generator_1.arrowFnBlock)(['props'], [
39
+ function () {
40
+ css_1 = emitUseStyles(file, component);
41
+ hasState_1 && emitUseStore(file, state_1);
42
+ emitUseRef(file, component);
43
+ emitUseContextProvider(file, component);
44
+ emitUseContext(file, component);
45
+ emitUseMount(file, component);
46
+ emitUseWatch(file, component);
47
+ emitUseCleanup(file, component);
48
+ emitJSX(file, component);
49
+ },
50
+ ], [component.propsTypeRef || 'any']),
51
+ ]));
52
+ file.exportDefault(component.name);
53
+ emitStyles(file, css_1);
54
+ return '// GENERATED BY MITOSIS\n\n' + file.toString();
55
+ }
56
+ catch (e) {
57
+ console.error(e);
58
+ return e.stack || String(e);
59
+ }
60
+ };
61
+ };
62
+ exports.componentToQwik = componentToQwik;
63
+ function emitUseMount(file, component) {
64
+ if (component.hooks.onMount) {
65
+ // This is called useMount, but in practice it is used as
66
+ // useClientEffect. Not sure if this is correct, but for now.
67
+ var code = component.hooks.onMount.code;
68
+ file.src.emit(file.import(file.qwikModule, 'useClientEffect$').localName, '(()=>{', code, '});');
69
+ }
70
+ }
71
+ function emitUseWatch(file, component) {
72
+ if (component.hooks.onUpdate) {
73
+ component.hooks.onUpdate.forEach(function (onUpdate) {
74
+ file.src.emit(file.import(file.qwikModule, 'useWatch$').localName, '((track)=>{');
75
+ emitTrackExpressions(file.src, onUpdate.deps);
76
+ file.src.emit(convertTypeScriptToJS(onUpdate.code), '});');
77
+ });
78
+ }
79
+ }
80
+ function emitTrackExpressions(src, deps) {
81
+ if (deps && deps.startsWith('[') && deps.endsWith(']')) {
82
+ var dependencies = deps.substring(1, deps.length - 1).split(',');
83
+ dependencies.forEach(function (dep) {
84
+ var lastDotIdx = dep.lastIndexOf('.');
85
+ var objExp = dep.substring(0, lastDotIdx).replace(/\?$/, '');
86
+ var objProp = dep.substring(lastDotIdx + 1);
87
+ src.emit(objExp, '&&track(', objExp, ',"', objProp, '");');
88
+ });
89
+ }
90
+ }
91
+ function emitUseCleanup(file, component) {
92
+ if (component.hooks.onUnMount) {
93
+ var code = component.hooks.onUnMount.code;
94
+ file.src.emit(file.import(file.qwikModule, 'useCleanup$').localName, '(()=>{', code, '});');
95
+ }
96
+ }
97
+ function emitJSX(file, component) {
98
+ var directives = new Map();
99
+ var handlers = new Map();
100
+ var styles = new Map();
101
+ var parentSymbolBindings = {};
102
+ file.src.emit('return ', (0, jsx_1.renderJSXNodes)(file, directives, handlers, component.children, styles, parentSymbolBindings));
103
+ }
104
+ function emitUseContextProvider(file, component) {
105
+ Object.keys(component.context.set).forEach(function (ctxKey) {
106
+ var context = component.context.set[ctxKey];
107
+ file.src.emit(file.import(file.qwikModule, 'useContextProvider').localName, '(', context.name, ',', file.import(file.qwikModule, 'useStore').localName, '({');
108
+ context.value &&
109
+ Object.keys(context.value).forEach(function (prop) {
110
+ var propValue = context.value[prop];
111
+ file.src.emit(prop, ':');
112
+ if (isGetter(propValue)) {
113
+ var methodMap = stateToMethodOrGetter(component.state);
114
+ var code = (0, convertMethodToFunction_1.convertMethodToFunction)(extractGetterBody(propValue), methodMap, getLexicalScopeVars(component));
115
+ file.src.emit('(()=>{', code, '})(),');
116
+ }
117
+ else if (typeof propValue == 'function') {
118
+ throw new Error('Qwik: Functions are not supported in context');
119
+ }
120
+ else {
121
+ file.src.emit(JSON.stringify(propValue));
122
+ }
123
+ });
124
+ file.src.emit('})', ');');
125
+ });
126
+ }
127
+ function emitUseContext(file, component) {
128
+ Object.keys(component.context.get).forEach(function (ctxKey) {
129
+ var context = component.context.get[ctxKey];
130
+ file.src.emit('const ', ctxKey, '=', file.import(file.qwikModule, 'useContext').localName, '(', context.name, ');');
131
+ });
132
+ }
133
+ function emitUseRef(file, component) {
134
+ Object.keys(component.refs).forEach(function (refKey) {
135
+ file.src.emit("const ", refKey, '=', file.import(file.qwikModule, 'useRef').localName, '();');
136
+ });
137
+ }
138
+ function emitUseStyles(file, component) {
139
+ var css = (0, collect_css_1.collectCss)(component);
140
+ if (css) {
141
+ file.src.emit(file.import(file.qwikModule, 'useScopedStyles$').localName, '(STYLES);');
142
+ }
143
+ return css;
144
+ }
145
+ function emitStyles(file, css) {
146
+ if (css) {
147
+ file.exportConst('STYLES', '`' + css.replace(/`/g, '\\`') + '`');
148
+ }
149
+ }
150
+ function emitUseStore(file, stateInit) {
151
+ var state = stateInit[0];
152
+ file.src.emit('const state=', file.import(file.qwikModule, 'useStore').localName, '(');
153
+ if (stateInit.length == 1) {
154
+ file.src.emit(JSON.stringify(state));
155
+ }
156
+ else {
157
+ file.src.emit('()=>{const state=', JSON.stringify(state), ';');
158
+ file.src.emitList(stateInit.slice(1), ';');
159
+ file.src.emit(';return state;');
160
+ file.src.emit('}');
161
+ }
162
+ file.src.emit(');');
163
+ }
164
+ function emitTypes(file, component) {
165
+ var _a, _b;
166
+ if (file.options.isTypeScript) {
167
+ (_a = component.types) === null || _a === void 0 ? void 0 : _a.forEach(function (t) { return file.src.emit(t, '\n'); });
168
+ (_b = component.interfaces) === null || _b === void 0 ? void 0 : _b.forEach(function (i) { return file.src.emit(i); });
169
+ }
170
+ }
171
+ function emitStateMethodsAndRewriteBindings(file, component) {
172
+ var _a;
173
+ var lexicalArgs = getLexicalScopeVars(component);
174
+ var state = emitStateMethods(file, component.state, lexicalArgs);
175
+ var methodMap = stateToMethodOrGetter(component.state);
176
+ (_a = component.children) === null || _a === void 0 ? void 0 : _a.forEach(function (node) { return rewriteBindings(node, methodMap, lexicalArgs); });
177
+ return state;
178
+ }
179
+ function getLexicalScopeVars(component) {
180
+ return __spreadArray(__spreadArray(['props', 'state'], Object.keys(component.refs), true), Object.keys(component.context.get), true);
181
+ }
182
+ function emitImports(file, component) {
183
+ var _a;
184
+ // <SELF> is used for self-referencing within the file.
185
+ file.import('<SELF>', component.name);
186
+ (_a = component.imports) === null || _a === void 0 ? void 0 : _a.forEach(function (i) {
187
+ Object.keys(i.imports).forEach(function (key) {
188
+ var keyValue = i.imports[key];
189
+ file.import(i.path.replace('.lite', '').replace('.tsx', ''), keyValue, key);
190
+ });
191
+ });
192
+ }
193
+ var CODE_PREFIX = '@builder.io/mitosis/';
194
+ var FUNCTION = CODE_PREFIX + 'function:';
195
+ var METHOD = CODE_PREFIX + 'method:';
196
+ var GETTER = CODE_PREFIX + 'method:get ';
197
+ function emitStateMethods(file, componentState, lexicalArgs) {
198
+ var state = {};
199
+ var stateInit = [state];
200
+ var methodMap = stateToMethodOrGetter(componentState);
201
+ Object.keys(componentState).forEach(function (key) {
202
+ var code = componentState[key];
203
+ if (isCode(code)) {
204
+ var prefixIdx = code.indexOf(':') + 1;
205
+ if (isGetter(code)) {
206
+ prefixIdx += 'get '.length;
207
+ }
208
+ else if (isFunction(code)) {
209
+ prefixIdx += 'function '.length;
210
+ }
211
+ code = code.substring(prefixIdx);
212
+ code = (0, convertMethodToFunction_1.convertMethodToFunction)(code, methodMap, lexicalArgs).replace('(', "(".concat(lexicalArgs.join(','), ","));
213
+ var functionName = code.split(/\(/)[0];
214
+ stateInit.push("state.".concat(key, "=").concat(functionName, "(state, props)"));
215
+ if (!file.options.isTypeScript) {
216
+ // Erase type information
217
+ code = convertTypeScriptToJS(code);
218
+ }
219
+ file.src.emit('function ', code);
220
+ }
221
+ else {
222
+ state[key] = code;
223
+ }
224
+ });
225
+ return stateInit;
226
+ }
227
+ function convertTypeScriptToJS(code) {
228
+ // HACK, proper implementation should use Babel
229
+ // return code.replace(/(\w+):\s+[\w\[\]"']+/gm, (_, ident) => ident);
230
+ return (0, babel_transform_1.babelTransformExpression)(code, {});
231
+ }
232
+ function isGetter(code) {
233
+ return typeof code === 'string' && code.startsWith(GETTER);
234
+ }
235
+ function isCode(code) {
236
+ return typeof code === 'string' && code.startsWith(CODE_PREFIX);
237
+ }
238
+ function isFunction(code) {
239
+ return typeof code === 'string' && code.startsWith(FUNCTION);
240
+ }
241
+ function extractGetterBody(code) {
242
+ var start = code.indexOf('{');
243
+ var end = code.lastIndexOf('}');
244
+ return code.substring(start + 1, end).trim();
245
+ }
246
+ function stateToMethodOrGetter(state) {
247
+ var methodMap = {};
248
+ Object.keys(state).forEach(function (key) {
249
+ var code = state[key];
250
+ if (typeof code == 'string' && code.startsWith(METHOD)) {
251
+ methodMap[key] = code.startsWith(GETTER) ? 'getter' : 'method';
252
+ }
253
+ });
254
+ return methodMap;
255
+ }
256
+ function rewriteBindings(node, methodMap, lexicalArgs) {
257
+ var _a;
258
+ Object.keys(node.bindings).forEach(function (key) {
259
+ var binding = node.bindings[key];
260
+ if (binding === null || binding === void 0 ? void 0 : binding.code) {
261
+ binding.code = (0, convertMethodToFunction_1.convertMethodToFunction)(binding.code, methodMap, lexicalArgs);
262
+ }
263
+ if (key.startsWith('on') && (binding === null || binding === void 0 ? void 0 : binding.code)) {
264
+ var args = (binding === null || binding === void 0 ? void 0 : binding.arguments) || [];
265
+ binding.code = "(".concat(args.join(','), ") => ").concat(binding.code);
266
+ delete node.bindings[key];
267
+ node.bindings[key + '$'] = binding;
268
+ }
269
+ });
270
+ (_a = node.children) === null || _a === void 0 ? void 0 : _a.forEach(function (child) { return rewriteBindings(child, methodMap, lexicalArgs); });
271
+ }
@@ -35,6 +35,7 @@ function createFileSet(options) {
35
35
  isModule: opts.output != 'cjs',
36
36
  isTypeScript: opts.output == 'ts',
37
37
  isJSX: opts.jsx,
38
+ isBuilder: true,
38
39
  };
39
40
  var fileSet = {
40
41
  high: new src_generator_1.File('high.' + extension, srcOptions, opts.qwikLib, opts.qrlPrefix),
@@ -98,7 +99,7 @@ function generateStyles(fromFile, dstFile, symbol, scoped) {
98
99
  }
99
100
  function renderUseLexicalScope(file) {
100
101
  return function () {
101
- return this.emit('const state=', file.import(file.qwikModule, 'useLexicalScope').name, '()[0]');
102
+ return this.emit('const state=', file.import(file.qwikModule, 'useLexicalScope').localName, '()[0]');
102
103
  };
103
104
  }
104
105
  exports.renderUseLexicalScope = renderUseLexicalScope;
@@ -0,0 +1 @@
1
+ export declare function convertMethodToFunction(code: string, methods: Record<string, 'method' | 'getter'>, lexicalArgs: string[]): string;