@builder.io/mitosis 0.4.0 → 0.4.2
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/src/constants/media-sizes.js +4 -5
- package/dist/src/generators/alpine/generate.js +114 -132
- package/dist/src/generators/alpine/render-mount-hook.js +8 -5
- package/dist/src/generators/alpine/render-update-hooks.js +13 -13
- package/dist/src/generators/angular/helpers.js +19 -9
- package/dist/src/generators/angular/index.js +599 -584
- package/dist/src/generators/builder.js +200 -165
- package/dist/src/generators/context/angular.js +32 -26
- package/dist/src/generators/context/helpers/context-with-symbol-key.js +25 -21
- package/dist/src/generators/context/qwik.js +21 -21
- package/dist/src/generators/context/react.js +22 -22
- package/dist/src/generators/context/rsc.js +20 -22
- package/dist/src/generators/context/solid.js +22 -22
- package/dist/src/generators/context/svelte.js +33 -28
- package/dist/src/generators/context/vue.js +1 -1
- package/dist/src/generators/helpers/context.js +6 -13
- package/dist/src/generators/helpers/functions.js +8 -8
- package/dist/src/generators/helpers/on-mount.js +9 -5
- package/dist/src/generators/helpers/rsc.js +7 -9
- package/dist/src/generators/html.js +995 -534
- package/dist/src/generators/liquid.js +81 -79
- package/dist/src/generators/lit/collect-class-string.js +10 -12
- package/dist/src/generators/lit/generate.js +204 -159
- package/dist/src/generators/marko/generate.js +193 -183
- package/dist/src/generators/minify.js +5 -9
- package/dist/src/generators/mitosis.js +135 -134
- package/dist/src/generators/qwik/component-generator.js +159 -154
- package/dist/src/generators/qwik/component.js +51 -81
- package/dist/src/generators/qwik/directives.js +68 -84
- package/dist/src/generators/qwik/helpers/add-prevent-default.js +6 -7
- package/dist/src/generators/qwik/helpers/convert-method-to-function.js +38 -38
- package/dist/src/generators/qwik/helpers/handlers.js +16 -25
- package/dist/src/generators/qwik/helpers/stable-inject.js +7 -8
- package/dist/src/generators/qwik/helpers/stable-serialize.js +7 -8
- package/dist/src/generators/qwik/helpers/state.js +36 -43
- package/dist/src/generators/qwik/helpers/styles.js +19 -40
- package/dist/src/generators/qwik/jsx.js +63 -75
- package/dist/src/generators/qwik/src-generator.js +134 -167
- package/dist/src/generators/react/blocks.js +122 -127
- package/dist/src/generators/react/generator.js +314 -259
- package/dist/src/generators/react/helpers.js +23 -28
- package/dist/src/generators/react/state.js +34 -51
- package/dist/src/generators/react-native/index.js +72 -84
- package/dist/src/generators/react-native/sanitize-react-native-block-styles.js +14 -26
- package/dist/src/generators/rsc.js +30 -36
- package/dist/src/generators/solid/blocks.js +46 -43
- package/dist/src/generators/solid/helpers/styles.js +11 -11
- package/dist/src/generators/solid/index.js +199 -190
- package/dist/src/generators/solid/state/helpers.js +76 -90
- package/dist/src/generators/solid/state/signals.js +30 -37
- package/dist/src/generators/solid/state/state.js +35 -48
- package/dist/src/generators/solid/state/store.js +40 -46
- package/dist/src/generators/stencil/collect-class-string.js +10 -12
- package/dist/src/generators/stencil/generate.js +158 -145
- package/dist/src/generators/svelte/blocks.js +146 -144
- package/dist/src/generators/svelte/helpers.js +10 -17
- package/dist/src/generators/svelte/svelte.js +321 -287
- package/dist/src/generators/swift-ui.js +162 -101
- package/dist/src/generators/taro.js +32 -36
- package/dist/src/generators/template.js +71 -74
- package/dist/src/generators/vue/blocks.js +116 -107
- package/dist/src/generators/vue/compositionApi.js +67 -50
- package/dist/src/generators/vue/helpers.js +71 -96
- package/dist/src/generators/vue/optionsApi.js +134 -105
- package/dist/src/generators/vue/vue.js +210 -207
- package/dist/src/helpers/babel-transform.js +40 -63
- package/dist/src/helpers/bindings.js +4 -12
- package/dist/src/helpers/camel-case.js +4 -5
- package/dist/src/helpers/capitalize.js +1 -1
- package/dist/src/helpers/component-file-extensions.js +23 -35
- package/dist/src/helpers/create-mitosis-component.js +22 -26
- package/dist/src/helpers/create-mitosis-context.js +5 -12
- package/dist/src/helpers/create-mitosis-node.js +10 -12
- package/dist/src/helpers/dash-case.js +2 -2
- package/dist/src/helpers/dedent.js +18 -22
- package/dist/src/helpers/event-handlers.js +1 -1
- package/dist/src/helpers/fast-clone.js +1 -1
- package/dist/src/helpers/filter-empty-text-nodes.js +2 -2
- package/dist/src/helpers/generic-format.js +7 -8
- package/dist/src/helpers/get-bindings.js +4 -4
- package/dist/src/helpers/get-components-used.js +3 -3
- package/dist/src/helpers/get-components.js +4 -4
- package/dist/src/helpers/get-custom-imports.js +10 -12
- package/dist/src/helpers/get-prop-functions.js +7 -8
- package/dist/src/helpers/get-props-ref.js +8 -8
- package/dist/src/helpers/get-props.js +12 -13
- package/dist/src/helpers/get-refs.js +4 -4
- package/dist/src/helpers/get-state-object-string.js +42 -61
- package/dist/src/helpers/get-state-used.js +7 -8
- package/dist/src/helpers/get-styles.js +6 -6
- package/dist/src/helpers/getters-to-functions.js +8 -12
- package/dist/src/helpers/handle-missing-state.js +3 -3
- package/dist/src/helpers/has-bindings-text.js +6 -6
- package/dist/src/helpers/has-component.js +4 -4
- package/dist/src/helpers/has-props.js +3 -3
- package/dist/src/helpers/has-stateful-dom.js +4 -4
- package/dist/src/helpers/has.js +3 -3
- package/dist/src/helpers/indent.js +2 -3
- package/dist/src/helpers/is-children.js +4 -5
- package/dist/src/helpers/is-component.js +1 -1
- package/dist/src/helpers/is-html-attribute.js +2 -11
- package/dist/src/helpers/is-mitosis-node.js +1 -1
- package/dist/src/helpers/is-root-text-node.js +1 -1
- package/dist/src/helpers/is-upper-case.js +1 -1
- package/dist/src/helpers/is-valid-attribute-name.js +1 -1
- package/dist/src/helpers/json.js +2 -2
- package/dist/src/helpers/map-refs.js +29 -40
- package/dist/src/helpers/merge-options.js +20 -31
- package/dist/src/helpers/mitosis-imports.js +5 -6
- package/dist/src/helpers/nodes/for.js +2 -3
- package/dist/src/helpers/nullable.js +1 -3
- package/dist/src/helpers/on-event.js +32 -36
- package/dist/src/helpers/output.js +4 -5
- package/dist/src/helpers/parse-node.js +13 -5
- package/dist/src/helpers/parsers.js +10 -14
- package/dist/src/helpers/patterns.js +6 -8
- package/dist/src/helpers/plugins/process-code/index.js +114 -120
- package/dist/src/helpers/plugins/process-signals.js +114 -125
- package/dist/src/helpers/plugins/process-target-blocks.js +18 -20
- package/dist/src/helpers/process-http-requests.js +8 -4
- package/dist/src/helpers/remove-surrounding-block.js +2 -2
- package/dist/src/helpers/render-imports.js +99 -111
- package/dist/src/helpers/replace-identifiers.js +40 -49
- package/dist/src/helpers/replace-new-lines-in-strings.js +3 -3
- package/dist/src/helpers/signals/signals.js +27 -35
- package/dist/src/helpers/slots.js +11 -20
- package/dist/src/helpers/state.js +1 -3
- package/dist/src/helpers/strip-meta-properties.js +6 -6
- package/dist/src/helpers/strip-state-and-props-refs.js +21 -32
- package/dist/src/helpers/styles/collect-css.js +39 -44
- package/dist/src/helpers/styles/collect-styled-components.js +30 -28
- package/dist/src/helpers/styles/helpers.js +23 -29
- package/dist/src/helpers/trace-reference-to-module-path.js +3 -4
- package/dist/src/helpers/transform-state-setters.js +16 -19
- package/dist/src/helpers/traverse-nodes.js +2 -2
- package/dist/src/helpers/try-prettier-format.js +3 -3
- package/dist/src/helpers/typescript-project.js +25 -25
- package/dist/src/helpers/typescript.js +1 -3
- package/dist/src/modules/plugins.js +20 -28
- package/dist/src/parsers/angular.js +49 -57
- package/dist/src/parsers/builder/builder.js +448 -343
- package/dist/src/parsers/builder/helpers.js +10 -10
- package/dist/src/parsers/context.js +15 -17
- package/dist/src/parsers/jsx/ast.js +11 -11
- package/dist/src/parsers/jsx/component-types.js +22 -32
- package/dist/src/parsers/jsx/context.js +12 -12
- package/dist/src/parsers/jsx/element-parser.js +43 -54
- package/dist/src/parsers/jsx/exports.js +12 -14
- package/dist/src/parsers/jsx/function-parser.js +108 -119
- package/dist/src/parsers/jsx/helpers.js +11 -13
- package/dist/src/parsers/jsx/hooks/helpers.js +14 -16
- package/dist/src/parsers/jsx/hooks/index.js +34 -44
- package/dist/src/parsers/jsx/hooks/use-target.js +27 -27
- package/dist/src/parsers/jsx/imports.js +7 -17
- package/dist/src/parsers/jsx/jsx.js +92 -91
- package/dist/src/parsers/jsx/props-types.js +8 -10
- package/dist/src/parsers/jsx/props.js +15 -23
- package/dist/src/parsers/jsx/signals.js +28 -29
- package/dist/src/parsers/jsx/state.d.ts +2 -4
- package/dist/src/parsers/jsx/state.js +59 -93
- package/dist/src/parsers/svelte/css/index.js +1 -1
- package/dist/src/parsers/svelte/helpers/bindings.js +18 -19
- package/dist/src/parsers/svelte/helpers/children.js +5 -6
- package/dist/src/parsers/svelte/helpers/expressions.js +7 -9
- package/dist/src/parsers/svelte/helpers/hooks.js +2 -2
- package/dist/src/parsers/svelte/helpers/post-process.js +49 -70
- package/dist/src/parsers/svelte/helpers/string.js +5 -5
- package/dist/src/parsers/svelte/html/actions.js +17 -10
- package/dist/src/parsers/svelte/html/each.js +11 -16
- package/dist/src/parsers/svelte/html/element.js +68 -78
- package/dist/src/parsers/svelte/html/fragment.js +3 -3
- package/dist/src/parsers/svelte/html/if-else.js +11 -18
- package/dist/src/parsers/svelte/html/index.js +14 -14
- package/dist/src/parsers/svelte/html/mustache-tag.js +5 -5
- package/dist/src/parsers/svelte/html/slot.js +5 -5
- package/dist/src/parsers/svelte/html/text.js +7 -14
- package/dist/src/parsers/svelte/index.js +23 -72
- package/dist/src/parsers/svelte/instance/context.js +17 -17
- package/dist/src/parsers/svelte/instance/expressions.js +2 -2
- package/dist/src/parsers/svelte/instance/functions.js +35 -30
- package/dist/src/parsers/svelte/instance/hooks.js +4 -5
- package/dist/src/parsers/svelte/instance/imports.js +11 -21
- package/dist/src/parsers/svelte/instance/index.js +22 -22
- package/dist/src/parsers/svelte/instance/properties.js +11 -23
- package/dist/src/parsers/svelte/instance/reactive.js +11 -11
- package/dist/src/parsers/svelte/instance/references.js +13 -13
- package/dist/src/parsers/svelte/instance/statements.js +3 -3
- package/dist/src/parsers/svelte/module/index.js +14 -26
- package/dist/src/parsers/svelte/typescript/index.js +27 -36
- package/dist/src/plugins/compile-away-builder-components.js +223 -156
- package/dist/src/plugins/compile-away-components.js +13 -21
- package/dist/src/plugins/map-styles.js +7 -7
- package/dist/src/symbols/symbol-processor.js +44 -62
- package/dist/src/targets.js +19 -19
- package/dist/src/types/mitosis-node.js +1 -1
- package/package.json +1 -1
|
@@ -1,34 +1,22 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
3
|
exports.parseProperties = void 0;
|
|
15
4
|
function parseProperties(json, node) {
|
|
16
|
-
var _a;
|
|
17
|
-
|
|
18
|
-
var declarations = (_b = node.declaration) === null || _b === void 0 ? void 0 : _b.declarations;
|
|
5
|
+
var _a, _b;
|
|
6
|
+
const declarations = (_a = node.declaration) === null || _a === void 0 ? void 0 : _a.declarations;
|
|
19
7
|
if (declarations === null || declarations === void 0 ? void 0 : declarations.length) {
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
8
|
+
const declaration = declarations[0];
|
|
9
|
+
const property = declaration.id.name;
|
|
10
|
+
const value = (_b = declaration.init) === null || _b === void 0 ? void 0 : _b.value;
|
|
11
|
+
const propertyObject = {
|
|
12
|
+
[property]: {
|
|
25
13
|
default: value,
|
|
26
14
|
},
|
|
27
|
-
|
|
28
|
-
json.props =
|
|
15
|
+
};
|
|
16
|
+
json.props = { ...json.props, ...propertyObject };
|
|
29
17
|
json.defaultProps = Object.fromEntries(Object.keys(json.props)
|
|
30
|
-
.filter(
|
|
31
|
-
.map(
|
|
18
|
+
.filter((key) => json.props[key].default)
|
|
19
|
+
.map((key) => [key, json.props[key].default]));
|
|
32
20
|
}
|
|
33
21
|
}
|
|
34
22
|
exports.parseProperties = parseProperties;
|
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseReactive = void 0;
|
|
4
|
-
|
|
4
|
+
const astring_1 = require("astring");
|
|
5
5
|
function parseReactive(json, node) {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
const body = node.body;
|
|
7
|
+
const expression = body === null || body === void 0 ? void 0 : body.expression;
|
|
8
8
|
if (!expression) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
json.state[
|
|
12
|
-
code:
|
|
9
|
+
const wrap = node.body.type !== 'BlockStatement';
|
|
10
|
+
const name = `reactive${Object.values(json.state).filter((index) => (index === null || index === void 0 ? void 0 : index.type) === 'getter').length}`;
|
|
11
|
+
json.state[name] = {
|
|
12
|
+
code: `get ${name}() ${wrap ? '{' : ''}${(0, astring_1.generate)(node.body)}${wrap ? '}' : ''}`,
|
|
13
13
|
type: 'getter',
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
16
|
else if (expression.type === 'AssignmentExpression') {
|
|
17
|
-
|
|
18
|
-
json.state[
|
|
19
|
-
code:
|
|
17
|
+
const { name } = expression.left;
|
|
18
|
+
json.state[name] = {
|
|
19
|
+
code: `get ${name}() {\n return ${(0, astring_1.generate)(expression.right)}}`,
|
|
20
20
|
type: 'getter',
|
|
21
21
|
};
|
|
22
22
|
}
|
|
@@ -25,7 +25,7 @@ function parseReactive(json, node) {
|
|
|
25
25
|
json.hooks.onUpdate = json.hooks.onUpdate || [];
|
|
26
26
|
json.hooks.onUpdate.push({
|
|
27
27
|
code: (0, astring_1.generate)(node.body),
|
|
28
|
-
deps:
|
|
28
|
+
deps: `[${expression.arguments.map((arg) => (0, astring_1.generate)(arg))}]`,
|
|
29
29
|
});
|
|
30
30
|
}
|
|
31
31
|
}
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseReferences = exports.getParsedValue = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
const astring_1 = require("astring");
|
|
5
|
+
const lodash_1 = require("lodash");
|
|
6
|
+
const expressions_1 = require("../helpers/expressions");
|
|
7
7
|
function getParsedValue(json, element) {
|
|
8
8
|
switch (element.type) {
|
|
9
9
|
case 'Identifier': {
|
|
@@ -23,23 +23,23 @@ function isPropertyOrStateReference(index) {
|
|
|
23
23
|
}
|
|
24
24
|
function parseReferences(json, node) {
|
|
25
25
|
var _a, _b, _c;
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
const declaration = node.declarations[0];
|
|
27
|
+
let code;
|
|
28
|
+
let type = 'property';
|
|
29
29
|
switch ((_a = declaration === null || declaration === void 0 ? void 0 : declaration.init) === null || _a === void 0 ? void 0 : _a.type) {
|
|
30
30
|
case 'ArrayExpression': {
|
|
31
|
-
code = declaration.init.elements.map(
|
|
31
|
+
code = declaration.init.elements.map((element) => {
|
|
32
32
|
return getParsedValue(json, element);
|
|
33
33
|
});
|
|
34
|
-
if ((0, lodash_1.some)(code,
|
|
35
|
-
|
|
36
|
-
json.state[
|
|
37
|
-
code:
|
|
34
|
+
if ((0, lodash_1.some)(code, (c) => isPropertyOrStateReference(c))) {
|
|
35
|
+
const name = declaration.id.name;
|
|
36
|
+
json.state[name] = {
|
|
37
|
+
code: `get ${name}() { return [${code.map((c) => {
|
|
38
38
|
if (isPropertyOrStateReference(c)) {
|
|
39
39
|
return c;
|
|
40
40
|
}
|
|
41
41
|
return JSON.stringify(c);
|
|
42
|
-
})
|
|
42
|
+
})}]}`,
|
|
43
43
|
type: 'getter',
|
|
44
44
|
};
|
|
45
45
|
return;
|
|
@@ -62,7 +62,7 @@ function parseReferences(json, node) {
|
|
|
62
62
|
}
|
|
63
63
|
json.state[declaration.id.name] = {
|
|
64
64
|
code: JSON.stringify(code),
|
|
65
|
-
type
|
|
65
|
+
type,
|
|
66
66
|
};
|
|
67
67
|
}
|
|
68
68
|
exports.parseReferences = parseReferences;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseStatementAtProgramLevel = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
4
|
+
const astring_1 = require("astring");
|
|
5
|
+
const hooks_1 = require("../helpers/hooks");
|
|
6
6
|
function parseStatementAtProgramLevel(json, node) {
|
|
7
|
-
|
|
7
|
+
const statement = (0, astring_1.generate)(node);
|
|
8
8
|
(0, hooks_1.addToOnInitHook)(json, statement);
|
|
9
9
|
}
|
|
10
10
|
exports.parseStatementAtProgramLevel = parseStatementAtProgramLevel;
|
|
@@ -1,40 +1,28 @@
|
|
|
1
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
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
3
|
exports.parseModule = void 0;
|
|
15
|
-
|
|
16
|
-
|
|
4
|
+
const astring_1 = require("astring");
|
|
5
|
+
const compiler_1 = require("svelte/compiler");
|
|
17
6
|
function handleExportNamedDeclaration(json, node) {
|
|
18
|
-
var _a;
|
|
19
|
-
|
|
20
|
-
var declarations = (_b = node.declaration) === null || _b === void 0 ? void 0 : _b.declarations;
|
|
7
|
+
var _a, _b, _c;
|
|
8
|
+
const declarations = (_a = node.declaration) === null || _a === void 0 ? void 0 : _a.declarations;
|
|
21
9
|
if (declarations === null || declarations === void 0 ? void 0 : declarations.length) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
((
|
|
26
|
-
|
|
27
|
-
|
|
10
|
+
const declaration = declarations[0];
|
|
11
|
+
const property = declaration.id.name;
|
|
12
|
+
const isFunction = ((_b = declaration.init) === null || _b === void 0 ? void 0 : _b.type) === 'FunctionExpression' ||
|
|
13
|
+
((_c = declaration.init) === null || _c === void 0 ? void 0 : _c.type) === 'ArrowFunctionExpression';
|
|
14
|
+
const exportObject = {
|
|
15
|
+
[property]: {
|
|
28
16
|
code: (0, astring_1.generate)(node),
|
|
29
|
-
isFunction
|
|
17
|
+
isFunction,
|
|
30
18
|
},
|
|
31
|
-
|
|
32
|
-
json.exports =
|
|
19
|
+
};
|
|
20
|
+
json.exports = { ...json.exports, ...exportObject };
|
|
33
21
|
}
|
|
34
22
|
}
|
|
35
23
|
function parseModule(ast, json) {
|
|
36
24
|
(0, compiler_1.walk)(ast.module, {
|
|
37
|
-
enter
|
|
25
|
+
enter(node) {
|
|
38
26
|
switch (node.type) {
|
|
39
27
|
case 'ExportNamedDeclaration':
|
|
40
28
|
handleExportNamedDeclaration(json, node);
|
|
@@ -22,35 +22,26 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
22
22
|
__setModuleDefault(result, mod);
|
|
23
23
|
return result;
|
|
24
24
|
};
|
|
25
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
26
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
27
|
-
if (ar || !(i in from)) {
|
|
28
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
29
|
-
ar[i] = from[i];
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
33
|
-
};
|
|
34
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
27
|
};
|
|
37
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
29
|
exports.collectTypes = exports.parseAttributes = exports.createTagRegex = exports.isTypeScriptComponent = void 0;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
30
|
+
const babel = __importStar(require("@babel/core"));
|
|
31
|
+
const generator_1 = __importDefault(require("@babel/generator"));
|
|
32
|
+
const parser = __importStar(require("@babel/parser"));
|
|
33
|
+
const types = __importStar(require("@babel/types"));
|
|
34
|
+
const lodash_1 = require("lodash");
|
|
44
35
|
function isTypeScriptComponent(string_) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
36
|
+
const regex = createTagRegex('script', 'gi');
|
|
37
|
+
const match = regex.exec(string_);
|
|
38
|
+
const { lang } = parseAttributes(((match === null || match === void 0 ? void 0 : match.length) && match[1]) || '');
|
|
48
39
|
return lang === 'ts';
|
|
49
40
|
}
|
|
50
41
|
exports.isTypeScriptComponent = isTypeScriptComponent;
|
|
51
42
|
/** Create a tag matching regexp. */
|
|
52
43
|
function createTagRegex(tagName, flags) {
|
|
53
|
-
return new RegExp(
|
|
44
|
+
return new RegExp(`/<!--[^]*?-->|<${tagName}(\\s[^]*?)?(?:>([^]*?)<\\/${tagName}>|\\/>)`, flags);
|
|
54
45
|
}
|
|
55
46
|
exports.createTagRegex = createTagRegex;
|
|
56
47
|
/** Transform an attribute string into a key-value object */
|
|
@@ -58,8 +49,8 @@ function parseAttributes(attributesString) {
|
|
|
58
49
|
return attributesString
|
|
59
50
|
.split(/\s+/)
|
|
60
51
|
.filter(Boolean)
|
|
61
|
-
.reduce(
|
|
62
|
-
|
|
52
|
+
.reduce((accumulator, attribute) => {
|
|
53
|
+
const [name, value] = attribute.split('=');
|
|
63
54
|
// istanbul ignore next
|
|
64
55
|
accumulator[name] = value ? value.replace(/["']/g, '') : true;
|
|
65
56
|
return accumulator;
|
|
@@ -67,10 +58,10 @@ function parseAttributes(attributesString) {
|
|
|
67
58
|
}
|
|
68
59
|
exports.parseAttributes = parseAttributes;
|
|
69
60
|
function getScriptContent(markup, module) {
|
|
70
|
-
|
|
71
|
-
|
|
61
|
+
const regex = createTagRegex('script', 'gi');
|
|
62
|
+
let match;
|
|
72
63
|
while ((match = regex.exec(markup)) !== null) {
|
|
73
|
-
|
|
64
|
+
const { context } = parseAttributes(match[1] || '');
|
|
74
65
|
if ((context !== 'module' && !module) || (context === 'module' && module)) {
|
|
75
66
|
return match[2];
|
|
76
67
|
}
|
|
@@ -79,26 +70,26 @@ function getScriptContent(markup, module) {
|
|
|
79
70
|
}
|
|
80
71
|
function collectTypes(string_, json) {
|
|
81
72
|
var _a;
|
|
82
|
-
|
|
83
|
-
|
|
73
|
+
const module = getScriptContent(string_, true); // module
|
|
74
|
+
const instance = getScriptContent(string_, false); // instance
|
|
84
75
|
function traverse(script_) {
|
|
85
|
-
|
|
76
|
+
const ast = parser.parse(script_, {
|
|
86
77
|
sourceType: 'module',
|
|
87
78
|
plugins: ['typescript'],
|
|
88
79
|
});
|
|
89
80
|
babel.traverse(ast, {
|
|
90
|
-
enter
|
|
81
|
+
enter(path) {
|
|
91
82
|
var _a;
|
|
92
83
|
// alias or interface (e.g. type Props = { } or interface Props {} )
|
|
93
84
|
if (types.isTSTypeAliasDeclaration(path.node) ||
|
|
94
85
|
types.isTSInterfaceDeclaration(path.node)) {
|
|
95
|
-
json.types =
|
|
86
|
+
json.types = [...((_a = json.types) !== null && _a !== void 0 ? _a : []), (0, generator_1.default)(path.node).code];
|
|
96
87
|
path.skip();
|
|
97
88
|
}
|
|
98
89
|
else if (types.isTSTypeAnnotation(path.node)) {
|
|
99
90
|
// add to actual ref
|
|
100
|
-
|
|
101
|
-
|
|
91
|
+
const reference = (0, generator_1.default)(path.parent).code;
|
|
92
|
+
const type = (0, generator_1.default)(path.node.typeAnnotation).code;
|
|
102
93
|
// add to ref
|
|
103
94
|
if (Object.prototype.hasOwnProperty.call(json.refs, reference)) {
|
|
104
95
|
json.refs[reference].typeParameter = type;
|
|
@@ -115,15 +106,15 @@ function collectTypes(string_, json) {
|
|
|
115
106
|
traverse(module);
|
|
116
107
|
traverse(instance);
|
|
117
108
|
// add prop type declaration to json.types and set the propsTypeRef
|
|
118
|
-
if ((0, lodash_1.some)(json.props,
|
|
119
|
-
|
|
120
|
-
propertyTypeDeclaration += Object.keys((0, lodash_1.pickBy)(json.props,
|
|
121
|
-
.map(
|
|
122
|
-
return
|
|
109
|
+
if ((0, lodash_1.some)(json.props, (property) => !!property.type)) {
|
|
110
|
+
let propertyTypeDeclaration = `type Props = {`;
|
|
111
|
+
propertyTypeDeclaration += Object.keys((0, lodash_1.pickBy)(json.props, (property) => !!property.type))
|
|
112
|
+
.map((key) => {
|
|
113
|
+
return `${key}: ${json.props[key].type};`;
|
|
123
114
|
})
|
|
124
115
|
.join('\n');
|
|
125
116
|
propertyTypeDeclaration += '}';
|
|
126
|
-
json.types =
|
|
117
|
+
json.types = [...((_a = json.types) !== null && _a !== void 0 ? _a : []), propertyTypeDeclaration];
|
|
127
118
|
json.propsTypeRef = 'Props';
|
|
128
119
|
}
|
|
129
120
|
}
|