Skip to content

feat: add excludeNodeModules option #503

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion packages/plugin-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,19 @@ export default defineConfig({
})
```

> `node_modules` are never processed by this plugin (but esbuild will)
> [!NOTE]
> `node_modules` are not processed by this plugin by default (unlike esbuild), regardless of the `include` option. If you want to include them, you can set the `excludeNodeModules` option to `false`.

### excludeNodeModules

By default, the plugin excludes `node_modules` from being processed. If you want to include them, you can set this option to `false`:

```js
react({ excludeNodeModules: false })
```

> [!NOTE]
> Including `node_modules` can slow down the development server, so use this option with caution.

### jsxImportSource

Expand Down
12 changes: 10 additions & 2 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ async function loadBabel() {
export interface Options {
include?: string | RegExp | Array<string | RegExp>
exclude?: string | RegExp | Array<string | RegExp>

/**
* Whether to exclude `node_modules` from being processed.
* @default true
*/
excludeNodeModules?: boolean

/**
* Control where the JSX factory is imported from.
* https://esbuild.github.io/api/#jsx-import-source
Expand Down Expand Up @@ -113,6 +120,7 @@ const tsRE = /\.tsx?$/
export default function viteReact(opts: Options = {}): PluginOption[] {
const include = opts.include ?? defaultIncludeRE
const exclude = opts.exclude
const excludeNodeModules = opts.excludeNodeModules ?? true
const filter = createFilter(include, exclude)

const jsxImportSource = opts.jsxImportSource ?? 'react'
Expand Down Expand Up @@ -225,12 +233,12 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
...(exclude
? makeIdFiltersToMatchWithQuery(ensureArray(exclude))
: []),
/\/node_modules\//,
...(excludeNodeModules ? [/\/node_modules\//] : []),
],
},
},
async handler(code, id, options) {
if (id.includes('/node_modules/')) return
if (excludeNodeModules && id.includes('/node_modules/')) return

const [filepath] = id.split('?')
if (!filter(filepath)) return
Expand Down