Skip to content
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
16 changes: 13 additions & 3 deletions packages/plugin-react/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import path from 'node:path'
import type { ParserOptions, TransformOptions, types as t } from '@babel/core'
import * as babel from '@babel/core'
import { createFilter } from 'vite'
import { createFilter, loadEnv, normalizePath, resolveEnvPrefix } from 'vite'
import type { Plugin, PluginOption, ResolvedConfig } from 'vite'
import MagicString from 'magic-string'
import type { SourceMap } from 'magic-string'
Expand Down Expand Up @@ -118,8 +119,17 @@ export default function viteReact(opts: Options = {}): PluginOption[] {
const viteBabel: Plugin = {
name: 'vite:react-babel',
enforce: 'pre',
config(_, { mode }) {
// Copied from https://github.com/vitejs/vite/blob/4e9bdd4fb3654a9d43917e1cb682d3d2bad25115/packages/vite/src/node/config.ts#L488-L490
config(userConfig, { mode }) {
// Copied from https://github.com/vitejs/vite/blob/4e9bdd4fb3654a9d43917e1cb682d3d2bad25115/packages/vite/src/node/config.ts#L477-L494

const resolvedRoot = normalizePath(
userConfig.root ? path.resolve(userConfig.root) : process.cwd()
)
const envDir = userConfig.envDir
? normalizePath(path.resolve(resolvedRoot, userConfig.envDir))
: resolvedRoot
loadEnv(mode, envDir, resolveEnvPrefix(userConfig))

const isProduction =
(process.env.NODE_ENV || process.env.VITE_USER_NODE_ENV || mode) ===
'production'
Expand Down
1 change: 1 addition & 0 deletions playground/react-env/.env.staging
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
NODE_ENV=production
14 changes: 14 additions & 0 deletions playground/react-env/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { useState } from 'react'

function App() {
const [count, setCount] = useState(0)
return (
<div className="App">
<header className="App-header">
<h1 id="title">Hello Vite + React</h1>
</header>
</div>
)
}

export default App
10 changes: 10 additions & 0 deletions playground/react-env/__tests__/react.spec.ts
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 work', async () => {
expect(
await page.textContent('h1', {
timeout: 100
})
).toMatch('Hello Vite + React')
})
10 changes: 10 additions & 0 deletions playground/react-env/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>
18 changes: 18 additions & 0 deletions playground/react-env/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "test-react-env",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../packages/vite/bin/vite",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@vitejs/plugin-react": "workspace:*"
}
}
16 changes: 16 additions & 0 deletions playground/react-env/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import react from '@vitejs/plugin-react'
import type { UserConfig } from 'vite'

// Overriding the NODE_ENV set by vitest
process.env.NODE_ENV = ''

const config: UserConfig = {
plugins: [react()],
mode: 'staging',
build: {
// to make tests faster
minify: false
}
}

export default config
Loading