Skip to content

Commit

Permalink
refactor: migrate plugin-react-refresh to plugin-react
Browse files Browse the repository at this point in the history
See vitejs#3588 for full commit history

Co-authored-by: Shinigami <[email protected]>
Co-authored-by: Peng Xiao <[email protected]>
  • Loading branch information
3 people committed Nov 8, 2021
1 parent 86a8a13 commit 9b3259d
Show file tree
Hide file tree
Showing 29 changed files with 2,916 additions and 2,051 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ jobs:
- name: Build plugin-vue
run: yarn build-plugin-vue

- name: Build plugin-react
run: yarn build-plugin-react

- name: Test serve
run: yarn test-serve --runInBand

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
"docs": "vitepress dev docs",
"build-docs": "vitepress build docs",
"serve-docs": "vitepress serve docs",
"build": "run-s build-vite build-plugin-vue",
"build": "run-s build-vite build-plugin-vue build-plugin-react",
"build-vite": "cd packages/vite && yarn build",
"build-plugin-vue": "cd packages/plugin-vue && yarn build",
"build-plugin-react": "cd packages/plugin-react && yarn build",
"ci-build-vite": "cd packages/vite && yarn ci-build",
"ci-docs": "run-s build-vite build-plugin-vue build-docs"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/playground/file-delete-restore/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
"react-dom": "^17.0.1"
},
"devDependencies": {
"@vitejs/plugin-react-refresh": "^1.3.1"
"@vitejs/plugin-react": "^1.0.0"
}
}
7 changes: 2 additions & 5 deletions packages/playground/file-delete-restore/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
const reactRefresh = require('@vitejs/plugin-react-refresh')
const react = require('@vitejs/plugin-react')

/**
* @type {import('vite').UserConfig}
*/
module.exports = {
plugins: [reactRefresh()],
plugins: [react()],
build: {
// to make tests faster
minify: false
},
esbuild: {
jsxInject: `import React from 'react'`
}
}
56 changes: 56 additions & 0 deletions packages/playground/react-emotion/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { useState } from 'react'
import { css } from '@emotion/react'

import _Switch from 'react-switch'
const Switch = _Switch.default || _Switch;

export function Counter() {
const [count, setCount] = useState(0)

return (
<button
css={css`
border: 2px solid #000;
`}
onClick={() => setCount((count) => count + 1)}
>
count is: {count}
</button>
)
}

function FragmentTest() {
const [checked, setChecked] = useState(false)
return (
<>
<Switch checked={checked} onChange={setChecked} />
<p>
<Counter />
</p>
</>
)
}

function App() {
return (
<div className="App">
<header className="App-header">
<h1>Hello Vite + React + @emotion/react</h1>
<FragmentTest />
<p>
Edit <code>App.jsx</code> and save to test HMR updates.
</p>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
)
}

export default App
45 changes: 45 additions & 0 deletions packages/playground/react-emotion/__tests__/react.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { editFile, untilUpdated } from '../../testUtils'

test('should render', async () => {
expect(await page.textContent('h1')).toMatch(
'Hello Vite + React + @emotion/react'
)
})

test('should update', async () => {
expect(await page.textContent('button')).toMatch('count is: 0')
await page.click('button')
expect(await page.textContent('button')).toMatch('count is: 1')
})

test('should hmr', async () => {
editFile('App.jsx', (code) =>
code.replace('Vite + React + @emotion/react', 'Updated')
)
await untilUpdated(() => page.textContent('h1'), 'Hello Updated')
// preserve state
expect(await page.textContent('button')).toMatch('count is: 1')
})

test('should update button style', async () => {
function getButtonBorderStyle() {
return page.evaluate(() => {
return window.getComputedStyle(document.querySelector('button')).border
})
}

const styles = await page.evaluate(() => {
return document.querySelector('button').style
})

expect(await getButtonBorderStyle()).toMatch('2px solid rgb(0, 0, 0)')

editFile('App.jsx', (code) =>
code.replace('border: 2px solid #000', 'border: 4px solid red')
)

await untilUpdated(getButtonBorderStyle, '4px solid rgb(255, 0, 0)')

// preserve state
expect(await page.textContent('button')).toMatch('count is: 1')
})
8 changes: 8 additions & 0 deletions packages/playground/react-emotion/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<div id="app"></div>
<script type="module">
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App.jsx'

ReactDOM.render(React.createElement(App), document.getElementById('app'))
</script>
27 changes: 27 additions & 0 deletions packages/playground/react-emotion/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "test-react-emotion",
"private": true,
"version": "0.0.0",
"scripts": {
"dev": "vite",
"build": "vite build",
"debug": "node --inspect-brk ../../vite/bin/vite",
"serve": "vite preview"
},
"dependencies": {
"@emotion/react": "^11.4.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-switch": "^6.0.0"
},
"devDependencies": {
"@babel/plugin-proposal-pipeline-operator": "^7.14.5",
"@emotion/babel-plugin": "^11.3.0",
"@vitejs/plugin-react": "^1.0.0"
},
"babel": {
"presets": [
"@babel/preset-env"
]
}
}
19 changes: 19 additions & 0 deletions packages/playground/react-emotion/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import react from '@vitejs/plugin-react'

const config: import('vite').UserConfig = {
plugins: [
react({
jsxImportSource: '@emotion/react',
babel: {
plugins: ['@emotion/babel-plugin']
}
})
],
clearScreen: false,
build: {
// to make tests faster
minify: false
}
}

export default config
2 changes: 1 addition & 1 deletion packages/playground/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"react-dom": "^17.0.1"
},
"devDependencies": {
"@vitejs/plugin-react-refresh": "^1.3.1"
"@vitejs/plugin-react": "^1.0.0"
},
"babel": {
"presets": [
Expand Down
15 changes: 0 additions & 15 deletions packages/playground/react/vite.config.js

This file was deleted.

11 changes: 11 additions & 0 deletions packages/playground/react/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import react from '@vitejs/plugin-react'

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

export default config
2 changes: 1 addition & 1 deletion packages/playground/ssr-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"react-router-dom": "^5.2.0"
},
"devDependencies": {
"@vitejs/plugin-react-refresh": "^1.3.1",
"@vitejs/plugin-react": "^1.0.0",
"compression": "^1.7.4",
"cross-env": "^7.0.3",
"express": "^4.17.1",
Expand Down
7 changes: 2 additions & 5 deletions packages/playground/ssr-react/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
const reactRefresh = require('@vitejs/plugin-react-refresh')
const react = require('@vitejs/plugin-react')

/**
* @type {import('vite').UserConfig}
*/
module.exports = {
plugins: [reactRefresh()],
esbuild: {
jsxInject: `import React from 'react';`
},
plugins: [react()],
build: {
minify: false
}
Expand Down
Loading

0 comments on commit 9b3259d

Please sign in to comment.