Skip to content

[Bug]: Wrong in the 'if' conditional statement tree shaking, even when calling side effect functions #7905

@zhylmzr

Description

@zhylmzr

Reproduction link or steps

I discovered this issue while using the beta versions of Vite and Vue. here's the error example:

rolldown playground

For comparison, here's the same example in Rollup.

rollup playrgound

Here's the example code listed below for easy reference.

const isFunction = (val) => typeof val === "function";
const extend = Object.assign;
const h = (a, b, c) => {}

// @__NO_SIDE_EFFECTS__
function defineComponent(options, extraOptions = {}) {
  // 1. no problem if use a ternary conditional statement
  // return isFunction(options) ? (
  //   /* @__PURE__ */ (() => extend({ name: options.name }, extraOptions, { setup: options }))()
  // ) : options;

  // 2. wrong tree sharking
  if (isFunction(options)) {
    return /* @__PURE__ */ (() => extend({ name: options.name }, extraOptions, { setup: options }))()
  }

  return options
}

const bar = defineComponent(() => h('div', null, 'hello world'))
console.log(bar) // side-effect

What is expected?

Works just like Rollup.

What is actually happening?

The code was incorrectly removed by tree shaking.

System Info

System:
    OS: macOS 15.6.1
    CPU: (8) arm64 Apple M1
    Memory: 274.03 MB / 16.00 GB
    Shell: 4.0.0 - /opt/homebrew/bin/fish
  Binaries:
    Node: 22.14.0 - /Users/zhylmzr/.volta/tools/image/node/22.14.0/bin/node
    Yarn: 4.5.3 - /Users/zhylmzr/.volta/tools/image/yarn/4.5.3/bin/yarn
    npm: 10.9.2 - /Users/zhylmzr/.volta/tools/image/node/22.14.0/bin/npm
    pnpm: 9.8.0 - /Users/zhylmzr/.volta/bin/pnpm
    bun: 1.3.5 - /Users/zhylmzr/.bun/bin/bun
    Watchman: 2025.11.10.00 - /opt/homebrew/bin/watchman
  Browsers:
    Chrome: 143.0.7499.170
    Safari: 18.6

Any additional comments?

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions