Skip to content
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

Adds a config flag to add React Refresh code #15

Open
wants to merge 5 commits 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
20 changes: 19 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default function viteNodeCGPlugin(pluginConfig: PluginConfig): Plugin {
})

let config: ResolvedConfig
let reactPreamble: string
let dSrvProtocol: string
let dSrvHost: string
let assetManifest: Manifest
Expand All @@ -87,6 +88,15 @@ export default function viteNodeCGPlugin(pluginConfig: PluginConfig): Plugin {
const tags = []

if (config.mode === 'development') {
if (reactPreamble) {
tags.push(
`<script type="module">${reactPreamble.replace(/__BASE__/g, `${dSrvProtocol}://${path.posix.join(
dSrvHost,
'bundles',
bundleName
)}/`)}</script>`
)
Comment on lines +92 to +98
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to my knowledge and information available online, in order to enable React Refresh, you have to add these lines in HTML:

<script type="module">
  import RefreshRuntime from 'http://localhost:5173/@react-refresh'
  RefreshRuntime.injectIntoGlobalHook(window)
  window.$RefreshReg$ = () => {}
  window.$RefreshSig$ = () => (type) => type
  window.__vite_plugin_react_preamble_installed__ = true
</script>

reference: https://vite.dev/guide/backend-integration

The script lines here is very different. Is there any official documentation about this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the default preamble that plugin-react exports is exactly that: https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/src/fast-refresh.ts#L23-L29

I think that means this is setup correctly?

}
tags.push(
`<script type="module" src="${dSrvProtocol}://${path.posix.join(
dSrvHost,
Expand Down Expand Up @@ -233,9 +243,17 @@ export default function viteNodeCGPlugin(pluginConfig: PluginConfig): Plugin {
}
},

configResolved(resolvedConfig: ResolvedConfig) {
async configResolved(resolvedConfig: ResolvedConfig) {
// Capture resolved config for use in injectAssets
config = resolvedConfig
// Check to see if one of the plugins is vite:react-refresh
if (resolvedConfig.plugins.find((plugin) => plugin.name === 'vite:react-refresh')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.find() returns the found object but this is only used as a boolean. should use .some() instead.

// If it is, import it and get the preamble code from it if possible
reactPreamble = (await import('@vitejs/plugin-react'))?.default?.preambleCode
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this part should handle when @vitejs/plugin-react is not installed, in which case it would throw an error

if (!reactPreamble) {
console.warn('Unable to get React refresh preamble')
}
}
},

buildStart(options: InputOptions) {
Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"type": "module",
"scripts": {
"build": "tsc",
"dev": "tsc --watch"
"dev": "tsc --watch",
"prepare": "npm run build"
},
"author": {
"name": "Daniel Shields",
Expand All @@ -36,7 +37,13 @@
"vite": "^5.1.5"
},
"peerDependencies": {
"@vitejs/plugin-react": "^4.3.4",
"vite": "^4.0.4 || ^5.1.5"
},
"packageManager": "pnpm@8.15.4+sha256.cea6d0bdf2de3a0549582da3983c70c92ffc577ff4410cbf190817ddc35137c2"
"peerDependenciesMeta": {
"@vitejs/plugin-react": {
"optional": true
}
},
"packageManager": "pnpm@8.15.9+sha512.499434c9d8fdd1a2794ebf4552b3b25c0a633abcee5bb15e7b5de90f32f47b513aca98cd5cfd001c31f0db454bc3804edccd578501e4ca293a6816166bbd9f81"
}
Loading