From 9a6ce3379d6390cf4b952e4ce261f9a975ffd8eb Mon Sep 17 00:00:00 2001 From: Lauren Tan Date: Sat, 11 May 2024 00:19:59 -0400 Subject: [PATCH] [healthcheck] Fix build runReactBabelPluginReactCompiler brings in fbt which is unnecessary for OSS so I removed it. Also makes it so healthckeck is installed as an executable ghstack-source-id: ec6c76f8be01483d4ca75d9d74037b3966fccbdb Pull Request resolved: https://github.com/facebook/react-forget/pull/2955 --- compiler/packages/healthcheck/package.json | 7 ++-- .../packages/healthcheck/rollup.config.js | 15 ++++++-- .../healthcheck/src/checks/reactCompiler.ts | 36 +++++++++++++++++-- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/compiler/packages/healthcheck/package.json b/compiler/packages/healthcheck/package.json index 0dbfbc7a32..b84b40473d 100644 --- a/compiler/packages/healthcheck/package.json +++ b/compiler/packages/healthcheck/package.json @@ -2,13 +2,16 @@ "name": "healthcheck", "version": "0.0.0", "description": "Health check script to test violations of the rules of react.", - "main": "dist/index.js", + "bin": "dist/index.js", "scripts": { "build": "rimraf dist && rollup --config --bundleConfigAsCjs", "test": "echo 'no tests'" }, "dependencies": { - "babel-plugin-react-compiler": "*", + "@babel/core": "^7.24.4", + "@babel/parser": "^7.24.4", + "zod": "^3.22.4", + "zod-validation-error": "^3.0.3", "chalk": "4", "fast-glob": "^3.3.2", "ora": "5.4.1", diff --git a/compiler/packages/healthcheck/rollup.config.js b/compiler/packages/healthcheck/rollup.config.js index 0394ee9535..ef9cca60a3 100644 --- a/compiler/packages/healthcheck/rollup.config.js +++ b/compiler/packages/healthcheck/rollup.config.js @@ -15,7 +15,16 @@ import terser from "@rollup/plugin-terser"; import prettier from "rollup-plugin-prettier"; import banner2 from "rollup-plugin-banner2"; -const NO_INLINE = new Set([]); +const NO_INLINE = new Set([ + "@babel/core", + "@babel/parser", + "chalk", + "fast-glob", + "ora", + "yargs", + "zod", + "zod-validation-error", +]); const DEV_ROLLUP_CONFIG = { input: "src/index.ts", @@ -48,7 +57,9 @@ const DEV_ROLLUP_CONFIG = { }), prettier(), banner2( - () => `/** + () => `#!/usr/bin/env node + +/** * Copyright (c) Meta Platforms, Inc. and affiliates. * * This source code is licensed under the MIT license found in the diff --git a/compiler/packages/healthcheck/src/checks/reactCompiler.ts b/compiler/packages/healthcheck/src/checks/reactCompiler.ts index 0db4ce0b59..3c4ed28094 100644 --- a/compiler/packages/healthcheck/src/checks/reactCompiler.ts +++ b/compiler/packages/healthcheck/src/checks/reactCompiler.ts @@ -5,9 +5,11 @@ * LICENSE file in the root directory of this source tree. */ -import { +import type * as BabelCore from "@babel/core"; +import { transformFromAstSync } from "@babel/core"; +import * as BabelParser from "@babel/parser"; +import BabelPluginReactCompiler, { ErrorSeverity, - runBabelPluginReactCompiler, type CompilerErrorDetailOptions, type PluginOptions, } from "babel-plugin-react-compiler/src"; @@ -63,6 +65,32 @@ function isActionableDiagnostic(detail: CompilerErrorDetailOptions) { } } +function runBabelPluginReactCompiler( + text: string, + file: string, + language: "flow" | "typescript", + options: Partial | null +): BabelCore.BabelFileResult { + const ast = BabelParser.parse(text, { + sourceFilename: file, + plugins: [language, "jsx"], + sourceType: "module", + }); + const result = transformFromAstSync(ast, text, { + filename: file, + highlightCode: false, + retainLines: true, + plugins: [[BabelPluginReactCompiler, options]], + sourceType: "module", + }); + if (result?.code == null) { + throw new Error( + `Expected BabelPluginReactForget to codegen successfully, got: ${result}` + ); + } + return result; +} + function compile(sourceCode: string, filename: string) { try { runBabelPluginReactCompiler( @@ -71,7 +99,9 @@ function compile(sourceCode: string, filename: string) { "typescript", COMPILER_OPTIONS ); - } catch {} + } catch (e) { + console.error(e); + } } const JsFileExtensionRE = /(js|ts|jsx|tsx|mjs)$/;