Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Add flag when emitting assets to not emit files that are not referenced in the output #4774

Closed
lukastaegert opened this issue Dec 28, 2022 · 5 comments · Fixed by #4805

Comments

@lukastaegert
Copy link
Member

lukastaegert commented Dec 28, 2022

Feature Use Case

Currently, it is possible that an asset is emitted, but the usage of that asset is removed by tree-shaking. E.g. in the original code we have something like

import asset from './asset.svg';

if (COMPILER_FLAG) {
  const image = document.createElement('img');
  image.src = asset;
  document.body.appendChild(image);
}

Now one plugin resolves asset as import.meta.ROLLUP_FILE_URL_abc123 and another plugin replaces COMPILER_FLAG with false. In that scenario, the file would still be emitted but it is not referenced.

Feature Proposal

Add a new property needsCodeReference: boolean to EmittedAsset. If it is set to true, then the asset is only emitted if at least one corresponding import.meta.ROLLUP_FILE_URL_referenceId can be found in the emitted chunks.

If the flag is not provided, it is treated as false for backwards compatibility.

See also #4761 for another issue that is addressed by this.

Note: Assets can be deduplicated if they have the source. In that case, several reference ids can reference the same asset. In that scenario, the usage of one of the reference ids is of course enough for the asset to be emitted.

@lukastaegert lukastaegert changed the title RFC: Add flag when emitting assets to not emit the files when they are not referenced in the output RFC: Add flag when emitting assets to not emit files that are not referenced in the output Dec 28, 2022
@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4805 as part of rollup@3.11.0. You can test it via npm install rollup.

@stabas
Copy link

stabas commented Apr 2, 2024

But is there a way/option to emit assets that are not referenced?

@lukastaegert
Copy link
Member Author

Of course, just do not add needsCodeReference when emitting them. See https://rollupjs.org/plugin-development/#this-emitfile for how to emit files in general.

@stabas
Copy link

stabas commented Apr 5, 2024

I'm using astro for my project, which uses vite for build. How do I configure "needsCodeReference" in rollout options?

astro.config.mjs

...
vite: {
    build: {
        rollupOptions: {
            output: {
                ...
            }
        }
    }
}
...

@lukastaegert
Copy link
Member Author

lukastaegert commented Apr 5, 2024

Only the plugin emitting the asset can configure it. In order to emit assets yourself, you need to write a plugin. But outside a plugin, you would not have access to the file reference id anyway, so this whole thing only makes sense in a plugin context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants