Skip to content

Expected polyfills not applied when using @vitejs/plugin-legacy when using [email protected] or later #404

@mxmason

Description

@mxmason

Describe the regression

When using @vitejs/plugin-legacy, the final build doesn't contain the polyfills it should. The behavior of rolldown-vite is different than that of vite.

Reproduction

https://stackblitz.com/edit/vitejs-vite-xjjempqm?file=vite.config.ts

Expected Behavior

I expect a polyfill for private class fields to be introduced to the final bundle when targeting certain browsers. given the following source code...

class BuilderReport {
  #name = 'Vite';
  getName() {
    return this.#name;
  }
}

const report = new BuilderReport()
console.log(report.getName())

...and this config for @vitejs/plugin-legacy:

legacy({
  modernPolyfills: true,
  targets: ['ios_saf >= 13', 'safari >= 13'],
  renderLegacyChunks: false,
})

...the#name field should be polyfilled. This does happen in Vite, but not in rolldown-vite.

Actual Behavior

In rolldown-vite builds, the build process does not add a polyfill for #name.

Edit to add: This difference in behavior only occurs in versions in [email protected] and up. In my repro link, running rolldown @ 7.1.2 explicitly produces the results I expect:

# Pinning to 7.1.2 applies polyfills
npx -y [email protected] build --outDir ./dist-rolldown

# Using latest does not apply polyfills
npx -y rolldown-vite build --outDir ./dist-rolldown

Maybe this has to do with enableNativePlugin being set to true by default?

Steps to Reproduce

  1. Run npm install
  2. Run npm run build && npm run build:rolldown
  3. In the dist directory, which is produced by Vite, note that the JS asset includes a __privateAdd() polyfill for the #name field
  4. In the dist-rolldown directory, which is produced by rolldown-vite, note that the JS asset includes the #name field with no polyfill

System Info

System:
    OS: macOS 15.6.1
    CPU: (10) arm64 Apple M4
    Memory: 106.47 MB / 16.00 GB
    Shell: 5.9 - /bin/zsh
  Binaries:
    Node: 20.11.1 - ~/.local/state/fnm_multishells/25078_1757435370919/bin/node
    npm: 10.2.4 - ~/.local/state/fnm_multishells/25078_1757435370919/bin/npm
    bun: 1.2.20 - ~/.bun/bin/bun
  Browsers:
    Chrome: 140.0.7339.81
    Safari: 18.6

Used Package Manager

npm

Logs

No response

Validations

Metadata

Metadata

Assignees

Labels

feat: native pluginsrelated to exprimental.enableNativePlugins

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions