Skip to content

Commit

Permalink
fix: skip id query string in transform (fixes #23)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Dec 15, 2022
1 parent 6f3e372 commit 7f22049
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

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

Expand Down
27 changes: 27 additions & 0 deletions playground/worker/__tests__/worker.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { test } from "@playwright/test";
import {
setupDevServer,
setupBuildAndPreview,
setupWaitForLogs,
} from "../../utils";

test("Worker build", async ({ page }) => {
const { testUrl, server } = await setupBuildAndPreview("worker");
const waitForLogs = await setupWaitForLogs(page);
await page.goto(testUrl);
await waitForLogs("Worker lives!", "Worker imported!");

await server.httpServer.close();
});

test("Worker HMR", async ({ page }) => {
const { testUrl, server, editFile } = await setupDevServer("worker");
const waitForLogs = await setupWaitForLogs(page);
await page.goto(testUrl);
await waitForLogs("Worker lives!", "Worker imported!");

editFile("src/worker-via-url.ts", ["Worker lives!", "Worker updates!"]);
await waitForLogs("Worker updates!");

await server.close();
});
13 changes: 13 additions & 0 deletions playground/worker/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + Worker</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/index.tsx"></script>
</body>
</html>
17 changes: 17 additions & 0 deletions playground/worker/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "playground-hmr",
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react-dom": "^18.0.9",
"@types/react": "^18.0.26",
"@vitejs/plugin-react-swc": "../../dist"
}
}
1 change: 1 addition & 0 deletions playground/worker/public/vite.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions playground/worker/src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { useState } from "react";
import MyWorker from "./worker-via-import?worker&inline";

new MyWorker();

export const App = () => {
const [count, setCount] = useState(0);

return <button onClick={() => setCount(count + 1)}>count is {count}</button>;
};
11 changes: 11 additions & 0 deletions playground/worker/src/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { App } from "./App";

new Worker(new URL("./worker-via-url.ts", import.meta.url), { type: "module" });

createRoot(document.getElementById("root")!).render(
<StrictMode>
<App />
</StrictMode>,
);
7 changes: 7 additions & 0 deletions playground/worker/src/worker-via-import.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function printAlive(): void {
console.log("Worker imported!");
}

printAlive();

export {};
7 changes: 7 additions & 0 deletions playground/worker/src/worker-via-url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function printAlive(): void {
console.log("Worker lives!");
}

printAlive();

export {};
26 changes: 26 additions & 0 deletions playground/worker/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"include": ["src", "vite.config.ts"],
"compilerOptions": {
"module": "ESNext",
"lib": ["ESNext", "DOM", "DOM.Iterable"],
"target": "ESNext",
"jsx": "react-jsx",
"types": ["vite/client"],
"noEmit": true,
"isolatedModules": true,
"skipLibCheck": true,

/* Imports */
"moduleResolution": "node", // Allow `index` imports
"resolveJsonModule": true, // Allow json import
"forceConsistentCasingInFileNames": true, // Avoid difference in case between file name and import
"esModuleInterop": false,

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"useUnknownInCatchVariables": true
}
}
6 changes: 6 additions & 0 deletions playground/worker/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";

export default defineConfig({
plugins: [react()],
});
15 changes: 15 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ const react = (options?: Options): PluginOption[] => [
),
},
],
async transform(code, id, transformOptions) {
async transform(code, _id, transformOptions) {
const id = _id.split("?")[0];
if (id.includes("node_modules")) return;

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

0 comments on commit 7f22049

Please sign in to comment.