Skip to content

Static flags are not tree-shaken #7235

@pi0

Description

@pi0

Rolldown: 1.0.0-beta.52

In rollup, static flags (imported from another file) can be tree-shaken correctly.

Example: (stackblitz)

// src/flags.js
export const flag = false;
// src/index.js
import { flag } from './flags.js';

if (flag) {
  console.log('should not see me in bundle!');
} else {
  console.log('flag is false correctly');
}

Rollup bundle:

{
  console.log('flag is false correctly');
}

Rolldown bundle:

//#region src/flags.js
const flag = false;

//#endregion
//#region src/index.js
if (flag) console.log("should not see me in bundle!");
else console.log("flag is false correctly");

//#endregion

More context:

  • This works as intended when the const flag is in the same file
  • Nitro v3 relies on this for tree-shaking server internals, and currently, tree-shaking is not effective with rolldown/rolldown-vite, causing lots of unnecessary code to be bundled. If it is a non goal of rolldown for any reason we might consider alternative methods like static code replacement.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions