FredKSchott / rollup-plugin-polyfill-node

A modern Node.js polyfill for your Rollup bundle.
Other
179 stars 57 forks source link

feat: allow filtering of polyfills #82

Open tbrannam opened 6 months ago

tbrannam commented 6 months ago

Some background to this feature: Often the need for a polyfill may be due to a misconfigured project, where a ‘browser’ import was not properly resolved. Webpack 5 has defaulted to disabling polyfills by default. This behavior is advantageous in that the developer is more aware of the inclusion of polyfills, and perhaps may be able to determine why an unexpected polyfill may be included.

This change does not undo the default behavior, but does allow a developer to opt-in to a more restrictive approach.

The default implementation will silently polyfill from the complete list of supported modules.

This feature adds support to allow the rollup-project to explicitly allow specific polyfills, or simply log their use as an advisory to the developer.

If a polyfill is rejected, the project will expose retain the import as an external reference.

    nodePolyfills({
      onPolyfill: (module, impl) => {
        const allowList = ['util']
        const allow = allowList.includes(module)
        if (allow === false) {
          console.warn('module will be maintained as external import', module)
        }

        if (impl === undefined) {
           console.warn('module will be implemented with a mock module', module)
           // alternatively, return a string with alternative polyfill
        }

        return allow
      }
    })
tbrannam commented 6 months ago

I'm looking to extend this PR with an addition that allows alternative polyfill #81