From 0d2f2f444497bb389118d30624caf06d184cdf9a Mon Sep 17 00:00:00 2001 From: Michael Fix Date: Wed, 15 Aug 2018 20:57:24 -0700 Subject: [PATCH] Code split and load prettier dynamically (#471) * code split and load prettier dynamically; * move prettier to dependencies --- components/Settings.js | 11 +++++------ lib/util.js | 10 ++++++---- package.json | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/components/Settings.js b/components/Settings.js index 01f9bc1..ec18dee 100644 --- a/components/Settings.js +++ b/components/Settings.js @@ -31,12 +31,11 @@ class Settings extends React.Component { } format() { - try { - const newCode = formatCode(this.props.code) - this.props.onChange('code', newCode) - } catch (e) { - // pass, create a toast here in the future. - } + return formatCode(this.props.code) + .then(this.props.onChange.bind(this, 'code')) + .catch(() => { + // create toast here in the future + }) } render() { diff --git a/lib/util.js b/lib/util.js index 48fa357..82369b0 100644 --- a/lib/util.js +++ b/lib/util.js @@ -1,6 +1,4 @@ import morph from 'morphmorph' -import prettier from 'prettier/standalone' -import babylonParser from 'prettier/parser-babylon' const KEY = 'CARBON_STATE' @@ -43,10 +41,14 @@ export const fileToDataURL = blob => reader.readAsDataURL(blob) }) -export const formatCode = code => - prettier.format(code, { +export const formatCode = async code => { + const prettier = await import('prettier/standalone') + const babylonParser = await import('prettier/parser-babylon') + + return prettier.format(code, { parser: 'babylon', plugins: [babylonParser], semi: false, singleQuote: true }) +} diff --git a/package.json b/package.json index 8ffd63c..e1902e0 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "next": "^6.0.3", "next-offline": "^2.9.0", "now-logs": "^0.0.7", + "prettier": "^1.8.1", "puppeteer": "1.7.0", "react": "16.3.*", "react-click-outside": "^3.0.0", @@ -67,7 +68,6 @@ "husky": "^0.14.3", "lint-staged": "^7.0.4", "now": "^11.1.7", - "prettier": "^1.8.1", "webpack": "^3.10.0" }, "lint-staged": {