Skip to content

vite:esm-shim can corrupt Vite 8/Rolldown ESM main bundle and emit empty entry #906

Closed
kitschpatrol/electron-vite
#1

Description

@pscanf

Describe the bug

With [email protected] and Vite 8/Rolldown, the injected vite:esm-shim plugin can corrupt an ESM main-process bundle and cause the main entry to be emitted as an empty file.

In our app, the Electron main build emits:

dist/main/index.js  0.00 kB

Removing only the vite:esm-shim plugin from the resolved main config makes the same build emit a valid bundle:

dist/main/index.js  ~3.5 MB

Versions

electron-vite: 6.0.0-beta.1
vite: 8.0.13
electron: 42.1.0
node: 24.14.0 / Electron Node 24.15.0
package type: module
main output format: es

What appears to happen

vite:esm-shim checks rendered chunks with:

const CJSyntaxRe = /__filename|__dirname|require\(|require\.resolve\(/

If it sees CommonJS-looking syntax, it tries to insert the shim after the last static import, using a regex over the fully rendered chunk.

In this app's generated main chunk, that static import regex matches import-looking text inside embedded string content, not a real top-level ESM import. I instrumented the shim and it selected this false match as the last import:

import {XxxModel, XxxOption} from './XxxModel.js';

That match was around offset 2649169 in the rendered chunk. The shim was then inserted at that position, corrupting the chunk. Rolldown subsequently emitted the entry chunk as empty.

Relevant debug output

Instrumenting an equivalent shim plugin produced:

renderChunk index.js es 3498223 true
inject at 2649169 imports 60 last "import {XxxModel, XxxOption} from './XxxModel.js';"
rendering chunks...
bundle index.js 0

Expected behavior

The shim should not parse rendered JavaScript with a regex that can match import-like text inside strings/comments. It should either avoid this injection when safe globals already exist, use an AST-aware insertion strategy, or insert at a known safe position.

Workaround

We currently remove only vite:esm-shim from the resolved Electron main Vite config:

config.plugins = config.plugins.filter((plugin) => plugin.name !== "vite:esm-shim");

This works for us because our code already uses import.meta.dirname, and Vite/Rolldown emits the necessary helpers at the top of the bundle.

Activity

  1. Hona commented on Jul 4, 2026

    @Hona

    I'm running into this too one OpenCode Desktop

  2. added a commit that references this issue on Aug 7, 2026
    e9a9478
  3. added a commit that references this issue on Aug 27, 2026
    caaded7
  4. alex8088 commented on Sep 28, 2026

    @alex8088
    Owner

    6.0.0-beta.4 is out

  5. alex8088 commented on Oct 22, 2026

    @alex8088
    Owner

    v6.0.0 is out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions