Skip to content

Commit 0b8adcf

Browse files
committed
Make node: protocol imports configurable
1 parent bb83b3a commit 0b8adcf

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# vite-plugin-node-polyfills
22

3-
A Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.
3+
A Vite plugin to polyfill Node's Core Modules for browser environments. Supports [`node:` protocol imports](https://nodejs.org/dist/latest-v16.x/docs/api/esm.html#node-imports).
44

55
### Why do I need this?
66

@@ -34,7 +34,10 @@ import { nodePolyfills } from 'vite-plugin-node-polyfills'
3434
// https://vitejs.dev/config/
3535
export default defineConfig({
3636
plugins: [
37-
nodePolyfills(),
37+
nodePolyfills({
38+
// Whether to polyfill `node:` protocol imports.
39+
protocolImports: true,
40+
}),
3841
],
3942
})
4043
```

src/env.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ declare module 'node-stdlib-browser/helpers/esbuild/plugin' {
22
import { Plugin } from 'esbuild'
33
import stdLibBrowser from 'node-stdlib-browser'
44

5-
export default function(options: typeof stdLibBrowser): Plugin
5+
export default function(options: Record<string, string>): Plugin
66
}
77

88
declare module 'node-stdlib-browser/helpers/rollup/plugin' {

src/index.ts

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,56 @@ import { handleCircularDependancyWarning } from 'node-stdlib-browser/helpers/rol
55
import esbuildPlugin from 'node-stdlib-browser/helpers/esbuild/plugin'
66
import type { Plugin } from 'vite'
77

8-
export const nodePolyfills = (_options = {}): Plugin => {
8+
interface PolyfillOptions {
9+
protocolImports: boolean
10+
}
11+
12+
/**
13+
* Returns a Vite plugin to polyfill Node's Core Modules for browser environments. Supports `node:` protocol imports.
14+
*
15+
* @example Use it in `vite.config.ts`
16+
*
17+
* ```ts
18+
* // vite.config.ts
19+
* import { defineConfig } from 'vite'
20+
* import { nodePolyfills } from 'vite-plugin-node-polyfills'
21+
*
22+
* export default defineConfig({
23+
* plugins: [
24+
* nodePolyfills({
25+
* // Whether to polyfill `node:` protocol imports.
26+
* protocolImports: true,
27+
* }),
28+
* ],
29+
* })
30+
* ```
31+
*/
32+
export const nodePolyfills = (options: Partial<PolyfillOptions> = {}): Plugin => {
933
const require = createRequire(import.meta.url)
1034
const globalShims = require.resolve('node-stdlib-browser/helpers/esbuild/shim')
35+
const optionsResolved: PolyfillOptions = {
36+
protocolImports: true,
37+
// User options take priority.
38+
...options,
39+
}
1140

1241
return {
1342
name: 'vite-plugin-node-polyfills',
1443
config: (_config, _env) => {
44+
const polyfills = Object.entries(stdLibBrowser).reduce((included: Record<string, string>, [name, value]) => {
45+
if (!optionsResolved.protocolImports) {
46+
const isProtocolImport = /^node:/.test(name)
47+
48+
if (isProtocolImport) {
49+
return included
50+
}
51+
}
52+
53+
included[name] = value
54+
55+
return included
56+
}, {})
57+
1558
return {
1659
build: {
1760
rollupOptions: {
@@ -41,14 +84,14 @@ export const nodePolyfills = (_options = {}): Plugin => {
4184
globalShims,
4285
],
4386
plugins: [
44-
esbuildPlugin(stdLibBrowser),
87+
esbuildPlugin(polyfills),
4588
],
4689
},
4790
},
4891
resolve: {
4992
// https://github.com/niksy/node-stdlib-browser/blob/3e7cd7f3d115ac5c4593b550e7d8c4a82a0d4ac4/README.md?plain=1#L150
5093
alias: {
51-
...stdLibBrowser,
94+
...polyfills,
5295
},
5396
},
5497
}

test/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import { resolve } from 'path'
1+
import { resolve } from 'node:path'
22

33
console.log(resolve('.'))

test/vite.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { nodePolyfills } from 'vite-plugin-node-polyfills'
55
// https://vitejs.dev/config/
66
export default defineConfig({
77
plugins: [
8-
nodePolyfills(),
8+
nodePolyfills({
9+
protocolImports: true,
10+
}),
911
],
1012
root: resolve(__dirname),
1113
})

0 commit comments

Comments
 (0)