mirror of
https://github.com/ritz078/transform.git
synced 2026-11-30 04:30:12 +03:00
move workers to src/workers
This commit is contained in:
1 parent
de82081213
commit
466278ae6b
8 files changed
+2
-2
No files matched your search
@@ -0,0 +1,77 @@
|
||||
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";
|
||||
|
||||
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 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.JSON_TO_MOBX_TREE) {
|
||||
jsonToMobx(value, id);
|
||||
}
|
||||
} catch (e) {
|
||||
if (IS_DEV) {
|
||||
console.error(e);
|
||||
}
|
||||
_self.postMessage({
|
||||
id,
|
||||
err: e.message
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,97 @@
|
||||
import { codegen } from "@graphql-codegen/core";
|
||||
import { parse } from "graphql";
|
||||
import * as typescriptPlugin from "@graphql-codegen/typescript";
|
||||
import * as typescriptOperation from "@graphql-codegen/typescript-operations";
|
||||
import * as flowPlugin from "@graphql-codegen/flow";
|
||||
import * as flowOperation from "@graphql-codegen/flow-operations";
|
||||
import * as introspection from "@graphql-codegen/introspection";
|
||||
import * as fragmentMatcher from "@graphql-codegen/fragment-matcher";
|
||||
import * as schemaAst from "@graphql-codegen/schema-ast";
|
||||
import * as java from "@graphql-codegen/java";
|
||||
import * as apolloAngular from "@graphql-codegen/typescript-apollo-angular";
|
||||
import * as reactApollo from "@graphql-codegen/typescript-react-apollo";
|
||||
import * as stencilApollo from "@graphql-codegen/typescript-stencil-apollo";
|
||||
import * as tsResolvers from "@graphql-codegen/typescript-resolvers";
|
||||
import * as flowResolvers from "@graphql-codegen/flow-resolvers";
|
||||
import * as javaResolvers from "@graphql-codegen/java-resolvers";
|
||||
import * as tsMongoDB from "@graphql-codegen/typescript-mongodb";
|
||||
import * as urql from "@graphql-codegen/typescript-urql";
|
||||
import { GraphqlTransforms } from "@constants/graphqlTransforms";
|
||||
|
||||
const _self: any = self;
|
||||
|
||||
function getPlugins(type: GraphqlTransforms) {
|
||||
switch (type) {
|
||||
case GraphqlTransforms.TO_TYPESCRIPT:
|
||||
return [typescriptPlugin, typescriptOperation];
|
||||
case GraphqlTransforms.TO_FLOW:
|
||||
return [flowPlugin, flowOperation];
|
||||
case GraphqlTransforms.TO_INTROSPECTION_JSON:
|
||||
return [introspection];
|
||||
case GraphqlTransforms.TO_FRAGMENT_MATCHER:
|
||||
return [fragmentMatcher];
|
||||
case GraphqlTransforms.TO_SCHEMA_AST:
|
||||
return [schemaAst];
|
||||
case GraphqlTransforms.TO_JAVA:
|
||||
return [java];
|
||||
case GraphqlTransforms.TO_REACT_APOLLO:
|
||||
return [typescriptPlugin, typescriptOperation, reactApollo];
|
||||
case GraphqlTransforms.TO_APOLLO_ANGULAR:
|
||||
return [typescriptPlugin, typescriptOperation, apolloAngular];
|
||||
case GraphqlTransforms.TO_STENCIL_APOLLO:
|
||||
return [typescriptPlugin, typescriptOperation, stencilApollo];
|
||||
case GraphqlTransforms.TO_TYPESCRIPT_RESOLVERS_SIGNATURE:
|
||||
return [typescriptPlugin, tsResolvers];
|
||||
case GraphqlTransforms.TO_JAVA_RESOLVERS_SIGNATURE:
|
||||
return [javaResolvers];
|
||||
case GraphqlTransforms.TO_FLOW_RESOLVERS_SIGNATURE:
|
||||
return [flowPlugin, flowResolvers];
|
||||
case GraphqlTransforms.TO_URQL:
|
||||
return [typescriptPlugin, typescriptOperation, urql];
|
||||
case GraphqlTransforms.TO_TYPESCRIPT_MONGODB:
|
||||
return [typescriptPlugin, tsMongoDB];
|
||||
}
|
||||
}
|
||||
|
||||
_self.onmessage = async ({ data: { id, payload } }) => {
|
||||
const { value, document = "", type, extension = "tsx" } = payload;
|
||||
|
||||
try {
|
||||
const plugins = getPlugins(type);
|
||||
|
||||
const pluginMap = {};
|
||||
|
||||
plugins.forEach((plugin, i) => {
|
||||
pluginMap[i + 1] = plugin;
|
||||
});
|
||||
|
||||
const result = await codegen({
|
||||
filename: `a.${extension}`,
|
||||
schema: parse(value),
|
||||
// @ts-ignore
|
||||
plugins: plugins.map((_plugin, i) => ({
|
||||
[i + 1]: {}
|
||||
})),
|
||||
documents: document.trim()
|
||||
? [
|
||||
{
|
||||
filePath: "",
|
||||
content: parse(document)
|
||||
}
|
||||
]
|
||||
: [],
|
||||
config: {},
|
||||
pluginMap
|
||||
});
|
||||
|
||||
_self.postMessage({
|
||||
id,
|
||||
payload: result
|
||||
});
|
||||
} catch (e) {
|
||||
_self.postMessage({
|
||||
id,
|
||||
err: e.message
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,20 @@
|
||||
import postcssJs from "postcss-js";
|
||||
import postcss from "postcss";
|
||||
|
||||
const _self: any = self;
|
||||
|
||||
_self.onmessage = ({ data: { payload, id } }) => {
|
||||
try {
|
||||
const root = postcss.parse(payload);
|
||||
|
||||
_self.postMessage({
|
||||
id,
|
||||
payload: JSON.stringify(postcssJs.objectify(root))
|
||||
});
|
||||
} catch (e) {
|
||||
_self.postMessage({
|
||||
id,
|
||||
err: e.message
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,37 @@
|
||||
import { BuiltInParserName } from "prettier";
|
||||
import { prettify } from "@utils/prettify";
|
||||
|
||||
interface Data {
|
||||
data: {
|
||||
payload: {
|
||||
value: string;
|
||||
language: BuiltInParserName;
|
||||
};
|
||||
id: string | number;
|
||||
};
|
||||
}
|
||||
|
||||
const _self = self as any;
|
||||
|
||||
_self.onmessage = ({
|
||||
data: {
|
||||
payload: { value, language },
|
||||
id
|
||||
}
|
||||
}: Data) => {
|
||||
(async function() {
|
||||
try {
|
||||
const payload = await prettify(language, value);
|
||||
|
||||
_self.postMessage({
|
||||
id,
|
||||
payload
|
||||
});
|
||||
} catch (e) {
|
||||
_self.postMessage({
|
||||
id,
|
||||
err: e.message
|
||||
});
|
||||
}
|
||||
})();
|
||||
};
|
||||
@@ -0,0 +1,44 @@
|
||||
import SVGO from "svgo";
|
||||
|
||||
const _self: any = self;
|
||||
|
||||
interface Data {
|
||||
payload: Payload;
|
||||
id: string;
|
||||
}
|
||||
|
||||
interface Payload {
|
||||
value: string;
|
||||
settings: any;
|
||||
}
|
||||
|
||||
_self.onmessage = ({ data: { id, payload } }: { data: Data }) => {
|
||||
delete payload.settings.optimizeSvg;
|
||||
|
||||
const plugins = Object.keys(payload.settings).filter(
|
||||
key => payload.settings[key]
|
||||
);
|
||||
|
||||
try {
|
||||
const svgo = new SVGO({
|
||||
full: true,
|
||||
// @ts-ignore
|
||||
plugins
|
||||
});
|
||||
|
||||
svgo.optimize(payload.value).then(result => {
|
||||
_self.postMessage({
|
||||
id,
|
||||
payload: result.data
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
if (IS_DEV) {
|
||||
console.error(e);
|
||||
}
|
||||
_self.postMessage({
|
||||
id,
|
||||
err: e.message
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,25 @@
|
||||
import convert from "@svgr/core/lib/convert";
|
||||
|
||||
const _self: any = self;
|
||||
|
||||
_self.onmessage = async ({ data: { payload, id } }) => {
|
||||
const { value, native } = payload;
|
||||
|
||||
try {
|
||||
const result = await convert(value, {
|
||||
plugins: [require("@svgr/plugin-jsx").default],
|
||||
svgo: false,
|
||||
native
|
||||
});
|
||||
|
||||
_self.postMessage({
|
||||
payload: result,
|
||||
id
|
||||
});
|
||||
} catch (e) {
|
||||
_self.postMessage({
|
||||
id,
|
||||
error: e.message
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user