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

Commit b5ca646

Browse files
committed
v2.2.0 [publish]
1 parent a28b087 commit b5ca646

File tree

11 files changed

+391
-303
lines changed

11 files changed

+391
-303
lines changed

.github/workflows/publish.yml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ jobs:
88
runs-on: ubuntu-latest
99
if: ${{ contains(github.event.head_commit.message, '[publish]') }}
1010
steps:
11-
- uses: actions/checkout@v2
12-
- run: yarn install --frozen-lockfile
13-
- run: yarn prettier-ci
14-
- run: yarn build
15-
- uses: ArnaudBarre/npm-publish@v1
11+
- uses: actions/checkout@v3
12+
- uses: xhyrom/[email protected]
13+
- run: bun install
14+
- run: bun ci
15+
- uses: ArnaudBarre/npm-publish@v1.1
1616
with:
17+
working-directory: dist
1718
npm-token: ${{ secrets.NPM_TOKEN }}

.gitignore

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
.idea/
22
node_modules/
3-
*.js
4-
*.d.ts
5-
!src/refresh-runtime.js
3+
dist/

CHANGELOG.md

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

3+
## 2.2.0
4+
5+
- 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)
6+
- Extract line and column in SWC errors to make overlay filename clickable
7+
- Fix plugin name (`react-refresh` -> `swc-react-refresh`)
8+
39
## 2.1.0
410

511
Add source maps support

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# vite-plugin-swc-react-refresh [![npm](https://img.shields.io/npm/v/vite-plugin-swc-react-refresh)](https://www.npmjs.com/package/vite-plugin-swc-react-refresh)
22

3-
Use the versatility of [swc](https://swc.rs/) for development and the maturity of [esbuild](https://esbuild.github.io/) for production.
3+
Use the versatility of [SWC](https://swc.rs/) for development and the maturity of [esbuild](https://esbuild.github.io/) for production.
44

55
- ✅ A fast Fast Refresh (~20x faster than Babel)
66
- ✅ Compatible with [automatic JSX runtime](https://reactjs.org/blog/2020/09/22/introducing-the-new-jsx-transform.html)

bun.lockb

19.8 KB
Binary file not shown.

bunfig.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[install.lockfile]
2+
print = "yarn"

package.json

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,27 @@
11
{
22
"name": "vite-plugin-swc-react-refresh",
3-
"description": "Use the versatility of swc for development and the maturity of esbuild for production",
4-
"version": "2.1.0",
3+
"version": "2.2.0",
54
"license": "MIT",
6-
"author": "Arnaud Barré (https://github.com/ArnaudBarre)",
7-
"main": "src/swc-react-refresh.js",
8-
"files": [
9-
"src/*.js",
10-
"src/*.d.ts"
11-
],
12-
"repository": "github:ArnaudBarre/vite-plugin-swc-react-refresh",
13-
"keywords": [
14-
"vite",
15-
"vite-plugin",
16-
"react",
17-
"swc",
18-
"react-refresh",
19-
"fast refresh"
20-
],
215
"scripts": {
22-
"build": "tsc",
6+
"build": "scripts/bundle.ts",
237
"prettier": "yarn prettier-ci --write",
24-
"prettier-ci": "prettier --check '**/*.{js,ts,json,md,yml}'"
8+
"prettier-ci": "prettier --ignore-path=.gitignore --check '**/*.{js,ts,json,md,yml}'",
9+
"ci": "tsc && bun prettier-ci && bun run build"
2510
},
2611
"prettier": {
2712
"trailingComma": "all"
2813
},
2914
"dependencies": {
30-
"@swc/core": "^1.2.245"
15+
"@swc/core": "^1.3.10"
3116
},
3217
"peerDependencies": {
3318
"vite": "^2 || ^3"
3419
},
3520
"devDependencies": {
36-
"@types/node": "^18.0.6",
21+
"@nabla/tnode": "^0.7.0",
22+
"@types/node": "^18.11.4",
3723
"prettier": "^2.7.1",
38-
"typescript": "^4.7.4",
39-
"vite": "^3.0.5"
24+
"typescript": "^4.8.4",
25+
"vite": "^3.2.1"
4026
}
4127
}

scripts/bundle.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env tnode
2+
import { rmSync, writeFileSync } from "fs";
3+
import { execSync } from "child_process";
4+
import { build } from "esbuild";
5+
6+
import * as packageJSON from "../package.json";
7+
8+
rmSync("dist", { force: true, recursive: true });
9+
10+
build({
11+
bundle: true,
12+
entryPoints: ["src/index.ts"],
13+
outdir: "dist",
14+
platform: "node",
15+
target: "node14",
16+
legalComments: "inline",
17+
external: Object.keys(packageJSON.peerDependencies).concat(
18+
Object.keys(packageJSON.dependencies),
19+
),
20+
}).then(() => {
21+
execSync("cp src/refresh-runtime.js LICENSE README.md dist/");
22+
23+
writeFileSync(
24+
"dist/index.d.ts",
25+
`import { PluginOption } from "vite";
26+
export declare const swcReactRefresh: () => PluginOption;
27+
`,
28+
);
29+
30+
writeFileSync(
31+
"dist/package.json",
32+
JSON.stringify(
33+
{
34+
name: packageJSON.name,
35+
description:
36+
"Use the versatility of SWC for development and the maturity of esbuild for production",
37+
version: packageJSON.version,
38+
author: "Arnaud Barré (https://github.com/ArnaudBarre)",
39+
license: packageJSON.license,
40+
repository: "github:ArnaudBarre/vite-plugin-swc-react-refresh",
41+
main: "index.js",
42+
keywords: [
43+
"vite",
44+
"vite-plugin",
45+
"react",
46+
"swc",
47+
"react-refresh",
48+
"fast refresh",
49+
],
50+
peerDependencies: packageJSON.peerDependencies,
51+
dependencies: packageJSON.dependencies,
52+
},
53+
null,
54+
2,
55+
),
56+
);
57+
});
Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import fs from "fs";
2-
import path from "path";
1+
import { readFileSync } from "fs";
2+
import { join, extname } from "path";
33
import { SourceMapPayload } from "module";
4-
import { transform } from "@swc/core";
4+
import { Output, ParserConfig, transform } from "@swc/core";
55
import { PluginOption } from "vite";
66

77
const runtimePublicPath = "/@react-refresh";
@@ -16,8 +16,15 @@ const importReactRE = /(^|\n)import\s+(\*\s+as\s+)?React(,|\s+)/;
1616
let define: { [key: string]: string } | undefined;
1717
let automaticRuntime = false;
1818

19+
const parserMap = new Map<string, ParserConfig>([
20+
[".tsx", { syntax: "typescript", tsx: true }],
21+
[".ts", { syntax: "typescript", tsx: false }],
22+
[".jsx", { syntax: "ecmascript", jsx: true }],
23+
[".js", { syntax: "ecmascript", jsx: false }],
24+
]);
25+
1926
export const swcReactRefresh = (): PluginOption => ({
20-
name: "react-refresh",
27+
name: "swc-react-refresh",
2128
apply: "serve",
2229
config: (config) => {
2330
if (config.esbuild) {
@@ -34,33 +41,49 @@ export const swcReactRefresh = (): PluginOption => ({
3441
resolveId: (id) => (id === runtimePublicPath ? id : undefined),
3542
load: (id) =>
3643
id === runtimePublicPath
37-
? fs.readFileSync(path.join(__dirname, "refresh-runtime.js"), "utf-8")
44+
? readFileSync(join(__dirname, "refresh-runtime.js"), "utf-8")
3845
: undefined,
3946
transformIndexHtml: () => [
4047
{ tag: "script", attrs: { type: "module" }, children: preambleCode },
4148
],
4249
async transform(code, id) {
4350
if (id.includes("node_modules")) return;
44-
if (!/\.[jt]sx?$/.test(id)) return;
45-
46-
const result = await transform(code, {
47-
filename: id,
48-
swcrc: false,
49-
configFile: false,
50-
sourceMaps: true,
51-
jsc: {
52-
target: "es2020",
53-
transform: {
54-
react: {
55-
refresh: true,
56-
development: true,
57-
useBuiltins: true,
58-
runtime: automaticRuntime ? "automatic" : undefined,
51+
const parser = parserMap.get(extname(id));
52+
if (!parser) return;
53+
54+
let result: Output;
55+
try {
56+
result = await transform(code, {
57+
filename: id,
58+
swcrc: false,
59+
configFile: false,
60+
sourceMaps: true,
61+
jsc: {
62+
target: "es2020",
63+
parser,
64+
transform: {
65+
react: {
66+
refresh: true,
67+
development: true,
68+
useBuiltins: true,
69+
runtime: automaticRuntime ? "automatic" : undefined,
70+
},
71+
optimizer: { globals: { vars: define } },
5972
},
60-
optimizer: { globals: { vars: define } },
6173
},
62-
},
63-
});
74+
});
75+
} catch (e: any) {
76+
const message: string = e.message;
77+
const fileStartIndex = message.indexOf("╭─[");
78+
if (fileStartIndex !== -1) {
79+
const match = message.slice(fileStartIndex).match(/:(\d+):(\d+)]/);
80+
if (match) {
81+
e.line = match[1];
82+
e.column = match[2];
83+
}
84+
}
85+
throw e;
86+
}
6487
let mappingPrefix = "";
6588

6689
if (

tsconfig.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,16 @@
55
"module": "CommonJS",
66
"lib": ["ES2020"],
77
"target": "ES2020",
8-
"declaration": true,
98
"skipLibCheck": true,
109

10+
/* Transpile with esbuild */
11+
"noEmit": true,
12+
"isolatedModules": true,
13+
1114
/* Imports */
1215
"moduleResolution": "node", // Allow `index` imports
1316
"resolveJsonModule": true, // Allow json import
1417
"forceConsistentCasingInFileNames": true, // Avoid difference in case between file name and import
15-
"esModuleInterop": true, // Allow import fs from "fs"
1618

1719
/* Linting */
1820
"strict": true,

0 commit comments

Comments
 (0)