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

Add originalFIleName property to emitted assets #5596

Merged
merged 2 commits into from
Aug 3, 2024

Conversation

lukastaegert
Copy link
Member

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:

Description

This is a long-requested feature for emitting assets. It adds a new optional property originalFileName to this.emitFile({type: 'asset'}) calls that will be present in the output.assetFileNames option and in the generateBundle hook to identify from which file an asset was emitted.
This was a long-requested feature, but I was hesitant to implement it because it requires changes in plugins that emit files to work. In short:

Plugins that use this.emitFile for an asset that corresponds to an actual source file should not set the originalFileName property to the absolute path of the source file.

Not only will this property then passed on, the source file will also automatically be watched in watch mode.

cc @patak-dev @sapphi-red @yyx990803 @antfu: As Vite implements many asset handling plugins, does this API look fine to you and would you be interested in supporting it in Vite?

Sorry, something went wrong.

Copy link

vercel bot commented Jul 29, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rollup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 29, 2024 5:04am

Copy link

github-actions bot commented Jul 29, 2024

Thank you for your contribution! ❤️

You can try out this pull request locally by installing Rollup via

npm install rollup/rollup#gh-4724_emit_with_file_name

Notice: Ensure you have installed the latest stable Rust toolchain. If you haven't installed it yet, please see https://www.rust-lang.org/tools/install to learn how to download Rustup and install Rust.

or load it into the REPL:
https://rollup-5e46muujl-rollup-js.vercel.app/repl/?pr=5596

Copy link

Performance report!

Rough benchmark

Command Mean [s] Min [s] Max [s] Relative
node _benchmark/previous/bin/rollup -i ./perf/entry.js -o _benchmark/result/previous.js 9.247 ± 0.101 9.175 9.362 1.01 ± 0.01
node _benchmark/current/bin/rollup -i ./perf/entry.js -o _benchmark/result/current.js 9.191 ± 0.091 9.105 9.286 1.00

Internal benchmark

  • BUILD: 8176ms, 753 MB
    • initialize: 0ms, 25.9 MB
    • generate module graph: 3148ms, 575 MB
      • generate ast: 1513ms, 568 MB
    • sort and bind modules: 446ms, 618 MB
    • mark included statements: 4585ms, 753 MB
      • treeshaking pass 1: 1575ms (+34ms, +2.2%), 719 MB
      • treeshaking pass 2: 750ms, 738 MB
      • treeshaking pass 3: 291ms, 745 MB
      • treeshaking pass 4: 271ms, 745 MB
      • treeshaking pass 5: 314ms, 752 MB
      • treeshaking pass 6: 259ms, 749 MB
      • treeshaking pass 7: 242ms, 750 MB
      • treeshaking pass 8: 233ms, 752 MB
      • treeshaking pass 9: 212ms, 754 MB
      • treeshaking pass 10: 215ms, 752 MB
      • treeshaking pass 11: 211ms, 753 MB
  • GENERATE: 881ms, 1.02 GB
    • initialize render: 0ms, 914 MB
    • generate chunks: 87ms, 923 MB
      • optimize chunks: 0ms, 917 MB
    • render chunks: 774ms, 1 GB
    • transform chunks: 17ms, 1.02 GB
    • generate bundle: 0ms, 1.02 GB

Copy link

codecov bot commented Jul 29, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.05%. Comparing base (8b96791) to head (5e84d72).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #5596   +/-   ##
=======================================
  Coverage   99.05%   99.05%           
=======================================
  Files         242      242           
  Lines        9304     9307    +3     
  Branches     2463     2465    +2     
=======================================
+ Hits         9216     9219    +3     
  Misses         58       58           
  Partials       30       30           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lukastaegert lukastaegert merged commit 32e8d5b into master Aug 3, 2024
39 checks passed
@lukastaegert lukastaegert deleted the gh-4724_emit_with_file_name branch August 3, 2024 04:36
Copy link

github-actions bot commented Aug 3, 2024

This PR has been released as part of rollup@4.20.0. You can test it via npm install rollup.

Copy link

@Bonniemarie216 Bonniemarie216 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍🏽

@sapphi-red
Copy link
Contributor

@lukastaegert
Sorry for the late comment. The API looks mostly fine to me.
One thing I noticed is that when an asset is deduped only a single original file can be tracked.
Here's the example, a.txt and b.txt have the same content so only a single asset is output (this behavior itself is expected).
https://stackblitz.com/edit/rollup-template-pvmdib?file=rollup.config.js,package.json
But in this case, the originalFileName only contains a.txt so the user cannot know b.txt was merged into a.txt.

In Vite, we had a internal list to manage the mapping between the original file name and the output file, so it won't be a problem. But maybe there's some users that wants to know that information.

@lukastaegert
Copy link
Member Author

That is a good point. We can still rework the API to be a list instead in Rollup 5.

github-merge-queue bot pushed a commit to rolldown/rolldown that referenced this pull request Aug 23, 2024
<!-- Thank you for contributing! -->

### Description

Add originalFileName to EmittedAsset, ref
rollup/rollup#5596

<!-- Please insert your description here and provide especially info
about the "what" this PR is solving -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

output.assetFileNames(): provide original file path of assets
3 participants