-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: enforce "pre" for builds with SWC plugins (fixes #56)
- Loading branch information
1 parent
8ad1f6c
commit f52d870
Showing
13 changed files
with
252 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
playground/emotion-plugin/__tests__/emotion-plugin.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", {}]], | ||
}), | ||
], | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters