-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: pre-compile ui and richtext-lexical with react compiler (#7688)
This noticeably improves performance in the admin panel, for example when there are multiple richtext editors on one page (& likely performance in other areas too, though I mainly tested rich text). The babel plugin currently only optimizes files with a 'use client' directive at the top - thus we have to make sure to add use client wherever possible, even if it's imported by a parent client component. There's one single component that broke when it was compiled using the React compiler (it stopped being reactive and failed one of our admin e2e tests): 150808f opting out of it completely fixed that issue Fixes #7366
- Loading branch information
Showing
182 changed files
with
897 additions
and
587 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,7 @@ | |
"clean:all": "node ./scripts/delete-recursively.js '@node_modules' 'media/*' '**/dist/' '**/.cache/*' '**/.next/*' '**/.turbo/*' '**/tsconfig.tsbuildinfo' '**/payload*.tgz' '**/meta_*.json'", | ||
"clean:build": "node ./scripts/delete-recursively.js 'media/' '**/dist/' '**/.cache/' '**/.next/' '**/.turbo/' '**/tsconfig.tsbuildinfo' '**/payload*.tgz' '**/meta_*.json'", | ||
"clean:cache": "node ./scripts/delete-recursively.js node_modules/.cache! packages/payload/node_modules/.cache! .next/*", | ||
"dev": "pnpm runts ./test/dev.ts", | ||
"dev": "tsx ./test/dev.ts", | ||
"dev:generate-graphql-schema": "pnpm runts ./test/generateGraphQLSchema.ts", | ||
"dev:generate-importmap": "pnpm runts ./test/generateImportMap.ts", | ||
"dev:generate-types": "pnpm runts ./test/generateTypes.ts", | ||
|
@@ -81,6 +81,8 @@ | |
"test:e2e": "pnpm runts ./test/runE2E.ts", | ||
"test:e2e:debug": "cross-env NODE_OPTIONS=--no-deprecation NODE_NO_WARNINGS=1 PWDEBUG=1 DISABLE_LOGGING=true playwright test", | ||
"test:e2e:headed": "cross-env NODE_OPTIONS=--no-deprecation NODE_NO_WARNINGS=1 DISABLE_LOGGING=true playwright test --headed", | ||
"test:e2e:prod": "pnpm bf && rm -rf test/packed && rm -rf test/node_modules && rm -f test/pnpm-lock.yaml && pnpm run script:pack --all --no-build --dest test/packed && pnpm runts test/setupProd.ts && cd test && pnpm i --ignore-workspace && cd .. && pnpm runts ./test/runE2E.ts --prod", | ||
"test:e2e:prod:ci": "rm -rf test/node_modules && rm -f test/pnpm-lock.yaml && pnpm run script:pack --all --no-build --dest test/packed && pnpm runts test/setupProd.ts && cd test && pnpm i --ignore-workspace && cd .. && pnpm runts ./test/runE2E.ts --prod", | ||
"test:int": "cross-env NODE_OPTIONS=\"--no-deprecation\" NODE_NO_WARNINGS=1 DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=test/jest.config.js --runInBand", | ||
"test:int:postgres": "cross-env NODE_OPTIONS=\"--no-deprecation\" NODE_NO_WARNINGS=1 PAYLOAD_DATABASE=postgres DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=test/jest.config.js --runInBand", | ||
"test:unit": "cross-env NODE_OPTIONS=\"--no-deprecation\" NODE_NO_WARNINGS=1 DISABLE_LOGGING=true jest --forceExit --detectOpenHandles --config=jest.config.js --runInBand", | ||
|
@@ -162,7 +164,6 @@ | |
"react": "^19.0.0 || ^19.0.0-rc-06d0b89e-20240801", | ||
"react-dom": "^19.0.0 || ^19.0.0-rc-06d0b89e-20240801" | ||
}, | ||
"packageManager": "[email protected]", | ||
"engines": { | ||
"node": "^18.20.2 || >=20.9.0", | ||
"pnpm": "^9.7.0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
const fs = require('fs') | ||
|
||
// Plugin options can be found here: https://github.com/facebook/react/blob/main/compiler/packages/babel-plugin-react-compiler/src/Entrypoint/Options.ts#L38 | ||
const ReactCompilerConfig = { | ||
sources: (filename) => { | ||
const isInNodeModules = filename.includes('node_modules') | ||
if (isInNodeModules || ( !filename.endsWith('.tsx') && !filename.endsWith('.jsx') && !filename.endsWith('.js'))) { | ||
return false | ||
} | ||
|
||
// Only compile files with 'use client' directives. We do not want to | ||
// accidentally compile React Server Components | ||
const file = fs.readFileSync(filename, 'utf8') | ||
if (file.includes("'use client'")) { | ||
return true | ||
} | ||
console.log('React compiler - skipping file: ' + filename) | ||
return false | ||
}, | ||
} | ||
|
||
module.exports = function (api) { | ||
api.cache(false) | ||
|
||
return { | ||
plugins: [ | ||
['babel-plugin-react-compiler', ReactCompilerConfig], // must run first! | ||
/* [ | ||
'babel-plugin-transform-remove-imports', | ||
{ | ||
test: '\\.(scss|css)$', | ||
}, | ||
],*/ | ||
], | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,11 +36,14 @@ | |
"dist" | ||
], | ||
"scripts": { | ||
"build": "rm -rf dist && rm -rf tsconfig.tsbuildinfo && pnpm copyfiles && pnpm build:types && pnpm build:swc && pnpm build:esbuild", | ||
"build": "pnpm build:reactcompiler", | ||
"build:babel": "rm -rf dist_optimized && babel dist --out-dir dist_optimized --source-maps --extensions .ts,.js,.tsx,.jsx,.cjs,.mjs && rm -rf dist && mv dist_optimized dist", | ||
"build:clean": "find . \\( -type d \\( -name build -o -name dist -o -name .cache \\) -o -type f -name tsconfig.tsbuildinfo \\) -exec rm -rf {} + && pnpm build", | ||
"build:esbuild": "node bundle.js", | ||
"build:esbuild": "node bundle.js && rm -rf dist/exports/client && mv dist/exports/client_optimized dist/exports/client", | ||
"build:reactcompiler": "rm -rf dist && rm -rf tsconfig.tsbuildinfo && pnpm build:swc && pnpm build:babel && pnpm copyfiles && pnpm build:esbuild && pnpm build:types", | ||
"build:swc": "swc ./src -d ./dist --config-file .swcrc --strip-leading-paths", | ||
"build:types": "tsc --emitDeclarationOnly --outDir dist", | ||
"build_without_reactcompiler": "rm -rf dist && rm -rf tsconfig.tsbuildinfo && pnpm copyfiles && pnpm build:types && pnpm build:swc && pnpm build:esbuild && rm -rf dist/exports/client && mv dist/exports/client_unoptimized dist/exports/client", | ||
"clean": "rimraf {dist,*.tsbuildinfo}", | ||
"copyfiles": "copyfiles -u 1 \"src/**/*.{html,css,ttf,woff,woff2,eot,svg,jpg,png,json}\" dist/", | ||
"prepublishOnly": "pnpm clean && pnpm turbo build", | ||
|
@@ -64,6 +67,11 @@ | |
"uuid": "10.0.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/cli": "^7.24.5", | ||
"@babel/core": "^7.24.5", | ||
"@babel/preset-env": "^7.24.5", | ||
"@babel/preset-react": "^7.24.1", | ||
"@babel/preset-typescript": "^7.24.1", | ||
"@lexical/eslint-plugin": "0.17.0", | ||
"@payloadcms/eslint-config": "workspace:*", | ||
"@payloadcms/next": "workspace:*", | ||
|
@@ -73,8 +81,11 @@ | |
"@types/node": "20.12.5", | ||
"@types/react": "npm:[email protected]", | ||
"@types/react-dom": "npm:[email protected]", | ||
"babel-plugin-react-compiler": "0.0.0-experimental-1cd8995-20240814", | ||
"babel-plugin-transform-remove-imports": "^1.8.0", | ||
"esbuild": "0.23.0", | ||
"esbuild-sass-plugin": "3.3.1", | ||
"eslint-plugin-react-compiler": "0.0.0-experimental-d0e920e-20240815", | ||
"payload": "workspace:*", | ||
"swc-plugin-transform-remove-imports": "1.15.0" | ||
}, | ||
|
1 change: 1 addition & 0 deletions
1
packages/richtext-lexical/src/features/align/client/toolbarAlignGroup.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/richtext-lexical/src/features/blocks/client/component/BlockContent.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/richtext-lexical/src/features/blocks/client/component/FormSavePlugin.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
'use client' | ||
import type { Data, FormState } from 'payload' | ||
import type React from 'react' | ||
|
||
|
1 change: 1 addition & 0 deletions
1
packages/richtext-lexical/src/features/blocks/client/component/removeEmptyArrayValues.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
'use client' | ||
import type { FormState } from 'payload' | ||
|
||
/** | ||
|
1 change: 1 addition & 0 deletions
1
packages/richtext-lexical/src/features/blocks/client/nodes/BlocksNode.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/richtext-lexical/src/features/blocks/client/nodes/InlineBlocksNode.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
'use client' | ||
import type { | ||
EditorConfig, | ||
LexicalEditor, | ||
|
1 change: 1 addition & 0 deletions
1
packages/richtext-lexical/src/features/blocks/client/plugin/commands.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
'use client' | ||
import type { LexicalCommand } from 'lexical' | ||
|
||
import { createCommand } from 'lexical' | ||
|
8 changes: 1 addition & 7 deletions
8
...-lexical/src/features/experimental_table/client/plugins/TableHoverActionsPlugin/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/richtext-lexical/src/features/experimental_table/client/utils/debounce.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/richtext-lexical/src/features/experimental_table/client/utils/useDebounce.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
'use client' | ||
import { useMemo, useRef } from 'react' | ||
|
||
import debounce from './debounce.js' | ||
|
Oops, something went wrong.