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 simple way to manually declare pure functions #4718

Merged
merged 11 commits into from Nov 27, 2022

Conversation

lukastaegert
Copy link
Member

@lukastaegert lukastaegert commented Nov 18, 2022

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:
supports #4327

Description

For features like chunk merging by minimal chunk size, it is very important to properly detect side effect free code as otherwise, chunks will not be merged that could otherwise be merged.
Sometimes, however, there are limits to what Rollup can detect, and by default, Rollup is always conservative about its assumptions. As a quick fix, this feature allows to mark function calls as pure solely by name matching:

treeshake.manualPureFunctions
Type: string[]
CLI: --treeshake.manualPureFunctions <names>

Allows to manually define a list of function names that should always be considered "pure", i.e. they have no side effects like changing global state etc. when called. The check is performed solely by name.

This can not only help with dead code removal, but can also improve JavaScript chunk generation especially when using experimentalMinChunkSize.

Besides any functions matching that name, any properties on a pure function and any functions returned from a pure functions will also be considered pure.

// rollup.config.js
export default {
  treeshake: {
    preset: 'smallest',
    manualPureFunctions: ['styled', 'local']
  }
  // ...
};

// code
import styled from 'styled-components';
const local = console.log;

local(); // removed
styled.div`
  color: blue;
`; // removed
styled?.div(); // removed
styled()(); // removed
styled().div(); // removed

@github-actions
Copy link

github-actions bot commented Nov 18, 2022

Thank you for your contribution! ❤️

You can try out this pull request locally by installing Rollup via

npm install rollup/rollup#manual-pure-functions

or load it into the REPL:
https://rollupjs.org/repl/?pr=4718

@codecov
Copy link

codecov bot commented Nov 18, 2022

Codecov Report

Merging #4718 (0290189) into master (4ee9a20) will increase coverage by 0.00%.
The diff coverage is 98.61%.

❗ Current head 0290189 differs from pull request most recent head c0d18bd. Consider uploading reports for the commit c0d18bd to get more accurate results

@@           Coverage Diff           @@
##           master    #4718   +/-   ##
=======================================
  Coverage   99.03%   99.03%           
=======================================
  Files         216      217    +1     
  Lines        7664     7690   +26     
  Branches     2116     2126   +10     
=======================================
+ Hits         7590     7616   +26     
  Misses         24       24           
  Partials       50       50           
Impacted Files Coverage Δ
src/Module.ts 100.00% <ø> (ø)
src/ast/nodes/ArrayExpression.ts 100.00% <ø> (ø)
src/ast/nodes/ObjectExpression.ts 100.00% <ø> (ø)
src/ast/nodes/PropertyDefinition.ts 100.00% <ø> (ø)
src/ast/nodes/shared/ClassNode.ts 100.00% <ø> (ø)
src/ast/nodes/shared/ObjectMember.ts 100.00% <ø> (ø)
src/ast/utils/PathTracker.ts 100.00% <ø> (ø)
src/utils/options/options.ts 100.00% <ø> (ø)
src/ast/nodes/shared/MethodTypes.ts 96.29% <75.00%> (ø)
src/Graph.ts 100.00% <100.00%> (ø)
... and 18 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@rollup-bot
Copy link
Collaborator

This PR has been released as part of rollup@3.4.0-0. Note that this is a pre-release, so to test it, you need to install Rollup via npm install rollup@3.4.0-0 or npm install rollup@beta. It will likely become part of a regular release later.

@rollup-bot
Copy link
Collaborator

This PR has been released as part of rollup@3.4.0-1. Note that this is a pre-release, so to test it, you need to install Rollup via npm install rollup@3.4.0-1 or npm install rollup@beta. It will likely become part of a regular release later.

@lukastaegert lukastaegert merged commit 290b07d into master Nov 27, 2022
@lukastaegert lukastaegert deleted the manual-pure-functions branch November 27, 2022 06:32
@rollup-bot
Copy link
Collaborator

This PR has been released as part of rollup@3.5.0. You can test it via npm install rollup.

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.

None yet

2 participants