Comments (10)
And deleting it breaks the build.
If you're on nextjs 10.2+, you can replace it by
const nextConfig = {
experimental: {
externalDir: true,
},
};
export default nextConfig;
See https://github.com/belgattitude/nextjs-monorepo-example#step-33-next-config. It basically does the same but with support for css files too. In my experience it does not affect the bundle
I replaced that config for next-transpile-modules, and the bundle stayed the same.
The experimental.externalDir (or the previous webpack rule you had) is enough. No need for ntm in general. In my experience it does not affect the bundle
There's probably something else to find out
from nextjs-monorepo-example.
externalDir works perfectly! Thanks.
Yes, we're using a bundle analyzer, and the only way to prevent importing a lot of unneeded modules on _app.ts
seems to be not importing from the barrel file:
from nextjs-monorepo-example.
Yes! I ended up changing the file from Javascript to Typescript and using import
, it previously was a js file because we used to import it from next.config.js
, that change reduced the bundle by ~190kb.
We don't build the packages for the frontend, we just build one of them for the backend, so I'm not sure what's happening with sideEffects
, honestly, the combination of multiple technologies + versions + configs to make the monorepo setup work, make the debugging process a bit complicated, mostly since learning about the underlying tech to make the monorepo work competes against learning/improving customer-facing tech, that's why I really appreciate what you did with this open-source monorepo, it makes it much smoother.
from nextjs-monorepo-example.
That explains yes. Thank for sharing the explanation. Nice
from nextjs-monorepo-example.
We are having issues with tree shaking and barrel (index.ts) files in our setup, we have 3 big shared libraries and we're getting (a lot) more code than we need from them on every Next.js page.
If you suspect something linked to the barrel, I would check if there's no circular deps
ie:
index.ts
export { compOne } from "./compOne";
export { compTwo } from "./compTwo";
compTwo.ts
❌ import { compOne } from './index.ts'; // or equivalent to import compOne from './';
✅ import { compOne } from './compOne'; // that would fix circular deps that webpack won't really digest
export compTwo = () => compOne(12) * 2;
So I'm curious to know what config is enabling tree shaking to work properly, even without needing "sideEffects": false in the package.json files.
sideEffects is the thing yes... at least when the lib/package is built (prebuild, preconstructed) and published before getting consumed (by nextjs or ...).
In the nextjs-monorepo-example we do not (but we could)... so sideEffect to false won't change anything. Packages are consumed without prior build.
Let me know if helps a bit. Cause I'm not sure I have enough info 😄
from nextjs-monorepo-example.
Thanks for the answer, we'll check for circular deps!
For now, we were able to reduce the bundle size by using nested imports instead of the barrel file.
We're also consuming the packages directly from their source (using Vercel's monorepo support and Typescript), and checking our config I noticed that we still have this in next.config.js
:
const resolvedBaseUrl = path.resolve(config.context, "../../");
config.module.rules = [
...config.module.rules,
{
test: /\.(tsx|ts|js|mjs|jsx)$/,
include: [resolvedBaseUrl],
use: options.defaultLoaders.babel,
exclude: (excludePath) => /node_modules/.test(excludePath),
},
];
And deleting it breaks the build. I think I took that from a previous version of this repo because the current one doesn't have it, I'm curious if it affects the bundle or not.
This is the error we get after deleting it:
You may need an appropriate loader to handle this file type, currently, no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| import { ErrorBoundaryModal } from "./ErrorBoundaryModal";
|
> export class ErrorBoundary extends React.Component<UserDataProps, ErrorProps> {
| state = {
| error: null,
EDIT:
I replaced that config for next-transpile-modules
, and the bundle stayed the same.
from nextjs-monorepo-example.
Have you tried a bundle-analyzer ?
from nextjs-monorepo-example.
I'm curious now. Webpack 5 right? Not swc ?
What version of nextjs are you using?
Ps: worth to disable this if set
from nextjs-monorepo-example.
Ok, mystery solved!
The issue was caused by a require
statement (pointing to one of our shared libs) in a config file that was imported in _app.tsx
, but the crazy thing is that our setup is affected by setting "sideEffects": false
in the libs package.json
files.
We have 3 shared libs.
If I set "sideEffects": false
in all of them:
from nextjs-monorepo-example.
Nice.
Have you tried to change the require into an import ? Out of curiosity...
I didn't know package.json would be read when using aliases. So you build your packages prior to consume them?
from nextjs-monorepo-example.
Related Issues (20)
- Automated releases HOT 1
- Early cache invalidation in multistage Docker build HOT 4
- Problem with prisma in monorepo
- How debug server side for next.js `nextjs-app` using vscode? HOT 3
- Adding commitzen command HOT 2
- Action Required: Fix Renovate Configuration
- Does it support NodeNext .mjs, .mts ? HOT 8
- RFC: Move to pnpm HOT 1
- Example with standalone build HOT 3
- changeset publish does not replace workspace: ranges with current version in workspace HOT 4
- Best practice for add a backend server HOT 1
- nextjs-app compilation error and warning HOT 2
- Semantic error TS6059 File 'packages/ts-utils/src/index.ts' is not under 'rootDir' HOT 3
- Error: No mesh config file found HOT 1
- Make repo size lower
- add prettier for formatting and remove prettier for linting HOT 3
- Debug Nexjs app in monorepo with "NODE_OPTIONS='inspect'" HOT 10
- Deprecated package @types/prettier HOT 1
- storybook recognizing tailwind config from outside HOT 3
- Upgrade eslint to V9 and typescript-eslint v7 HOT 12
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nextjs-monorepo-example.