So, I've been working with NextJS for a while now and I never really looked at the build folder nor the info shown in the console when the build succeeds but I did notice something strange the one time I did look at it.
I have been using TypeScript and wrote a little babel config with TS's config to use absolute paths. So the way I am importing components is by having an absolute path ~/components which is an alias of ./src/components. All imports throughout the app are using components imported from ~/components and there's other absolutes paths in there as well. So, here's what I saw when I built my app
Notice how every single page is almost of the same size? Even the about and 404 pages are HUGE. So my guess was that importing modules using the absolute imports is what's causing the issue and because of that, each page is linking to a JS file that has the entire app code in it . I am not exactly sure and I can't seem to find any resources online that can help me optimize these pages. Honestly, I don't even know what part of the app needs to be configured. Whether it's webpack or babel, I have no clue. So, I was hoping to get some help on this and if there's some further configuration that I have to add so each page uses only as much JavaScript as it needs.
Here's my typescript and babel configs:
TypeScript
{
"compilerOptions": {
"lib": ["dom", "dom.iterable", "esnext"],
"baseUrl": "./src",
"paths": {
"~/components": ["./components"],
"~/styles": ["./styles"],
"~/assets": ["./assets"],
"~/types": ["./types"],
"~/pages": ["./pages"],
"~/utils": ["./utils"],
"~/context": ["./context"],
"~/data": ["./data"],
"~/services": ["./services"]
},
"allowJs": false,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"target": "ESNext",
"jsx": "preserve",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"skipLibCheck": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noImplicitAny": true
},
"exclude": ["node_modules"],
"include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"]
}
Babel
{
"presets": ["next/babel"],
"plugins": [
["styled-components", { "ssr": true }],
[
"module-resolver",
{
"alias": {
"~/components": "./src/components",
"~/styles": "./src/styles",
"~/types": "./src/types",
"~/pages": "./src/pages",
"~/assets": "./src/assets",
"~/utils": "./src/utils",
"~/context": "./src/context",
"~/data": "./src/data",
"~/services": "./src/services"
}
}
],
["import", {
"libraryName": "antd",
"style": true
}]
]
}

