From dca54c8932cfad5905c8b079be551ba741e07268 Mon Sep 17 00:00:00 2001 From: Sathya Gunasekaran Date: Thu, 2 Nov 2023 07:00:05 -0700 Subject: [PATCH] [playground] Use standalone prettier MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All of these warnings go away: ``` ➜ playground git:(remove-prettier) ✗ yarn dev yarn run v1.22.19 $ NODE_ENV=development && next dev ready - started server on 0.0.0.0:3000, url: http://localhost:3000 warn - You have enabled experimental feature (appDir) in next.config.js. warn - Experimental features are not covered by semver, and may cause unexpected or broken application behavior. Use at your own risk. info - Thank you for testing `appDir` please leave your feedback at https://nextjs.link/app-feedback event - compiled client and server successfully in 964 ms (199 modules) wait - compiling /page (client and server)... warn - ../../node_modules/prettier/index.js Critical dependency: the request of a dependency is an expression ../../node_modules/prettier/index.js Critical dependency: require function is used in a way in which dependencies cannot be statically extracted ../../node_modules/prettier/index.js Critical dependency: the request of a dependency is an expression ../../node_modules/prettier/index.js Critical dependency: the request of a dependency is an expression ../../node_modules/prettier/third-party.js Critical dependency: the request of a dependency is an expression wait - compiling... warn - ../../node_modules/prettier/index.js Critical dependency: the request of a dependency is an expression ../../node_modules/prettier/index.js Critical dependency: require function is used in a way in which dependencies cannot be statically extracted ../../node_modules/prettier/index.js Critical dependency: the request of a dependency is an expression ../../node_modules/prettier/index.js Critical dependency: the request of a dependency is an expression ../../node_modules/prettier/third-party.js Critical dependency: the request of a dependency is an expression ``` Standalone prettier is meant to be used in the browser: https://prettier.io/docs/en/browser --- .../playground/components/Editor/Output.tsx | 45 ++++++++++--------- compiler/apps/playground/package.json | 2 +- compiler/yarn.lock | 7 ++- 3 files changed, 32 insertions(+), 22 deletions(-) diff --git a/compiler/apps/playground/components/Editor/Output.tsx b/compiler/apps/playground/components/Editor/Output.tsx index 39abcdfc0f..6313620893 100644 --- a/compiler/apps/playground/components/Editor/Output.tsx +++ b/compiler/apps/playground/components/Editor/Output.tsx @@ -14,13 +14,13 @@ import { } from "@heroicons/react/outline"; import MonacoEditor, { DiffEditor } from "@monaco-editor/react"; import { type CompilerError } from "babel-plugin-react-forget"; -import prettier from "prettier"; -import prettierParserBabel from "prettier/parser-babel"; -import { memo, useCallback, useMemo, useState } from "react"; +import parserBabel from "prettier/plugins/babel"; +import * as prettierPluginEstree from "prettier/plugins/estree"; +import * as prettier from "prettier/standalone"; +import { memo, useCallback, useEffect, useState } from "react"; import { type Store } from "../../lib/stores"; import TabbedWindow from "../TabbedWindow"; import { monacoOptions } from "./monacoOptions"; - const MemoizedOutput = memo(Output); export default MemoizedOutput; @@ -54,7 +54,7 @@ type Props = { compilerOutput: CompilerOutput; }; -function tabify(source: string, compilerOutput: CompilerOutput) { +async function tabify(source: string, compilerOutput: CompilerOutput) { const tabs = new Map(); const reorderedTabs = new Map(); const concattedResults = new Map(); @@ -106,7 +106,7 @@ function tabify(source: string, compilerOutput: CompilerOutput) { output={text} diff={lastPassOutput ?? null} showInfoPanel={true} - > + >, ); lastPassOutput = text; } @@ -115,14 +115,14 @@ function tabify(source: string, compilerOutput: CompilerOutput) { // Make a synthetic Program so we can have a single AST with all the top level // FunctionDeclarations const ast = t.program(topLevelFnDecls); - const { code, sourceMapUrl } = codegen(ast, source); + const { code, sourceMapUrl } = await codegen(ast, source); reorderedTabs.set( "JS", + >, ); if (sourceMapUrl) { reorderedTabs.set( @@ -133,7 +133,7 @@ function tabify(source: string, compilerOutput: CompilerOutput) { className="w-full h-96" title="Generated Code" /> - + , ); } } @@ -143,23 +143,23 @@ function tabify(source: string, compilerOutput: CompilerOutput) { return reorderedTabs; } -function codegen( +async function codegen( ast: t.Program, - source: string -): { code: any; sourceMapUrl: string | null } { + source: string, +): Promise<{ code: any; sourceMapUrl: string | null }> { const generated = generate( ast, { sourceMaps: true, sourceFileName: "input.js" }, - source + source, ); const sourceMapUrl = getSourceMapUrl( generated.code, - JSON.stringify(generated.map) + JSON.stringify(generated.map), ); - const codegenOutput = prettier.format(generated.code, { + const codegenOutput = await prettier.format(generated.code, { semi: true, parser: "babel", - plugins: [prettierParserBabel], + plugins: [parserBabel, prettierPluginEstree], }); return { code: codegenOutput, sourceMapUrl }; } @@ -172,16 +172,21 @@ function getSourceMapUrl(code: string, map: string): string | null { code = utf16ToUTF8(code); map = utf16ToUTF8(map); return `https://evanw.github.io/source-map-visualization/#${btoa( - `${code.length}\0${code}${map.length}\0${map}` + `${code.length}\0${code}${map.length}\0${map}`, )}`; } function Output({ store, compilerOutput }: Props) { const [tabsOpen, setTabsOpen] = useState>(() => new Set()); - const tabs = useMemo( - () => tabify(store.source, compilerOutput), - [store.source, compilerOutput] + const [tabs, setTabs] = useState>( + () => new Map(), ); + useEffect(() => { + tabify(store.source, compilerOutput).then((tabs) => { + setTabs(tabs); + }); + }, [store.source, compilerOutput]); + const consoleLogError = useCallback(() => { if (compilerOutput.kind === "err") { console.error(compilerOutput.error); diff --git a/compiler/apps/playground/package.json b/compiler/apps/playground/package.json index 659b6a117a..ad629ce68c 100644 --- a/compiler/apps/playground/package.json +++ b/compiler/apps/playground/package.json @@ -29,7 +29,7 @@ "monaco-editor": "^0.34.1", "next": "^13.5.6", "notistack": "^3.0.0-alpha.7", - "prettier": "^2.8.8", + "prettier": "3.0.3", "pretty-format": "^29.3.1", "react": "18.2.0", "react-dom": "18.2.0" diff --git a/compiler/yarn.lock b/compiler/yarn.lock index 1bacc7b147..4775305740 100644 --- a/compiler/yarn.lock +++ b/compiler/yarn.lock @@ -9958,11 +9958,16 @@ prelude-ls@~1.1.2: resolved "https://registry.yarnpkg.com/prelude-ls/-/prelude-ls-1.1.2.tgz#21932a549f5e52ffd9a827f570e04be62a97da54" integrity sha512-ESF23V4SKG6lVSGZgYNpbsiaAkdab6ZgOxe52p7+Kid3W3u3bxR4Vfd/o21dmN7jSt0IwgZ4v5MUd26FEtXE9w== -prettier@2.8.8, prettier@^2.8.8: +prettier@2.8.8: version "2.8.8" resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.8.8.tgz#e8c5d7e98a4305ffe3de2e1fc4aca1a71c28b1da" integrity sha512-tdN8qQGvNjw4CHbY+XXk0JgCXn9QiF21a55rBe5LJAU+kDyC4WQn4+awm2Xfk2lQMk5fKup9XgzTZtGkjBdP9Q== +prettier@3.0.3: + version "3.0.3" + resolved "https://registry.yarnpkg.com/prettier/-/prettier-3.0.3.tgz#432a51f7ba422d1469096c0fdc28e235db8f9643" + integrity sha512-L/4pUDMxcNa8R/EthV08Zt42WBO4h1rarVtK0K+QJG0X187OLo7l699jWw0GKuwzkPQ//jMFA/8Xm6Fh3J/DAg== + pretty-format@^24: version "24.9.0" resolved "https://registry.yarnpkg.com/pretty-format/-/pretty-format-24.9.0.tgz#12fac31b37019a4eea3c11aa9a959eb7628aa7c9"