Skip to content

the vite plugin applies unenv polyfills in the wrong order #9562

@threepointone

Description

@threepointone

What versions & operating system are you using?

wrangler@4.19.2, @cloudflare/vite-plugin@1.5.1

Please provide a link to a minimal reproduction

https://github.com/threepointone/workers-process-access-error

Describe the Bug

The issue: we use unenv to polyfill node.js compatibility in workers. However, with the vite plugin, globalThis.process gets assigned with the polyfilled process object only after importing and evaluating dependencies, which means any dependency that uses process will not have access to the polyfilled process object.

For this discussion, I made a module called early-process-access that logs the value of process.stderr, the contents of which are simply:

console.log(
  "inside early-process-access, logging process.stderr",
  process.stderr
);

And I have a worker, that imports this module, and then also logs the same itself, like so:

import "early-process-access";

console.log("in main worker, logging process.stderr", process.stderr);

export default {
  async fetch(request, env, ctx) {
    return new Response("Hello, world!");
  },
};

When we run dev/prod, we expect this log:

inside early-process-access, logging process.stderr WriteStream { fd: 2, columns: 80, rows: 24, isTTY: false }
in main worker, logging process.stderr WriteStream { fd: 2, columns: 80, rows: 24, isTTY: false }

wrangler (works as expected)

when using wrangler, this works as expected. Indeed, if you run wrangler build, and inspect the output, you'll see code in this order:

// ...
// node_modules/wrangler/_virtual_unenv_global_polyfill-@cloudflare-unenv-preset-node-process
globalThis.process = process_default;

// node_modules/early-process-access/index.js
console.log(
  "inside early-process-access, logging process.stderr",
  process.stderr
);

// index.js
console.log("in main worker, logging process.stderr", process.stderr);
// ...

vite (does not work as expected)

when using vite, the output is:

inside early-process-access, logging process.stderr undefined

in main worker, logging process.stderr WriteStream { fd: 2, columns: 80, rows: 24, isTTY: false }

we see undefined there, because process.stderr is not polyfilled yet. Indeed, if you run vite build and inspect the output, you'll see code in this order:

//...
console.log(
  "inside early-process-access, logging process.stderr",
  process.stderr
);

globalThis.process = _process;

console.log("in main worker, logging process.stderr", process.stderr);
// ...

(note that globalThis.process gets assigned after the early-process-access module is imported and evaled.)

Please provide any relevant error logs

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No fields configured for Bug.

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions