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 support for preserving comments in code #967

Closed
wants to merge 2 commits into from

Conversation

tido64
Copy link
Contributor

@tido64 tido64 commented Apr 17, 2023

Summary

Add support for preserving comments in code. Useful for preserving /*#__PURE__*/ annotations that aid tree shaking.

Test plan

Test case here: microsoft/rnx-kit#2365

Size
Before 1567764
After 1258413
Diff -309351

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 17, 2023
@robhogan
Copy link
Contributor

Thanks! This seems pretty well defined - I wonder whether we could just go straight to stable with this. WDYT @motiz88?

@motiz88
Copy link
Contributor

motiz88 commented Apr 17, 2023

This seems pretty well defined - I wonder whether we could just go straight to stable with this.

@robhogan: Makes sense to me. I guess this is something all transformers (even non-Babel-based ones) would be able to implement, so no concerns there. The only real gap IMO is that we should document this in https://facebook.github.io/metro/docs/configuration/#transformer-options if stable. To avoid confusion, I wouldn't explicitly mention tree-shaking, and instead just briefly describe what the option itself does.

@tido64 tido64 force-pushed the tido/preserve-comments branch 2 times, most recently from 967c64a to 6ba1071 Compare April 18, 2023 05:44
tido64 added 2 commits April 19, 2023 13:08
Useful for preserving `/*#__PURE__*/` annotations that aid tree shaking.
@tido64 tido64 force-pushed the tido/preserve-comments branch from 6ba1071 to bd567e5 Compare April 19, 2023 11:08
@@ -111,6 +111,7 @@ const getDefaultValues = (projectRoot: ?string): ConfigT => ({
},
output: {
ascii_only: true,
comments: false,
Copy link
Contributor

Choose a reason for hiding this comment

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

  1. I don't see a need to specify this explicitly.
  2. Our default minifier, Terser, has a documented default of "some", set here, so this overrides that and may be an unwanted change in behaviour. Per 1, let's just remove this explicit Metro default.
Suggested change
comments: 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.

Previously, we had comments: false in Babel, which basically omits all comments, so no comments ever reach the minifier. To preserve that behaviour, we need to set comments: false here. If you still want to remove it, that's fine with me, but it will produce a different output than today.

Copy link
Contributor

Choose a reason for hiding this comment

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

I see! Yeah, in that case we should probably land this with a behaviour-preserving default so it can go out as a patch, then align with terser's default in the next major. (Would you mind sending a separate PR with that?)

The only thing I don't get is: if this is supposed to be behaviour-preserving, why did the buildGraph test snapshot change? Do we have a good test covering the actual default from Metro's config?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It changed because it's unminified, so the comments have yet to be stripped out.

I can send out a separate PR to remove this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for merging this PR! Here's the follow-up: #972

@facebook-github-bot
Copy link
Contributor

@motiz88 has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot
Copy link
Contributor

@motiz88 merged this pull request in 8d8f317.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants