Skip to content

Commit

Permalink
fix: enforce "pre" for builds with SWC plugins (fixes #56)
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudBarre committed Feb 12, 2023
1 parent 8ad1f6c commit f52d870
Show file tree
Hide file tree
Showing 13 changed files with 252 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Support HMR for MDX (fixes #52)
- Fix: when using plugins, apply SWC before esbuild so that automatic runtime is respected for JSX (fixes #56)
- Fix: use jsxImportSource in optimizeDeps

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

test("Emotion plugin build", async ({ page }) => {
const { testUrl, server } = await setupBuildAndPreview("emotion-plugin");
await page.goto(testUrl);

const button = page.locator("button");
await button.hover();
await expectColor(button, "color", "#646cff");

await button.click();
await expect(button).toHaveText("count is 1");

const code = page.locator("code");
await expectColor(code, "color", "#646cff");

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

test("Emotion plugin HMR", async ({ page }) => {
const { testUrl, server, editFile } = await setupDevServer("emotion-plugin");
const waitForLogs = await setupWaitForLogs(page);
await page.goto(testUrl);
await waitForLogs("[vite] connected.");

const button = page.locator("button");
await button.hover();
await expectColor(button, "color", "#646cff");

await button.click();
await expect(button).toHaveText("count is 1");

const code = page.locator("code");
await expectColor(code, "color", "#646cff");

editFile("src/Button.jsx", [
"background-color: #d26ac2;",
"background-color: #646cff;",
]);
await waitForLogs("[vite] hot updated: /src/Button.jsx");
await expect(button).toHaveText("count is 1");
await expectColor(button, "backgroundColor", "#646cff");

editFile("src/App.jsx", ['color="#646cff"', 'color="#d26ac2"']);
await waitForLogs("[vite] hot updated: /src/App.jsx");
await expect(button).toHaveText("count is 1");
await expectColor(button, "color", "#d26ac2");

editFile("src/Button.jsx", ["color: #646cff;", "color: #d26ac2;"]);
await waitForLogs("[vite] hot updated: /src/Button.jsx");
await expect(button).toHaveText("count is 1");
await expectColor(code, "color", "#d26ac2");

await server.close();
});
13 changes: 13 additions & 0 deletions playground/emotion-plugin/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 + TS + Emotion</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/index.jsx"></script>
</body>
</html>
21 changes: 21 additions & 0 deletions playground/emotion-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "playground-emotion",
"private": true,
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"@emotion/react": "^11.10.5",
"@emotion/styled": "^11.10.5",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@types/react": "^18.0.27",
"@types/react-dom": "^18.0.10",
"@vitejs/plugin-react-swc": "../../dist",
"@swc/plugin-emotion": "^2.5.41"
}
}
1 change: 1 addition & 0 deletions playground/emotion-plugin/public/vite.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
26 changes: 26 additions & 0 deletions playground/emotion-plugin/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#root {
max-width: 1280px;
margin: 0 auto;
padding: 2rem;
text-align: center;
}

.logo {
height: 6em;
padding: 1.5em;
will-change: filter;
}
.logo:hover {
filter: drop-shadow(0 0 2em #646cffaa);
}
.logo.emotion:hover {
filter: drop-shadow(0 0 2em #d26ac2aa);
}

.card {
padding: 2em;
}

.read-the-docs {
color: #888;
}
28 changes: 28 additions & 0 deletions playground/emotion-plugin/src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import "./App.css";
import { Button, StyledCode } from "./Button";

export const App = () => (
<div>
<div>
<a href="https://vitejs.dev" target="_blank" rel="noreferrer">
<img src="/vite.svg" className="logo" alt="Vite logo" />
</a>
<a href="https://emotion.sh/" target="_blank" rel="noreferrer">
<img
src="https://emotion.sh/logo-96x96.png"
className="logo emotion"
alt="Emotion logo"
/>
</a>
</div>
<div className="card">
<Button color="#646cff" />
<p>
Edit <StyledCode>src/Button.tsx</StyledCode> and save to test HMR
</p>
</div>
<p className="read-the-docs">
Click on the Vite and Emotion logos to learn more
</p>
</div>
);
30 changes: 30 additions & 0 deletions playground/emotion-plugin/src/Button.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import styled from "@emotion/styled";
import { css } from "@emotion/react";
import { useState } from "react";

// Ensure HMR of styled component alongside other components
export const StyledCode = styled.code`
color: #646cff;
`;

export const Button = ({ color }) => {
const [count, setCount] = useState(0);

return (
<button
css={css`
padding: 10px 16px;
background-color: #d26ac2;
font-size: 20px;
border-radius: 4px;
border: 0px;
&:hover {
color: ${color};
}
`}
onClick={() => setCount(count + 1)}
>
count is {count}
</button>
);
};
24 changes: 24 additions & 0 deletions playground/emotion-plugin/src/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
:root {
font-family: Inter, Avenir, Helvetica, Arial, sans-serif;
font-size: 16px;
line-height: 24px;
font-weight: 400;

color-scheme: light dark;
color: rgba(255, 255, 255, 0.87);
background-color: #242424;

font-synthesis: none;
text-rendering: optimizeLegibility;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
-webkit-text-size-adjust: 100%;
}

body {
margin: 0;
display: flex;
place-items: center;
min-width: 320px;
min-height: 100vh;
}
10 changes: 10 additions & 0 deletions playground/emotion-plugin/src/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { StrictMode } from "react";
import { createRoot } from "react-dom/client";
import { App } from "./App";
import "./index.css";

createRoot(document.getElementById("root")).render(
<StrictMode>
<App />
</StrictMode>,
);
11 changes: 11 additions & 0 deletions playground/emotion-plugin/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react-swc";

export default defineConfig({
plugins: [
react({
jsxImportSource: "@emotion/react",
plugins: [["@swc/plugin-emotion", {}]],
}),
],
});
25 changes: 25 additions & 0 deletions pnpm-lock.yaml

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

1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ import(/* @vite-ignore */ import.meta.url).then((currentExports) => {
? {
name: "vite:react-swc",
apply: "build",
enforce: "pre", // Run before esbuild
transform: (code, _id) =>
transformWithOptions(_id.split("?")[0], code, options, {
useBuiltins: true,
Expand Down

0 comments on commit f52d870

Please sign in to comment.