CSS imports disappear in webpack 5 production builds when sideEffects is omitted
0 reputation · 05 Nov 2020, 20:10 UTC
0 reputation · 05 Nov 2020, 20:10 UTC
Goal: Determine how webpack 5 should interpret a missing sideEffects field in package.json when deciding whether to preserve CSS imports during production‑mode tree shaking.
In development mode, webpack does not apply sideEffects‑based tree shaking, so CSS imports remain in the bundle regardless of the field. In production mode, the absence of a sideEffects field leads webpack to treat modules as pure and drop CSS imports, causing styles to disappear even though they work locally.
The unresolved decision is whether webpack should treat an unspecified sideEffects field as an explicit opt‑out of side‑effect elimination (i.e., assume no side effects) or as a default that assumes all modules have side effects. This choice affects bundle size correctness and the reliability of style‑loading across environments.
35275 reputation · 05 Nov 2020, 22:11 UTC
Webpack 5 treats an omitted sideEffects field in package.json as true – it assumes the module can have side effects. Therefore CSS imports are preserved during production‑mode tree‑shaking unless a sideEffects: false declaration is present somewhere in the build.
sideEffects: true (or omitted), modules are kept.sideEffects is set to false (or an empty array) does the optimizer treat the module as pure and strip it out if unused.sideEffects: false in your own package.json or in a dependency’s package.json.optimization.sideEffects: false in webpack.config.js.package.json: look for a line like \"sideEffects\": false. If present, change it to \"sideEffects\": true or remove the field to restore the default behaviour.node_modules for package.json files containing \"sideEffects\": false. If a library you import has this flag, consider upgrading, patching, or adding a sideEffects: [\"*.css\"] whitelist in your own config to preserve CSS files.module.exports = {
mode: 'production',
optimization: {
sideEffects: true
}
};
Rebuild and check if the CSS assets appear. If they do, the issue was a sideEffects mis‑declaration.webpack-bundle-analyzer or generate a stats.json to see whether CSS files or style-loader chunks are present. Missing entries confirm that CSS was removed.Is there an explicit sideEffects: false line in your own package.json or in any dependency you import? That detail will determine whether the recommendation changes.
With Webpack 5, you don’t need to set sideEffects to preserve CSS. If styles disappear in production, check for an explicit sideEffects: false in your own package.json or any dependency, or for an overriding optimization.sideEffects: false in the build config. Correcting that will restore the styles while maintaining tree‑shaking benefits.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.