mirror of
https://github.com/ritz078/transform.git
synced 2026-11-21 00:32:56 +03:00
* feat(init): added EditorPanel with prettier, copy and settings support * chore(static): ignore static * feat: added navigator * feat: * feat(settings): added sessionStorage support for settings * feat(message): add messaging support in editors * feat(preview): added support for preview panel * fix(settings): use localstorage for settings instead of session storage * feat: * feat(css-to-js): implemented React section * feat(schema): add json to schema conversions * feat(others): implemented xml, yaml and markdown conversions * feat(babel): added flow to typescript converter * feat(graphql): implemented graphql to typescript * feat(graphql): added some transformations * fix: add support for different default value in one language * feat(graphql): added components, resolver signatures and mongodb * feat: * scala case class * fix(memory): worker-loader patch applied to fix memory overflow crash * feat: added json schema to typescript * icon: <GraphQLIcon />, * feat(navigator): shorter naming and new categories * route fixes * navigator * perf(workers): create separate file and remove fallback * fix(navigator): completed * build(snack): * fixes * fixes * fix(safari): * added json-to-mobx-state tree * fix(native): snacksession * fixes * added logo * navigator * fixes * custom header * fixes * refactor(settings): move settings to left side * feat: set up redirects * feat(seo): added title and description * feat(error): added custom error page * ga-script * feat: * fixes * fixe * fixes * fixes * fix:
90 lines
2.1 KiB
TypeScript
90 lines
2.1 KiB
TypeScript
import { transform } from "@babel/standalone";
|
|
import jsonToProptypes from "babel-plugin-json-to-proptypes";
|
|
import jsonToMobxTree from "@assets/vendor/babel-plugin-js-to-mobx-state-tree";
|
|
import { merge } from "lodash";
|
|
import { prettify } from "@utils/prettify";
|
|
import { BabelTransforms } from "@constants/babelTransforms";
|
|
import objStylesToTemplate from "babel-plugin-object-styles-to-template";
|
|
import babelFlowToTs from "babel-plugin-flow-to-typescript";
|
|
|
|
const _self: any = self;
|
|
|
|
interface Data {
|
|
id: string;
|
|
payload: {
|
|
value: string;
|
|
type: BabelTransforms;
|
|
settings?: any;
|
|
};
|
|
}
|
|
|
|
async function handleJsonToProptypes(value, id) {
|
|
let code = JSON.parse(value);
|
|
|
|
if (typeof code !== "object" || Array.isArray(code)) {
|
|
code = merge({}, ...code);
|
|
}
|
|
|
|
const result = transform(`const propTypes = ${JSON.stringify(code)}`, {
|
|
plugins: [jsonToProptypes]
|
|
}).code;
|
|
|
|
const prettyCode = await prettify("javascript", result);
|
|
|
|
_self.postMessage({
|
|
id,
|
|
payload: prettyCode
|
|
});
|
|
}
|
|
|
|
function objectStylesToTemplate(value, id, settings) {
|
|
_self.postMessage({
|
|
id,
|
|
payload: transform(value, {
|
|
plugins: [[objStylesToTemplate, settings]]
|
|
}).code
|
|
});
|
|
}
|
|
|
|
function flowToTS(value, id) {
|
|
_self.postMessage({
|
|
id,
|
|
payload: transform(value, {
|
|
plugins: [babelFlowToTs]
|
|
}).code
|
|
});
|
|
}
|
|
|
|
function jsonToMobx(value, id) {
|
|
_self.postMessage({
|
|
id,
|
|
payload: transform(`const myModel = ${value}`, {
|
|
plugins: [jsonToMobxTree]
|
|
}).code
|
|
});
|
|
}
|
|
|
|
_self.onmessage = ({ data: { id, payload } }: { data: Data }) => {
|
|
const { value, type, settings } = payload;
|
|
|
|
try {
|
|
if (type === BabelTransforms.JSON_TO_PROPTYPES) {
|
|
handleJsonToProptypes(value, id);
|
|
} else if (type === BabelTransforms.OBJECT_STYLES_TO_TEMPLATE) {
|
|
objectStylesToTemplate(value, id, settings);
|
|
} else if (type === BabelTransforms.FLOW_TO_TYPESCRIPT) {
|
|
flowToTS(value, id);
|
|
} else if (type === BabelTransforms.JSON_TO_MOBX_TREE) {
|
|
jsonToMobx(value, id);
|
|
}
|
|
} catch (e) {
|
|
if (IS_DEV) {
|
|
console.error(e);
|
|
}
|
|
_self.postMessage({
|
|
id,
|
|
err: e.message
|
|
});
|
|
}
|
|
};
|