Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(ui): add ability to compile using react compiler #6483

Merged
merged 5 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/next/.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"development": false,
"useBuiltins": true
}
}
},
"experimental": {
"plugins": [
[
Expand Down
2 changes: 2 additions & 0 deletions packages/ui/.browserslistrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> 1%
not dead
2 changes: 1 addition & 1 deletion packages/ui/.swcrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"development": false,
"useBuiltins": true
}
}
},
"experimental": {
"plugins": [
[
Expand Down
55 changes: 55 additions & 0 deletions packages/ui/babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const fs = require('fs')

const ReactCompilerConfig = {
sources: (filename) => {

if(!filename.endsWith('.tsx') && !filename.endsWith('.jsx')) {
return false
}

// read file and check if 'use client' is at top. if not, return false
// if it is, return true
const file = fs.readFileSync(filename, 'utf8')
if(file.includes("'use client'")) {
//console.log("Compiling: " + filename)
return true
}
console.log("Skipping: " + filename)
return false
},
//runtimeModule: "react"
}

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)$',
},
],
],
presets: [
[
'@babel/preset-env',
{
modules: false,
useBuiltIns: 'usage',
corejs: '3.22',
},
],
[
'@babel/preset-react',
{
throwIfNamespace: false,
runtime: 'automatic',
},
],
'@babel/preset-typescript',
],
}
}
11 changes: 10 additions & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,10 @@
],
"scripts": {
"build": "pnpm copyfiles && pnpm build:swc && pnpm build:types",
"build:babel": "babel src --out-dir dist --source-maps --extensions .ts,.js,.tsx,.jsx,.cjs,.mjs",
"build:reactcompiler": "pnpm copyfiles && pnpm build:babel && pnpm build:types",
"build:remove-artifact": "rm dist/prod/index.js",
"build:swc": "swc ./src -d ./dist --config-file .swcrc",
"build:swc": "swc ./src -d dist --config-file .swcrc",
"build:types": "tsc --emitDeclarationOnly --outDir dist",
"build:webpack": "webpack --config webpack.config.js",
"clean": "rimraf {dist,*.tsbuildinfo}",
Expand Down Expand Up @@ -116,13 +118,20 @@
"uuid": "9.0.1"
},
"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",
"@payloadcms/eslint-config": "workspace:*",
"@types/body-scroll-lock": "^3.1.0",
"@types/qs": "6.9.7",
"@types/react": "npm:[email protected]",
"@types/react-datepicker": "6.2.0",
"@types/react-dom": "npm:[email protected]",
"@types/uuid": "8.3.4",
"babel-plugin-react-compiler": "0.0.0-experimental-592953e-20240517",
"babel-plugin-transform-remove-imports": "^1.7.1",
"css-loader": "^6.10.0",
"css-minimizer-webpack-plugin": "^6.0.0",
"mini-css-extract-plugin": "1.6.2",
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/ArrayAction/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React from 'react'

import { Chevron } from '../../icons/Chevron/index.js'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/Banner/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import LinkImport from 'next/link.js' // TODO: abstract this out to support all routers
import type { MouseEvent } from 'react'

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/Card/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { ElementType } from 'react'

import React from 'react'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/CodeEditor/CodeEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import EditorImport from '@monaco-editor/react'
import React from 'react'

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/CodeEditor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React, { Suspense, lazy } from 'react'

import type { Props } from './types.js'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/DatePicker/DatePicker.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { ReactDatePickerProps } from 'react-datepicker'

import React from 'react'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/DatePicker/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React, { Suspense, lazy } from 'react'

import type { Props } from './types.js'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { UseDraggableArguments } from '@dnd-kit/core'

import React, { Fragment } from 'react'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/DraggableSortable/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { DragEndEvent } from '@dnd-kit/core'

import {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { UseDraggableArguments } from '@dnd-kit/core'

import { useSortable } from '@dnd-kit/sortable'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/Drawer/useDrawerSlug.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import { useId } from 'react'

import { useEditDepth } from '../../providers/EditDepth/index.js'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/Dropzone/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React from 'react'

import { useTranslation } from '../../providers/Translation/index.js'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/ErrorPill/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { I18nClient } from '@payloadcms/translations'

import React from 'react'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/FieldSelect/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { FieldWithPath } from 'payload/types'

import React, { Fragment, type JSX, useState } from 'react'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/FileDetails/FileMeta/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import { formatFilesize } from 'payload/utilities'
import React, { useState } from 'react'

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/FileDetails/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import { isImage } from 'payload/utilities'
import React from 'react'

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/IDLabel/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React from 'react'

import './index.scss'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import { getTranslation } from '@payloadcms/translations'
import React from 'react'

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/Localizer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import { getTranslation } from '@payloadcms/translations'
import { useRouter } from 'next/navigation.js'
import React from 'react'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React from 'react'

import { Chevron } from '../../../icons/Chevron/index.js'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/Pagination/Page/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React from 'react'

export type PageProps = {
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/Pagination/Separator/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React from 'react'

export const Separator: React.FC = () => <span className="paginator__separator">&mdash;</span>
1 change: 1 addition & 0 deletions packages/ui/src/elements/Popup/PopupTrigger/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React from 'react'

import './index.scss'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/PreviewSizes/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type {
Data,
FileSize,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { ClearIndicatorProps } from 'react-select'

import React from 'react'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/ReactSelect/Control/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { ControlProps } from 'react-select'

import React from 'react'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { DropdownIndicatorProps } from 'react-select'

import { Chevron } from '@payloadcms/ui/icons/Chevron'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/ReactSelect/MultiValue/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { MultiValueProps } from 'react-select'

import React from 'react'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { MultiValueProps } from 'react-select'

import React from 'react'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { MultiValueRemoveProps } from 'react-select'

import React, { type JSX } from 'react'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/ReactSelect/SingleValue/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { SingleValueProps } from 'react-select'

import React from 'react'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { ValueContainerProps } from 'react-select'

import React from 'react'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React from 'react'

export type RenderCustomComponentProps = {
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/SearchFilter/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import { getTranslation } from '@payloadcms/translations'
// TODO: abstract the `next/navigation` dependency out from this component
import { usePathname, useRouter } from 'next/navigation.js'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/SortComplex/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { OptionObject } from 'payload/types'
import type { SanitizedCollectionConfig } from 'payload/types'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { DefaultCellComponentProps } from 'payload/types'

import React from 'react'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/TableColumns/buildColumnState.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import { type CellComponentProps, type SanitizedCollectionConfig } from 'payload/types'
import React from 'react'

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/TableColumns/filterFields.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { FieldMap, MappedField } from '../../providers/ComponentMap/buildComponentMap/types.js'

// 1. Skips fields that are hidden, disabled, or presentational-only (i.e. `ui` fields)
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/ThumbnailCard/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { SanitizedCollectionConfig, TypeWithID } from 'payload/types'

import React from 'react'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React from 'react'

import type { Props } from './types.js'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React from 'react'

import type { Props } from './types.js'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
/* eslint-disable @typescript-eslint/no-floating-promises */
import type { PaginatedDocs } from 'payload/database'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { Option, OptionObject } from 'payload/types'

import { getTranslation } from '@payloadcms/translations'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React from 'react'

import type { Props } from './types.js'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/WhereBuilder/Condition/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React, { useEffect, useState } from 'react'

import type { FieldCondition } from '../types.js'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/WhereBuilder/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import { getTranslation } from '@payloadcms/translations'
import React, { useEffect, useState } from 'react'

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/elements/WhereBuilder/reduceFieldMap.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { Column } from '../Table/index.js'

import fieldTypes from './field-types.js'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/fields/Array/ArrayRow.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import type { FieldPermissions } from 'payload/auth'
import type { ArrayField, Row } from 'payload/types'

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React from 'react'

import { Search } from '../../../../graphics/Search/index.js'
Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/fields/Blocks/SectionTitle/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
'use client'
import React from 'react'

import { useField } from '../../../forms/useField/index.js'
Expand Down
Loading