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:✨remove unused specifier in importInfo #963

Merged
merged 10 commits into from
Apr 15, 2024

Conversation

goo-yyh
Copy link
Contributor

@goo-yyh goo-yyh commented Mar 19, 2024

  1. import { a } from 'x';
  2. import a from 'x';
  3. import * as a from 'x';

如果模块 x 有副作用,但是变量 a 没有被使用,tree-shaking 完应该变成:

import 'x';

现在 master 只有 1 加了这个优化,补充 2 和 3

Summary by CodeRabbit

  • New Features

    • Enhanced tree shaking capabilities to better handle import statements with unused specifiers, especially when the imported module has side effects.
  • Tests

    • Added test assertions to validate the removal of unnecessary specifiers in different scenarios.
  • Documentation

    • Documented new test cases and scenarios for tree shaking improvements.

@sorrycc
Copy link
Member

sorrycc commented Mar 19, 2024

image

需要有结尾行。

const content = files["index.js"];

assert(
content.includes("/a.js") &&
Copy link
Member

Choose a reason for hiding this comment

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

这部分的 assert 是不是直接判定 现在 module 是什么样是不是更加直接

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

Copy link
Member

@stormslowly stormslowly left a comment

Choose a reason for hiding this comment

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

assert 部分修改下吧,这样看得更加直接一点

@goo-yyh
Copy link
Contributor Author

goo-yyh commented Mar 19, 2024

image 需要有结尾行。

done

@goo-yyh
Copy link
Contributor Author

goo-yyh commented Mar 19, 2024

assert 部分修改下吧,这样看得更加直接一点

done

// 1. import { a } from 'x';
// 2. import a from 'x';
// 3. import * as a from 'x';
// if specifier not use and x has sideEffect, become import 'x';
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// if specifier not use and x has sideEffect, become import 'x';
// if specifier is not used and x has sideEffect, convert to import 'x';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

done

@goo-yyh goo-yyh requested a review from sorrycc March 19, 2024 13:27
@stormslowly stormslowly deleted the feat/lyy_unused_specifier branch March 25, 2024 05:24
@stormslowly stormslowly restored the feat/lyy_unused_specifier branch March 25, 2024 05:40
@stormslowly stormslowly reopened this Mar 25, 2024
Copy link
Contributor

coderabbitai bot commented Mar 25, 2024

Walkthrough

The update enhances the UselessImportStmtsRemover logic within the farm_tree_shake plugin, improving the removal of redundant import specifiers, even with side effects. This advancement is demonstrated through new test files and fixtures, showcasing efficient tree-shaking across various import scenarios.

Changes

Files Summary
crates/mako/src/plugins/farm_tree_shake/.../remove_useless_stmts.rs Enhanced logic to remove unused import specifiers, considering side effects.
e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/expect.js Test file validating removal of unused specifiers in builds with side-effectful imports.
e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/{a.js, b.js, c.js} New files defining constants, logging messages, and exporting defaults.
e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/entry-{a,b,c}.js New entry files importing from a.js, b.js, and c.js respectively.

🐇✨
In the garden of code, where the tree-shake blooms,
A rabbit hopped by, cleaning up the rooms.
"Away with the clutter!" it cheerfully chants,
As it prunes away specs, no ifs, ands, or can'ts.
Now sleeker and faster, the bundles do run,
Thanks to the rabbit, the job is well done.
🌿🚀

Note

Pull Request Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://coderabbit.ai


Commits Files that changed from the base of the PR and between 2a6f8c0 and 0f030c1.
Files selected for processing (1)
  • crates/mako/src/plugins/farm_tree_shake/remove_useless_stmts.rs (2 hunks)

Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Review Status

Actionable comments generated: 0

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 8028e53 and 1f4e5e9.
Files ignored due to path filters (1)
  • e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/mako.config.json is excluded by: !**/*.json
Files selected for processing (8)
  • crates/mako/src/plugins/farm_tree_shake/remove_useless_stmts.rs (2 hunks)
  • e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/expect.js (1 hunks)
  • e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/a.js (1 hunks)
  • e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/b.js (1 hunks)
  • e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/c.js (1 hunks)
  • e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/entry-a.js (1 hunks)
  • e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/entry-b.js (1 hunks)
  • e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/entry-c.js (1 hunks)
Files skipped from review due to trivial changes (1)
  • e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/entry-b.js
Additional comments: 7
e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/entry-a.js (1)
  • 1-1: The import statement aligns with the PR's objective to optimize default imports when the imported variable is not used. Ensure that the usage of a is indeed optimized out in the broader context of the codebase.
Verification successful

The verification process indicates that the import statement "import a from './a.js';" is only found in entry-a.js, aligning with the PR's objective to optimize default imports when the imported variable is not used elsewhere in the codebase. This suggests that the optimization goal was likely achieved for this specific case. However, it's important to note that this verification does not explicitly confirm the absence of a's usage within entry-a.js itself or potential indirect usages. The broader impact and effectiveness of the optimization should still be assessed in the context of the entire codebase.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Verify that 'a' is not used elsewhere in the codebase, indicating successful optimization.
rg "import a from './a.js';" -C3

Length of output: 140

e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/entry-c.js (1)
  • 1-1: The import statement aligns with the PR's objective to optimize named imports when the imported variables are not used. Ensure that the usage of c and ccc is indeed optimized out in the broader context of the codebase.
e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/a.js (1)
  • 1-5: The file correctly demonstrates a module with side effects, aligning with the PR's objectives to test the optimization of imports when the imported variable is not used but the module has side effects.
e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/b.js (1)
  • 1-6: The file correctly demonstrates a module with side effects, aligning with the PR's objectives to test the optimization of imports when the imported variable is not used but the module has side effects.
e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/src/c.js (1)
  • 1-4: The file correctly demonstrates a module with side effects, aligning with the PR's objectives to test the optimization of named imports when the imported variables are not used but the module has side effects.
e2e/fixtures/tree-shaking.side-effects-true.unused-specifier/expect.js (1)
  • 1-61: The assertions are correctly designed to validate the optimization of import statements, aligning with the PR's objectives. This demonstrates thorough testing to ensure the effectiveness of the tree-shaking enhancements.
crates/mako/src/plugins/farm_tree_shake/remove_useless_stmts.rs (1)
  • 10-16: > 📝 NOTE

This review was outside the diff hunks and was mapped to the diff hunk with the greatest overlap. Original lines [1-123]

The logic to remove unused specifiers in import and export statements aligns with the PR's objectives and demonstrates good Rust practices. Ensure that all scenarios for import optimization are thoroughly tested, including edge cases.

@stormslowly
Copy link
Member

image

@stormslowly stormslowly merged commit 2f939b4 into master Apr 15, 2024
8 checks passed
@delete-merged-branch delete-merged-branch bot deleted the feat/lyy_unused_specifier branch April 15, 2024 09:46
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.

3 participants