Skip to content

Commit

Permalink
feat!: prebundle payload, ui, richtext-lexical (#6579)
Browse files Browse the repository at this point in the history
# Breaking Changes

### New file import locations

Exports from the `payload` package have been _significantly_ cleaned up.
Now, just about everything is able to be imported from `payload`
directly, rather than an assortment of subpath exports. This means that
things like `import { buildConfig } from 'payload/config'` are now just
imported via `import { buildConfig } from 'payload'`. The mental model
is significantly simpler for developers, but you might need to update
some of your imports.

Payload now exposes only three exports:

1. `payload` - all types and server-only Payload code
2. `payload/shared` - utilities that can be used in either the browser
or in Node environments
3. `payload/node` - heavy utilities that should only be imported in Node
scripts and never be imported into bundled code like Next.js

### UI library pre-bundling

With this release, we've dramatically sped up the compile time for
Payload by pre-bundling our entire UI package for use inside of the
Payload admin itself. There are new exports that should be used within
Payload custom components:

1. `@payloadcms/ui/client` - all client components 
2. `@payloadcms/ui/server` - all server components

For all of your custom Payload admin UI components, you should be
importing from one of these two pre-compiled barrel files rather than
importing from the more deeply nested exports directly. That will keep
compile times nice and speedy, and will also make sure that the bundled
JS for your admin UI is kept small.

For example, whereas before, if you imported the Payload `Button`, you
would have imported it like this:

```ts
import { Button } from '@payloadcms/ui/elements/Button'
```

Now, you would import it like this:

```ts
import { Button } from '@payloadcms/ui/client'
```

This is a significant DX / performance optimization that we're pretty
pumped about.

However, if you are importing or re-using Payload UI components
_outside_ of the Payload admin UI, for example in your own frontend
apps, you can import from the individual component exports which will
make sure that the bundled JS is kept to a minimum in your frontend
apps. So in your own frontend, you can continue to import directly to
the components that you want to consume rather than importing from the
pre-compiled barrel files.

Individual component exports will now come with their corresponding CSS
and everything will work perfectly as-expected.

### Specific exports have changed

- `'@payloadcms/ui/templates/Default'` and
`'@payloadcms/ui/templates/Minimal`' are now exported from
`'@payloadcms/next/templates'`
- Old: `import { LogOut } from '@payloadcms/ui/icons/LogOut'` new:
`import { LogOutIcon } from '@payloadcms/ui/icons/LogOut'`

## Background info

In effort to make local dev as fast as possible, we need to import as
few files as possible so that the compiler has less to process. One way
we've achieved this in the Admin Panel was to _remove_ all .scss imports
from all components in the `@payloadcms/ui` module using a build
process. This stripped all `import './index.scss'` statements out of
each component before injecting them into `dist`. Instead, it bundles
all of the CSS into a single `main.css` file, and we import _that_ at
the root of the app.

While this concept is _still_ the right solution to the problem, this
particular approach is not viable when using these components outside
the Admin Panel, where not only does this root stylesheet not exist, but
where it would also bloat your app with unused styles. Instead, we need
to _keep_ these .scss imports in place so they are imported directly
alongside your components, as expected. Then, we need create a _new_
build step that _separately_ compiles the components _without_ their
stylesheets—this way your app can consume either as needed from the new
`client` and `server` barrel files within `@payloadcms/ui`, i.e. from
within `@payloadcms/next` and all other admin-specific packages and
plugins.

This way, all other applications will simply import using the direct
file paths, just as they did before. Except now they come with
stylesheets.

And we've gotten a pretty awesome initial compilation performance boost.

---------

Co-authored-by: James <james@trbl.design>
Co-authored-by: Alessio Gravili <alessio@gravili.de>
  • Loading branch information
3 people authored Jun 17, 2024
1 parent 3b3b1ce commit 9e76c8f
Show file tree
Hide file tree
Showing 1,297 changed files with 9,152 additions and 9,618 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ test-results
.localstack
.turbo

meta_client.json
meta_server.json
meta_index.json
meta_shared.json

.turbo

# Ignore test directory media folder/files
Expand Down
1 change: 1 addition & 0 deletions .idea/payload.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/(payload)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* THIS FILE WAS GENERATED AUTOMATICALLY BY PAYLOAD. */
import configPromise from '@payload-config'
import { RootLayout } from '@payloadcms/next/layouts'
// import '@payloadcms/ui/styles.css' // Uncomment this line if `@payloadcms/ui/client` in `tsconfig.json` points to `/ui/dist` instead of `/ui/src`
/* DO NOT MODIFY IT BECAUSE IT COULD BE REWRITTEN AT ANY TIME. */
import React from 'react'

Expand Down
16 changes: 9 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
"private": true,
"type": "module",
"scripts": {
"bf": "pnpm run build:force",
"build": "pnpm run build:core",
"build:all": "turbo build",
"build:app": "next build",
"build:app:analyze": "cross-env ANALYZE=true next build",
"build:clean": "pnpm clean:build",
"build:core": "turbo build --filter \"!@payloadcms/plugin-*\"",
"build:core:force": "pnpm clean:build && turbo build --filter \"!@payloadcms/plugin-*\" --no-cache --force",
"build:create-payload-app": "turbo build --filter create-payload-app",
Expand All @@ -16,6 +18,7 @@
"build:email-nodemailer": "turbo build --filter email-nodemailer",
"build:email-resend": "turbo build --filter email-resend",
"build:eslint-config-payload": "turbo build --filter eslint-config-payload",
"build:force": "pnpm run build:core:force",
"build:graphql": "turbo build --filter graphql",
"build:live-preview": "turbo build --filter live-preview",
"build:live-preview-react": "turbo build --filter live-preview-react",
Expand Down Expand Up @@ -43,8 +46,8 @@
"build:translations": "turbo build --filter translations",
"build:ui": "turbo build --filter ui",
"clean": "turbo clean",
"clean:all": "node ./scripts/delete-recursively.js '@node_modules' 'media' '**/dist' '**/.cache' '**/.next' '**/.turbo' '**/tsconfig.tsbuildinfo' '**/payload*.tgz'",
"clean:build": "node ./scripts/delete-recursively.js 'media' '**/dist' '**/.cache' '**/.next' '**/.turbo' '**/tsconfig.tsbuildinfo' '**/payload*.tgz'",
"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": "cross-env NODE_OPTIONS=--no-deprecation node ./test/dev.js",
"dev:generate-graphql-schema": "cross-env NODE_OPTIONS=--no-deprecation tsx ./test/generateGraphQLSchema.ts",
Expand All @@ -55,6 +58,7 @@
"docker:start": "docker compose -f packages/plugin-cloud-storage/docker-compose.yml up -d",
"docker:stop": "docker compose -f packages/plugin-cloud-storage/docker-compose.yml down",
"fix": "eslint \"packages/**/*.ts\" --fix",
"force:build": "pnpm run build:core:force",
"lint": "eslint \"packages/**/*.ts\"",
"lint-staged": "lint-staged",
"obliterate-playwright-cache-macos": "rm -rf ~/Library/Caches/ms-playwright && find /System/Volumes/Data/private/var/folders -type d -name 'playwright*' -exec rm -rf {} +",
Expand Down Expand Up @@ -91,7 +95,7 @@
"@payloadcms/eslint-config": "workspace:*",
"@payloadcms/live-preview-react": "workspace:*",
"@playwright/test": "1.43.0",
"@swc/cli": "^0.1.62",
"@swc/cli": "0.3.12",
"@swc/jest": "0.2.36",
"@testing-library/jest-dom": "6.4.2",
"@testing-library/react": "14.2.1",
Expand Down Expand Up @@ -147,13 +151,12 @@
"slash": "3.0.0",
"sort-package-json": "^2.10.0",
"swc-plugin-transform-remove-imports": "^1.12.1",
"tempfile": "^3.0.0",
"tempy": "^1.0.1",
"ts-node": "10.9.1",
"tsx": "^4.7.1",
"turbo": "^1.13.3",
"typescript": "5.4.5",
"uuid": "^9.0.1"
"uuid": "10.0.0"
},
"peerDependencies": {
"react": "^19.0.0 || ^19.0.0-rc-f994737d14-20240522",
Expand All @@ -166,8 +169,7 @@
"pnpm": {
"allowedDeprecatedVersions": {
"abab": "2",
"domexception": "4",
"uuid": "3.4.0"
"domexception": "4"
},
"overrides": {
"@types/react": "npm:types-react@19.0.0-beta.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-payload-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
],
"scripts": {
"build": "pnpm pack-template-files && pnpm typecheck && pnpm build:swc",
"build:swc": "swc ./src -d ./dist --config-file .swcrc",
"build:swc": "swc ./src -d ./dist --config-file .swcrc --strip-leading-paths",
"clean": "rimraf {dist,*.tsbuildinfo}",
"pack-template-files": "tsx src/scripts/pack-template-files.ts",
"prepublishOnly": "pnpm clean && pnpm build",
Expand Down
2 changes: 1 addition & 1 deletion packages/create-payload-app/src/lib/wrap-next-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import chalk from 'chalk'
import { Syntax, parseModule } from 'esprima-next'
import fs from 'fs'

import { log , warning } from '../utils/log.js'
import { log, warning } from '../utils/log.js'

export const withPayloadStatement = {
cjs: `const { withPayload } = require('@payloadcms/next/withPayload')\n`,
Expand Down
2 changes: 1 addition & 1 deletion packages/create-payload-app/src/utils/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import chalk from 'chalk'
import path from 'path'
import terminalLink from 'terminal-link'

import type { PackageManager , ProjectTemplate } from '../types.js'
import type { PackageManager, ProjectTemplate } from '../types.js'

import { getValidTemplates } from '../lib/templates.js'

Expand Down
2 changes: 1 addition & 1 deletion packages/create-payload-app/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"emitDeclarationOnly": true,
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
"rootDir": "./src" /* Specify the root folder within your source files. */,
"strict": true,
"strict": true
},
"exclude": ["dist", "build", "tests", "test", "node_modules", ".eslintrc.js"],
"include": ["src/**/*.ts", "src/**/*.spec.ts", "src/**/*.tsx", "src/**/*.d.ts", "src/**/*.json"]
Expand Down
2 changes: 1 addition & 1 deletion packages/db-mongodb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ npm install @payloadcms/db-mongodb
## Usage

```ts
import { buildConfig } from 'payload/config'
import { buildConfig } from 'payload'
import { mongooseAdapter } from '@payloadcms/db-mongodb'

export default buildConfig({
Expand Down
6 changes: 3 additions & 3 deletions packages/db-mongodb/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
"predefinedMigrations"
],
"scripts": {
"build": "pnpm build:swc && pnpm build:types",
"build:swc": "swc ./src -d ./dist --config-file .swcrc-build",
"build": "pnpm build:types && pnpm build:swc",
"build:swc": "swc ./src -d ./dist --config-file .swcrc-build --strip-leading-paths",
"build:types": "tsc --emitDeclarationOnly --outDir dist",
"clean": "rimraf {dist,*.tsbuildinfo}",
"prepublishOnly": "pnpm clean && pnpm turbo build"
Expand All @@ -39,7 +39,7 @@
"mongoose": "6.12.3",
"mongoose-paginate-v2": "1.7.22",
"prompts": "2.4.2",
"uuid": "^9.0.1"
"uuid": "10.0.0"
},
"devDependencies": {
"@payloadcms/eslint-config": "workspace:*",
Expand Down
2 changes: 1 addition & 1 deletion packages/db-mongodb/src/connect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-var-requires */
import type { ConnectOptions } from 'mongoose'
import type { Connect } from 'payload/database'
import type { Connect } from 'payload'

import mongoose from 'mongoose'

Expand Down
5 changes: 2 additions & 3 deletions packages/db-mongodb/src/count.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { QueryOptions } from 'mongoose'
import type { Count } from 'payload/database'
import type { PayloadRequestWithData } from 'payload/types'
import type { Count, PayloadRequestWithData } from 'payload'

import { flattenWhereToOperators } from 'payload/database'
import { flattenWhereToOperators } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand Down
3 changes: 1 addition & 2 deletions packages/db-mongodb/src/create.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { Create } from 'payload/database'
import type { Document, PayloadRequestWithData } from 'payload/types'
import type { Create, Document, PayloadRequestWithData } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand Down
3 changes: 1 addition & 2 deletions packages/db-mongodb/src/createGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { CreateGlobal } from 'payload/database'
import type { PayloadRequestWithData } from 'payload/types'
import type { CreateGlobal, PayloadRequestWithData } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand Down
3 changes: 1 addition & 2 deletions packages/db-mongodb/src/createGlobalVersion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { CreateGlobalVersion } from 'payload/database'
import type { Document , PayloadRequestWithData } from 'payload/types'
import type { CreateGlobalVersion, Document, PayloadRequestWithData } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand Down
4 changes: 2 additions & 2 deletions packages/db-mongodb/src/createMigration.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* eslint-disable no-restricted-syntax, no-await-in-loop */
import type { CreateMigration, MigrationTemplateArgs } from 'payload/database'
import type { CreateMigration, MigrationTemplateArgs } from 'payload'

import fs from 'fs'
import path from 'path'
import { getPredefinedMigration } from 'payload/database'
import { getPredefinedMigration } from 'payload'
import { fileURLToPath } from 'url'

const migrationTemplate = ({ downSQL, imports, upSQL }: MigrationTemplateArgs): string => `import {
Expand Down
3 changes: 1 addition & 2 deletions packages/db-mongodb/src/createVersion.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { CreateVersion } from 'payload/database'
import type { Document , PayloadRequestWithData } from 'payload/types'
import type { CreateVersion, Document, PayloadRequestWithData } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand Down
3 changes: 1 addition & 2 deletions packages/db-mongodb/src/deleteMany.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { DeleteMany } from 'payload/database'
import type { PayloadRequestWithData } from 'payload/types'
import type { DeleteMany, PayloadRequestWithData } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand Down
3 changes: 1 addition & 2 deletions packages/db-mongodb/src/deleteOne.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { DeleteOne } from 'payload/database'
import type { Document , PayloadRequestWithData } from 'payload/types'
import type { DeleteOne, Document, PayloadRequestWithData } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand Down
3 changes: 1 addition & 2 deletions packages/db-mongodb/src/deleteVersions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { DeleteVersions } from 'payload/database'
import type { PayloadRequestWithData } from 'payload/types'
import type { DeleteVersions, PayloadRequestWithData } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand Down
2 changes: 1 addition & 1 deletion packages/db-mongodb/src/destroy.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Destroy } from 'payload/database'
import type { Destroy } from 'payload'

import mongoose from 'mongoose'

Expand Down
5 changes: 2 additions & 3 deletions packages/db-mongodb/src/find.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { PaginateOptions } from 'mongoose'
import type { Find } from 'payload/database'
import type { PayloadRequestWithData } from 'payload/types'
import type { Find, PayloadRequestWithData } from 'payload'

import { flattenWhereToOperators } from 'payload/database'
import { flattenWhereToOperators } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand Down
5 changes: 2 additions & 3 deletions packages/db-mongodb/src/findGlobal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type { FindGlobal } from 'payload/database'
import type { PayloadRequestWithData } from 'payload/types'
import type { FindGlobal, PayloadRequestWithData } from 'payload'

import { combineQueries } from 'payload/database'
import { combineQueries } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand Down
6 changes: 2 additions & 4 deletions packages/db-mongodb/src/findGlobalVersions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import type { PaginateOptions } from 'mongoose'
import type { FindGlobalVersions } from 'payload/database'
import type { PayloadRequestWithData } from 'payload/types'
import type { FindGlobalVersions, PayloadRequestWithData } from 'payload'

import { flattenWhereToOperators } from 'payload/database'
import { buildVersionGlobalFields } from 'payload/versions'
import { buildVersionGlobalFields, flattenWhereToOperators } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand Down
3 changes: 1 addition & 2 deletions packages/db-mongodb/src/findOne.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { MongooseQueryOptions } from 'mongoose'
import type { FindOne } from 'payload/database'
import type { Document , PayloadRequestWithData } from 'payload/types'
import type { Document, FindOne, PayloadRequestWithData } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand Down
5 changes: 2 additions & 3 deletions packages/db-mongodb/src/findVersions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { PaginateOptions } from 'mongoose'
import type { FindVersions } from 'payload/database'
import type { PayloadRequestWithData } from 'payload/types'
import type { FindVersions, PayloadRequestWithData } from 'payload'

import { flattenWhereToOperators } from 'payload/database'
import { flattenWhereToOperators } from 'payload'

import type { MongooseAdapter } from './index.js'

Expand Down
5 changes: 2 additions & 3 deletions packages/db-mongodb/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import type { TransactionOptions } from 'mongodb'
import type { MongoMemoryReplSet } from 'mongodb-memory-server'
import type { ClientSession, ConnectOptions, Connection } from 'mongoose'
import type { Payload } from 'payload'
import type { BaseDatabaseAdapter, DatabaseAdapterObj } from 'payload/database'
import type { BaseDatabaseAdapter, DatabaseAdapterObj, Payload } from 'payload'

import fs from 'fs'
import mongoose from 'mongoose'
import path from 'path'
import { createDatabaseAdapter } from 'payload/database'
import { createDatabaseAdapter } from 'payload'

import type { CollectionModel, GlobalModel } from './types.js'

Expand Down
5 changes: 2 additions & 3 deletions packages/db-mongodb/src/init.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint-disable no-param-reassign */
import type { PaginateOptions } from 'mongoose'
import type { Init } from 'payload/database'
import type { SanitizedCollectionConfig } from 'payload/types'
import type { Init, SanitizedCollectionConfig } from 'payload'

import mongoose from 'mongoose'
import paginate from 'mongoose-paginate-v2'
import { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload/versions'
import { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload'

import type { MongooseAdapter } from './index.js'
import type { CollectionModel } from './types.js'
Expand Down
9 changes: 2 additions & 7 deletions packages/db-mongodb/src/migrateFresh.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
import type { PayloadRequestWithData } from 'payload/types'
import type { PayloadRequestWithData } from 'payload'

import {
commitTransaction,
initTransaction,
killTransaction,
readMigrationFiles,
} from 'payload/database'
import { commitTransaction, initTransaction, killTransaction, readMigrationFiles } from 'payload'
import prompts from 'prompts'

import type { MongooseAdapter } from './index.js'
Expand Down
3 changes: 1 addition & 2 deletions packages/db-mongodb/src/models/buildCollectionSchema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { PaginateOptions, Schema } from 'mongoose'
import type { SanitizedConfig } from 'payload/config'
import type { SanitizedCollectionConfig } from 'payload/types'
import type { SanitizedCollectionConfig, SanitizedConfig } from 'payload'

import paginate from 'mongoose-paginate-v2'

Expand Down
2 changes: 1 addition & 1 deletion packages/db-mongodb/src/models/buildGlobalModel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SanitizedConfig } from 'payload/config'
import type { SanitizedConfig } from 'payload'

import mongoose from 'mongoose'

Expand Down
7 changes: 4 additions & 3 deletions packages/db-mongodb/src/models/buildSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/* eslint-disable class-methods-use-this */
/* eslint-disable @typescript-eslint/no-use-before-define */
import type { IndexOptions, Schema, SchemaOptions, SchemaTypeOptions } from 'mongoose'
import type { SanitizedConfig, SanitizedLocalizationConfig } from 'payload/config'
import type {
ArrayField,
Block,
Expand All @@ -23,21 +22,23 @@ import type {
RelationshipField,
RichTextField,
RowField,
SanitizedConfig,
SanitizedLocalizationConfig,
SelectField,
Tab,
TabsField,
TextField,
TextareaField,
UploadField,
} from 'payload/types'
} from 'payload'

import mongoose from 'mongoose'
import {
fieldAffectsData,
fieldIsLocalized,
fieldIsPresentationalOnly,
tabHasName,
} from 'payload/types'
} from 'payload/shared'

export type BuildSchemaOptions = {
allowIDField?: boolean
Expand Down
Loading

0 comments on commit 9e76c8f

Please sign in to comment.