Closed br0p0p closed 1 year ago
very good, hope this auto config by plugin self.
const withTM = require("next-transpile-modules")(["@xx/react-basic", "@xx/basicjs"]);
const withTMPeerDependencies = Object.keys({
...require("@xx/basicjs/package.json").peerDependencies,
...require("@xx/react-basic/package.json").peerDependencies
});
//region handle next-transpile-modules peerDependencies
if (isServer) config.externals = [...withTMPeerDependencies, ...config.externals];
withTMPeerDependencies.map((denpendency) => {
config.resolve.alias[denpendency] = path.resolve(__dirname, ".", "node_modules", denpendency);
});
//endregion
Well I'm (very) late to the party.
Thank you very much for this PR.
Could you maybe add the unstable_
prefix to the option as long as we don't have automated tests for those? Then I'll happily merge it :)
cf. pinned issue, closing (thank you for this still!)
I quickly threw this together to reduce the amount of custom Webpack config I need to provide in a project I'm working on.
This
setExternals
option looks forpeerDependencies
of packages specified in the transpilation list and both adds them to theexternals
list (whenisServer
is truthy) and aliases these dependencies to the NextJS app'snode_modules
instead of the peer dependency'snode_modules
.Like I mentioned this was a very quick update and I'm not a Webpack expert so I may have missed something. I'm also happy to change the name of the option from
setExternals
to something else. (maybeautoExternals
is better?)Example
Say I have a monorepo with a NextJS application (named
app
) and two other Typescript libraries namedfoo
andbar
.app
depends on both directly butfoo
declaresbar
andreact
aspeerDependencies
.If I enable
setExternals
and provide thefoo
andbar
packages in themodules
list, I don't have to manually specify thereact
external or alias while also transpillingbar
(as it does withfoo
) instead of treating it like an already transpiledpeerDependency
.Some background
I'm working in a monorepo with multiple apps that depend on other monorepo packages which also sometimes rely on other monorepo packages.
Providing a way to do this automatically saves a lot of manual Webpack config in
next.config.js
for my setup that would be replicated across many apps. I assume this will be helpful to others with similar setups as well.