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