JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 134131
  • Score
    100M100P100Q168099F
  • License ISC

use node builtins in browser with rollup

Package Exports

  • rollup-plugin-node-builtins

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (rollup-plugin-node-builtins) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

rollup-plugin-node-builtins

npm install --save-dev rollup-plugin-node-builtins

Allows the node builtins to be required/imported. Doing so gives the proper shims to support modules that were designed for Browserify, some modules require rollup-plugin-node-globals.

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.

  • process*
  • events
  • stream*
  • util
  • path
  • buffer*
  • querystring
  • url
  • string_decoder*
  • punycode
  • http*†
  • https*†
  • os*
  • assert*
  • constants

* requires node-globals plugin

† the http and https modules are actually the same and don't differentiate based on protocol

for all other modules this just provides the commonjs module form browserify and you will likely need to use rollup-plugin-commonjs, rollup-plugin-node-resolve, and rollup-plugin-json in order for them to work, some like crypto, are complex enough that they don't work very well with rollup at all, others may work.

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 builtins from 'rollup-plugin-node-builtins';
rollup({
  entry: 'main.js',
  plugins: [
    builtins()
  ]
})

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 builtins from 'rollup-plugin-node-builtins';
import globals from 'rollup-plugin-node-globals';
rollup({
  entry: 'main.js',
  plugins: [
    globals(),
    builtins()
  ]
})

Then something like

If you need more compex things like a module not listed above then you need to do the following: node_modules/rollup-plugin-node-globals/**, node_modules/buffer-es6/**, , node_modules/process-es6/** and node_modules/rollup-plugin-node-builtins/src/es6/** to the commonjs excludes if you use that plugin and make sure you set browser to be true in nodeResolve. Also it should come before nodeResolve and globals should come after commonjs. For example:

Config for using this with some of the non ported to es6 modules

import commonjs from 'rollup-plugin-commonjs';
import nodeResolve from 'rollup-plugin-node-resolve';
import globals from 'rollup-plugin-node-globals';
import builtins from 'rollup-plugin-node-builtins';
import json from 'rollup-plugin-json';
rollup({
  entry: 'main.js',
  plugins: [
    builtins(),
    nodeResolve({ jsnext: true, main: true, browser: true }),
    commonjs({
      ignoreGlobal: true
    }),
    globals(),
    json()
  ]
})

License

MIT except ES6 ports of browserify modules which are whatever the original library was.