-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add preact example, remove optimizer experimental status, enabl…
…e by default (#3854) Co-authored-by: eryue0220 <eryue0220@gmail.com>
- Loading branch information
1 parent
b092985
commit 4b946ca
Showing
21 changed files
with
717 additions
and
133 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
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,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Preact Example</title> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
<script type="module" src="src/main.tsx"></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,28 @@ | ||
{ | ||
"name": "@vitest/example-preact-testing-lib", | ||
"private": true, | ||
"scripts": { | ||
"build": "tsc && vite build", | ||
"coverage": "vitest run --coverage", | ||
"dev": "vite", | ||
"preview": "vite preview", | ||
"test": "vitest", | ||
"test:ui": "vitest --ui" | ||
}, | ||
"dependencies": { | ||
"preact": "^10.15.1", | ||
"react": "npm:@preact/compat", | ||
"react-dom": "npm:@preact/compat", | ||
"react-router-dom": "^6.3.0" | ||
}, | ||
"devDependencies": { | ||
"@preact/preset-vite": "^2.5.0", | ||
"@testing-library/jest-dom": "^5.16.4", | ||
"@testing-library/preact": "^3.2.3", | ||
"@vitest/ui": "latest", | ||
"less": "^4.1.3", | ||
"typescript": "^4.8.4", | ||
"vite": "latest", | ||
"vitest": "latest" | ||
} | ||
} |
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,23 @@ | ||
.app { | ||
text-align: center; | ||
|
||
header { | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
justify-content: center; | ||
min-height: 100vh; | ||
background-color: #282c34; | ||
font-size: calc(10px + 2vmin); | ||
color: white; | ||
|
||
.app-link { | ||
color: white; | ||
text-decoration: none; | ||
|
||
&:hover { | ||
color: #747bff; | ||
} | ||
} | ||
} | ||
} |
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,18 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { fireEvent, render, screen } from '@testing-library/preact' | ||
import { BrowserRouter } from 'react-router-dom' | ||
|
||
import App from './App' | ||
|
||
describe('Preact Demo Test Suite', () => { | ||
it('basic', () => { | ||
render(<BrowserRouter><App /></BrowserRouter>) | ||
expect(screen.getByText(/Hello Vite & Preact!/i)).toBeInTheDocument() | ||
}) | ||
|
||
it('click event', async () => { | ||
render(<BrowserRouter><App /></BrowserRouter>) | ||
fireEvent.click(screen.getByRole('button')) | ||
expect(await screen.findByText(/count is: 1/i)).toBeInTheDocument() | ||
}) | ||
}) |
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,36 @@ | ||
import { useCount } from './hooks/useCount' | ||
import './App.less' | ||
|
||
export default function App() { | ||
const { count, inc } = useCount() | ||
|
||
return ( | ||
<div class="app"> | ||
<header> | ||
<h1>Hello Vite & Preact!</h1> | ||
<p> | ||
<button onClick={inc}>Count is: {count}</button> | ||
</p> | ||
<p> | ||
<a | ||
className="app-link" | ||
href="https://preactjs.com/" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Learn Preact | ||
</a> | ||
{' | '} | ||
<a | ||
className="app-link" | ||
href="https://vitejs.dev/guide/features.html" | ||
target="_blank" | ||
rel="noopener noreferrer" | ||
> | ||
Vite Docs | ||
</a> | ||
</p> | ||
</header> | ||
</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,12 @@ | ||
import { act, renderHook } from '@testing-library/preact' | ||
import { useCount } from './useCount' | ||
|
||
describe('useCount hook', () => { | ||
it('should increment', () => { | ||
const { result } = renderHook(() => useCount()) | ||
act(() => { | ||
result.current.inc() | ||
}) | ||
expect(result.current.count).toBe(1) | ||
}) | ||
}) |
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 { useCallback, useState } from 'preact/hooks' | ||
|
||
export function useCount() { | ||
const [count, setCount] = useState(0) | ||
const inc = useCallback(() => setCount(x => x + 1), []) | ||
return { | ||
count, | ||
inc, | ||
} | ||
} |
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 @@ | ||
body { | ||
margin: 0; | ||
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', | ||
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', | ||
sans-serif; | ||
-webkit-font-smoothing: antialiased; | ||
-moz-osx-font-smoothing: grayscale; | ||
} | ||
|
||
code { | ||
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', | ||
monospace; | ||
} |
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,5 @@ | ||
import { render } from 'preact' | ||
import App from './App' | ||
import './main.less' | ||
|
||
render(<App />, document.getElementById('root')) |
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,5 @@ | ||
import { afterEach } from 'vitest' | ||
import { cleanup } from '@testing-library/preact' | ||
import '@testing-library/jest-dom' | ||
|
||
afterEach(() => cleanup()) |
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 @@ | ||
{ | ||
"compilerOptions": { | ||
"target": "ESNext", | ||
"useDefineForClassFields": true, | ||
"lib": ["DOM", "DOM.Iterable", "ESNext"], | ||
"allowJs": false, | ||
"skipLibCheck": true, | ||
"esModuleInterop": false, | ||
"allowSyntheticDefaultImports": true, | ||
"strict": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "ESNext", | ||
"moduleResolution": "Node", | ||
"resolveJsonModule": true, | ||
"isolatedModules": true, | ||
"noEmit": true, | ||
"jsx": "preserve", | ||
"jsxFactory": "h", | ||
"jsxFragmentFactory": "Fragment", | ||
"types": ["vitest/globals"] | ||
}, | ||
"include": ["src"], | ||
"references": [{ "path": "./tsconfig.node.json" }] | ||
} |
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,9 @@ | ||
{ | ||
"compilerOptions": { | ||
"composite": true, | ||
"module": "ESNext", | ||
"moduleResolution": "Node", | ||
"allowSyntheticDefaultImports": true | ||
}, | ||
"include": ["vite.config.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,20 @@ | ||
/// <reference types="vitest" /> | ||
/// <reference types="vite/client" /> | ||
|
||
import { defineConfig } from 'vite' | ||
import preact from '@preact/preset-vite' | ||
|
||
export default defineConfig({ | ||
plugins: [preact()], | ||
resolve: { | ||
// react-router-dom specifies "module" field in package.json for ESM entry | ||
// if it's not mapped, it uses the "main" field which is CommonJS that redirects to CJS preact | ||
mainFields: ['module'], | ||
}, | ||
test: { | ||
globals: true, | ||
environment: 'jsdom', | ||
setupFiles: './test/setup.ts', | ||
css: true, | ||
}, | ||
}) |
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
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
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
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
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
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
Oops, something went wrong.