webpack icon indicating copy to clipboard operation
webpack copied to clipboard

Unused assets left by code optimization

Open MattIPv4 opened this issue 1 month ago • 3 comments

Bug Description

When using code optimization to drop unused code, assets that were emitted but no longer referenced are still emitted.

Link to Minimal Reproduction and step to reproduce

https://github.com/MattIPv4/webpack-asset-repro

  • npm ci
  • npm run build
  • ls dist/*.png
  • for each file.png, cat dist/main.js | grep <file.png>

One png should be referenced in dist/main.js, while the other won't be as it was used by dead code.

Expected Behavior

Assets that are emitted but later made unused by code optimization should be removed from the emitted assets

Actual Behavior

Assets are always emitted even if all references to them are dropped by code optimization

Environment

System:
    OS: macOS 26.1
    CPU: (14) arm64 Apple M4 Pro
    Memory: 97.52 MB / 24.00 GB
  Binaries:
    Node: 22.13.1 - /Users/mattcowley/.local/state/fnm_multishells/85154_1765079840717/bin/node
    npm: 10.9.2 - /Users/mattcowley/.local/state/fnm_multishells/85154_1765079840717/bin/npm
    pnpm: 10.20.0 - /Users/mattcowley/.local/state/fnm_multishells/85154_1765079840717/bin/pnpm
  Browsers:
    Chrome: 142.0.7444.177
    Firefox Developer Edition: 146.0
    Safari: 26.1
    Safari Technology Preview: 26.0
  Packages:
    webpack-cli: ^6.0.1 => 6.0.1

Is this a regression?

None

Last Working Version

No response

Additional Context

No response

MattIPv4 avatar Dec 07 '25 04:12 MattIPv4

You need to add the following line to your package.json file. See more: optimization.sideEffects

+ "sideEffects": false

xiaoxiaojx avatar Dec 09 '25 11:12 xiaoxiaojx

That worked! ...but I don't understand why? If the import of the image were to be seen as a side effect, why isn't the import itself left in place? All that gets left is a completely unreferenced image asset?

MattIPv4 avatar Dec 09 '25 12:12 MattIPv4

Hm... in the real world it doesn't seem to work though... https://github.com/alveusgg/extension/pull/401 if I pnpm build here, ls build/static/media/ambassadors/*/emote*.png still returns files generated even though they're unused.

MattIPv4 avatar Dec 09 '25 13:12 MattIPv4