FredKSchott / rollup-plugin-polyfill-node

A modern Node.js polyfill for your Rollup bundle.
Other
175 stars 55 forks source link

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

Open jmandel opened 3 years ago

jmandel commented 3 years ago

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.

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:

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 named dep:tt.