-
Notifications
You must be signed in to change notification settings - Fork 3.3k
feat(next): turbopack build support, fix incorrect handling of external packages #14475
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,24 +15,6 @@ export const withPayload = (nextConfig = {}, options = {}) => { | |
| env.NEXT_PUBLIC_ENABLE_ROUTER_CACHE_REFRESH = 'true' | ||
| } | ||
|
|
||
| if (process.env.PAYLOAD_PATCH_TURBOPACK_WARNINGS !== 'false') { | ||
| const turbopackWarningText = | ||
| 'Packages that should be external need to be installed in the project directory, so they can be resolved from the output files.\nTry to install it into the project directory by running' | ||
|
|
||
| const consoleWarn = console.warn | ||
| console.warn = (...args) => { | ||
| // Force to disable serverExternalPackages warnings: https://github.com/vercel/next.js/issues/68805 | ||
| if ( | ||
| (typeof args[1] === 'string' && args[1].includes(turbopackWarningText)) || | ||
| (typeof args[0] === 'string' && args[0].includes(turbopackWarningText)) | ||
| ) { | ||
| return | ||
| } | ||
|
|
||
| consoleWarn(...args) | ||
| } | ||
| } | ||
|
|
||
| const poweredByHeader = { | ||
| key: 'X-Powered-By', | ||
| value: 'Next.js, Payload', | ||
|
|
@@ -88,25 +70,32 @@ export const withPayload = (nextConfig = {}, options = {}) => { | |
| }, | ||
| serverExternalPackages: [ | ||
| ...(nextConfig.serverExternalPackages || []), | ||
| 'drizzle-kit', | ||
| 'drizzle-kit/api', | ||
| 'pino', | ||
| 'libsql', | ||
| 'pino-pretty', | ||
| 'graphql', | ||
| // Do not bundle server-only packages during dev to improve compile speed | ||
| // These packages always need to be external, both during dev and production. This is because they install dependencies | ||
| // that will error when trying to bundle them (e.g. drizzle-kit, libsql, esbuild etc.). | ||
| // We cannot externalize those problem-packages directly. We can only externalize packages that are manually installed | ||
| // by the end user. Otherwise, the require('externalPackage') calls generated by the bundler would fail during runtime, | ||
| // as you cannot import dependencies of dependencies in a lot of package managers like pnpm. We'd have to force users | ||
| // to install the dependencies directly. | ||
| // Thus, we externalize the "entry-point" = the package that is installed by the end user, which would be our db adapters. | ||
| // | ||
| // | ||
| // External because it installs mongoose (part of default serverExternalPackages: https://github.com/vercel/next.js/blob/canary/packages/next/src/lib/server-external-packages.json => would throw warning if we don't exclude the entry-point package): | ||
| '@payloadcms/db-mongodb', | ||
| // External because they install dependencies like drizzle, libsql, esbuild etc.: | ||
| '@payloadcms/db-postgres', | ||
| '@payloadcms/db-sqlite', | ||
| '@payloadcms/db-vercel-postgres', | ||
| '@payloadcms/drizzle', | ||
| // External because they install @aws-sdk/client-s3: | ||
| '@payloadcms/payload-cloud', | ||
| // TODO: We need to externalize @payloadcms/storage-s3 as well, once Next.js has the ability to exclude @payloadcms/storage-s3/client from being externalized. | ||
| // Do not bundle additional server-only packages during dev to improve compilation speed | ||
| ...(process.env.NODE_ENV === 'development' && options.devBundleServerPackages === false | ||
| ? [ | ||
| 'payload', | ||
| '@payloadcms/db-mongodb', | ||
| '@payloadcms/db-postgres', | ||
| '@payloadcms/db-sqlite', | ||
| '@payloadcms/db-vercel-postgres', | ||
| '@payloadcms/drizzle', | ||
| '@payloadcms/email-nodemailer', | ||
| '@payloadcms/email-resend', | ||
| '@payloadcms/graphql', | ||
| '@payloadcms/payload-cloud', | ||
| '@payloadcms/plugin-redirects', | ||
| // TODO: Add the following packages, excluding their /client subpath exports, once Next.js supports it | ||
| //'@payloadcms/plugin-cloud-storage', | ||
|
|
@@ -129,45 +118,14 @@ export const withPayload = (nextConfig = {}, options = {}) => { | |
|
|
||
| return { | ||
| ...incomingWebpackConfig, | ||
| externals: [ | ||
| ...(incomingWebpackConfig?.externals || []), | ||
| 'drizzle-kit', | ||
| 'drizzle-kit/api', | ||
| 'sharp', | ||
| 'libsql', | ||
| 'require-in-the-middle', | ||
| ], | ||
| ignoreWarnings: [ | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is probably a relict from the old |
||
| ...(incomingWebpackConfig?.ignoreWarnings || []), | ||
| { module: /node_modules\/mongodb\/lib\/utils\.js/ }, | ||
| { file: /node_modules\/mongodb\/lib\/utils\.js/ }, | ||
| { module: /node_modules\/mongodb\/lib\/bson\.js/ }, | ||
| { file: /node_modules\/mongodb\/lib\/bson\.js/ }, | ||
| ], | ||
| externals: [...(incomingWebpackConfig?.externals || []), 'require-in-the-middle'], | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the dependencies in webpack.externals are now always externalized server-side - see serverExternalDependencies. On the client, those dependencies are (and should) never imported anyways, thus no point in manually declaring them here |
||
| plugins: [ | ||
| ...(incomingWebpackConfig?.plugins || []), | ||
| // Fix cloudflare:sockets error: https://github.com/vercel/next.js/discussions/50177 | ||
| new webpackOptions.webpack.IgnorePlugin({ | ||
| resourceRegExp: /^pg-native$|^cloudflare:sockets$/, | ||
| }), | ||
| ], | ||
| resolve: { | ||
| ...(incomingWebpackConfig?.resolve || {}), | ||
| alias: { | ||
| ...(incomingWebpackConfig?.resolve?.alias || {}), | ||
| }, | ||
| fallback: { | ||
| ...(incomingWebpackConfig?.resolve?.fallback || {}), | ||
| '@aws-sdk/credential-providers': false, | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a relict as well. With our clean server-client separation, those dependencies shouldn't sneak into the client bundle anyways.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. webpack also is used for the server-side bundle in nextjs |
||
| '@mongodb-js/zstd': false, | ||
| aws4: false, | ||
| kerberos: false, | ||
| 'mongodb-client-encryption': false, | ||
| snappy: false, | ||
| 'supports-color': false, | ||
| 'yocto-queue': false, | ||
| }, | ||
| }, | ||
| } | ||
| }, | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These are always externalized now, not just during dev, thus the removal