@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,8 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
|
|
3
|
-
if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
|
|
4
|
-
return cooked;
|
|
5
|
-
};
|
|
6
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
3
|
if (k2 === undefined) k2 = k;
|
|
8
4
|
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
@@ -26,86 +22,78 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
26
22
|
__setModuleDefault(result, mod);
|
|
27
23
|
return result;
|
|
28
24
|
};
|
|
29
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
30
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
31
|
-
if (ar || !(i in from)) {
|
|
32
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
33
|
-
ar[i] = from[i];
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
37
|
-
};
|
|
38
25
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
39
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
40
27
|
};
|
|
41
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
29
|
exports.componentToSvelte = void 0;
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
30
|
+
const babel_transform_1 = require("../../helpers/babel-transform");
|
|
31
|
+
const dedent_1 = require("../../helpers/dedent");
|
|
32
|
+
const fast_clone_1 = require("../../helpers/fast-clone");
|
|
33
|
+
const get_props_1 = require("../../helpers/get-props");
|
|
34
|
+
const get_refs_1 = require("../../helpers/get-refs");
|
|
35
|
+
const get_state_object_string_1 = require("../../helpers/get-state-object-string");
|
|
36
|
+
const getters_to_functions_1 = require("../../helpers/getters-to-functions");
|
|
37
|
+
const is_mitosis_node_1 = require("../../helpers/is-mitosis-node");
|
|
38
|
+
const merge_options_1 = require("../../helpers/merge-options");
|
|
39
|
+
const on_event_1 = require("../../helpers/on-event");
|
|
40
|
+
const patterns_1 = require("../../helpers/patterns");
|
|
41
|
+
const process_code_1 = require("../../helpers/plugins/process-code");
|
|
42
|
+
const render_imports_1 = require("../../helpers/render-imports");
|
|
43
|
+
const slots_1 = require("../../helpers/slots");
|
|
44
|
+
const strip_meta_properties_1 = require("../../helpers/strip-meta-properties");
|
|
45
|
+
const collect_css_1 = require("../../helpers/styles/collect-css");
|
|
46
|
+
const helpers_1 = require("../../helpers/styles/helpers");
|
|
47
|
+
const function_1 = require("fp-ts/lib/function");
|
|
48
|
+
const legacy_1 = __importDefault(require("neotraverse/legacy"));
|
|
49
|
+
const prettierPluginSvelte = __importStar(require("prettier-plugin-svelte"));
|
|
50
|
+
const parser_babel_1 = __importDefault(require("prettier/parser-babel"));
|
|
51
|
+
const parser_html_1 = __importDefault(require("prettier/parser-html"));
|
|
52
|
+
const parser_postcss_1 = __importDefault(require("prettier/parser-postcss"));
|
|
53
|
+
const parser_typescript_1 = __importDefault(require("prettier/parser-typescript"));
|
|
54
|
+
const standalone_1 = require("prettier/standalone");
|
|
55
|
+
const plugins_1 = require("../../modules/plugins");
|
|
56
|
+
const context_1 = require("../helpers/context");
|
|
57
|
+
const functions_1 = require("../helpers/functions");
|
|
58
|
+
const blocks_1 = require("./blocks");
|
|
59
|
+
const helpers_2 = require("./helpers");
|
|
60
|
+
const getContextCode = (json) => {
|
|
61
|
+
const contextGetters = json.context.get;
|
|
75
62
|
return Object.entries(contextGetters)
|
|
76
|
-
.map(
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
var contextType = (0, context_1.getContextType)({ component: json, context: context });
|
|
63
|
+
.map(([key, context]) => {
|
|
64
|
+
const { name } = context;
|
|
65
|
+
const contextType = (0, context_1.getContextType)({ component: json, context });
|
|
80
66
|
switch (contextType) {
|
|
81
67
|
case 'reactive':
|
|
82
68
|
case 'normal':
|
|
83
|
-
return
|
|
69
|
+
return `let ${key} = getContext(${name}.key);`;
|
|
84
70
|
}
|
|
85
71
|
})
|
|
86
72
|
.join('\n');
|
|
87
73
|
};
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
var processCode = (0, helpers_2.stripStateAndProps)({ json: json, options: options });
|
|
74
|
+
const setContextCode = ({ json, options, }) => {
|
|
75
|
+
const processCode = (0, helpers_2.stripStateAndProps)({ json, options });
|
|
91
76
|
return Object.values(json.context.set)
|
|
92
|
-
.map(
|
|
93
|
-
|
|
94
|
-
|
|
77
|
+
.map((context) => {
|
|
78
|
+
const { value, name, ref } = context;
|
|
79
|
+
const nameIsStringLiteral = (name.startsWith("'") && name.endsWith("'")) ||
|
|
95
80
|
(name.startsWith('"') && name.endsWith('"'));
|
|
96
|
-
|
|
97
|
-
|
|
81
|
+
const key = nameIsStringLiteral ? name : `${name}.key`;
|
|
82
|
+
const valueStr = value
|
|
98
83
|
? processCode((0, get_state_object_string_1.stringifyContextValue)(value))
|
|
99
84
|
: ref
|
|
100
85
|
? processCode(ref)
|
|
101
86
|
: 'undefined';
|
|
102
|
-
|
|
87
|
+
const contextType = (0, context_1.getContextType)({ component: json, context });
|
|
103
88
|
switch (contextType) {
|
|
104
89
|
case 'normal':
|
|
105
|
-
return
|
|
90
|
+
return `setContext(${key}, ${valueStr});`;
|
|
106
91
|
case 'reactive':
|
|
107
|
-
|
|
108
|
-
return
|
|
92
|
+
const storeName = `${name}ContextStoreValue`;
|
|
93
|
+
return `
|
|
94
|
+
const ${storeName} = writable(${valueStr});
|
|
95
|
+
setContext(${key}, ${storeName});
|
|
96
|
+
`;
|
|
109
97
|
}
|
|
110
98
|
})
|
|
111
99
|
.join('\n');
|
|
@@ -117,7 +105,7 @@ var setContextCode = function (_a) {
|
|
|
117
105
|
* <input bind:value={state.name}/>
|
|
118
106
|
* when easily identified, for more idiomatic svelte code
|
|
119
107
|
*/
|
|
120
|
-
|
|
108
|
+
const useBindValue = (json, options) => {
|
|
121
109
|
function normalizeStr(str) {
|
|
122
110
|
return str
|
|
123
111
|
.trim()
|
|
@@ -129,10 +117,10 @@ var useBindValue = function (json, options) {
|
|
|
129
117
|
}
|
|
130
118
|
(0, legacy_1.default)(json).forEach(function (item) {
|
|
131
119
|
if ((0, is_mitosis_node_1.isMitosisNode)(item)) {
|
|
132
|
-
|
|
120
|
+
const { value, onChange } = item.bindings;
|
|
133
121
|
if (value && onChange) {
|
|
134
|
-
|
|
135
|
-
if (normalizeStr(onChange.code) ===
|
|
122
|
+
const { arguments: cusArgs = ['event'] } = onChange;
|
|
123
|
+
if (normalizeStr(onChange.code) === `${normalizeStr(value.code)}=${cusArgs[0]}.target.value`) {
|
|
136
124
|
delete item.bindings.value;
|
|
137
125
|
delete item.bindings.onChange;
|
|
138
126
|
item.bindings['bind:value'] = value;
|
|
@@ -141,236 +129,282 @@ var useBindValue = function (json, options) {
|
|
|
141
129
|
}
|
|
142
130
|
});
|
|
143
131
|
};
|
|
144
|
-
|
|
132
|
+
const DEFAULT_OPTIONS = {
|
|
145
133
|
stateType: 'variables',
|
|
146
134
|
prettier: true,
|
|
147
135
|
};
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
136
|
+
const componentToSvelte = (userProvidedOptions) => ({ component }) => {
|
|
137
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
138
|
+
const options = (0, merge_options_1.initializeOptions)({
|
|
139
|
+
target: 'svelte',
|
|
140
|
+
component,
|
|
141
|
+
defaults: DEFAULT_OPTIONS,
|
|
142
|
+
userOptions: userProvidedOptions,
|
|
143
|
+
});
|
|
144
|
+
options.plugins = [
|
|
145
|
+
...(options.plugins || []),
|
|
146
|
+
(0, on_event_1.processOnEventHooksPlugin)(),
|
|
147
|
+
functions_1.FUNCTION_HACK_PLUGIN,
|
|
148
|
+
// Strip types from any JS code that ends up in the template, because Svelte does not support TS code in templates.
|
|
149
|
+
(0, process_code_1.CODE_PROCESSOR_PLUGIN)((codeType) => {
|
|
150
|
+
switch (codeType) {
|
|
151
|
+
case 'bindings':
|
|
152
|
+
case 'properties':
|
|
153
|
+
return babel_transform_1.convertTypeScriptToJS;
|
|
154
|
+
case 'hooks':
|
|
155
|
+
case 'hooks-deps':
|
|
156
|
+
case 'state':
|
|
157
|
+
case 'context-set':
|
|
158
|
+
case 'dynamic-jsx-elements':
|
|
159
|
+
case 'types':
|
|
160
|
+
return (x) => x;
|
|
161
|
+
}
|
|
162
|
+
}),
|
|
163
|
+
(0, process_code_1.CODE_PROCESSOR_PLUGIN)((codeType) => {
|
|
164
|
+
switch (codeType) {
|
|
165
|
+
case 'hooks':
|
|
166
|
+
return (0, function_1.flow)((0, helpers_2.stripStateAndProps)({ json, options }), babel_transform_1.babelTransformCode);
|
|
167
|
+
case 'bindings':
|
|
168
|
+
case 'hooks-deps':
|
|
169
|
+
case 'state':
|
|
170
|
+
return (0, function_1.flow)((0, helpers_2.stripStateAndProps)({ json, options }), patterns_1.stripGetter);
|
|
171
|
+
case 'properties':
|
|
172
|
+
case 'context-set':
|
|
173
|
+
return (0, function_1.flow)((0, helpers_2.stripStateAndProps)({ json, options }));
|
|
174
|
+
case 'dynamic-jsx-elements':
|
|
175
|
+
case 'types':
|
|
176
|
+
return (x) => x;
|
|
177
|
+
}
|
|
178
|
+
}),
|
|
179
|
+
];
|
|
180
|
+
// Make a copy we can safely mutate, similar to babel's toolchain
|
|
181
|
+
let json = (0, fast_clone_1.fastClone)(component);
|
|
182
|
+
json = (0, plugins_1.runPreJsonPlugins)({ json, plugins: options.plugins });
|
|
183
|
+
useBindValue(json, options);
|
|
184
|
+
(0, getters_to_functions_1.gettersToFunctions)(json);
|
|
185
|
+
const filteredProps = Array.from((0, get_props_1.getProps)(json))
|
|
186
|
+
.filter((prop) => !(0, slots_1.isSlotProperty)(prop))
|
|
187
|
+
// map $prop to prop for reactive state
|
|
188
|
+
.map((x) => (x.startsWith('$') ? x.slice(1) : x));
|
|
189
|
+
// this helps make sure we don't have duplicate props
|
|
190
|
+
const props = Array.from(new Set(filteredProps));
|
|
191
|
+
const refs = Array.from((0, get_refs_1.getRefs)(json))
|
|
192
|
+
.map((0, helpers_2.stripStateAndProps)({ json, options }))
|
|
193
|
+
.filter((x) => !props.includes(x));
|
|
194
|
+
json = (0, plugins_1.runPostJsonPlugins)({ json, plugins: options.plugins });
|
|
195
|
+
const css = (0, collect_css_1.collectCss)(json);
|
|
196
|
+
(0, strip_meta_properties_1.stripMetaProperties)(json);
|
|
197
|
+
let usesWritable = false;
|
|
198
|
+
const dataString = (0, function_1.pipe)((0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
199
|
+
data: true,
|
|
200
|
+
functions: false,
|
|
201
|
+
getters: false,
|
|
202
|
+
format: options.stateType === 'proxies' ? 'object' : 'variables',
|
|
203
|
+
keyPrefix: options.stateType === 'variables' ? 'let ' : '',
|
|
204
|
+
valueMapper: (code, _t, _p, key) => {
|
|
205
|
+
var _a;
|
|
206
|
+
if (((_a = json.state[key]) === null || _a === void 0 ? void 0 : _a.propertyType) === 'reactive') {
|
|
207
|
+
usesWritable = true;
|
|
208
|
+
return `writable(${code})`;
|
|
209
|
+
}
|
|
210
|
+
return code;
|
|
211
|
+
},
|
|
212
|
+
}), babel_transform_1.babelTransformCode);
|
|
213
|
+
const getterString = (0, function_1.pipe)((0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
214
|
+
data: false,
|
|
215
|
+
getters: true,
|
|
216
|
+
functions: false,
|
|
217
|
+
format: 'variables',
|
|
218
|
+
keyPrefix: '$: ',
|
|
219
|
+
valueMapper: (code) => {
|
|
220
|
+
return code
|
|
221
|
+
.trim()
|
|
222
|
+
.replace(/^([a-zA-Z_\$0-9]+)/, '$1 = ')
|
|
223
|
+
.replace(/\)/, ') => ');
|
|
224
|
+
},
|
|
225
|
+
}), babel_transform_1.babelTransformCode);
|
|
226
|
+
const functionsString = (0, function_1.pipe)((0, get_state_object_string_1.getStateObjectStringFromComponent)(json, {
|
|
227
|
+
data: false,
|
|
228
|
+
getters: false,
|
|
229
|
+
functions: true,
|
|
230
|
+
format: 'variables',
|
|
231
|
+
}), babel_transform_1.babelTransformCode);
|
|
232
|
+
const hasData = dataString.length > 4;
|
|
233
|
+
let str = '';
|
|
234
|
+
const tsLangAttribute = options.typescript ? `lang='ts'` : '';
|
|
235
|
+
if (options.typescript && ((_a = json.types) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
236
|
+
str += (0, dedent_1.dedent) `
|
|
237
|
+
<script context='module' ${tsLangAttribute}>
|
|
238
|
+
${json.types ? json.types.join('\n\n') + '\n' : ''}
|
|
239
|
+
</script>
|
|
240
|
+
\n
|
|
241
|
+
\n
|
|
242
|
+
`;
|
|
243
|
+
}
|
|
244
|
+
// prepare svelte imports
|
|
245
|
+
let svelteImports = [];
|
|
246
|
+
let svelteStoreImports = [];
|
|
247
|
+
if (json.hooks.onMount.length) {
|
|
248
|
+
svelteImports.push('onMount');
|
|
249
|
+
}
|
|
250
|
+
if ((_c = (_b = json.hooks.onUpdate) === null || _b === void 0 ? void 0 : _b.filter((x) => !x.deps)) === null || _c === void 0 ? void 0 : _c.length) {
|
|
251
|
+
svelteImports.push('afterUpdate');
|
|
252
|
+
}
|
|
253
|
+
if ((_e = (_d = json.hooks.onUnMount) === null || _d === void 0 ? void 0 : _d.code) === null || _e === void 0 ? void 0 : _e.length) {
|
|
254
|
+
svelteImports.push('onDestroy');
|
|
255
|
+
}
|
|
256
|
+
if ((0, context_1.hasGetContext)(component)) {
|
|
257
|
+
svelteImports.push('getContext');
|
|
258
|
+
}
|
|
259
|
+
if ((0, context_1.hasSetContext)(component)) {
|
|
260
|
+
svelteImports.push('setContext');
|
|
261
|
+
}
|
|
262
|
+
if (usesWritable) {
|
|
263
|
+
svelteStoreImports.push('writable');
|
|
264
|
+
}
|
|
265
|
+
str += (0, dedent_1.dedent) `
|
|
266
|
+
<script ${tsLangAttribute}>
|
|
267
|
+
${!svelteImports.length ? '' : `import { ${svelteImports.sort().join(', ')} } from 'svelte'`}
|
|
268
|
+
${!svelteStoreImports.length
|
|
269
|
+
? ''
|
|
270
|
+
: `import { ${svelteStoreImports.sort().join(', ')} } from 'svelte/store'`}
|
|
271
|
+
|
|
272
|
+
${(0, render_imports_1.renderPreComponent)({
|
|
273
|
+
explicitImportFileExtension: options.explicitImportFileExtension,
|
|
274
|
+
component: json,
|
|
275
|
+
target: 'svelte',
|
|
276
|
+
})}
|
|
277
|
+
|
|
278
|
+
${!hasData || options.stateType === 'variables' ? '' : `import onChange from 'on-change'`}
|
|
279
|
+
${props
|
|
280
|
+
.map((name) => {
|
|
281
|
+
var _a, _b, _c;
|
|
282
|
+
if (name === 'children') {
|
|
283
|
+
return '';
|
|
262
284
|
}
|
|
263
|
-
|
|
264
|
-
|
|
285
|
+
let propDeclaration = `export let ${name}`;
|
|
286
|
+
if (options.typescript && json.propsTypeRef && json.propsTypeRef !== 'any') {
|
|
287
|
+
propDeclaration += `: ${json.propsTypeRef.split(' |')[0]}['${name}']`;
|
|
265
288
|
}
|
|
266
|
-
if ((
|
|
267
|
-
|
|
289
|
+
if ((_b = (_a = json.props) === null || _a === void 0 ? void 0 : _a[name]) === null || _b === void 0 ? void 0 : _b.optional) {
|
|
290
|
+
propDeclaration += `= undefined`;
|
|
268
291
|
}
|
|
269
|
-
if (
|
|
270
|
-
|
|
292
|
+
if (json.defaultProps && json.defaultProps.hasOwnProperty(name)) {
|
|
293
|
+
propDeclaration += `=${(_c = json.defaultProps[name]) === null || _c === void 0 ? void 0 : _c.code}`;
|
|
271
294
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
}
|
|
288
|
-
var propDeclaration = "export let ".concat(name);
|
|
289
|
-
if (options.typescript && json.propsTypeRef && json.propsTypeRef !== 'any') {
|
|
290
|
-
propDeclaration += ": ".concat(json.propsTypeRef.split(' |')[0], "['").concat(name, "']");
|
|
291
|
-
}
|
|
292
|
-
if ((_b = (_a = json.props) === null || _a === void 0 ? void 0 : _a[name]) === null || _b === void 0 ? void 0 : _b.optional) {
|
|
293
|
-
propDeclaration += "= undefined";
|
|
294
|
-
}
|
|
295
|
-
if (json.defaultProps && json.defaultProps.hasOwnProperty(name)) {
|
|
296
|
-
propDeclaration += "=".concat((_c = json.defaultProps[name]) === null || _c === void 0 ? void 0 : _c.code);
|
|
297
|
-
}
|
|
298
|
-
propDeclaration += ';';
|
|
299
|
-
return propDeclaration;
|
|
300
|
-
})
|
|
301
|
-
.join('\n'),
|
|
302
|
-
// https://github.com/sveltejs/svelte/issues/7311
|
|
303
|
-
(0, helpers_1.hasStyle)(json)
|
|
304
|
-
? (0, dedent_1.dedent)(templateObject_2 || (templateObject_2 = __makeTemplateObject(["\n \tfunction stringifyStyles(stylesObj) {\n let styles = '';\n for (let key in stylesObj) {\n const dashedKey = key.replace(/[A-Z]/g, function(match) {\n return '-' + match.toLowerCase();\n });\n styles += dashedKey + \":\" + stylesObj[key] + \";\";\n }\n return styles;\n }\n "], ["\n \tfunction stringifyStyles(stylesObj) {\n let styles = '';\n for (let key in stylesObj) {\n const dashedKey = key.replace(/[A-Z]/g, function(match) {\n return '-' + match.toLowerCase();\n });\n styles += dashedKey + \":\" + stylesObj[key] + \";\";\n }\n return styles;\n }\n "]))) : '', getContextCode(json), functionsString.length < 4 ? '' : functionsString, getterString.length < 4 ? '' : getterString, refs.map(function (ref) { return "let ".concat(ref); }).join('\n'), options.stateType === 'proxies'
|
|
305
|
-
? dataString.length < 4
|
|
306
|
-
? ''
|
|
307
|
-
: "let state = onChange(".concat(dataString, ", () => state = state)")
|
|
308
|
-
: dataString, (_h = (_g = json.hooks.onInit) === null || _g === void 0 ? void 0 : _g.code) !== null && _h !== void 0 ? _h : '', json.hooks.onMount.map(function (hook) { return "onMount(() => { ".concat(hook.code, " });"); }).join('\n'), ((_j = json.hooks.onUpdate) === null || _j === void 0 ? void 0 : _j.map(function (_a, index) {
|
|
309
|
-
var code = _a.code, deps = _a.deps;
|
|
310
|
-
if (!deps) {
|
|
311
|
-
return "afterUpdate(() => { ".concat(code, " });");
|
|
295
|
+
propDeclaration += ';';
|
|
296
|
+
return propDeclaration;
|
|
297
|
+
})
|
|
298
|
+
.join('\n')}
|
|
299
|
+
${
|
|
300
|
+
// https://github.com/sveltejs/svelte/issues/7311
|
|
301
|
+
(0, helpers_1.hasStyle)(json)
|
|
302
|
+
? (0, dedent_1.dedent) `
|
|
303
|
+
function stringifyStyles(stylesObj) {
|
|
304
|
+
let styles = '';
|
|
305
|
+
for (let key in stylesObj) {
|
|
306
|
+
const dashedKey = key.replace(/[A-Z]/g, function(match) {
|
|
307
|
+
return '-' + match.toLowerCase();
|
|
308
|
+
});
|
|
309
|
+
styles += dashedKey + ":" + stylesObj[key] + ";";
|
|
312
310
|
}
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
var depsArrayStr = depsArray
|
|
327
|
-
.map(function (x) { return (isStoreAccessDep(x) ? getReactiveDepName(x) : x); })
|
|
328
|
-
.join(', ');
|
|
329
|
-
/**
|
|
330
|
-
* We create a reactive value for each `onUpdate`'s dependency that
|
|
331
|
-
* accesses a store so that Svelte has accurate dependency tracking.
|
|
332
|
-
*
|
|
333
|
-
* Otherwise, if the dependency is a value within a store, Svelte will
|
|
334
|
-
* rerun the effect every time the parent store is changed in any way.
|
|
335
|
-
*/
|
|
336
|
-
return "\n function ".concat(fnName, "(..._args").concat(options.typescript ? ': any[]' : '', ") {\n ").concat(code, "\n }\n ").concat(reactiveDepsWorkaround, "\n $: ").concat(fnName, "(...[").concat(depsArrayStr, "]);\n ");
|
|
337
|
-
}).join('\n')) || '',
|
|
338
|
-
// make sure this is after all other state/code is initialized
|
|
339
|
-
setContextCode({ json: json, options: options }), !((_k = json.hooks.onUnMount) === null || _k === void 0 ? void 0 : _k.code) ? '' : "onDestroy(() => { ".concat(json.hooks.onUnMount.code, " });"), json.children
|
|
340
|
-
.map(function (item) {
|
|
341
|
-
return (0, blocks_1.blockToSvelte)({
|
|
342
|
-
json: item,
|
|
343
|
-
options: options,
|
|
344
|
-
parentComponent: json,
|
|
345
|
-
});
|
|
346
|
-
})
|
|
347
|
-
.join('\n'), !css.trim().length
|
|
311
|
+
return styles;
|
|
312
|
+
}
|
|
313
|
+
`
|
|
314
|
+
: ''}
|
|
315
|
+
${getContextCode(json)}
|
|
316
|
+
|
|
317
|
+
${functionsString.length < 4 ? '' : functionsString}
|
|
318
|
+
${getterString.length < 4 ? '' : getterString}
|
|
319
|
+
|
|
320
|
+
${refs.map((ref) => `let ${ref}`).join('\n')}
|
|
321
|
+
|
|
322
|
+
${options.stateType === 'proxies'
|
|
323
|
+
? dataString.length < 4
|
|
348
324
|
? ''
|
|
349
|
-
:
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
parser_postcss_1.default,
|
|
359
|
-
parser_babel_1.default,
|
|
360
|
-
parser_typescript_1.default,
|
|
361
|
-
prettierPluginSvelte,
|
|
362
|
-
],
|
|
363
|
-
});
|
|
364
|
-
}
|
|
365
|
-
catch (err) {
|
|
366
|
-
console.warn('Could not prettify');
|
|
367
|
-
console.warn(str, err);
|
|
368
|
-
}
|
|
325
|
+
: `let state = onChange(${dataString}, () => state = state)`
|
|
326
|
+
: dataString}
|
|
327
|
+
${(_g = (_f = json.hooks.onInit) === null || _f === void 0 ? void 0 : _f.code) !== null && _g !== void 0 ? _g : ''}
|
|
328
|
+
|
|
329
|
+
${json.hooks.onMount.map((hook) => `onMount(() => { ${hook.code} });`).join('\n')}
|
|
330
|
+
|
|
331
|
+
${((_h = json.hooks.onUpdate) === null || _h === void 0 ? void 0 : _h.map(({ code, deps }, index) => {
|
|
332
|
+
if (!deps) {
|
|
333
|
+
return `afterUpdate(() => { ${code} });`;
|
|
369
334
|
}
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
335
|
+
const fnName = `onUpdateFn_${index}`;
|
|
336
|
+
const depsArray = deps
|
|
337
|
+
.slice(1, deps.length - 1)
|
|
338
|
+
.split(',')
|
|
339
|
+
.map((x) => x.trim());
|
|
340
|
+
const getReactiveDepName = (dep) => `${fnName}_${dep.slice(1).replace(/(\.|\?)/g, '_')}`;
|
|
341
|
+
const isStoreAccessDep = (dep) => dep.startsWith('$');
|
|
342
|
+
const reactiveDepsWorkaround = depsArray
|
|
343
|
+
.filter(isStoreAccessDep)
|
|
344
|
+
.map((dep) => `$: ${getReactiveDepName(dep)} = ${dep};`)
|
|
345
|
+
.join('\n');
|
|
346
|
+
const depsArrayStr = depsArray
|
|
347
|
+
.map((x) => (isStoreAccessDep(x) ? getReactiveDepName(x) : x))
|
|
348
|
+
.join(', ');
|
|
349
|
+
/**
|
|
350
|
+
* We create a reactive value for each `onUpdate`'s dependency that
|
|
351
|
+
* accesses a store so that Svelte has accurate dependency tracking.
|
|
352
|
+
*
|
|
353
|
+
* Otherwise, if the dependency is a value within a store, Svelte will
|
|
354
|
+
* rerun the effect every time the parent store is changed in any way.
|
|
355
|
+
*/
|
|
356
|
+
return `
|
|
357
|
+
function ${fnName}(..._args${options.typescript ? ': any[]' : ''}) {
|
|
358
|
+
${code}
|
|
359
|
+
}
|
|
360
|
+
${reactiveDepsWorkaround}
|
|
361
|
+
$: ${fnName}(...[${depsArrayStr}]);
|
|
362
|
+
`;
|
|
363
|
+
}).join('\n')) || ''}
|
|
364
|
+
|
|
365
|
+
${
|
|
366
|
+
// make sure this is after all other state/code is initialized
|
|
367
|
+
setContextCode({ json, options })}
|
|
368
|
+
|
|
369
|
+
${!((_j = json.hooks.onUnMount) === null || _j === void 0 ? void 0 : _j.code) ? '' : `onDestroy(() => { ${json.hooks.onUnMount.code} });`}
|
|
370
|
+
</script>
|
|
371
|
+
|
|
372
|
+
${json.children
|
|
373
|
+
.map((item) => (0, blocks_1.blockToSvelte)({
|
|
374
|
+
json: item,
|
|
375
|
+
options: options,
|
|
376
|
+
parentComponent: json,
|
|
377
|
+
}))
|
|
378
|
+
.join('\n')}
|
|
379
|
+
|
|
380
|
+
${!css.trim().length
|
|
381
|
+
? ''
|
|
382
|
+
: `<style>
|
|
383
|
+
${css}
|
|
384
|
+
</style>`}
|
|
385
|
+
`;
|
|
386
|
+
str = (0, plugins_1.runPreCodePlugins)({ json, code: str, plugins: options.plugins });
|
|
387
|
+
if (options.prettier !== false) {
|
|
388
|
+
try {
|
|
389
|
+
str = (0, standalone_1.format)(str, {
|
|
390
|
+
parser: 'svelte',
|
|
391
|
+
plugins: [
|
|
392
|
+
// To support running in browsers
|
|
393
|
+
parser_html_1.default,
|
|
394
|
+
parser_postcss_1.default,
|
|
395
|
+
parser_babel_1.default,
|
|
396
|
+
parser_typescript_1.default,
|
|
397
|
+
prettierPluginSvelte,
|
|
398
|
+
],
|
|
399
|
+
});
|
|
400
|
+
}
|
|
401
|
+
catch (err) {
|
|
402
|
+
console.warn('Could not prettify');
|
|
403
|
+
console.warn(str, err);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
str = str.replace(/<script>\n<\/script>/g, '').trim();
|
|
407
|
+
str = (0, plugins_1.runPostCodePlugins)({ json, code: str, plugins: options.plugins });
|
|
408
|
+
return str;
|
|
374
409
|
};
|
|
375
410
|
exports.componentToSvelte = componentToSvelte;
|
|
376
|
-
var templateObject_1, templateObject_2, templateObject_3;
|