Skip to content

Commit

Permalink
[compiler] Migrate compiler packages to tsup
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
poteto committed Mar 7, 2025
1 parent cc68006 commit 5d30190
Show file tree
Hide file tree
Showing 21 changed files with 517 additions and 298 deletions.
2 changes: 1 addition & 1 deletion compiler/apps/playground/next-env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
/// <reference types="next/image-types/global" />

// NOTE: This file should not be edited
// see https://nextjs.org/docs/app/api-reference/config/typescript for more information.
// see https://nextjs.org/docs/app/building-your-application/configuring/typescript for more information.
1 change: 1 addition & 0 deletions compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"prettier-plugin-hermes-parser": "^0.26.0",
"prompt-promise": "^1.0.3",
"rimraf": "^5.0.10",
"tsup": "^8.4.0",
"typescript": "^5.4.3",
"wait-on": "^7.2.0",
"yargs": "^17.7.2"
Expand Down
4 changes: 2 additions & 2 deletions compiler/packages/babel-plugin-react-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
"!*.tsbuildinfo"
],
"scripts": {
"build": "rimraf dist && scripts/build.js",
"build": "rimraf dist && tsup",
"test": "./scripts/link-react-compiler-runtime.sh && yarn snap:ci",
"jest": "yarn build && ts-node node_modules/.bin/jest",
"snap": "node ../snap/dist/main.js",
"snap:build": "yarn workspace snap run build",
"snap:ci": "yarn snap:build && yarn snap",
"ts:analyze-trace": "scripts/ts-analyze-trace.sh",
"lint": "yarn eslint src",
"watch": "scripts/build.js --watch"
"watch": "yarn build --watch"
},
"dependencies": {
"@babel/types": "^7.19.0"
Expand Down
61 changes: 0 additions & 61 deletions compiler/packages/babel-plugin-react-compiler/scripts/build.js

This file was deleted.

3 changes: 0 additions & 3 deletions compiler/packages/babel-plugin-react-compiler/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"moduleResolution": "Bundler",
"rootDir": "src",
"outDir": "dist",
// https://github.com/microsoft/TypeScript/issues/30925
"tsBuildInfoFile": "dist/tsconfig.tsbuildinfo",
"jsx": "react-jsxdev",
// weaken strictness from preset
"importsNotUsedAsValues": "remove",
Expand All @@ -16,7 +14,6 @@
"target": "ES2015",
// ideally turn off only during dev, or on a per-file basis
"noUnusedLocals": false,
"composite": true,
"removeComments": true
},
"exclude": [
Expand Down
29 changes: 29 additions & 0 deletions compiler/packages/babel-plugin-react-compiler/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {defineConfig} from 'tsup';

export default defineConfig({
entry: ['./src/index.ts'],
outDir: './dist',
external: ['@babel/types'],
splitting: false,
sourcemap: false,
dts: false,
bundle: true,
format: 'cjs',
platform: 'node',
banner: {
js: `/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @lightSyntaxTransform
* @noflow
* @nolint
* @preventMunge
* @preserve-invariant-messages
*/
"use no memo";`,
},
});
6 changes: 3 additions & 3 deletions compiler/packages/eslint-plugin-react-compiler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"description": "ESLint plugin to display errors found by the React compiler.",
"main": "dist/index.js",
"scripts": {
"build": "rimraf dist && scripts/build.js",
"test": "tsc && jest",
"watch": "scripts/build.js --watch"
"build": "rimraf dist && tsup",
"test": "jest",
"watch": "yarn build --watch"
},
"files": [
"dist"
Expand Down
67 changes: 0 additions & 67 deletions compiler/packages/eslint-plugin-react-compiler/scripts/build.js

This file was deleted.

33 changes: 19 additions & 14 deletions compiler/packages/eslint-plugin-react-compiler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@

import ReactCompilerRule from './rules/ReactCompilerRule';

module.exports = {
rules: {
'react-compiler': ReactCompilerRule,
},
configs: {
recommended: {
plugins: {
'react-compiler': {
rules: {
'react-compiler': ReactCompilerRule,
},
const meta = {
name: 'eslint-plugin-react-compiler',
};

const rules = {
'react-compiler': ReactCompilerRule,
};

const configs = {
recommended: {
plugins: {
'react-compiler': {
rules: {
'react-compiler': ReactCompilerRule,
},
},
rules: {
'react-compiler/react-compiler': 'error',
},
},
rules: {
'react-compiler/react-compiler': 'error',
},
},
};

export {configs, rules, meta};
3 changes: 0 additions & 3 deletions compiler/packages/eslint-plugin-react-compiler/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
"rootDir": "../",
"noEmit": true,
"jsx": "react-jsxdev",
"paths": {
"*": ["./src/types/*"]
},

// weaken strictness from preset
"importsNotUsedAsValues": "remove",
Expand Down
35 changes: 35 additions & 0 deletions compiler/packages/eslint-plugin-react-compiler/tsup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import {defineConfig} from 'tsup';

export default defineConfig({
entry: ['./src/index.ts'],
outDir: './dist',
external: [
'@babel/core',
'@babel/plugin-proposal-private-methods',
'hermes-parser',
'zod',
'zod-validation-error',
],
splitting: false,
sourcemap: false,
dts: false,
bundle: true,
format: 'cjs',
platform: 'node',
banner: {
js: `/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @lightSyntaxTransform
* @noflow
* @nolint
* @preventMunge
* @preserve-invariant-messages
*/
"use no memo";`,
},
});
4 changes: 2 additions & 2 deletions compiler/packages/make-read-only-util/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
"src"
],
"scripts": {
"build": "rimraf dist && scripts/build.js",
"build": "rimraf dist && tsup",
"test": "jest src",
"watch": "scripts/build.js --watch"
"watch": "yarn build --watch"
},
"dependencies": {
"invariant": "^2.2.4",
Expand Down
61 changes: 0 additions & 61 deletions compiler/packages/make-read-only-util/scripts/build.js

This file was deleted.

Loading

0 comments on commit 5d30190

Please sign in to comment.