Skip to content

Commit 6663d19

Browse files
committed
[compiler] Migrate compiler packages to tsup
Currently in the `compiler` workspace, we invoke esbuild directly to build most packages (with the exception of `snap`). This has been mostly fine, but does not allow us to do things like generate type declaration files. I would like #32416 to be able to consume the merged eslint-plugin-react-compiler from source rather than via npm, and one of the things that has come up from my exploration in that stack using the compiler from source is that babel-plugin-react-compiler is missing type declarations. This is primarily because React's build process uses rollup + rollup-plugin-typescript, which runs tsc. So the merged plugin needs to typecheck properly in order to build. An alternative might be to migrate to something like babel with rollup instead to simply strip types rather than typecheck before building. The minor downside of that approach is that we would need to manually maintain a d.ts file for eslint-plugin-react-hooks. For now I would like to see if this PR helps us make progress rather than go for the slightly worse alternative. [`tsup`](https://github.com/egoist/tsup) is esbuild based so build performance is comparable. It is slower when generating d.ts files, but it's still much faster than rollup which we used prior to esbuild. For now, I have turned off `dts` by default, and it is only passed when publishing on npm. If you want to also generate d.ts files you can run `yarn build --dts`. ``` # BEFORE: build all compiler packages (esbuild) $ time yarn build ✨ Done in 15.61s. yarn build 13.82s user 1.54s system 96% cpu 15.842 total # --- # AFTER: build all compiler packages (tsup) $ time yarn build ✨ Done in 12.39s. yarn build 12.58s user 1.68s system 106% cpu 13.350 total # --- # AFTER: build all compiler packages and type declarations (tsup) $ time yarn build --dts ✨ Done in 30.69s. yarn build 43.57s user 3.20s system 150% cpu 31.061 total ``` I still need to test if this unblocks #32416 but this stack can be landed independently though as we could probably just release type declarations on npm. No one should be using the compiler directly, but if they really wanted to, lack of type declarations would not stop them (cf React secret internals). Note that I still kept esbuild as we still use it directly for forgive.
1 parent cc68006 commit 6663d19

File tree

23 files changed

+521
-299
lines changed

23 files changed

+521
-299
lines changed

compiler/apps/playground/next-env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
/// <reference types="next/image-types/global" />
33

44
// NOTE: This file should not be edited
5-
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
5+
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.

compiler/apps/playground/playwright.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default defineConfig({
2323
// Test directory
2424
testDir: path.join(__dirname, '__tests__/e2e'),
2525
// If a test fails, retry it additional 2 times
26-
retries: 2,
26+
retries: 3,
2727
// Artifacts folder where screenshots, videos, and traces are stored.
2828
outputDir: 'test-results/',
2929
// Note: we only use text snapshots, so its safe to omit the host environment name

compiler/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
"prettier-plugin-hermes-parser": "^0.26.0",
3838
"prompt-promise": "^1.0.3",
3939
"rimraf": "^5.0.10",
40+
"tsup": "^8.4.0",
4041
"typescript": "^5.4.3",
4142
"wait-on": "^7.2.0",
4243
"yargs": "^17.7.2"

compiler/packages/babel-plugin-react-compiler/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@
99
"!*.tsbuildinfo"
1010
],
1111
"scripts": {
12-
"build": "rimraf dist && scripts/build.js",
12+
"build": "rimraf dist && tsup",
1313
"test": "./scripts/link-react-compiler-runtime.sh && yarn snap:ci",
1414
"jest": "yarn build && ts-node node_modules/.bin/jest",
1515
"snap": "node ../snap/dist/main.js",
1616
"snap:build": "yarn workspace snap run build",
1717
"snap:ci": "yarn snap:build && yarn snap",
1818
"ts:analyze-trace": "scripts/ts-analyze-trace.sh",
1919
"lint": "yarn eslint src",
20-
"watch": "scripts/build.js --watch"
20+
"watch": "yarn build --watch"
2121
},
2222
"dependencies": {
2323
"@babel/types": "^7.19.0"

compiler/packages/babel-plugin-react-compiler/scripts/build.js

Lines changed: 0 additions & 61 deletions
This file was deleted.

compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Reanimated.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import {hasOwnProperty} from '../Utils/utils';
33
import {PluginOptions} from './Options';
44

55
function hasModule(name: string): boolean {
6+
if (typeof require === 'undefined') {
7+
return false;
8+
}
69
try {
710
return !!require.resolve(name);
811
} catch (error: any) {

compiler/packages/babel-plugin-react-compiler/tsconfig.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
"moduleResolution": "Bundler",
66
"rootDir": "src",
77
"outDir": "dist",
8-
// https://github.com/microsoft/TypeScript/issues/30925
9-
"tsBuildInfoFile": "dist/tsconfig.tsbuildinfo",
108
"jsx": "react-jsxdev",
119
// weaken strictness from preset
1210
"importsNotUsedAsValues": "remove",
@@ -16,7 +14,6 @@
1614
"target": "ES2015",
1715
// ideally turn off only during dev, or on a per-file basis
1816
"noUnusedLocals": false,
19-
"composite": true,
2017
"removeComments": true
2118
},
2219
"exclude": [
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import {defineConfig} from 'tsup';
2+
3+
export default defineConfig({
4+
entry: ['./src/index.ts'],
5+
outDir: './dist',
6+
external: ['@babel/types'],
7+
splitting: false,
8+
sourcemap: false,
9+
dts: false,
10+
bundle: true,
11+
format: 'cjs',
12+
platform: 'node',
13+
banner: {
14+
js: `/**
15+
* Copyright (c) Meta Platforms, Inc. and affiliates.
16+
*
17+
* This source code is licensed under the MIT license found in the
18+
* LICENSE file in the root directory of this source tree.
19+
*
20+
* @lightSyntaxTransform
21+
* @noflow
22+
* @nolint
23+
* @preventMunge
24+
* @preserve-invariant-messages
25+
*/
26+
27+
"use no memo";`,
28+
},
29+
});

compiler/packages/eslint-plugin-react-compiler/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"description": "ESLint plugin to display errors found by the React compiler.",
55
"main": "dist/index.js",
66
"scripts": {
7-
"build": "rimraf dist && scripts/build.js",
8-
"test": "tsc && jest",
9-
"watch": "scripts/build.js --watch"
7+
"build": "rimraf dist && tsup",
8+
"test": "jest",
9+
"watch": "yarn build --watch"
1010
},
1111
"files": [
1212
"dist"

compiler/packages/eslint-plugin-react-compiler/scripts/build.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)