Skip to content

Commit

Permalink
Skip react-refresh on SSR (Fix #2) [publish]
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Nov 2, 2022
1 parent b5ca646 commit 317a2bf
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 2.2.1

Skip react-refresh on SSR (Fixes [#2](https://github.com/ArnaudBarre/vite-plugin-swc-react-refresh/issues/2))

## 2.2.0

- Always provide parser options to fix issue with `.jsx` imports. Relying on file extension for this is more buggy [than I though](https://github.com/swc-project/swc/issues/3297)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vite-plugin-swc-react-refresh",
"version": "2.2.0",
"version": "2.2.1",
"license": "MIT",
"scripts": {
"build": "scripts/bundle.ts",
Expand Down
14 changes: 5 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export const swcReactRefresh = (): PluginOption => ({
transformIndexHtml: () => [
{ tag: "script", attrs: { type: "module" }, children: preambleCode },
],
async transform(code, id) {
async transform(code, id, transformOptions) {
if (id.includes("node_modules")) return;
const parser = parserMap.get(extname(id));
if (!parser) return;
Expand All @@ -63,7 +63,7 @@ export const swcReactRefresh = (): PluginOption => ({
parser,
transform: {
react: {
refresh: true,
refresh: !transformOptions?.ssr,
development: true,
useBuiltins: true,
runtime: automaticRuntime ? "automatic" : undefined,
Expand Down Expand Up @@ -96,16 +96,12 @@ export const swcReactRefresh = (): PluginOption => ({
}

if (result.code.includes("$RefreshReg$")) {
mappingPrefix += ";;;;;;;;;;;;";
mappingPrefix += ";;;;;;;;";
result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
let prevRefreshReg;
let prevRefreshSig;
if (!window.$RefreshReg$) throw new Error("React refresh preamble was not loaded. Something is wrong.");
prevRefreshReg = window.$RefreshReg$;
prevRefreshSig = window.$RefreshSig$;
const prevRefreshReg = window.$RefreshReg$;
const prevRefreshSig = window.$RefreshSig$;
window.$RefreshReg$ = RefreshRuntime.getRefreshReg("${id}");
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
Expand Down

0 comments on commit 317a2bf

Please sign in to comment.