Merge pull request #49 from transform-it/web-worker

moved prettier to worker
This commit is contained in:
Ritesh Kumar authored and GitHub committed 2017-09-12 03:24:03 +05:30
commit 644167a9cc
4 files changed
+91 -46

No files matched your search

+46 -45
View File
@@ -91,17 +91,27 @@ export default class ConversionPanel extends PureComponent {
value: "",
splitValue: "",
info: "",
infoType: ""
infoType: "",
isPrettierAvailable: false
};
componentDidMount() {
const { defaultText, splitValue } = this.props;
this.onChange(defaultText, splitValue);
const MyWorker = require('worker-loader!../utils/worker.js')
this.worker = new MyWorker()
this.worker.onmessage = this.setPrettyResult
}
isPrettierAvailable() {
return isBrowser && !!window.prettier;
setPrettyResult = ({ data }) => {
const {available, prettyCode, section} = data
this.setState({
isPrettierAvailable: available,
[section]: prettyCode
})
}
fetchJSON = async () => {
@@ -119,7 +129,13 @@ export default class ConversionPanel extends PureComponent {
}
};
prettify = (code, mode, section) => {
this.worker.postMessage({ code, mode, section })
}
onChange = async (newValue, leftSplitValue) => {
const { prettifyRightPanel } = this.props
const nValue = newValue || this.state.value;
const splitValue =
leftSplitValue && typeof leftSplitValue === "string"
@@ -128,8 +144,18 @@ export default class ConversionPanel extends PureComponent {
try {
const code = await this.props.getTransformedValue(nValue, splitValue);
if (prettifyRightPanel) {
this.worker.postMessage({
code,
mode: this.props.rightMode,
section: 'resultValue'
})
} else {
this.setState({
resultValue: code
})
}
this.setState({
resultValue: code,
info: "",
infoType: ""
});
@@ -155,46 +181,35 @@ export default class ConversionPanel extends PureComponent {
}
};
setResult = result => {
this.setState({
value: result
});
};
setSplitValue = splitValue => this.setState({ splitValue });
prettifyCode = () => {
if (!this.isPrettierAvailable()) return;
if (!this.state.isPrettierAvailable) return;
const { leftMode } = this.props;
const { value } = this.state;
this.setResult(
window.prettier.format(value, { parser: prettierParsers[leftMode] })
);
this.worker.postMessage({
code: value,
mode: leftMode,
section: 'value'
})
};
prettifySplitCode = () => {
if (!this.isPrettierAvailable()) return;
if (!this.state.isPrettierAvailable) return;
const { splitMode } = this.props;
const { splitValue } = this.state;
this.setSplitValue(
window.prettier.format(splitValue, { parser: prettierParsers[splitMode] })
);
this.worker.postMessage({
code: splitValue,
mode: splitMode,
section: 'splitValue'
})
};
copyCode = () => {
const { rightMode } = this.props;
const { resultValue } = this.state;
const parser = prettierParsers[rightMode];
copy(
parser
? window.prettier.format(resultValue, {
parser
})
: resultValue
);
copy(resultValue);
this.setState({
info: "Code copied to clipboard.",
infoType: "success"
@@ -203,7 +218,7 @@ export default class ConversionPanel extends PureComponent {
getPrettifyClass = () => {
return cn("btn", {
disabled: !this.isPrettierAvailable()
disabled: !this.state.isPrettierAvailable
})
}
@@ -216,7 +231,6 @@ export default class ConversionPanel extends PureComponent {
initialCheckboxValue,
leftTitle,
rightTitle,
prettifyRightPanel,
splitLeft,
splitTitle,
splitMode,
@@ -251,7 +265,6 @@ export default class ConversionPanel extends PureComponent {
return (
<div className="wrapper">
<script src="https://bundle.run/prettier@1.6.1" />
<style jsx>{`
.wrapper {
display: flex;
@@ -498,19 +511,7 @@ export default class ConversionPanel extends PureComponent {
</div>
{isBrowser && (
<CodeMirror
value={
this.isPrettierAvailable() &&
prettifyRightPanel &&
prettierParsers[rightMode] &&
resultValue ? (
window.prettier.format(resultValue, {
parser: prettierParsers[rightMode],
printWidth: 70
})
) : (
resultValue
)
}
value={resultValue}
options={{
readOnly: true,
mode: modeMapping[rightMode] || rightMode,
+2 -1
View File
@@ -66,6 +66,7 @@
"prettier": "^1.6.1",
"style-loader": "^0.18.2",
"sw-precache-webpack-plugin": "^0.11.4",
"webpack-bundle-analyzer": "^2.9.0"
"webpack-bundle-analyzer": "^2.9.0",
"worker-loader": "^0.8.1"
}
}
+36
View File
@@ -0,0 +1,36 @@
try {
importScripts('https://bundle.run/prettier@1.6.1')
} catch (e) {
self.postMessage({ available: false })
}
const prettierParsers = {
css: "postcss",
javascript: "babylon",
jsx: "babylon",
graphql: "graphql",
json: "json",
typescript: "typescript",
flow: "flow"
};
if (self.prettier) {
postMessage({ available: true })
}
onmessage = function ({ data }) {
const {code, mode, section } = data
const prettyCode = self.prettier &&
prettierParsers[mode] &&
code ? (
self.prettier.format(code, {
parser: prettierParsers[mode],
printWidth: 70
})
) : (
code
)
postMessage({ available: !!self.prettier, prettyCode, section })
}
+7
View File
@@ -5791,6 +5791,13 @@ wordwrap@0.0.2:
version "0.0.2"
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.2.tgz#b79669bb42ecb409f83d583cad52ca17eaa1643f"
worker-loader@^0.8.1:
version "0.8.1"
resolved "https://registry.yarnpkg.com/worker-loader/-/worker-loader-0.8.1.tgz#e8e995331ea34df5bf68296824bfb7f0ad578d43"
dependencies:
loader-utils "^1.0.2"
schema-utils "^0.3.0"
wrap-ansi@^2.0.0:
version "2.1.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-2.1.0.tgz#d8fc3d284dd05794fe84973caecdd1cf824fdd85"