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 2 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: 20 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ export interface PluginConfig {
* @default './src'
*/
srcDir?: string | undefined

react?: boolean
Hoishin marked this conversation as resolved.
Show resolved Hide resolved
}

export default function viteNodeCGPlugin(pluginConfig: PluginConfig): Plugin {
Expand All @@ -46,6 +48,8 @@ export default function viteNodeCGPlugin(pluginConfig: PluginConfig): Plugin {
'!**.d.ts',
]

const react = pluginConfig?.react ?? false;

// string array of paths to all input files (always ignore ts declaration files)
const inputs = globbySync(inputPatterns)

Expand Down Expand Up @@ -87,6 +91,22 @@ export default function viteNodeCGPlugin(pluginConfig: PluginConfig): Plugin {
const tags = []

if (config.mode === 'development') {
if (react) {
tags.push(
`<script type="module">
import RefreshRuntime from "${dSrvProtocol}://${path.posix.join(
dSrvHost,
'bundles',
bundleName,
'@react-refresh'
)}"
RefreshRuntime.injectIntoGlobalHook(window)
window.$RefreshReg$ = () => {}
window.$RefreshSig$ = () => (type) => type
window.__vite_plugin_react_preamble_installed__ = true
</script>`
)
}
tags.push(
`<script type="module" src="${dSrvProtocol}://${path.posix.join(
dSrvHost,
Expand Down
3 changes: 2 additions & 1 deletion package.json
Hoishin marked this conversation as resolved.
Show resolved Hide resolved
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 Down