Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: rollup-plugin-node-builtins #51

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions packages/node-builtins/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# @rollup/plugin-node-builtins

```
npm install --save-dev @rollup/plugin-node-builtins
```

Allows the node builtins to be `require`d/`import`ed. Doing so gives the proper shims to support modules that were designed for Browserify, some modules require [rollup-plugin-node-globals](https://github.com/calvinmetcalf/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
- timers\*
- console\*‡
- vm\*§
- zlib\*
- tty
- domain
- dns∆
- dgram∆
- child_process∆
- cluster∆
- module∆
- net∆
- readline∆
- repl∆
- tls∆
- fs˚
- crypto˚

\* requires [node-globals plugin](https://github.com/calvinmetcalf/rollup-plugin-node-globals)

† 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

```js
import builtins from 'rollup-plugin-node-builtins';
rollup({
entry: 'main.js',
plugins: [builtins()]
});
```

and now if main contains this, it should just work

```js
import EventEmitter from 'events';
import { inherits } from 'util';

// etc
```

Config for something more complicated like http

```js
import builtins from 'rollup-plugin-node-builtins';
import globals from 'rollup-plugin-node-globals';
rollup({
entry: 'main.js',
plugins: [globals(), builtins()]
});
```

# License

MIT except ES6 ports of browserify modules which are whatever the original library was.
18 changes: 18 additions & 0 deletions packages/node-builtins/build-constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const fs = require('fs');
const constants = require('constants');
const path = require('path');

const out = fs.createWriteStream(path.join(__dirname, 'dist', 'constants.js'));

Object.keys(constants).forEach((key) => {
const value = constants[key];
out.write(`export var ${key} = ${JSON.stringify(value)};\n`);
});
out.write('export default {\n ');
Object.keys(constants).forEach((key, i) => {
if (i) {
out.write(',\n ');
}
out.write(`${key}: ${key}`);
});
out.end('\n};\n');
38 changes: 38 additions & 0 deletions packages/node-builtins/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@rollup/plugin-node-builtins",
"version": "2.1.1",
"description": "use node builtins in browser with rollup",
"license": "ISC",
"repository": {
"type": "git",
"url": "git@github.com:calvinmetcalf/rollup-plugin-node-builtins.git"
},
"author": "",
"main": "dist/rollup-plugin-node-builtins.cjs.js",
"scripts": {
"build": "rollup -c -f cjs -o dist/rollup-plugin-node-builtins.cjs.js && rollup -c -f es -o dist/rollup-plugin-node-builtins.es6.js && node build-constants.js",
"prebuild": "rm -rf dist && mkdir dist",
"prepublish": "npm test",
"pretest": "npm run build",
"test": "mocha"
},
"keywords": [
"rollup-plugin"
],
"dependencies": {
"browserify-fs": "^1.0.0",
"buffer-es6": "^4.9.2",
"crypto-browserify": "^3.11.0",
"process-es6": "^0.11.2"
},
"devDependencies": {
"babel-preset-es2015-rollup": "^3.0.0",
"debug": "^2.2.0",
"mocha": "^3.0.2",
"rollup": "^0.37.0",
"rollup-plugin-babel": "^2.4.0",
"rollup-plugin-node-globals": "^1.0.7",
"serve": "^2.0.0"
},
"jsnext:main": "dist/rollup-plugin-node-builtins.es6.js"
}
9 changes: 9 additions & 0 deletions packages/node-builtins/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import babel from 'rollup-plugin-babel';

const external = Object.keys(require('./package.json').dependencies).concat('path');

export default {
entry: 'src/index.js',
plugins: [babel()],
external
};
Loading