How to combine CSS modules to a single file in final build ? #48123
Replies: 4 comments
-
I was wondering the same thing, probably a webpack config inside next.config.js would work, I’m still searching for it ! |
Beta Was this translation helpful? Give feedback.
-
Any news on this? Im also in search for a solution like this. |
Beta Was this translation helpful? Give feedback.
-
try in next.config webpack(config, { dev, isServer }) {
if (!dev && !isServer) {
// Optional: Improve CSS chunk splitting for FOUC mitigation
config.optimization = {
...config.optimization,
splitChunks: {
...config.optimization?.splitChunks,
cacheGroups: {
...config.optimization?.splitChunks?.cacheGroups,
styles: {
name: 'styles',
test: /\.css$/,
chunks: 'all',
enforce: true,
priority: 200,
},
},
},
};
}
return config;
} |
Beta Was this translation helpful? Give feedback.
-
Next.js doesn’t really give you an option to combine all CSS Modules into one big file. The reason is that it tries to keep things optimized — each page only loads the CSS it needs, which makes the app faster. If you do want everything in a single file, there are a couple of workarounds: => Put the styles you need everywhere into a global CSS file (globals.css or imported in _app.js). That file will be bundled as one. You can try tweaking Webpack to merge CSS, but it’s not officially supported in Next.js and can cause issues. Most people just handle shared/animation styles in global CSS and leave the rest split. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
Is it possible to configure NextJS to not do this , but rather combine all modules into single css file ?
In case you are wondering why would I want this , using framer-motion with modular css creates some weird transitions during transition / animation.
Additional information
No response
Example
No response
Beta Was this translation helpful? Give feedback.
All reactions