@builder.io/mitosis 0.0.115-7 → 0.0.115

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/package.json CHANGED
@@ -22,7 +22,7 @@
22
22
  "name": "Builder.io",
23
23
  "url": "https://www.builder.io"
24
24
  },
25
- "version": "0.0.115-7",
25
+ "version": "0.0.115",
26
26
  "homepage": "https://github.com/BuilderIO/mitosis",
27
27
  "main": "./dist/src/index.js",
28
28
  "exports": {
@@ -129,6 +129,5 @@
129
129
  "universalify": "^2.0.0",
130
130
  "vite": "^3.2.2",
131
131
  "vitest": "^0.24.5"
132
- },
133
- "stableVersion": "0.0.115-6"
132
+ }
134
133
  }
@@ -1,235 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.findSignals = exports.createTypescriptProject = exports.mapSignalTypeInTSFile = exports.mapSignalType = exports.getSignalImportName = exports.getSignalMitosisImportForTarget = void 0;
4
- var core_1 = require("@babel/core");
5
- var function_1 = require("fp-ts/lib/function");
6
- var ts_morph_1 = require("ts-morph");
7
- var babel_transform_1 = require("../../helpers/babel-transform");
8
- var imports_1 = require("./imports");
9
- var getSignalMappingForTarget = function (target) {
10
- switch (target) {
11
- case 'svelte':
12
- var importDeclaration = core_1.types.importDeclaration([core_1.types.importSpecifier(core_1.types.identifier('Writable'), core_1.types.identifier('Writable'))], core_1.types.stringLiteral('svelte/store'));
13
- importDeclaration.importKind = 'type';
14
- return {
15
- getTypeReference: function (generics) {
16
- if (generics === void 0) { generics = []; }
17
- return core_1.types.tsTypeReference(core_1.types.identifier('Writable'), core_1.types.tsTypeParameterInstantiation(generics));
18
- },
19
- importDeclaration: importDeclaration,
20
- };
21
- default:
22
- return undefined;
23
- }
24
- };
25
- var getSignalMitosisImportForTarget = function (target) {
26
- var signalType = getSignalMappingForTarget(target);
27
- if (!signalType) {
28
- return undefined;
29
- }
30
- return (0, imports_1.mapImportDeclarationToMitosisImport)(signalType.importDeclaration);
31
- };
32
- exports.getSignalMitosisImportForTarget = getSignalMitosisImportForTarget;
33
- var getSignalSymbol = function (project) {
34
- var symbolExport = project.createSourceFile('homepage3.lite.tsx', "import { Signal } from '@builder.io/mitosis';");
35
- // Find the original Signal symbol
36
- var signalSymbol = undefined;
37
- symbolExport.forEachDescendant(function (node) {
38
- var _a;
39
- if (ts_morph_1.Node.isImportSpecifier(node)) {
40
- signalSymbol = (_a = node.getSymbol()) === null || _a === void 0 ? void 0 : _a.getAliasedSymbol();
41
- }
42
- });
43
- if (signalSymbol === undefined) {
44
- throw new Error('Could not find Signal symbol');
45
- }
46
- return signalSymbol;
47
- };
48
- var getSignalImportName = function (code) {
49
- var foundSignalUsage = false;
50
- var signalImportName = undefined;
51
- (0, babel_transform_1.babelTransformExpression)(code, {
52
- ImportSpecifier: function (path) {
53
- if (core_1.types.isIdentifier(path.node.imported) && path.node.imported.name === 'Signal') {
54
- if (path.parentPath.isImportDeclaration() &&
55
- path.parentPath.node.source.value === '@builder.io/mitosis') {
56
- /**
57
- * in case the import is aliased, we need to use the local name,
58
- * e.g. `import { Signal as MySignal } from '@builder.io/mitosis'`
59
- */
60
- signalImportName = path.node.local.name;
61
- path.stop();
62
- }
63
- }
64
- },
65
- });
66
- if (!signalImportName) {
67
- return undefined;
68
- }
69
- (0, babel_transform_1.babelTransformExpression)(code, {
70
- TSTypeReference: function (path) {
71
- if (core_1.types.isIdentifier(path.node.typeName) && path.node.typeName.name === signalImportName) {
72
- foundSignalUsage = true;
73
- path.stop();
74
- }
75
- },
76
- });
77
- return foundSignalUsage ? signalImportName : undefined;
78
- };
79
- exports.getSignalImportName = getSignalImportName;
80
- var addSignalImport = function (_a) {
81
- var code = _a.code, target = _a.target;
82
- var signalType = getSignalMappingForTarget(target);
83
- if (!signalType) {
84
- return code;
85
- }
86
- return (0, babel_transform_1.babelTransformExpression)(code, {
87
- Program: function (path) {
88
- path.node.body.unshift(signalType.importDeclaration);
89
- },
90
- });
91
- };
92
- /**
93
- * Finds all `Signal` types and replaces them with the correct type for the given target.
94
- * e.g. `Signal<string>` becomes `Writable<string>` for Svelte.
95
- */
96
- var mapSignalType = function (_a) {
97
- var code = _a.code, target = _a.target, _b = _a.signalImportName, signalImportName = _b === void 0 ? (0, exports.getSignalImportName)(code) : _b;
98
- var signalType = getSignalMappingForTarget(target);
99
- return (0, babel_transform_1.babelTransformExpression)(code, {
100
- TSTypeReference: function (path) {
101
- var _a;
102
- if (core_1.types.isIdentifier(path.node.typeName) && path.node.typeName.name === signalImportName) {
103
- var params = ((_a = path.node.typeParameters) === null || _a === void 0 ? void 0 : _a.params) || [];
104
- var newType = (signalType === null || signalType === void 0 ? void 0 : signalType.getTypeReference)
105
- ? signalType.getTypeReference(params)
106
- : // if no mapping exists, drop `Signal` and just use the generic type passed to `Signal` as-is.
107
- params[0];
108
- path.replaceWith(newType);
109
- }
110
- },
111
- });
112
- };
113
- exports.mapSignalType = mapSignalType;
114
- /**
115
- * Processes the `Signal` type usage in a plain TS file:
116
- * - Finds the Signal import name
117
- * - Maps the Signal type to the target's equivalent
118
- * - Adds the equivalent of the Signal import to the file
119
- */
120
- var mapSignalTypeInTSFile = function (_a) {
121
- var code = _a.code, target = _a.target;
122
- var signalImportName = (0, exports.getSignalImportName)(code);
123
- if (!signalImportName) {
124
- return code;
125
- }
126
- return (0, function_1.pipe)((0, exports.mapSignalType)({ target: target, code: code, signalImportName: signalImportName }), function (code) {
127
- return addSignalImport({ code: code, target: target });
128
- });
129
- };
130
- exports.mapSignalTypeInTSFile = mapSignalTypeInTSFile;
131
- var getProject = function (tsConfigFilePath) {
132
- try {
133
- return new ts_morph_1.Project({ tsConfigFilePath: tsConfigFilePath });
134
- }
135
- catch (err) {
136
- throw new Error('Error creating Typescript Project. Make sure `tsConfigFilePath` points to a valid tsconfig.json file');
137
- }
138
- };
139
- var createTypescriptProject = function (tsConfigFilePath) {
140
- var project = getProject(tsConfigFilePath);
141
- var signalSymbol = getSignalSymbol(project);
142
- return { project: project, signalSymbol: signalSymbol };
143
- };
144
- exports.createTypescriptProject = createTypescriptProject;
145
- var getPropsSymbol = function (ast) {
146
- var propsSymbol = undefined;
147
- ast.forEachChild(function (node) {
148
- var _a;
149
- if (propsSymbol !== undefined)
150
- return;
151
- if (ts_morph_1.Node.isArrowFunction(node) || ts_morph_1.Node.isFunctionDeclaration(node)) {
152
- if (node.hasModifier(ts_morph_1.SyntaxKind.ExportKeyword) &&
153
- node.hasModifier(ts_morph_1.SyntaxKind.DefaultKeyword)) {
154
- propsSymbol = (_a = node.getParameters()[0]) === null || _a === void 0 ? void 0 : _a.getSymbol();
155
- }
156
- }
157
- });
158
- return propsSymbol;
159
- };
160
- var getContextSymbols = function (ast) {
161
- var contextSymbols = new Set();
162
- ast.forEachDescendant(function (node) {
163
- if (!ts_morph_1.Node.isVariableDeclaration(node))
164
- return;
165
- var initializer = node.getInitializer();
166
- if (!ts_morph_1.Node.isCallExpression(initializer))
167
- return;
168
- if (initializer.getExpression().getText() !== 'useContext')
169
- return;
170
- var contextSymbol = node.getNameNode().getSymbol();
171
- if (contextSymbol === undefined)
172
- return;
173
- contextSymbols.add(contextSymbol);
174
- });
175
- return contextSymbols;
176
- };
177
- var findSignals = function (args) {
178
- var project = args.project, signalSymbol = args.signalSymbol;
179
- var ast = args.code
180
- ? args.project.createSourceFile('homepage2.lite.tsx', args.code)
181
- : args.filePath
182
- ? args.project.getSourceFileOrThrow(args.filePath)
183
- : undefined;
184
- if (ast === undefined) {
185
- throw new Error('Could not find AST. Please provide either `code` or `filePath` configs.');
186
- }
187
- var reactiveValues = {
188
- props: new Set(),
189
- state: new Set(),
190
- context: new Set(),
191
- };
192
- var propsSymbol = getPropsSymbol(ast);
193
- var contextSymbols = getContextSymbols(ast);
194
- ast.forEachDescendant(function (parentNode) {
195
- var _a;
196
- if (ts_morph_1.Node.isPropertyAccessExpression(parentNode)) {
197
- var node = parentNode.getExpression();
198
- var aliasSymbol = (_a = node.getType().getTargetType()) === null || _a === void 0 ? void 0 : _a.getAliasSymbol();
199
- var isSignal = aliasSymbol === signalSymbol;
200
- if (!isSignal)
201
- return;
202
- var isInsideType_1 = false;
203
- var isInsideDeclaration = false;
204
- node.getParentWhile(function (parent, child) {
205
- // stop once we hit the function block
206
- if (ts_morph_1.Node.isBlock(child) || ts_morph_1.Node.isBlock(parent)) {
207
- return false;
208
- }
209
- // crawl up parents to make sure we're not inside a type
210
- if (ts_morph_1.Node.isTypeNode(parent) || ts_morph_1.Node.isTypeAliasDeclaration(parent)) {
211
- isInsideType_1 = true;
212
- return false;
213
- }
214
- return true;
215
- });
216
- if (isInsideType_1)
217
- return;
218
- if (isInsideDeclaration)
219
- return;
220
- var nodeSymbol = node.getSymbol();
221
- if (ts_morph_1.Node.isPropertyAccessExpression(node) &&
222
- node.getExpression().getSymbol() === propsSymbol) {
223
- reactiveValues.props.add(node.getNameNode().getText());
224
- }
225
- else if (nodeSymbol && contextSymbols.has(nodeSymbol)) {
226
- reactiveValues.context.add(node.getText());
227
- }
228
- else {
229
- reactiveValues.state.add(node.getText());
230
- }
231
- }
232
- });
233
- return reactiveValues;
234
- };
235
- exports.findSignals = findSignals;