Skip to content

Commit

Permalink
Add support for overriding default polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmyersdev committed Sep 12, 2023
1 parent 517653d commit 821fb0a
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 608 deletions.
41 changes: 32 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,30 @@ pnpm install --save-dev vite-plugin-node-polyfills
yarn add --dev vite-plugin-node-polyfills
```

### Zero-config by default

Add the plugin to your `vite.config.ts` file.

```ts
import { defineConfig } from 'vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
nodePolyfills(),
],
})
```

### Customizable when you need it

The following options are available to customize it for your needs.

```ts
import { defineConfig } from 'vite'
import { nodePolyfills } from 'vite-plugin-node-polyfills'

// https://vitejs.dev/config/
export default defineConfig({
plugins: [
Expand All @@ -42,14 +60,19 @@ export default defineConfig({
include: ['path']
// To exclude specific polyfills, add them to this list. Note: if include is provided, this has no effect
exclude: [
'fs', // Excludes the polyfill for `fs` and `node:fs`.
'http', // Excludes the polyfill for `http` and `node:http`.
],
// Whether to polyfill specific globals.
globals: {
Buffer: true, // can also be 'build', 'dev', or false
global: true,
process: true,
},
// Override the default polyfills for specific modules.
overrides: {
// Since `fs` is not supported in browsers, we can use the `memfs` package to polyfill it.
fs: 'memfs',
},
// Whether to polyfill `node:` protocol imports.
protocolImports: true,
}),
Expand All @@ -61,6 +84,11 @@ export default defineConfig({

- If protocolImports is true, also adds node:[module]
```js
'_stream_duplex',
'_stream_passthrough',
'_stream_readable',
'_stream_transform',
'_stream_writable',
'assert',
'buffer',
'child_process',
Expand All @@ -74,27 +102,22 @@ export default defineConfig({
'events',
'fs',
'http',
'https',
'http2',
'https',
'module',
'net',
'os',
'path',
'punycode',
'process',
'punycode',
'querystring',
'readline',
'repl',
'stream',
'_stream_duplex',
'_stream_passthrough',
'_stream_readable',
'_stream_transform',
'_stream_writable',
'string_decoder',
'sys',
'timers/promises',
'timers',
'timers/promises',
'tls',
'tty',
'url',
Expand Down
1 change: 1 addition & 0 deletions examples/vanilla/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"devDependencies": {
"@types/node": "^18.7.23",
"lodash-es": "^4.17.21",
"memfs": "^4.2.1",
"ohmyfetch": "^0.4.20",
"typescript": "^5.0.0",
"vite": "^4.0.0",
Expand Down
8 changes: 6 additions & 2 deletions examples/vanilla/src/main.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Buffer } from 'node:buffer'
import { resolve } from 'node:path'
import * as process from 'node:process'
import fs from 'node:fs'
import fs from 'fs'
import { readFileSync } from 'node:fs'
import { cloneDeep } from 'lodash-es'
import { fetch } from 'ohmyfetch'

Expand All @@ -13,6 +14,9 @@ const something = {
},
}

fs.writeFileSync('./test.txt', 'Hello from fs!', 'utf-8')

console.log(fs)
console.log(fetch)
console.log(resolve('.'))
console.log(process)
Expand All @@ -21,5 +25,5 @@ console.log(globalThis.Array)
console.log(Buffer.from([0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff]).readBigUInt64BE(0))
console.log(Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]))
console.log(Array)
console.log(fs)
console.log(readFileSync('./test.txt', 'utf-8'))
console.log(cloneDeep(something))
4 changes: 3 additions & 1 deletion examples/vanilla/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ import { nodePolyfills } from 'vite-plugin-node-polyfills'
export default defineConfig({
plugins: [
nodePolyfills({
exclude: ['fs'],
globals: {
process: 'build',
},
overrides: {
fs: 'memfs',
},
protocolImports: true,
}),
],
Expand Down
Loading

0 comments on commit 821fb0a

Please sign in to comment.