# 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>
182 lines
5.5 KiB
TypeScript
182 lines
5.5 KiB
TypeScript
import type { Config } from 'payload'
|
|
import type { Payload } from 'payload'
|
|
import { jest } from '@jest/globals'
|
|
|
|
import nodemailer from 'nodemailer'
|
|
import { defaults } from 'payload'
|
|
|
|
import { payloadCloudPlugin } from './plugin.js'
|
|
import { nodemailerAdapter } from '@payloadcms/email-nodemailer'
|
|
|
|
const mockedPayload: Payload = jest.fn() as unknown as Payload
|
|
|
|
describe('plugin', () => {
|
|
let createTransportSpy: jest.Spied<any>
|
|
|
|
const skipVerify = true
|
|
|
|
beforeEach(() => {
|
|
createTransportSpy = jest.spyOn(nodemailer, 'createTransport').mockImplementationOnce(() => {
|
|
return {
|
|
verify: jest.fn(),
|
|
transporter: {
|
|
name: 'Nodemailer - SMTP',
|
|
},
|
|
} as unknown as ReturnType<typeof nodemailer.createTransport>
|
|
})
|
|
|
|
const createTestAccountSpy = jest.spyOn(nodemailer, 'createTestAccount').mockResolvedValueOnce({
|
|
pass: 'password',
|
|
user: 'user',
|
|
web: 'ethereal.email',
|
|
} as unknown as nodemailer.TestAccount)
|
|
})
|
|
|
|
describe('not in Payload Cloud', () => {
|
|
// eslint-disable-next-line jest/expect-expect
|
|
it('should return unmodified config', async () => {
|
|
const plugin = payloadCloudPlugin()
|
|
const config = await plugin(createConfig())
|
|
|
|
assertNoCloudStorage(config)
|
|
expect(config.email).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
describe('in Payload Cloud', () => {
|
|
beforeEach(() => {
|
|
process.env.PAYLOAD_CLOUD = 'true'
|
|
process.env.PAYLOAD_CLOUD_EMAIL_API_KEY = 'test-key'
|
|
process.env.PAYLOAD_CLOUD_DEFAULT_DOMAIN = 'test-domain.com'
|
|
})
|
|
|
|
describe('storage', () => {
|
|
// eslint-disable-next-line jest/expect-expect
|
|
it('should default to using payload cloud storage', async () => {
|
|
const plugin = payloadCloudPlugin()
|
|
const config = await plugin(createConfig())
|
|
|
|
assertCloudStorage(config)
|
|
})
|
|
|
|
// eslint-disable-next-line jest/expect-expect
|
|
it('should allow opt-out', async () => {
|
|
const plugin = payloadCloudPlugin({ storage: false })
|
|
const config = await plugin(createConfig())
|
|
|
|
assertNoCloudStorage(config)
|
|
})
|
|
})
|
|
|
|
describe('email', () => {
|
|
// eslint-disable-next-line jest/expect-expect
|
|
it('should default to using payload cloud email', async () => {
|
|
const plugin = payloadCloudPlugin()
|
|
const config = await plugin(createConfig())
|
|
|
|
expect(createTransportSpy).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
host: 'smtp.resend.com',
|
|
}),
|
|
)
|
|
})
|
|
|
|
// eslint-disable-next-line jest/expect-expect
|
|
it('should allow opt-out', async () => {
|
|
const plugin = payloadCloudPlugin({ email: false })
|
|
const config = await plugin(createConfig())
|
|
|
|
expect(config.email).toBeUndefined()
|
|
})
|
|
|
|
// eslint-disable-next-line jest/expect-expect
|
|
it('should allow PAYLOAD_CLOUD_EMAIL_* env vars to be unset', async () => {
|
|
delete process.env.PAYLOAD_CLOUD_EMAIL_API_KEY
|
|
delete process.env.PAYLOAD_CLOUD_DEFAULT_DOMAIN
|
|
|
|
const plugin = payloadCloudPlugin()
|
|
const config = await plugin(createConfig())
|
|
|
|
expect(config.email).toBeUndefined()
|
|
})
|
|
|
|
it('should not modify existing email transport', async () => {
|
|
const logSpy = jest.spyOn(console, 'log')
|
|
|
|
const existingTransport = nodemailer.createTransport({
|
|
name: 'existing-transport',
|
|
// eslint-disable-next-line @typescript-eslint/require-await
|
|
send: async (mail) => {
|
|
console.log('mock send', mail)
|
|
},
|
|
version: '0.0.1',
|
|
})
|
|
|
|
const configWithTransport = createConfig({
|
|
email: await nodemailerAdapter({
|
|
defaultFromAddress: 'test@test.com',
|
|
defaultFromName: 'Test',
|
|
transport: existingTransport,
|
|
skipVerify,
|
|
}),
|
|
})
|
|
|
|
const plugin = payloadCloudPlugin()
|
|
const config = await plugin(configWithTransport)
|
|
|
|
expect(logSpy).toHaveBeenCalledWith(
|
|
expect.stringContaining('Payload Cloud Email is enabled but'),
|
|
)
|
|
|
|
// expect(config.email).toBeUndefined()
|
|
})
|
|
|
|
it('should allow setting fromName and fromAddress', async () => {
|
|
const defaultFromName = 'Test'
|
|
const defaultFromAddress = 'test@test.com'
|
|
const configWithPartialEmail = createConfig({
|
|
email: await nodemailerAdapter({
|
|
defaultFromAddress,
|
|
defaultFromName,
|
|
skipVerify,
|
|
}),
|
|
})
|
|
|
|
const plugin = payloadCloudPlugin()
|
|
const config = await plugin(configWithPartialEmail)
|
|
const emailConfig = config.email as Awaited<ReturnType<typeof nodemailerAdapter>>
|
|
|
|
const initializedEmail = emailConfig({ payload: mockedPayload })
|
|
|
|
expect(initializedEmail.defaultFromName).toEqual(defaultFromName)
|
|
expect(initializedEmail.defaultFromAddress).toEqual(defaultFromAddress)
|
|
|
|
expect(createTransportSpy).toHaveBeenCalledWith(
|
|
expect.objectContaining({
|
|
host: 'smtp.resend.com',
|
|
}),
|
|
)
|
|
})
|
|
})
|
|
})
|
|
})
|
|
|
|
function assertCloudStorage(config: Config) {
|
|
expect(config.upload?.useTempFiles).toEqual(true)
|
|
}
|
|
|
|
function assertNoCloudStorage(config: Config) {
|
|
expect(config.upload?.useTempFiles).toBeFalsy()
|
|
}
|
|
|
|
async function assertCloudEmail(config: Config) {
|
|
expect(config.email && 'name' in config.email).toStrictEqual('Nodemailer - SMTP')
|
|
}
|
|
|
|
function createConfig(overrides?: Partial<Config>): Config {
|
|
return {
|
|
...defaults,
|
|
...overrides,
|
|
} as Config
|
|
}
|