
Security News
PyPI Now Supports iOS and Android Wheels for Mobile Python Development
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
rollup-plugin-node-polyfills
Advanced tools
The rollup-plugin-node-polyfills package provides polyfills for Node.js core modules, allowing you to use Node.js-specific modules in a browser environment when bundling with Rollup.
Buffer Polyfill
This feature allows you to use the Buffer module, which is a core module in Node.js, in a browser environment. The code sample demonstrates creating a Buffer from a string and converting it to a hexadecimal string.
import { Buffer } from 'buffer';
const buf = Buffer.from('Hello World', 'utf8');
console.log(buf.toString('hex'));
Stream Polyfill
This feature allows you to use the Stream module in a browser environment. The code sample demonstrates creating a readable stream and pushing data into it.
import { Readable } from 'stream';
const readable = new Readable();
readable.push('Hello World');
readable.push(null);
readable.on('data', (chunk) => {
console.log(chunk.toString());
});
Crypto Polyfill
This feature allows you to use the Crypto module in a browser environment. The code sample demonstrates creating a SHA-256 hash of a string.
import { createHash } from 'crypto';
const hash = createHash('sha256');
hash.update('Hello World');
console.log(hash.digest('hex'));
The rollup-plugin-polyfill-node package provides similar functionality by polyfilling Node.js core modules for use in a browser environment. It is an alternative to rollup-plugin-node-polyfills and offers a similar set of polyfills.
Browserify is a tool that allows you to use Node.js modules in the browser by bundling up all of your dependencies. It includes built-in support for many Node.js core modules, making it a more comprehensive solution compared to rollup-plugin-node-polyfills.
Webpack is a popular module bundler that can also polyfill Node.js core modules for use in the browser. With the appropriate configuration and plugins, Webpack can provide similar functionality to rollup-plugin-node-polyfills, but it is a more general-purpose tool with a broader range of features.
npm install --save-dev rollup-plugin-node-polyfills
Allows the node builtins to be require
d/import
ed.
The following modules include ES6 specific version which allow you to do named imports in addition to the default import and should work fine if you only use this plugin.
† the http and https modules are actually the same and don't differentiate based on protocol
‡ default export only, because it's console, seriously just use the global
§ vm does not have all corner cases and has less of them in a web worker
∆ not shimmed, just returns mock
˚ optional, add option to enable browserified shim
Crypto is not shimmed and and we just provide the commonjs one from browserify and it will likely not work, if you really want it please pass {crypto: true}
as an option.
Not all included modules rollup equally, streams (and by extension anything that requires it like http) are a mess of circular references that are pretty much impossible to tree-shake out, similarly url methods are actually a shortcut to a url object so those methods don't tree shake out very well, punycode, path, querystring, events, util, and process tree shake very well especially if you do named imports.
config for using this with something simple like events or querystring
import nodePolyfills from 'rollup-plugin-node-polyfills';
rollup({
entry: 'main.js',
plugins: [
nodePolyfills()
]
})
and now if main contains this, it should just work
import EventEmitter from 'events';
import {inherits} from 'util';
// etc
Config for something more complicated like http
import nodePolyfills from 'rollup-plugin-node-polyfills';
rollup({
entry: 'main.js',
plugins: [
nodePolyfills()
]
})
MIT except ES6 ports of browserify modules which are whatever the original library was.
FAQs
rollup-plugin-node-polyfills ===
The npm package rollup-plugin-node-polyfills receives a total of 1,007,427 weekly downloads. As such, rollup-plugin-node-polyfills popularity was classified as popular.
We found that rollup-plugin-node-polyfills demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.