1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
rollup-plugin-node-globals
===
Plugin to insert node globals including so code that works with browserify should work even if it uses process or buffers. This is based on [rollup-plugin-inject
](https://github.com/rollup/rollup-plugin-inject).
- process
- global
- Buffer
- `__dirname`
- `__filename`
Plus `process.nextTick` and `process.browser` are optimized to only pull in
themselves and `__dirname` and `__filename` point to the file on disk
There are a few options to control output
- `process` - pass `false` to disable process polyfilling
- `global` - pass `false` to disable global polyfilling
- `buffer` - pass `false` to disable Buffer polyfilling
- `dirname` - pass `false` to disable `__dirname` polyfilling
- `filename` - pass `false` to disable `__filename` polyfilling
- `baseDir` which is used for resolving `__dirname` and `__filename`.
# examples
```js
var foo;
if (process.browser) {
foo = 'bar';
} else {
foo = 'baz';
}
```
turns into
```js
import {browser} from 'path/to/process';
var foo;
if (browser) {
foo = 'bar';
} else {
foo = 'baz';
}
```
but with rollup that ends up being
```js
var browser = true;
var foo;
if (browser) {
foo = 'bar';
} else {
foo = 'baz';
}
```
or
```js
var timeout;
if (global.setImmediate) {
timeout = global.setImmediate;
} else {
timeout = global.setTimeout;
}
export default timeout;
```
turns into
```js
import {_global} from 'path/to/global.js';
var timeout;
if (_global.setImmediate) {
timeout = _global.setImmediate;
} else {
timeout = _global.setTimeout;
}
export default timeout;
```
which rollup turns into
```js
var _global = typeof global !== "undefined" ? global :
typeof self !== "undefined" ? self :
typeof window !== "undefined" ? window : {}
var timeout;
if (_global.setImmediate) {
timeout = _global.setImmediate;
} else {
timeout = _global.setTimeout;
}
var timeout$1 = timeout;
export default timeout$1;
```
With that top piece only showing up once no matter how many times global was used.
|