FredKSchott / FredKSchott/rollup-plugin-polyfill-node

Issues with global and other polyfills (especially with vite dev server)

Open
#14 0 comments 15 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
193
Forks
60
PR merge metrics
No merged PRs in 30d

Description

If I manually patch for #8, I still hit a number of issues that prevent this plugin from working with vite's dev server. I'm not sure where the underlying causes sit, so I'm outlining the issues here to start.

In the examples below, I'm working with a tiny test ("tt") module that contains just a single js file that references `global`.

```js
import nodePolyfills from 'rollup-plugin-polyfill-node';

export default {
plugins: [
nodePolyfills({
include: [
'*.js',
'node_modules/**/*.js',
// (1)
new RegExp('node_modules/.vite/.*js')],
// (2)
exclude: ['node_modules/polyfill-nodeglobal.js']
}),
]};
```

(1) In the dev server, requests come for files like "http://localhost:3000/node_modules/.vite/tt.js?v=75d8e834" -- and the `?v=` parameter prevents the standard `node_modules/**/*.js` from matching these requests.

(2) There is no actual `node_modules/polyfill-nodeglobal.js` file on my disk, but without this `exclude`, I get a **circular reference** in the global polyfill. That is, "http://localhost:3000/@id/__x00__polyfill-node:global.js" has content like:

```js
import { default as global } from '/@id/__x00__polyfill-node:global.js';

export default (typeof global !== "undefined" ? global :
typeof self !== "undefined" ? self :
typeof window !== "undefined" ? window : {});
```

(3) In the transformed content generated by this plugin, `map.sources` is populated with a value like `sources: [ '../tt/index.js', 'dep:tt' ]`. I'm not sure what the `dep:` array entry indicates, but the vite dev server crashes on this [when it tries to find a filesystem entry](https://github.com/vitejs/vite/blob/eb66b4350c635fb4f2ef2e8a9eb50958cde73743/packages/vite/src/node/server/sourcemap.ts#L15) named `dep:tt`.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.