Skip to content

Commit

Permalink
chore: add preact example (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
hardfist authored Jan 11, 2024
1 parent 46c73e1 commit 57ca4bf
Show file tree
Hide file tree
Showing 8 changed files with 200 additions and 0 deletions.
12 changes: 12 additions & 0 deletions rspack/preact/index.html
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 http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<div id="root"></div>
</body>
</html>
32 changes: 32 additions & 0 deletions rspack/preact/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"name": "example-react",
"version": "1.0.0",
"description": "",
"main": "index.js",
"private": true,
"scripts": {
"dev": "rspack serve",
"build": "rspack build"
},
"keywords": [],
"author": "",
"license": "MIT",
"dependencies": {
"@rspack/cli": "latest",
"@swc/helpers": "0.5.1",
"preact": "10.19.3"
},
"devDependencies": {
"@rspack/core": "latest",
"@prefresh/babel-plugin": "0.5.1",
"@prefresh/webpack": "4.0.0"
},
"resolve": {
"alias": {
"react": "preact/compat",
"react-dom/test-utils": "preact/test-utils",
"react-dom": "preact/compat",
"react/jsx-runtime": "preact/jsx-runtime"
}
}
}
59 changes: 59 additions & 0 deletions rspack/preact/rspack.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const rspack = require('@rspack/core');
const dev = process.env.NODE_ENV === 'development';
/** @type {import('@rspack/cli').Configuration} */
const config = {
entry: {
main: './src/index.jsx',
},
resolve: {
extensions: ['...', '.ts', '.tsx', '.jsx'],
alias: {
react: 'preact/compat',
'react-dom/test-utils': 'preact/test-utils',
'react-dom': 'preact/compat', // Must be below test-utils
'react/jsx-runtime': 'preact/jsx-runtime',
},
},
module: {
rules: [
{
test: /\.jsx$/,
use: {
loader: 'builtin:swc-loader',
options: {
sourceMap: true,
jsc: {
parser: {
syntax: 'ecmascript',
jsx: true,
},
externalHelpers: true,
preserveAllComments: false,
transform: {
react: {
runtime: 'automatic',
pragma: 'h',
pragmaFrag: 'Fragment',
throwIfNamespace: true,
useBuiltins: false,
},
},
},
},
},
type: 'javascript/auto',
},
{
test: /\.(png|svg|jpg)$/,
type: 'asset/resource',
},
],
},
plugins: [
new rspack.HtmlRspackPlugin({
template: './index.html',
}),
dev && new rspack.HotModuleReplacementPlugin()
].filter(Boolean),
};
module.exports = config;
38 changes: 38 additions & 0 deletions rspack/preact/src/App.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.App {
text-align: center;
}

.App-logo {
height: 40vmin;
pointer-events: none;
}

@media (prefers-reduced-motion: no-preference) {
.App-logo {
animation: App-logo-spin infinite 20s linear;
}
}

.App-header {
background-color: #282c34;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
font-size: calc(10px + 2vmin);
color: white;
}

.App-link {
color: #61dafb;
}

@keyframes App-logo-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
36 changes: 36 additions & 0 deletions rspack/preact/src/App.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import React from 'react';
import { useState, useEffect } from 'react';
import logo from './logo.svg';
import './App.css';

function App() {
const [count, setCount] = useState(1);
useEffect(() => {
setInterval(() => {
setCount(x => x+1);
},1000)
},[])
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<p>
Edit <code>src/App.js</code> and save to reload.
</p>
<div>
count: {count}
</div>
<a
className="App-link"
href="https://reactjs.org"
target="_blank"
rel="noopener noreferrer"
>
Learn React
</a>
</header>
</div>
);
}

export default App;
13 changes: 13 additions & 0 deletions rspack/preact/src/index.css
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;
}
9 changes: 9 additions & 0 deletions rspack/preact/src/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import './index.css';
import App from './App';

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<App />
);
1 change: 1 addition & 0 deletions rspack/preact/src/logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 57ca4bf

Please sign in to comment.