Skip to content

Commit 7f22049

Browse files
committed
fix: skip id query string in transform (fixes #23)
1 parent 6f3e372 commit 7f22049

13 files changed

+143
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Support Emotion via the new `jsxImportSource` option (fixes [#25](https://github.com/vitejs/vite-plugin-react-swc/issues/25))
66
- Fix HMR when using Vite `base` option (fixes [#18](https://github.com/vitejs/vite-plugin-react-swc/issues/18))
7+
- Fix usage with workers (fixes [#23](https://github.com/vitejs/vite-plugin-react-swc/issues/23))
78
- Fix usage with `Vite Ruby` and `Laravel Vite` ([#20](https://github.com/vitejs/vite-plugin-react-swc/pull/20))
89
- Fix plugin default export when using commonjs (fixes [#14](https://github.com/vitejs/vite-plugin-react-swc/issues/14))
910

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { test } from "@playwright/test";
2+
import {
3+
setupDevServer,
4+
setupBuildAndPreview,
5+
setupWaitForLogs,
6+
} from "../../utils";
7+
8+
test("Worker build", async ({ page }) => {
9+
const { testUrl, server } = await setupBuildAndPreview("worker");
10+
const waitForLogs = await setupWaitForLogs(page);
11+
await page.goto(testUrl);
12+
await waitForLogs("Worker lives!", "Worker imported!");
13+
14+
await server.httpServer.close();
15+
});
16+
17+
test("Worker HMR", async ({ page }) => {
18+
const { testUrl, server, editFile } = await setupDevServer("worker");
19+
const waitForLogs = await setupWaitForLogs(page);
20+
await page.goto(testUrl);
21+
await waitForLogs("Worker lives!", "Worker imported!");
22+
23+
editFile("src/worker-via-url.ts", ["Worker lives!", "Worker updates!"]);
24+
await waitForLogs("Worker updates!");
25+
26+
await server.close();
27+
});

playground/worker/index.html

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
6+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
7+
<title>Vite + React + Worker</title>
8+
</head>
9+
<body>
10+
<div id="root"></div>
11+
<script type="module" src="/src/index.tsx"></script>
12+
</body>
13+
</html>

playground/worker/package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "playground-hmr",
3+
"private": true,
4+
"scripts": {
5+
"dev": "vite",
6+
"build": "vite build"
7+
},
8+
"dependencies": {
9+
"react": "^18.2.0",
10+
"react-dom": "^18.2.0"
11+
},
12+
"devDependencies": {
13+
"@types/react-dom": "^18.0.9",
14+
"@types/react": "^18.0.26",
15+
"@vitejs/plugin-react-swc": "../../dist"
16+
}
17+
}

playground/worker/public/vite.svg

+1
Loading

playground/worker/src/App.tsx

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { useState } from "react";
2+
import MyWorker from "./worker-via-import?worker&inline";
3+
4+
new MyWorker();
5+
6+
export const App = () => {
7+
const [count, setCount] = useState(0);
8+
9+
return <button onClick={() => setCount(count + 1)}>count is {count}</button>;
10+
};

playground/worker/src/index.tsx

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { StrictMode } from "react";
2+
import { createRoot } from "react-dom/client";
3+
import { App } from "./App";
4+
5+
new Worker(new URL("./worker-via-url.ts", import.meta.url), { type: "module" });
6+
7+
createRoot(document.getElementById("root")!).render(
8+
<StrictMode>
9+
<App />
10+
</StrictMode>,
11+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function printAlive(): void {
2+
console.log("Worker imported!");
3+
}
4+
5+
printAlive();
6+
7+
export {};
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
function printAlive(): void {
2+
console.log("Worker lives!");
3+
}
4+
5+
printAlive();
6+
7+
export {};

playground/worker/tsconfig.json

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"include": ["src", "vite.config.ts"],
3+
"compilerOptions": {
4+
"module": "ESNext",
5+
"lib": ["ESNext", "DOM", "DOM.Iterable"],
6+
"target": "ESNext",
7+
"jsx": "react-jsx",
8+
"types": ["vite/client"],
9+
"noEmit": true,
10+
"isolatedModules": true,
11+
"skipLibCheck": true,
12+
13+
/* Imports */
14+
"moduleResolution": "node", // Allow `index` imports
15+
"resolveJsonModule": true, // Allow json import
16+
"forceConsistentCasingInFileNames": true, // Avoid difference in case between file name and import
17+
"esModuleInterop": false,
18+
19+
/* Linting */
20+
"strict": true,
21+
"noUnusedLocals": true,
22+
"noUnusedParameters": true,
23+
"noFallthroughCasesInSwitch": true,
24+
"useUnknownInCatchVariables": true
25+
}
26+
}

playground/worker/vite.config.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { defineConfig } from "vite";
2+
import react from "@vitejs/plugin-react-swc";
3+
4+
export default defineConfig({
5+
plugins: [react()],
6+
});

pnpm-lock.yaml

+15
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ const react = (options?: Options): PluginOption[] => [
4949
),
5050
},
5151
],
52-
async transform(code, id, transformOptions) {
52+
async transform(code, _id, transformOptions) {
53+
const id = _id.split("?")[0];
5354
if (id.includes("node_modules")) return;
5455

5556
const parser: ParserConfig | undefined = id.endsWith(".tsx")

0 commit comments

Comments
 (0)