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

#__PURE__ on member expressions should be handled #4893

Closed
dgreensp opened this issue Jun 6, 2022 · 10 comments · Fixed by #5050
Closed

#__PURE__ on member expressions should be handled #4893

dgreensp opened this issue Jun 6, 2022 · 10 comments · Fixed by #5050
Assignees
Milestone

Comments

@dgreensp
Copy link

dgreensp commented Jun 6, 2022

Describe the feature

If I have an export like export const foo = ..., this export will only be removed if the RHS can be determined to be side-effect-free, understandably. Member accesses in general are considered to potentially have side effects (with the exception of Math.PI; see implementation of is_pure_callee), which is very conservative. I have exports like export const HTMLElement = window.HTMLElement in a module, and it's inconvenient that SWC is worried about the side effects of an expression like window.HTMLElement.

There is a quite ugly workaround, involving temporarily reassigning the global Math. It would be great if there were some other way to mark an expression (or export) as side-effect-free, such as with a comment, a wrapper function with a magic name, or even some identifier besides Math that you can access properties on and have it considered to be pure, like if you name your local variable __pure you are ok.

Babel plugin or link to the feature description

No response

Additional context

No response

@Austaras
Copy link
Member

Austaras commented Jun 7, 2022

terser/terser#513

@Austaras
Copy link
Member

Reread the terser thread, seems like the only objection is maintenance cost.

@kdy1
Copy link
Member

kdy1 commented Jun 20, 2022

Honestly, I think the problem is that you have

export const HTMLElement = window.HTMLElement in a module.

I'm fine with the maintenance cost, but not sure about adding it to getter instead of a member expression

@Austaras
Copy link
Member

The maintenance cost of swc is sky high anyway

const href = location.href seems like a fairly common pattern

@kdy1
Copy link
Member

kdy1 commented Jun 20, 2022

I think access to global location can be marked as pure.

@kdy1
Copy link
Member

kdy1 commented Jun 20, 2022

I think we can add the pure comment support for member expressions, and I think we should.

Currently, this can be done via

export const MyExport = /*#__PURE__*/ (function () { return window.MyExport; })()

but this does not look like a good idea.

@kdy1 kdy1 changed the title Provide way to mark an expression "pure" / side-effect-free for tree-shaking #__PURE__ on member expressions should be handled Jun 20, 2022
@dgreensp
Copy link
Author

I didn't know about /*#__PURE__*/. I will try that out.

@dgreensp
Copy link
Author

For what I'm doing (Deno bundling), the comment here doesn't seem to have any effect:

export const MyExport = /*#__PURE__*/ (function () { return window.MyExport; })()

I was grepping the SWC code for __PURE__, and it looks like it is checked for in the minifier, not the optimizer? I'm not sure if that's right.

@kdy1
Copy link
Member

kdy1 commented Jun 21, 2022

Yeah, it's only handled by the minifier

@swc-bot
Copy link
Collaborator

swc-bot commented Oct 16, 2022

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@swc-project swc-project locked as resolved and limited conversation to collaborators Oct 16, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Development

Successfully merging a pull request may close this issue.

4 participants