Skip to content

feat!: support including files in node_modules #306

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

Merged
merged 11 commits into from
Jul 23, 2025
Merged
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
4 changes: 4 additions & 0 deletions packages/plugin-react-oxc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Allow processing files in `node_modules`

The default value of `exclude` options is now `[/\/node_modules\//]` to allow processing files in `node_modules` directory. It was previously `[]` and files in `node_modules` was always excluded regardless of the value of `exclude` option.

### Require Node 20.19+, 22.12+

This plugin now requires Node 20.19+ or 22.12+.
Expand Down
4 changes: 1 addition & 3 deletions packages/plugin-react-oxc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export default defineConfig({

### include/exclude

Includes `.js`, `.jsx`, `.ts` & `.tsx` by default. This option can be used to add fast refresh to `.mdx` files:
Includes `.js`, `.jsx`, `.ts` & `.tsx` and excludes `/node_modules/` by default. This option can be used to add fast refresh to `.mdx` files:

```js
import { defineConfig } from 'vite'
Expand All @@ -40,8 +40,6 @@ export default defineConfig({
})
```

> `node_modules` are never processed by this plugin (but Oxc will)
### jsxImportSource

Control where the JSX factory is imported from. Default to `'react'`
Expand Down
10 changes: 2 additions & 8 deletions packages/plugin-react-oxc/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,11 @@ export interface Options {
}

const defaultIncludeRE = /\.[tj]sx?(?:$|\?)/
const defaultExcludeRE = /\/node_modules\//

export default function viteReact(opts: Options = {}): Plugin[] {
const include = opts.include ?? defaultIncludeRE
const exclude = [
...(Array.isArray(opts.exclude)
? opts.exclude
: opts.exclude
? [opts.exclude]
: []),
/\/node_modules\//,
]
const exclude = opts.exclude ?? defaultExcludeRE

const jsxImportSource = opts.jsxImportSource ?? 'react'
const jsxImportRuntime = `${jsxImportSource}/jsx-runtime`
Expand Down
4 changes: 4 additions & 0 deletions packages/plugin-react/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Allow processing files in `node_modules`

The default value of `exclude` options is now `[/\/node_modules\//]` to allow processing files in `node_modules` directory. It was previously `[]` and files in `node_modules` was always excluded regardless of the value of `exclude` option.

### `react` and `react-dom` is no longer added to [`resolve.dedupe`](https://vite.dev/config/#resolve-dedupe) automatically

Adding values to `resolve.dedupe` forces Vite to resolve them differently from how Node.js does, which can be confusing and may not be expected. This plugin no longer adds `react` and `react-dom` to `resolve.dedupe` automatically.
Expand Down
4 changes: 1 addition & 3 deletions packages/plugin-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default defineConfig({

### include/exclude

Includes `.js`, `.jsx`, `.ts` & `.tsx` by default. This option can be used to add fast refresh to `.mdx` files:
Includes `.js`, `.jsx`, `.ts` & `.tsx` and excludes `/node_modules/` by default. This option can be used to add fast refresh to `.mdx` files:

```js
import { defineConfig } from 'vite'
Expand All @@ -36,8 +36,6 @@ export default defineConfig({
})
```

> `node_modules` are never processed by this plugin (but esbuild will)
### jsxImportSource

Control where the JSX factory is imported from. Default to `'react'`
Expand Down
16 changes: 3 additions & 13 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,12 @@ export type ViteReactPluginApi = {
}

const defaultIncludeRE = /\.[tj]sx?$/
const defaultExcludeRE = /\/node_modules\//
const tsRE = /\.tsx?$/

export default function viteReact(opts: Options = {}): Plugin[] {
const include = opts.include ?? defaultIncludeRE
const exclude = opts.exclude
const exclude = opts.exclude ?? defaultExcludeRE
const filter = createFilter(include, exclude)

const jsxImportSource = opts.jsxImportSource ?? 'react'
Expand Down Expand Up @@ -222,17 +223,10 @@ export default function viteReact(opts: Options = {}): Plugin[] {
filter: {
id: {
include: makeIdFiltersToMatchWithQuery(include),
exclude: [
...(exclude
? makeIdFiltersToMatchWithQuery(ensureArray(exclude))
: []),
/\/node_modules\//,
],
exclude: makeIdFiltersToMatchWithQuery(exclude),
},
},
async handler(code, id, options) {
if (id.includes('/node_modules/')) return

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

Expand Down Expand Up @@ -472,7 +466,3 @@ function getReactCompilerRuntimeModule(
}
return moduleName
}

function ensureArray<T>(value: T | T[]): T[] {
return Array.isArray(value) ? value : [value]
}
17 changes: 17 additions & 0 deletions playground/include-node-modules/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import test from '@vitejs/test-package'

function App() {
return (
<div>
<h1>Node Modules Include Test</h1>
<p>
This playground tests that files in node_modules are processed
correctly.
</p>

<p className="result">Result: {'' + test}</p>
</div>
)
}

export default App
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, test } from 'vitest'
import { page } from '~utils'

test('should render', async () => {
expect(await page.textContent('h1')).toMatch('Node Modules Include Test')
})

test('babel should run on files in node_modules', async () => {
expect(await page.textContent('.result')).toMatch('Result: true')
})
10 changes: 10 additions & 0 deletions playground/include-node-modules/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<div id="app"></div>
<script type="module">
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'

ReactDOM.createRoot(document.getElementById('app')).render(
React.createElement(App),
)
</script>
21 changes: 21 additions & 0 deletions playground/include-node-modules/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "@vitejs/test-node-modules-include",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "^19.1.0",
"react-dom": "^19.1.0"
},
"devDependencies": {
"@types/babel__core": "^7.20.5",
"@types/react": "^19.1.8",
"@types/react-dom": "^19.1.6",
"@vitejs/plugin-react": "workspace:*",
"@vitejs/test-package": "file:./test-package"
}
}
1 change: 1 addition & 0 deletions playground/include-node-modules/test-package/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default TEST_BABEL
6 changes: 6 additions & 0 deletions playground/include-node-modules/test-package/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"name": "test-package",
"version": "1.0.0",
"main": "index.js",
"type": "module"
}
28 changes: 28 additions & 0 deletions playground/include-node-modules/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'
import type { PluginItem as BabelPlugin } from '@babel/core'

export default defineConfig({
plugins: [
react({
exclude: [/\/node_modules\/(?!(\.pnpm\/)?test-package)/],
babel: {
plugins: [
({ types: t }): BabelPlugin => ({
name: 'test-replace-test-babel',
visitor: {
Identifier(path) {
if (path.node.name === 'TEST_BABEL') {
path.replaceWith(t.booleanLiteral(true))
}
},
},
}),
],
},
}),
],
optimizeDeps: {
exclude: ['@vitejs/test-package'],
},
})
2 changes: 1 addition & 1 deletion playground/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"preview": "vite preview"
},
"dependencies": {
"jsx-entry": "file:./jsx-entry",
"jsx-entry": "link:./jsx-entry",
"react": "^19.1.0",
"react-dom": "^19.1.0"
},
Expand Down
Loading
Loading