Rollup plugin for shimming Node.js built-in modules in browser environments
npx @tessl/cli install tessl/npm-rollup-plugin-node-builtins@2.1.00
# Rollup Plugin Node Builtins
1
2
Rollup Plugin Node Builtins is a Rollup plugin that provides browser-compatible polyfills for Node.js built-in modules. It enables Node.js code to run in browser environments by shimming all core Node.js modules with ES6-compatible implementations or external dependencies.
3
4
## Package Information
5
6
- **Package Name**: rollup-plugin-node-builtins
7
- **Package Type**: npm
8
- **Language**: JavaScript (ES6)
9
- **Installation**: `npm install rollup-plugin-node-builtins`
10
11
## Core Imports
12
13
```javascript
14
import builtins from "rollup-plugin-node-builtins";
15
```
16
17
For CommonJS:
18
19
```javascript
20
const builtins = require("rollup-plugin-node-builtins");
21
```
22
23
## Basic Usage
24
25
```javascript
26
import builtins from "rollup-plugin-node-builtins";
27
28
// Basic Rollup configuration
29
export default {
30
input: "src/main.js",
31
output: {
32
file: "dist/bundle.js",
33
format: "iife"
34
},
35
plugins: [builtins()]
36
};
37
38
// With optional crypto and fs support
39
export default {
40
input: "src/main.js",
41
output: {
42
file: "dist/bundle.js",
43
format: "iife"
44
},
45
plugins: [
46
builtins({
47
crypto: true, // Enable crypto-browserify
48
fs: true // Enable browserify-fs
49
})
50
]
51
};
52
```
53
54
## Capabilities
55
56
### Plugin Configuration
57
58
The main plugin function that creates a Rollup plugin for Node.js built-in module resolution.
59
60
```javascript { .api }
61
/**
62
* Creates a Rollup plugin for Node.js built-in module polyfills
63
* @param {Object} [opts] - Configuration options
64
* @param {boolean} [opts.crypto=false] - Enable crypto-browserify module
65
* @param {boolean} [opts.fs=false] - Enable browserify-fs module
66
* @returns {Object} Rollup plugin object with resolveId method
67
*/
68
function builtins(opts?: PluginOptions): RollupPlugin;
69
70
interface PluginOptions {
71
crypto?: boolean; // Enable crypto-browserify (default: false)
72
fs?: boolean; // Enable browserify-fs (default: false)
73
}
74
75
interface RollupPlugin {
76
resolveId(importee: string): string | undefined;
77
}
78
```
79
80
### Event System
81
82
Browser-compatible EventEmitter implementation that matches Node.js events API.
83
84
```javascript { .api }
85
// Import from 'events' module
86
import EventEmitter from "events";
87
import { EventEmitter } from "events";
88
89
class EventEmitter {
90
constructor();
91
92
// Static properties
93
static defaultMaxListeners: number; // Default: 10
94
static listenerCount(emitter: EventEmitter, type: string): number;
95
96
// Instance methods
97
setMaxListeners(n: number): EventEmitter;
98
getMaxListeners(): number;
99
emit(type: string, ...args: any[]): boolean;
100
on(type: string, listener: Function): EventEmitter;
101
addListener(type: string, listener: Function): EventEmitter;
102
once(type: string, listener: Function): EventEmitter;
103
prependListener(type: string, listener: Function): EventEmitter;
104
prependOnceListener(type: string, listener: Function): EventEmitter;
105
removeListener(type: string, listener: Function): EventEmitter;
106
removeAllListeners(type?: string): EventEmitter;
107
listeners(type: string): Function[];
108
listenerCount(type: string): number;
109
eventNames(): string[];
110
}
111
```
112
113
### Utility Functions
114
115
Core utility functions for formatting, inspection, and type checking.
116
117
```javascript { .api }
118
// Import from 'util' module
119
import { format, inspect, inherits } from "util";
120
121
// 'sys' module is an alias for 'util' (deprecated Node.js alias)
122
import { format, inspect, inherits } from "sys";
123
124
// String formatting with placeholders
125
function format(f: string, ...args: any[]): string;
126
127
// Object inspection and formatting
128
function inspect(obj: any, opts?: object): string;
129
130
// Function deprecation wrapper
131
function deprecate(fn: Function, msg: string): Function;
132
133
// Debug logger creation
134
function debuglog(set: string): Function;
135
136
// Prototype inheritance setup
137
function inherits(constructor: Function, superConstructor: Function): void;
138
139
// Shallow object extension
140
function _extend(origin: object, add: object): object;
141
142
// Console logging
143
function log(...args: any[]): void;
144
145
// Type checking functions
146
function isArray(ar: any): boolean;
147
function isBoolean(arg: any): boolean;
148
function isNull(arg: any): boolean;
149
function isNullOrUndefined(arg: any): boolean;
150
function isNumber(arg: any): boolean;
151
function isString(arg: any): boolean;
152
function isSymbol(arg: any): boolean;
153
function isUndefined(arg: any): boolean;
154
function isRegExp(re: any): boolean;
155
function isObject(arg: any): boolean;
156
function isDate(d: any): boolean;
157
function isError(e: any): boolean;
158
function isFunction(arg: any): boolean;
159
function isPrimitive(arg: any): boolean;
160
function isBuffer(maybeBuf: any): boolean;
161
```
162
163
### Path Utilities
164
165
Cross-platform path manipulation functions with POSIX-style behavior in browsers.
166
167
```javascript { .api }
168
// Import from 'path' module
169
import { resolve, join, dirname, basename, extname } from "path";
170
171
// Path resolution and normalization
172
function resolve(...paths: string[]): string;
173
function normalize(path: string): string;
174
function isAbsolute(path: string): boolean;
175
function join(...paths: string[]): string;
176
function relative(from: string, to: string): string;
177
178
// Path component extraction
179
function dirname(path: string): string;
180
function basename(path: string, ext?: string): string;
181
function extname(path: string): string;
182
183
// Path constants
184
const sep: string; // Path separator '/'
185
const delimiter: string; // Path delimiter ':'
186
```
187
188
### Assertion Testing
189
190
Comprehensive assertion functions for testing and validation.
191
192
```javascript { .api }
193
// Import from 'assert' module
194
import assert, { equal, strictEqual, deepEqual } from "assert";
195
196
// Basic assertion
197
function assert(value: any, message?: string): void;
198
function ok(value: any, message?: string): void;
199
200
// Equality assertions
201
function equal(actual: any, expected: any, message?: string): void;
202
function notEqual(actual: any, expected: any, message?: string): void;
203
function strictEqual(actual: any, expected: any, message?: string): void;
204
function notStrictEqual(actual: any, expected: any, message?: string): void;
205
206
// Deep equality assertions
207
function deepEqual(actual: any, expected: any, message?: string): void;
208
function notDeepEqual(actual: any, expected: any, message?: string): void;
209
function deepStrictEqual(actual: any, expected: any, message?: string): void;
210
function notDeepStrictEqual(actual: any, expected: any, message?: string): void;
211
212
// Exception testing
213
function throws(block: Function, error?: Function | RegExp, message?: string): void;
214
function doesNotThrow(block: Function, error?: Function | RegExp, message?: string): void;
215
216
// Error checking and manual failure
217
function ifError(err: any): void;
218
function fail(actual?: any, expected?: any, message?: string, operator?: string, stackStartFunction?: Function): void;
219
220
// Error class
221
class AssertionError extends Error {
222
name: string;
223
message: string;
224
actual: any;
225
expected: any;
226
operator: string;
227
constructor(options: object);
228
}
229
```
230
231
### URL Utilities
232
233
URL parsing, formatting, and resolution functions.
234
235
```javascript { .api }
236
// Import from 'url' module
237
import { parse, format, resolve } from "url";
238
239
// URL parsing and formatting
240
function parse(url: string, parseQueryString?: boolean, slashesDenoteHost?: boolean): UrlObject;
241
function format(urlObject: UrlObject): string;
242
function resolve(from: string, to: string): string;
243
function resolveObject(from: UrlObject, relative: UrlObject): UrlObject;
244
245
// URL object type
246
interface UrlObject {
247
protocol?: string;
248
slashes?: boolean;
249
auth?: string;
250
host?: string;
251
port?: string;
252
hostname?: string;
253
hash?: string;
254
search?: string;
255
query?: string | object;
256
pathname?: string;
257
path?: string;
258
href?: string;
259
}
260
261
// URL class
262
class Url {
263
constructor();
264
parse(url: string, parseQueryString?: boolean, slashesDenoteHost?: boolean): void;
265
format(): string;
266
resolve(relative: string): string;
267
resolveObject(relative: UrlObject): UrlObject;
268
parseHost(): void;
269
}
270
```
271
272
### Query String Utilities
273
274
Query string encoding and decoding functions.
275
276
```javascript { .api }
277
// Import from 'querystring' module
278
import { stringify, parse, encode, decode } from "querystring";
279
280
// Query string serialization
281
function stringify(obj: object, sep?: string, eq?: string, name?: string): string;
282
function encode(obj: object, sep?: string, eq?: string, name?: string): string;
283
284
// Query string parsing
285
function parse(qs: string, sep?: string, eq?: string, options?: object): object;
286
function decode(qs: string, sep?: string, eq?: string, options?: object): object;
287
```
288
289
### Stream System
290
291
Complete stream implementation with Readable, Writable, Duplex, Transform, and PassThrough streams.
292
293
```javascript { .api }
294
// Import from 'stream' module
295
import { Readable, Writable, Duplex, Transform, PassThrough } from "stream";
296
297
// Individual stream modules are also available as separate imports
298
import Readable from "stream/_stream_readable";
299
import Writable from "stream/_stream_writable";
300
import Duplex from "stream/_stream_duplex";
301
import Transform from "stream/_stream_transform";
302
import PassThrough from "stream/_stream_passthrough";
303
304
// Base stream class
305
class Stream extends EventEmitter {
306
constructor();
307
}
308
309
// Readable stream
310
class Readable extends Stream {
311
constructor(options?: object);
312
read(size?: number): any;
313
push(chunk: any, encoding?: string): boolean;
314
unshift(chunk: any): void;
315
pipe(destination: Writable, options?: object): Writable;
316
unpipe(destination?: Writable): Readable;
317
pause(): Readable;
318
resume(): Readable;
319
isPaused(): boolean;
320
setEncoding(encoding: string): Readable;
321
wrap(stream: Stream): Readable;
322
}
323
324
// Writable stream
325
class Writable extends Stream {
326
constructor(options?: object);
327
write(chunk: any, encoding?: string, callback?: Function): boolean;
328
end(chunk?: any, encoding?: string, callback?: Function): void;
329
cork(): void;
330
uncork(): void;
331
setDefaultEncoding(encoding: string): Writable;
332
}
333
334
// Duplex stream (readable and writable)
335
class Duplex extends Readable {
336
constructor(options?: object);
337
// Includes all Readable and Writable methods
338
}
339
340
// Transform stream
341
class Transform extends Duplex {
342
constructor(options?: object);
343
_transform(chunk: any, encoding: string, callback: Function): void;
344
push(chunk: any, encoding?: string): boolean;
345
}
346
347
// PassThrough stream (transform that passes data unchanged)
348
class PassThrough extends Transform {
349
constructor(options?: object);
350
}
351
```
352
353
### String Decoder
354
355
Buffer to string decoding with proper handling of multi-byte characters.
356
357
```javascript { .api }
358
// Import from 'string_decoder' module
359
import { StringDecoder } from "string_decoder";
360
361
class StringDecoder {
362
constructor(encoding?: string);
363
write(buffer: Buffer): string;
364
end(buffer?: Buffer): string;
365
}
366
```
367
368
### HTTP Client
369
370
Browser-adapted HTTP client functionality for making requests.
371
372
```javascript { .api }
373
// Import from 'http' module
374
import { request, get, Agent, METHODS, STATUS_CODES } from "http";
375
376
// HTTP request functions
377
function request(opts: object | string, cb?: Function): ClientRequest;
378
function get(opts: object | string, cb?: Function): ClientRequest;
379
380
// HTTP agent for connection pooling
381
class Agent {
382
constructor(options?: object);
383
}
384
385
// HTTP constants
386
const METHODS: string[]; // Array of HTTP method names
387
const STATUS_CODES: object; // Status code to message mapping
388
```
389
390
### Operating System Utilities
391
392
Browser-adapted OS information functions.
393
394
```javascript { .api }
395
// Import from 'os' module
396
import { platform, arch, endianness, hostname, type, release } from "os";
397
398
// System information
399
function endianness(): string; // CPU endianness
400
function hostname(): string; // System hostname
401
function loadavg(): number[]; // Load average (returns [0, 0, 0] in browser)
402
function uptime(): number; // System uptime in seconds
403
function freemem(): number; // Free memory in bytes
404
function totalmem(): number; // Total memory in bytes
405
function cpus(): object[]; // CPU information
406
function type(): string; // Operating system name
407
function release(): string; // Operating system release
408
function platform(): string; // Operating system platform
409
function arch(): string; // CPU architecture
410
function tmpdir(): string; // Temporary directory path
411
function tmpDir(): string; // Alias for tmpdir()
412
function networkInterfaces(): object; // Network interfaces
413
function getNetworkInterfaces(): object; // Alias for networkInterfaces()
414
415
// Constants
416
const EOL: string; // End of line character '\n'
417
```
418
419
### Compression (Zlib)
420
421
Pure JavaScript implementation of zlib compression algorithms.
422
423
```javascript { .api }
424
// Import from 'zlib' module
425
import { createGzip, createGunzip, gzip, gunzip, deflate, inflate } from "zlib";
426
427
// Stream creation functions
428
function createDeflate(options?: object): Deflate;
429
function createInflate(options?: object): Inflate;
430
function createDeflateRaw(options?: object): DeflateRaw;
431
function createInflateRaw(options?: object): InflateRaw;
432
function createGzip(options?: object): Gzip;
433
function createGunzip(options?: object): Gunzip;
434
function createUnzip(options?: object): Unzip;
435
436
// Compression classes (extend Transform stream)
437
class Deflate extends Transform {}
438
class Inflate extends Transform {}
439
class Gzip extends Transform {}
440
class Gunzip extends Transform {}
441
class DeflateRaw extends Transform {}
442
class InflateRaw extends Transform {}
443
class Unzip extends Transform {}
444
class Zlib extends Transform {}
445
446
// Synchronous compression functions
447
function deflate(buffer: Buffer, options?: object, callback?: Function): Buffer;
448
function deflateSync(buffer: Buffer, options?: object): Buffer;
449
function inflate(buffer: Buffer, options?: object, callback?: Function): Buffer;
450
function inflateSync(buffer: Buffer, options?: object): Buffer;
451
function gzip(buffer: Buffer, options?: object, callback?: Function): Buffer;
452
function gzipSync(buffer: Buffer, options?: object): Buffer;
453
function gunzip(buffer: Buffer, options?: object, callback?: Function): Buffer;
454
function gunzipSync(buffer: Buffer, options?: object): Buffer;
455
function deflateRaw(buffer: Buffer, options?: object, callback?: Function): Buffer;
456
function deflateRawSync(buffer: Buffer, options?: object): Buffer;
457
function inflateRaw(buffer: Buffer, options?: object, callback?: Function): Buffer;
458
function inflateRawSync(buffer: Buffer, options?: object): Buffer;
459
function unzip(buffer: Buffer, options?: object, callback?: Function): Buffer;
460
function unzipSync(buffer: Buffer, options?: object): Buffer;
461
462
// Constants
463
const codes: object; // Z_* compression constants
464
```
465
466
### Virtual Machine
467
468
Limited JavaScript code execution context functionality.
469
470
```javascript { .api }
471
// Import from 'vm' module
472
import { createContext, runInContext, runInThisContext, runInNewContext, createScript, isContext } from "vm";
473
474
// Context creation and execution
475
function createContext(sandbox?: object): object;
476
function runInContext(code: string, context: object, options?: object): any;
477
function runInThisContext(code: string, options?: object): any;
478
function runInNewContext(code: string, sandbox?: object, options?: object): any;
479
function createScript(code: string, options?: object): Script;
480
function isContext(context: object): boolean;
481
482
// Script class
483
class Script {
484
constructor(code: string, options?: object);
485
runInContext(context: object, options?: object): any;
486
runInThisContext(options?: object): any;
487
runInNewContext(sandbox?: object, options?: object): any;
488
}
489
```
490
491
### Timer Functions
492
493
Browser-adapted timer functions using native browser APIs.
494
495
```javascript { .api }
496
// Import from 'timers' module
497
import { setTimeout, clearTimeout, setInterval, clearInterval, setImmediate, clearImmediate } from "timers";
498
499
// Timer functions
500
function setTimeout(callback: Function, delay: number, ...args: any[]): object;
501
function clearTimeout(timeoutObject: object): void;
502
function setInterval(callback: Function, delay: number, ...args: any[]): object;
503
function clearInterval(intervalObject: object): void;
504
function setImmediate(callback: Function, ...args: any[]): object;
505
function clearImmediate(immediateObject: object): void;
506
507
// Internal timer functions
508
function enroll(item: object, msecs: number): void;
509
function unenroll(item: object): void;
510
function active(item: object): void;
511
function _unrefActive(item: object): void;
512
```
513
514
### Console Functions
515
516
Browser-adapted console logging functions.
517
518
```javascript { .api }
519
// Import from 'console' module - provides console object
520
import console from "console";
521
522
// Console methods
523
const console: {
524
log(...args: any[]): void;
525
info(...args: any[]): void;
526
warn(...args: any[]): void;
527
error(...args: any[]): void;
528
dir(obj: any): void;
529
assert(value: any, message?: string): void;
530
time(label: string): void;
531
timeEnd(label: string): void;
532
trace(): void;
533
};
534
```
535
536
### Domain Module
537
538
Simplified error handling domain implementation.
539
540
```javascript { .api }
541
// Import from 'domain' module
542
import { createDomain, create, Domain } from "domain";
543
544
// Domain creation
545
function createDomain(): Domain;
546
function create(): Domain;
547
548
// Domain class
549
class Domain extends EventEmitter {
550
constructor();
551
add(emitter: EventEmitter): void;
552
remove(emitter: EventEmitter): void;
553
bind(fn: Function): Function;
554
intercept(fn: Function): Function;
555
run(fn: Function): void;
556
dispose(): void;
557
enter(): void; // No-op in browser
558
exit(): void; // No-op in browser
559
}
560
```
561
562
### Punycode Utilities
563
564
Punycode encoding and decoding for internationalized domain names.
565
566
```javascript { .api }
567
// Import from 'punycode' module
568
import { decode, encode, toUnicode, toASCII, version, ucs2 } from "punycode";
569
570
// Punycode functions
571
function decode(input: string): string;
572
function encode(input: string): string;
573
function toUnicode(input: string): string;
574
function toASCII(input: string): string;
575
576
// Constants and utilities
577
const version: string; // Version '1.4.1'
578
const ucs2: {
579
decode(string: string): number[];
580
encode(codePoints: number[]): string;
581
};
582
```
583
584
### System Constants
585
586
Comprehensive system constants for errors, signals, file system, and SSL.
587
588
```javascript { .api }
589
// Import from 'constants' module
590
import {
591
E2BIG, EACCES, EADDRINUSE,
592
SIGHUP, SIGINT, SIGQUIT,
593
O_RDONLY, O_WRONLY, S_IRUSR,
594
SSL_OP_ALL, TLS1_VERSION,
595
PRIORITY_LOW, PRIORITY_HIGH
596
} from "constants";
597
598
// Error constants
599
const E2BIG: number;
600
const EACCES: number;
601
const EADDRINUSE: number;
602
// ... many more error constants
603
604
// Signal constants
605
const SIGHUP: number;
606
const SIGINT: number;
607
const SIGQUIT: number;
608
// ... many more signal constants
609
610
// File system constants
611
const O_RDONLY: number;
612
const O_WRONLY: number;
613
const S_IRUSR: number;
614
// ... many more file system constants
615
616
// SSL constants
617
const SSL_OP_ALL: number;
618
const TLS1_VERSION: number;
619
// ... many more SSL constants
620
621
// Process priority constants
622
const PRIORITY_LOW: number;
623
const PRIORITY_HIGH: number;
624
// ... additional priority constants
625
```
626
627
### TTY Module
628
629
Mock TTY functionality for browser environments.
630
631
```javascript { .api }
632
// Import from 'tty' module
633
import { isatty, ReadStream, WriteStream } from "tty";
634
635
// TTY functions (browser-adapted)
636
function isatty(): boolean; // Always returns false in browser
637
function ReadStream(): void; // Throws error - not supported in browser
638
function WriteStream(): void; // Throws error - not supported in browser
639
```
640
641
### Optional External Modules
642
643
The plugin can optionally enable crypto and fs modules through external packages.
644
645
```javascript { .api }
646
// Available when opts.crypto = true
647
// Uses crypto-browserify package
648
import { createHash, createHmac, randomBytes } from "crypto";
649
// Full crypto-browserify API available
650
651
// Available when opts.fs = true
652
// Uses browserify-fs package
653
import { readFile, writeFile, readFileSync, writeFileSync } from "fs";
654
// Full browserify-fs API available
655
```
656
657
### Mock Modules
658
659
Several Node.js modules return empty objects as they have no browser equivalent.
660
661
```javascript { .api }
662
// These modules return empty objects {}
663
import dns from "dns"; // Empty object
664
import dgram from "dgram"; // Empty object
665
import child_process from "child_process"; // Empty object
666
import cluster from "cluster"; // Empty object
667
import module from "module"; // Empty object
668
import net from "net"; // Empty object
669
import readline from "readline"; // Empty object
670
import repl from "repl"; // Empty object
671
import tls from "tls"; // Empty object
672
```
673
674
## Module Resolution Map
675
676
The plugin resolves Node.js built-in module imports to browser-compatible implementations:
677
678
| Node.js Module | Implementation | Type |
679
|----------------|----------------|------|
680
| `events` | `src/es6/events.js` | Full EventEmitter implementation |
681
| `util` | `src/es6/util.js` | Complete utility functions |
682
| `sys` | `src/es6/util.js` | Alias for util (deprecated) |
683
| `path` | `src/es6/path.js` | POSIX path operations |
684
| `assert` | `src/es6/assert.js` | All assertion functions |
685
| `url` | `src/es6/url.js` | URL parsing and formatting |
686
| `querystring` | `src/es6/qs.js` | Query string utilities |
687
| `punycode` | `src/es6/punycode.js` | Punycode encoding/decoding |
688
| `http`/`https` | `src/es6/http.js` | Browser-adapted HTTP client |
689
| `stream` | `src/es6/stream.js` | Complete stream classes |
690
| `_stream_readable` | `src/es6/readable-stream/readable.js` | Readable stream class |
691
| `_stream_writable` | `src/es6/readable-stream/writable.js` | Writable stream class |
692
| `_stream_duplex` | `src/es6/readable-stream/duplex.js` | Duplex stream class |
693
| `_stream_transform` | `src/es6/readable-stream/transform.js` | Transform stream class |
694
| `_stream_passthrough` | `src/es6/readable-stream/passthrough.js` | PassThrough stream class |
695
| `string_decoder` | `src/es6/string-decoder.js` | String decoding utilities |
696
| `zlib` | `src/es6/zlib.js` | Pure JS compression |
697
| `os` | `src/es6/os.js` | Browser-adapted OS info |
698
| `vm` | `src/es6/vm.js` | Limited code execution |
699
| `timers` | `src/es6/timers.js` | Browser timer functions |
700
| `console` | `src/es6/console.js` | Browser console object |
701
| `domain` | `src/es6/domain.js` | Simplified error domains |
702
| `tty` | `src/es6/tty.js` | Mock TTY functions |
703
| `constants` | `dist/constants.js` | System constants |
704
| `process` | `process-es6` | External dependency |
705
| `buffer` | `buffer-es6` | External dependency |
706
| `crypto` | `crypto-browserify` | Optional external |
707
| `fs` | `browserify-fs` | Optional external |
708
| Mock modules | `src/es6/empty.js` | Empty objects |
709
710
## Usage Examples
711
712
### Basic Plugin Usage
713
714
```javascript
715
// rollup.config.js
716
import builtins from "rollup-plugin-node-builtins";
717
718
export default {
719
input: "src/main.js",
720
output: {
721
file: "dist/bundle.js",
722
format: "iife"
723
},
724
plugins: [builtins()]
725
};
726
```
727
728
### Using Node.js Modules in Browser Code
729
730
```javascript
731
// Your application code can now use Node.js built-ins
732
import EventEmitter from "events";
733
import { format } from "util";
734
import { join } from "path";
735
import { parse } from "url";
736
737
// Create event emitter
738
const emitter = new EventEmitter();
739
emitter.on("data", (data) => {
740
console.log(format("Received: %s", data));
741
});
742
743
// Use path utilities
744
const filePath = join("/users", "documents", "file.txt");
745
746
// Parse URLs
747
const parsed = parse("https://example.com/path?query=value");
748
```
749
750
### Enabling Optional Modules
751
752
```javascript
753
// rollup.config.js with crypto and fs support
754
import builtins from "rollup-plugin-node-builtins";
755
756
export default {
757
input: "src/main.js",
758
output: {
759
file: "dist/bundle.js",
760
format: "iife"
761
},
762
plugins: [
763
builtins({
764
crypto: true, // Enables crypto-browserify
765
fs: true // Enables browserify-fs
766
})
767
]
768
};
769
770
// Now you can use crypto and fs in your code
771
import { createHash } from "crypto";
772
import { readFile } from "fs";
773
774
const hash = createHash("sha256");
775
hash.update("hello world");
776
console.log(hash.digest("hex"));
777
```