UNPKG

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