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

fix(preset-env): Consider browserslist config if env.target not configured #8921

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

egfx-notifications
Copy link

@egfx-notifications egfx-notifications commented May 6, 2024

Description:

The env.path option in .swcrc was originally implemented for browserslist, but implementation was (unintentionally?) removed during the migration to browserslist-rs (#2845).
Searching the current directory for browserslist configuration was also dropped in the process.

This PR reimplements searching for browserslist configuration using the lookup functionality provided by browserslist-rs

BREAKING CHANGE:

This may be considered a breaking change since the previous (broken) behavior for unconfigured env.targets was to return a BrowserData struct with all Options set to None. Now the result of a browserslist defaults query is returned instead. This probably was the original behavior with browserslist, though.
Update: Thinking about it again, targets_to_versions() is part of preset_env_bases public API so we should probably consider this a breaking change anyway.

Things to consider:

  • only works for non wasm targets as browserslist-rs does not provide configuration loading for wasm32 targets (which is probably to be expected that we won't check environment variables or search a filesystem in that case)
  • feat(preset-env): Let browserslist-rs handle default config path changes the default for an unconfigured env.path from an empty string for wasm targets / current dir for other targets to a None value. browserslist-rs already looks up the current dir if path is None and it felt better to let it handle this case itself
    The commit which migrated from browserslist to browserslist-rs mentioned fixing the default_path, but does not mention what was broken and I also did not find any other usage of the path property. If the previous implementation was intentional, just ignore that last commit in the PR, the first two sufficiently address the bug.

Related issue:
Fixes #3365
Implements #3085

@egfx-notifications egfx-notifications requested review from a team as code owners May 6, 2024 13:20
Copy link

changeset-bot bot commented May 6, 2024

🦋 Changeset detected

Latest commit: e0be2b6

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@CLAassistant
Copy link

CLAassistant commented May 6, 2024

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@egfx-notifications egfx-notifications force-pushed the fix/browserslist-config-fallback branch from 25eb315 to 7a7aaf8 Compare May 6, 2024 13:39
@egfx-notifications egfx-notifications requested a review from a team as a code owner May 6, 2024 13:39
@kdy1
Copy link
Member

kdy1 commented May 6, 2024

cc @kwonoj

@egfx-notifications
Copy link
Author

It seems some of the tests fail due to them expecting a specific prerecorded results, but this result has now changed due to the previous default without a target being BrowserData with only None values and now it is BrowserData according to browserslist-rs defaults query, so some stuff that browser now understand natively is not transpiled.

Seems forcing a browserslist query when target is none is not the best approach. I won't try any fixes before hearing your feedback on how to handle this. Should we make the env.path option mandatory if a browserslist config lookup is desired?

@kdy1 kdy1 modified the milestone: Planned Feb 26, 2025
@honzahruby
Copy link

@egfx-notifications Any update on this? 🙏

How about adding some constant to opt in this behavior? Something like env.targets === 'auto' or true.

@egfx-notifications
Copy link
Author

@egfx-notifications Any update on this? 🙏

No, still waiting on maintainer feedback before I put any more work into this.

How about adding some constant to opt in this behavior? Something like env.targets === 'auto' or true.

This would probably be a elegant non-breaking way.

@kdy1 kdy1 assigned kdy1 and unassigned kwonoj Feb 27, 2025
@kdy1 kdy1 added this to the Planned milestone Feb 27, 2025
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Can you rebase?

@egfx-notifications egfx-notifications force-pushed the fix/browserslist-config-fallback branch from 7a7aaf8 to d731765 Compare February 27, 2025 10:05
@egfx-notifications
Copy link
Author

Can you rebase?

Wait a sec, that rebase is faulty. I'm going to fix this manually asap.

@jodaka
Copy link

jodaka commented Feb 27, 2025

Nice to see some action here! Thanks @egfx-notifications

@egfx-notifications egfx-notifications force-pushed the fix/browserslist-config-fallback branch from d731765 to a5a35fa Compare February 27, 2025 15:58
@egfx-notifications
Copy link
Author

I think I addressed all of the changes between my original commits and now. Unfortunately I can't run cargo test locally right now as my GLIBC version is too old. Let's see what CI says. I may set up a newer Ubuntu VM tomorrow if necessary.

@kdy1
Copy link
Member

kdy1 commented Feb 27, 2025

No worries, I‘ll update the tests if needed

Copy link

codspeed-hq bot commented Feb 27, 2025

CodSpeed Performance Report

Merging #8921 will degrade performances by 3.55%

Comparing egfx-notifications:fix/browserslist-config-fallback (e0be2b6) with main (edea2c5)

Summary

⚡ 2 improvements
❌ 1 regressions
✅ 188 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark BASE HEAD Change
es/minifier/libs/jquery 128.9 ms 133.7 ms -3.55%
es/preset-env/usage/builtin_type 255.4 µs 48.3 µs ×5.3
es/preset-env/usage/property 118.4 µs 25.1 µs ×4.7

.clone()
.into_os_string()
.into_string()
.unwrap_or_else(|_| panic!("Invalid path \"{:?}\"", path))
Copy link
Member

Choose a reason for hiding this comment

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

I think we should not panic!() here. Can you return an instance of anyhow::Error instead?

.with_context(|| "failed to resolve browserslist query from browserslist config")?;

let versions =
BrowserData::parse_versions(distribs).expect("failed to parse browser version");
Copy link
Member

Choose a reason for hiding this comment

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

Same here. We don't have to panic.

kdy1
kdy1 previously approved these changes Mar 3, 2025
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Thank you, and sorry for a (very) late review!

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

I missed it while reviewing, but while trying to update tests to merge this PR, I found that this PR makes the swc crate ignore jsc.target.
Could you fix it?

You can run UPDATE=1 cargo test --test projects --test tsc --test error_msg from ./crates/swc to update test snapshots.

@kdy1 kdy1 dismissed their stale review March 24, 2025 05:37

jsc.target is broken

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

Successfully merging this pull request may close these issues.

Browserslist not being applied from .browserslistrc or package.json
7 participants