-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
475 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { useState } from 'react' | ||
|
||
function App() { | ||
const [count, setCount] = useState(0) | ||
|
||
return ( | ||
<div className="App"> | ||
<header className="App-header"> | ||
<p>Hello Vite + React!</p> | ||
<p> | ||
<button onClick={() => setCount(count => count + 1)}>count is: {count}</button> | ||
</p> | ||
<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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
<h1>hello world fsef</h1> | ||
<img width="300" src="/logo.png" alt=""> | ||
<div id="app"></div> | ||
<script id="main" type="module" src="/main.js"></script> | ||
<script id="main" type="module" src="/main.jsx"></script> | ||
<script> | ||
document.getElementById('main').onerror = (e) => { | ||
console.log('efsfsef') | ||
console.log(`entry script load failure:`, e) | ||
} | ||
</script> |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom' | ||
import App from './App.jsx' | ||
|
||
ReactDOM.render( | ||
<App/>, | ||
document.getElementById('app') | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
// @ts-check | ||
const fs = require('fs') | ||
|
||
const runtimePublicPath = '/@react-refresh.js' | ||
const runtimeFilePath = require.resolve( | ||
'react-refresh/cjs/react-refresh-runtime.development.js' | ||
) | ||
|
||
function debounce(fn, delay) { | ||
let handle | ||
return () => { | ||
clearTimeout(handle) | ||
handle = setTimeout(fn, delay) | ||
} | ||
} | ||
|
||
const runtimeCode = ` | ||
const exports = {} | ||
${fs.readFileSync(runtimeFilePath, 'utf-8')} | ||
${debounce.toString()} | ||
exports.performReactRefresh = debounce(exports.performReactRefresh, 16) | ||
export default exports | ||
` | ||
|
||
/** | ||
* @type { import('vite').Plugin } | ||
*/ | ||
const resolve = { | ||
name: 'react-refresh-resolve', | ||
resolveId(id) { | ||
if (id === 'react') { | ||
return this.resolve('@pika/react/source.development.js') | ||
} | ||
if (id === 'react-dom') { | ||
return this.resolve('@pika/react-dom/source.development.js') | ||
} | ||
if (id === runtimePublicPath) { | ||
return runtimeFilePath | ||
} | ||
}, | ||
|
||
load(id) { | ||
if (id === runtimeFilePath) { | ||
return runtimeCode | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* @type { import('vite').Plugin } | ||
*/ | ||
const transform = { | ||
name: 'react-refresh-transform', | ||
|
||
// make sure this is applied after vite's internal esbuild transform | ||
// which handles (j|t)sx | ||
enforce: 'post', | ||
|
||
transform(code, id) { | ||
// @ts-ignore | ||
if (!this.serverContext) { | ||
return | ||
} | ||
if (!/\.(t|j)sx?$/.test(id) || id.includes('node_modules')) { | ||
return | ||
} | ||
|
||
const isReasonReact = id.endsWith('.bs.js') | ||
const result = require('@babel/core').transformSync(code, { | ||
plugins: [ | ||
require('@babel/plugin-syntax-import-meta'), | ||
require('react-refresh/babel') | ||
], | ||
ast: !isReasonReact, | ||
sourceMaps: true, | ||
sourceFileName: id | ||
}) | ||
|
||
if (!/\$RefreshReg\$\(/.test(result.code)) { | ||
// no component detected in the file | ||
return code | ||
} | ||
|
||
const header = ` | ||
import RefreshRuntime from "${runtimePublicPath}"; | ||
let prevRefreshReg; | ||
let prevRefreshSig; | ||
if (!window.__vite_plugin_react_preamble_installed__) { | ||
throw new Error( | ||
"vite-plugin-react can't detect preamble. Something is wrong. See https://github.com/vitejs/vite-plugin-react/pull/11#discussion_r430879201" | ||
); | ||
} | ||
if (import.meta.hot) { | ||
prevRefreshReg = window.$RefreshReg$; | ||
prevRefreshSig = window.$RefreshSig$; | ||
window.$RefreshReg$ = (type, id) => { | ||
RefreshRuntime.register(type, ${JSON.stringify(id)} + " " + id) | ||
}; | ||
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform; | ||
}`.replace(/[\n]+/gm, '') | ||
|
||
const footer = ` | ||
if (import.meta.hot) { | ||
window.$RefreshReg$ = prevRefreshReg; | ||
window.$RefreshSig$ = prevRefreshSig; | ||
${ | ||
isReasonReact || isRefreshBoundary(result.ast) | ||
? `import.meta.hot.accept();` | ||
: `` | ||
} | ||
if (!window.__vite_plugin_react_timeout) { | ||
window.__vite_plugin_react_timeout = setTimeout(() => { | ||
window.__vite_plugin_react_timeout = 0; | ||
RefreshRuntime.performReactRefresh(); | ||
}, 30); | ||
} | ||
}` | ||
|
||
return { | ||
code: `${header}${result.code}${footer}`, | ||
map: result.map | ||
} | ||
}, | ||
|
||
transformIndexHtml() { | ||
return [ | ||
{ | ||
tag: 'script', | ||
attrs: { type: 'module' }, | ||
children: ` | ||
import RefreshRuntime from "${runtimePublicPath}" | ||
RefreshRuntime.injectIntoGlobalHook(window) | ||
window.$RefreshReg$ = () => {} | ||
window.$RefreshSig$ = () => (type) => type | ||
window.__vite_plugin_react_preamble_installed__ = true | ||
` | ||
} | ||
] | ||
} | ||
} | ||
|
||
/** | ||
* @param {import('@babel/types').File} ast | ||
*/ | ||
function isRefreshBoundary(ast) { | ||
// Every export must be a React component. | ||
return ast.program.body.every((node) => { | ||
if (node.type !== 'ExportNamedDeclaration') { | ||
return true | ||
} | ||
const { declaration, specifiers } = node | ||
if (declaration && declaration.type === 'VariableDeclaration') { | ||
return declaration.declarations.every( | ||
({ id }) => id.type === 'Identifier' && isComponentishName(id.name) | ||
) | ||
} | ||
return specifiers.every( | ||
({ exported }) => | ||
exported.type === 'Identifier' && isComponentishName(exported.name) | ||
) | ||
}) | ||
} | ||
|
||
/** | ||
* @param {string} name | ||
*/ | ||
function isComponentishName(name) { | ||
return typeof name === 'string' && name[0] >= 'A' && name[0] <= 'Z' | ||
} | ||
|
||
module.exports = [resolve, transform] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "@vitejs/plugin-react-refresh", | ||
"version": "1.0.0", | ||
"dependencies": { | ||
"@babel/core": "^7.12.10", | ||
"@babel/plugin-syntax-import-meta": "^7.10.4", | ||
"react-refresh": "^0.9.0" | ||
}, | ||
"devDependencies": { | ||
"@babel/types": "^7.12.10" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.