Skip to content
This repository was archived by the owner on Jul 24, 2025. It is now read-only.

Commit a28b087

Browse files
committed
Add source maps support [publish]
1 parent 95291df commit a28b087

File tree

3 files changed

+22
-8
lines changed

3 files changed

+22
-8
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 2.1.0
4+
5+
Add source maps support
6+
37
## 2.0.3
48

59
Include `react/jsx-dev-runtime` for dependencies optimisation when using automatic runtime.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "vite-plugin-swc-react-refresh",
33
"description": "Use the versatility of swc for development and the maturity of esbuild for production",
4-
"version": "2.0.3",
4+
"version": "2.1.0",
55
"license": "MIT",
66
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
77
"main": "src/swc-react-refresh.js",

src/swc-react-refresh.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import fs from "fs";
22
import path from "path";
3+
import { SourceMapPayload } from "module";
34
import { transform } from "@swc/core";
45
import { PluginOption } from "vite";
56

@@ -46,7 +47,7 @@ export const swcReactRefresh = (): PluginOption => ({
4647
filename: id,
4748
swcrc: false,
4849
configFile: false,
49-
50+
sourceMaps: true,
5051
jsc: {
5152
target: "es2020",
5253
transform: {
@@ -60,17 +61,20 @@ export const swcReactRefresh = (): PluginOption => ({
6061
},
6162
},
6263
});
64+
let mappingPrefix = "";
6365

6466
if (
6567
!automaticRuntime &&
6668
result.code.includes("React.createElement") &&
6769
!importReactRE.test(result.code)
6870
) {
6971
result.code = `import React from "react";\n${result.code}`;
72+
mappingPrefix += ";";
7073
}
71-
if (!result.code.includes("$RefreshReg$")) return result;
7274

73-
const header = `import * as RefreshRuntime from "${runtimePublicPath}";
75+
if (result.code.includes("$RefreshReg$")) {
76+
mappingPrefix += ";;;;;;;;;;;;";
77+
result.code = `import * as RefreshRuntime from "${runtimePublicPath}";
7478
7579
let prevRefreshReg;
7680
let prevRefreshSig;
@@ -81,14 +85,20 @@ prevRefreshReg = window.$RefreshReg$;
8185
prevRefreshSig = window.$RefreshSig$;
8286
window.$RefreshReg$ = RefreshRuntime.getRefreshReg("${id}");
8387
window.$RefreshSig$ = RefreshRuntime.createSignatureFunctionForTransform;
84-
`;
8588
86-
const footer = `
89+
${result.code}
90+
8791
window.$RefreshReg$ = prevRefreshReg;
8892
window.$RefreshSig$ = prevRefreshSig;
8993
import.meta.hot.accept();
90-
RefreshRuntime.enqueueUpdate();`;
94+
RefreshRuntime.enqueueUpdate();
95+
`;
96+
}
97+
98+
if (!mappingPrefix) return result;
9199

92-
return { code: `${header}${result.code}${footer}`, map: result.map };
100+
const sourceMap: SourceMapPayload = JSON.parse(result.map!);
101+
sourceMap.mappings = mappingPrefix + sourceMap.mappings;
102+
return { code: result.code, map: sourceMap };
93103
},
94104
});

0 commit comments

Comments
 (0)