Skip to content

Commit 821fb0a

Browse files
committed
Add support for overriding default polyfills
1 parent 517653d commit 821fb0a

File tree

6 files changed

+135
-608
lines changed

6 files changed

+135
-608
lines changed

README.md

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,30 @@ pnpm install --save-dev vite-plugin-node-polyfills
2828
yarn add --dev vite-plugin-node-polyfills
2929
```
3030

31+
### Zero-config by default
32+
3133
Add the plugin to your `vite.config.ts` file.
3234

3335
```ts
3436
import { defineConfig } from 'vite'
3537
import { nodePolyfills } from 'vite-plugin-node-polyfills'
3638

39+
// https://vitejs.dev/config/
40+
export default defineConfig({
41+
plugins: [
42+
nodePolyfills(),
43+
],
44+
})
45+
```
46+
47+
### Customizable when you need it
48+
49+
The following options are available to customize it for your needs.
50+
51+
```ts
52+
import { defineConfig } from 'vite'
53+
import { nodePolyfills } from 'vite-plugin-node-polyfills'
54+
3755
// https://vitejs.dev/config/
3856
export default defineConfig({
3957
plugins: [
@@ -42,14 +60,19 @@ export default defineConfig({
4260
include: ['path']
4361
// To exclude specific polyfills, add them to this list. Note: if include is provided, this has no effect
4462
exclude: [
45-
'fs', // Excludes the polyfill for `fs` and `node:fs`.
63+
'http', // Excludes the polyfill for `http` and `node:http`.
4664
],
4765
// Whether to polyfill specific globals.
4866
globals: {
4967
Buffer: true, // can also be 'build', 'dev', or false
5068
global: true,
5169
process: true,
5270
},
71+
// Override the default polyfills for specific modules.
72+
overrides: {
73+
// Since `fs` is not supported in browsers, we can use the `memfs` package to polyfill it.
74+
fs: 'memfs',
75+
},
5376
// Whether to polyfill `node:` protocol imports.
5477
protocolImports: true,
5578
}),
@@ -61,6 +84,11 @@ export default defineConfig({
6184

6285
- If protocolImports is true, also adds node:[module]
6386
```js
87+
'_stream_duplex',
88+
'_stream_passthrough',
89+
'_stream_readable',
90+
'_stream_transform',
91+
'_stream_writable',
6492
'assert',
6593
'buffer',
6694
'child_process',
@@ -74,27 +102,22 @@ export default defineConfig({
74102
'events',
75103
'fs',
76104
'http',
77-
'https',
78105
'http2',
106+
'https',
79107
'module',
80108
'net',
81109
'os',
82110
'path',
83-
'punycode',
84111
'process',
112+
'punycode',
85113
'querystring',
86114
'readline',
87115
'repl',
88116
'stream',
89-
'_stream_duplex',
90-
'_stream_passthrough',
91-
'_stream_readable',
92-
'_stream_transform',
93-
'_stream_writable',
94117
'string_decoder',
95118
'sys',
96-
'timers/promises',
97119
'timers',
120+
'timers/promises',
98121
'tls',
99122
'tty',
100123
'url',

examples/vanilla/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
"devDependencies": {
1010
"@types/node": "^18.7.23",
1111
"lodash-es": "^4.17.21",
12+
"memfs": "^4.2.1",
1213
"ohmyfetch": "^0.4.20",
1314
"typescript": "^5.0.0",
1415
"vite": "^4.0.0",

examples/vanilla/src/main.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { Buffer } from 'node:buffer'
22
import { resolve } from 'node:path'
33
import * as process from 'node:process'
4-
import fs from 'node:fs'
4+
import fs from 'fs'
5+
import { readFileSync } from 'node:fs'
56
import { cloneDeep } from 'lodash-es'
67
import { fetch } from 'ohmyfetch'
78

@@ -13,6 +14,9 @@ const something = {
1314
},
1415
}
1516

17+
fs.writeFileSync('./test.txt', 'Hello from fs!', 'utf-8')
18+
19+
console.log(fs)
1620
console.log(fetch)
1721
console.log(resolve('.'))
1822
console.log(process)
@@ -21,5 +25,5 @@ console.log(globalThis.Array)
2125
console.log(Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]).readBigUInt64BE(0))
2226
console.log(Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]))
2327
console.log(Array)
24-
console.log(fs)
28+
console.log(readFileSync('./test.txt', 'utf-8'))
2529
console.log(cloneDeep(something))

examples/vanilla/vite.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import { nodePolyfills } from 'vite-plugin-node-polyfills'
66
export default defineConfig({
77
plugins: [
88
nodePolyfills({
9-
exclude: ['fs'],
109
globals: {
1110
process: 'build',
1211
},
12+
overrides: {
13+
fs: 'memfs',
14+
},
1315
protocolImports: true,
1416
}),
1517
],

0 commit comments

Comments
 (0)