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

feat: add preserve_imports_with_side_effects option for simplify optimization #6962

Merged
merged 14 commits into from Mar 5, 2023

Conversation

nissy-dev
Copy link
Contributor

@nissy-dev nissy-dev commented Feb 19, 2023

Description: Fix #6771

This implementation is still rough, and there are still some implementation decisions that need to be decided on that I mentioned in my comments.

BREAKING CHANGE: Yes

Related issue (if exists):

Comment on lines 1488 to 1490

#[serde(default)]
pub drop_unused_imports: BoolConfig<false>,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There was a way to modify the simplify option directly, but I added it in this way to avoid breaking change. What do you think?

Copy link
Member

@kdy1 kdy1 Feb 23, 2023

Choose a reason for hiding this comment

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

Can you move this to the simplify option?

@kdy1 kdy1 added this to the Planned milestone Feb 19, 2023
@nissy-dev nissy-dev closed this Feb 21, 2023
@nissy-dev nissy-dev reopened this Feb 21, 2023
@kdy1
Copy link
Member

kdy1 commented Feb 22, 2023

I'll merge PRs with breaking changes at once

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!
As it's a breaking change anyway, we can use correct way.

Comment on lines 1488 to 1490

#[serde(default)]
pub drop_unused_imports: BoolConfig<false>,
Copy link
Member

@kdy1 kdy1 Feb 23, 2023

Choose a reason for hiding this comment

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

Can you move this to the simplify option?

@nissy-dev nissy-dev changed the title feat: add drop_unused_imports option for simplify optimization feat: add preserve_imports_with_side_effects option for simplify optimization Feb 23, 2023
@manucorporat
Copy link

I am thinking that it would be soo good, to have a callback to resolve if a indentifier has side effects or not:

So instead of being a bool it could be a function, that is being passed the import source and return a bool

@kdy1
Copy link
Member

kdy1 commented Feb 23, 2023

Nope, it should never be

@kdy1
Copy link
Member

kdy1 commented Feb 23, 2023

Rust-side api is fine, but js api is not fine

@nissy-dev
Copy link
Contributor Author

nissy-dev commented Feb 23, 2023

@manucorporat Which API do you use, JS API or Rust API?
It is possible to change the simplifier function (in crates/swc_ecma_transforms_optimization/src/simplify/mod.rs) to accept callbacks. If you want to do so, it would be great if you could give me more details about the callback you want to add.

@@ -2,7 +2,6 @@
"jsc": {
"transform": {
"optimizer": {
"simplify": false,
Copy link
Member

Choose a reason for hiding this comment

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

Why did you touch this? Please revert

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kdy1 Thank you for your reviews. You mean I need to modify simplify option which receive boolean value and json? Like this.

pub struct OptimizerConfig {
    #[serde(default)]
    pub simplify: SimplifyOption,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum SimplifyOption {
    Bool(bool),
    Json(SimplifyJsonOption),
}

impl Default for SimplifyOption {
    fn default() -> Self {
        SimplifyOption::Bool(true)
    }
}

#[derive(Debug, Default, Clone, Copy, Serialize, Deserialize)]
#[serde(deny_unknown_fields, rename_all = "camelCase")]
pub struct SimplifyJsonOption {
    pub preserve_imports_with_side_effects: bool,
}

If you think so, I will refactor in this way

Copy link
Member

Choose a reason for hiding this comment

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

Yes, thank you!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated!

@kdy1 kdy1 marked this pull request as draft February 28, 2023 03:37
@nissy-dev nissy-dev marked this pull request as ready for review March 2, 2023 15:05
@@ -30,6 +30,7 @@ use crate::debug_assert_valid;
pub fn dce(
config: Config,
unresolved_mark: Mark,
preserve_imports_with_side_effects: bool,
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 this should go into Config type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I moved this option to dce config.

@kdy1 kdy1 self-assigned this Mar 3, 2023
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!


swc-bump:

  • swc_ecma_transforms_optimization --breaking

@kdy1 kdy1 enabled auto-merge (squash) March 4, 2023 12:52
Copy link
Collaborator

@swc-bot swc-bot left a comment

Choose a reason for hiding this comment

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

Automated review comment generated by auto-rebase script

@kdy1 kdy1 merged commit 67d0a89 into swc-project:main Mar 5, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.38 Mar 6, 2023
@swc-project swc-project locked as resolved and limited conversation to collaborators Apr 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Dropping unused imports by simplifier()
4 participants