Open raftario opened 1 year ago
Same issue. Since I deploy the monorepo as a Docker image I don't want/need to bundle any server dependencies, so I'm doing this…
// apps/my-app/next.config.js
const { readdirSync } = require('node:fs');
const { builtinModules } = require('node:module');
const { join } = require('node:path');
const monorepoModules = (scope = undefined) => {
const nodeModulesPath = join(__dirname, '..', '..', 'node_modules');
return readdirSync(scope ? join(nodeModulesPath, scope) : nodeModulesPath, { withFileTypes: true })
.filter((dirent) => dirent.isDirectory() && !dirent.name.startsWith('.'))
.map((dirent) => scope ? `${scope}/${dirent.name}` : dirent.name)
.reduce((acc, name) => (
undefined === scope && name.startsWith('@')
? [ ...acc, ...monorepoModules(name) ]
: [ ...acc, name ]
), []);
};
module.exports = {
experimental: {
serverComponentsExternalPackages: monorepoModules()
.concat(builtinModules)
.concat(builtinModules.map((name) => `node:${name}`)),
},
// ...etc
};
Is there a better way to opt-out of bundling server dependencies?
any update here?
Edit by maintainers: Comment was automatically minimized because it was considered unhelpful. (If you think this was by mistake, let us know). Please only comment if it adds context to the issue. If you want to express that you have the same problem, use the upvote 👍 on the issue description or subscribe to the issue for updates. Thanks!
So any updates? I can't use @xenova/transformers due this issue. Any workarounds, please.
One possible workaround is editing the webpack config like so in your next.config.js
.
webpack: (config) => {
config.externals.push({
"PACKAGE_NAME": "commonjs PACKAGE_NAME",
})
return config
},
However this is pretty ugly and error prone and having the dedicated config option work properly within monorepos would be much nicer :)
A question on this : is a package is used within a package, does it get externalised? Use case:
main next app app lives in apps/frontend
prisma lives in packages/database
( @prisma/client and
prisma`)
we import prisma like thus: import prisma from
@companyname/database`
Will the 2 prisma packages be auto externalised, or do we need to tell it to externalise @companyname/database ?
https://nextjs.org/docs/app/api-reference/next-config-js/serverComponentsExternalPackages
I ask because out our vercel output, we get a large bundle warning for 2 of the functions., and it looks to be including prisma
a package is used within a package, does it get externalised?
My testing suggests not!
I guess problem is here https://github.com/vercel/next.js/blob/v14.2.5/packages/next/src/build/handle-externals.ts#L421 Need to adjust regexp to handle more pnpm-like cases
In my case issue was in incorrect exports in package package.json Ensure that you have at least one "default" condition export
Verify canary release
Provide environment information
What browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
No response
Describe the Bug
The experimental
serverComponentsExternalPackages
config option does not seem to have any effect when specifying workspace packages and webpack still attempts to bundle them. This behaviour is the same with both yarn@3 and pnpm@7.Expected Behavior
webpack should not attempt to bundle workspace packages specified in
serverComponentsExternalPackages
.Link to reproduction - Issues with a link to complete (but minimal) reproduction code will be addressed faster
https://github.com/raftario/next-server-components-external-workspace-packages
To Reproduce
yarn install
yarn build