-
Notifications
You must be signed in to change notification settings - Fork 7
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
base: main
Are you sure you want to change the base?
Changes from all commits
45d7c40
775e121
3f99b32
b953895
fb06deb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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>` | ||
) | ||
} | ||
tags.push( | ||
`<script type="module" src="${dSrvProtocol}://${path.posix.join( | ||
dSrvHost, | ||
|
@@ -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')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// If it is, import it and get the preamble code from it if possible | ||
reactPreamble = (await import('@vitejs/plugin-react'))?.default?.preambleCode | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this part should handle when |
||
if (!reactPreamble) { | ||
console.warn('Unable to get React refresh preamble') | ||
} | ||
} | ||
}, | ||
|
||
buildStart(options: InputOptions) { | ||
|
There was a problem hiding this comment.
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:
reference: https://vite.dev/guide/backend-integration
The script lines here is very different. Is there any official documentation about this?
There was a problem hiding this comment.
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?