Skip to content

Commit

Permalink
Merge pull request #37 from leticiavna/24-add-include-config
Browse files Browse the repository at this point in the history
Feat: add include polyfill option
  • Loading branch information
davidmyersdev authored Aug 26, 2023
2 parents ec47b5a + a58e00b commit f462a2e
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
52 changes: 50 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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).

### Why do I need this?
## Why do I need this?

```
Module "stream" has been externalized for browser compatibility. Cannot access "stream.Readable" in client code.
Expand Down Expand Up @@ -35,7 +35,9 @@ import { nodePolyfills } from 'vite-plugin-node-polyfills'
export default defineConfig({
plugins: [
nodePolyfills({
// To exclude specific polyfills, add them to this list.
// To add only specific polyfills, add them here. If no option is passed, adds all polyfills
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`.
],
Expand All @@ -51,3 +53,49 @@ export default defineConfig({
],
})
```

### All polyfills

- If protocolImports is true, also adds node:[module]
```js
'assert',
'buffer',
'child_process',
'cluster',
'console',
'constants',
'crypto',
'dgram',
'dns',
'domain',
'events',
'fs',
'http',
'https',
'http2',
'module',
'net',
'os',
'path',
'punycode',
'process',
'querystring',
'readline',
'repl',
'stream',
'_stream_duplex',
'_stream_passthrough',
'_stream_readable',
'_stream_transform',
'_stream_writable',
'string_decoder',
'sys',
'timers/promises',
'timers',
'tls',
'tty',
'url',
'util',
'vm',
'zlib',
```
24 changes: 21 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@ export type ModuleName = keyof typeof stdLibBrowser
export type ModuleNameWithoutNodePrefix<T = ModuleName> = T extends `node:${infer P}` ? P : never

export type PolyfillOptions = {
/**
* Includes specific modules. If empty, includes all modules
* @example
*
* ```ts
* nodePolyfills({
* include: ['fs', 'path'],
* })
* ```
*/
include?: ModuleNameWithoutNodePrefix[],
/**
* @example
*
Expand Down Expand Up @@ -51,6 +62,7 @@ export type PolyfillOptions = {
}

export type PolyfillOptionsResolved = {
include: ModuleNameWithoutNodePrefix[],
exclude: ModuleNameWithoutNodePrefix[],
globals: {
Buffer: BooleanOrBuildTarget,
Expand Down Expand Up @@ -112,6 +124,7 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
const globalShimsBannerPath = require.resolve('vite-plugin-node-polyfills/shims/banner')
const globalShimsBanner = readFileSync(globalShimsBannerPath, 'utf-8')
const optionsResolved: PolyfillOptionsResolved = {
include: [],
exclude: [],
protocolImports: true,
...options,
Expand All @@ -123,10 +136,15 @@ export const nodePolyfills = (options: PolyfillOptions = {}): Plugin => {
},
}

const compareExcludedModuleNames = (moduleName: string, excludedName: string) => {
return moduleName === excludedName || moduleName === `node:${excludedName}`;
}

const isExcluded = (name: string) => {
return optionsResolved.exclude.some((excludedName) => {
return name === excludedName || name === `node:${excludedName}`
})
if (optionsResolved.include.length) {
return !optionsResolved.include.some((excludedName) => compareExcludedModuleNames(name, excludedName));
};
return optionsResolved.exclude.some((excludedName) => compareExcludedModuleNames(name, excludedName));
}

const toOverride = (name: string): string | void => {
Expand Down
1 change: 1 addition & 0 deletions test/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default defineConfig({
// react(),
vue(),
nodePolyfills({
// include: ['fs', 'path', 'buffer'],
exclude: ['fs'],
globals: {
process: 'build',
Expand Down

0 comments on commit f462a2e

Please sign in to comment.