Skip to content

Commit

Permalink
fix: react fast refresh by reordering Vite React preamble script first
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler Brenneman committed Jun 19, 2024
1 parent 72838a2 commit a466c67
Showing 1 changed file with 12 additions and 16 deletions.
28 changes: 12 additions & 16 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,30 +25,26 @@ export function isObject(value: unknown): value is Record<string, unknown> {
function getViteScripts() {
const entries: string[] = []

for (const source of bundleEntries) {
if (/\.(js|ts)x?/.test(source)) {
entries.push(`<script type="module" src="/${source}"></script>`)
} else if (/\.(css|less|sass|scss|postcss)/.test(source)) {
entries.push(`<link rel="stylesheet" href="/${source}"/>`)
}
}

let scripts = `
<script type="module" src="/@vite/client"></script>
${entries.join('\n')}
`
entries.push('<script type="module" src="/@vite/client"></script>')

const isUsingReact = resolvedConfig.plugins.find(({ name }) => name === 'vite:react-refresh')

if (isUsingReact) {
scripts += `
entries.push(`
<script type="module">
${viteReact.preambleCode.replace('__BASE__', resolvedConfig.base)}
</script>
`
`)
}

for (const source of bundleEntries) {
if (/\.(js|ts)x?/.test(source)) {
entries.push(`<script type="module" src="/${source}"></script>`)
} else if (/\.(css|less|sass|scss|postcss)/.test(source)) {
entries.push(`<link rel="stylesheet" href="/${source}"/>`)
}
}

return scripts
return entries.join('\n')
}

/**
Expand Down

0 comments on commit a466c67

Please sign in to comment.