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

Add pure-like native function check for tree shake #2644

Closed
LongTengDao opened this issue Jan 5, 2019 · 3 comments
Closed

Add pure-like native function check for tree shake #2644

LongTengDao opened this issue Jan 5, 2019 · 3 comments

Comments

@LongTengDao
Copy link
Contributor

LongTengDao commented Jan 5, 2019

I saw there is a list for native pure function in rollup source code (src/ast/nodes/shared/pureFunctions.ts), it's useful for tree shake. Without that (and assumption of set/get did nothing side effect, native methods did not be overrided, etc), we can almost shake nothing, because every lib has it's initialization part which may use many native methods, and get/set many properties.

But there are some functions, not pure, but pure like. Pure functions should do nothing change to arguments, when pure-like functions (I don't know how to name them usually) do something change to arguments, but no side effect, so if the returned value of pure-like function is not used, it can also be considered to be shaked. Like: Object.assign Object.freeze...

Without this feature, so many external modules initialization part will leave...

Feature Use Case

module.js:

const object = Object.freeze(Object.assign(Object.create(null), { a:1, b:2 }));

export function f ( ) { return object; }

export function g ( ) { }

input.js:

import { g } from './module.js';

console.log(g);

Feature Proposal

Output this:

function g ( ) { }

console.log(g);

Not:

Object.freeze(Object.assign(Object.create(null), { a:1, b:2 }));

function g ( ) { }

console.log(g);
@LongTengDao
Copy link
Contributor Author

LongTengDao commented Feb 23, 2019

@lukastaegert Hi! Sorry to trouble you again. Is this feature I mentioned possible and easy to implement? Or there could be a custom api for pure/pure-like functions? Thank you!

@leeoniya
Copy link

have you seen #2429?

@LongTengDao
Copy link
Contributor Author

have you seen #2429?

Oh, that solution is better, thank you!

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

No branches or pull requests

2 participants