npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rsbuild/plugin-node-polyfill

v1.2.0

Published

An Rsbuild plugin to automatically inject polyfills for [Node.js builtin modules](https://nodejs.org/api/modules.html#built-in-modules) into the browser side.

Downloads

95,619

Readme

@rsbuild/plugin-node-polyfill

An Rsbuild plugin to automatically inject polyfills for Node.js builtin modules into the browser side.

When to use

Normally, we don't need to use Node builtin modules on the browser side. However, it is possible to use some Node builtin modules when the code will run on both the Node side and the browser side, and this plugin provides browser versions of polyfills for these Node builtin modules.

By using the Node Polyfill plugin, polyfills for Node builtin modules are automatically injected into the browser-side, allowing you to use these modules on the browser side with confidence.

Usage

Install:

npm add @rsbuild/plugin-node-polyfill -D

Add plugin to your rsbuild.config.ts:

// rsbuild.config.ts
import { pluginNodePolyfill } from '@rsbuild/plugin-node-polyfill'

export default {
  plugins: [pluginNodePolyfill()],
}

Node Polyfills

Globals

  • Buffer
  • process

When you use the above global variables in your code, the corresponding polyfill will be automatically injected.

For instance, the following code would inject the Buffer polyfill:

const bufferData = Buffer.from('abc')

You can disable this behavior through the globals option of the plugin:

pluginNodePolyfill({
  globals: {
    Buffer: false,
    process: false,
  },
})

Modules

  • assert
  • buffer
  • console
  • constants
  • crypto
  • domain
  • events
  • http
  • https
  • os
  • path
  • punycode
  • process
  • querystring
  • stream
  • _stream_duplex
  • _stream_passthrough
  • _stream_readable
  • _stream_transform
  • _stream_writable
  • string_decoder
  • sys
  • timers
  • tty
  • url
  • util
  • vm
  • zlib

When the above module is referenced in code via import / require syntax, the corresponding polyfill will be injected.

import { Buffer } from 'buffer'

const bufferData = Buffer.from('abc')

Fallbacks

  • child_process
  • cluster
  • dgram
  • dns
  • fs
  • module
  • net
  • readline
  • repl
  • tls

Currently there is no polyfill for the above modules on the browser side, so when you import the above modules, it will automatically fallback to an empty object.

import fs from 'fs'

console.log(fs) // -> {}

Options

globals

Used to specify whether to inject polyfills for global variables.

  • Type:
type Globals = {
  process?: boolean
  Buffer?: boolean
}
  • Default:
const defaultGlobals = {
  Buffer: true,
  process: true,
}

protocolImports

Whether to polyfill Node.js builtin modules starting with node:.

  • Type: boolean
  • Default: true

For example, if you disable protocolImports, modules such as node:path, node:http, etc. will not be polyfilled.

pluginNodePolyfill({
  protocolImports: false,
})

include

Specify an array of modules for which polyfills should be injected. If this option is set, only the specified modules will be polyfilled. include is mutually exclusive with exclude.

  • Type: string[]
  • Default: undefined
pluginNodePolyfill({
  include: ['buffer', 'crypto'], // Only "buffer" and "crypto" modules will be polyfilled.
})

exclude

Specify an array of modules for which polyfills should not be injected from the default. If this option is set, the specified modules will be excluded from polyfilled. exclude is mutually exclusive with include.

  • Type: string[]
  • Default: undefined
pluginNodePolyfill({
  exclude: ['http', 'https'], // All modules except "http" and "https" will be polyfilled.
})

overrides

Override the default polyfills for specific modules.

  • Type: Record<string, string>
  • Default: {}
pluginNodePolyfill({
  overrides: {
    fs: 'memfs',
  },
})

Exported variables

  • builtinMappingResolved: A map of Node.js builtin modules to their resolved corresponding polyfills modules.
  • resolvedPolyfillToModules: A map of resolved polyfill modules to the polyfill modules before resolving.

License

MIT.