mirror of
https://github.com/zhigang1992/examples.git
synced 2026-01-12 09:03:42 +08:00
Created preact example
This commit is contained in:
19
with-preact/App.js
Normal file
19
with-preact/App.js
Normal file
@@ -0,0 +1,19 @@
|
||||
import React from 'react';
|
||||
import { StyleSheet, Text, View } from 'react-native';
|
||||
|
||||
export default function App() {
|
||||
return (
|
||||
<View style={styles.container}>
|
||||
<Text>Demo Preact + Expo web</Text>
|
||||
</View>
|
||||
);
|
||||
}
|
||||
|
||||
const styles = StyleSheet.create({
|
||||
container: {
|
||||
flex: 1,
|
||||
backgroundColor: '#fff',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
},
|
||||
});
|
||||
56
with-preact/README.md
Normal file
56
with-preact/README.md
Normal file
@@ -0,0 +1,56 @@
|
||||
# Preact Example
|
||||
|
||||
> 💡 The most updated info is in the Expo docs: [Using Preact](https://github.com/expo/expo/blob/master/docs/pages/versions/unversioned/guides/using-preact.md)
|
||||
|
||||
[Preact](https://preactjs.com/) is "a fast 3kB alternative to React" with the same modern API as React. Preact was created by [Jason Miller](https://twitter.com/_developit).
|
||||
|
||||
## Before Preact
|
||||
|
||||
If you create a standard Expo web project (at the time of writing this), the bundle size will look something like the report below.
|
||||
|
||||
**❌ 60.75 KB Gzipped**
|
||||
|
||||

|
||||
|
||||
> 💡 You can [**enable** bundle reporting easily!](https://github.com/expo/expo/blob/master/docs/pages/versions/unversioned/guides/web-performance.md#-what-makes-my-app-large)
|
||||
|
||||
## After Preact
|
||||
|
||||
After modifying your project to use Preact instead of React DOM, the bundle will reduce drastically meaning your website will load faster and work much better for poor internet connections.
|
||||
|
||||
**✅ 27.98 KB Gzipped**
|
||||
|
||||

|
||||
|
||||
### ⚽️ Getting Started
|
||||
|
||||
To use Preact with React Native for web, you'll need to install the packages and alias them to React. You may also want to enable **reporting** so you can analyze your bundle size.
|
||||
|
||||
- Install Preact (requires Preact 10+): `yarn add preact-responder-event-plugin preact`
|
||||
- Run `expo customize:web` and select `webpack.config.js`
|
||||
- Modify the webpack config to use Preact instead of React:
|
||||
|
||||
```js
|
||||
const createExpoWebpackConfigAsync = require('@expo/webpack-config');
|
||||
|
||||
module.exports = async function(env, argv) {
|
||||
const config = await createExpoWebpackConfigAsync({
|
||||
...env,
|
||||
// Optionally you can enable the bundle size report
|
||||
report: true
|
||||
}, argv);
|
||||
|
||||
// Add more aliases
|
||||
config.resolve.alias = {
|
||||
...config.resolve.alias,
|
||||
// Use Preact aliases
|
||||
react$: 'preact/compat',
|
||||
'react-dom$': 'preact/compat',
|
||||
// Fix the responder system which react-native-web depends on
|
||||
'react-dom/unstable-native-dependencies$': 'preact-responder-event-plugin',
|
||||
};
|
||||
return config;
|
||||
};
|
||||
```
|
||||
|
||||
- That's it! Running `expo build:web` will now produce a significantly smaller bundle.
|
||||
12
with-preact/app.json
Normal file
12
with-preact/app.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"expo": {
|
||||
"name": "with-preact",
|
||||
"version": "1.0.0",
|
||||
"icon": "https://github.com/expo/expo/blob/master/templates/expo-template-blank/assets/icon.png?raw=true",
|
||||
"splash": {
|
||||
"image": "https://github.com/expo/expo/blob/master/templates/expo-template-blank/assets/splash.png?raw=true",
|
||||
"resizeMode": "contain",
|
||||
"backgroundColor": "#ffffff"
|
||||
}
|
||||
}
|
||||
}
|
||||
6
with-preact/babel.config.js
Normal file
6
with-preact/babel.config.js
Normal file
@@ -0,0 +1,6 @@
|
||||
module.exports = function(api) {
|
||||
api.cache(true);
|
||||
return {
|
||||
presets: ['babel-preset-expo'],
|
||||
};
|
||||
};
|
||||
16
with-preact/package.json
Normal file
16
with-preact/package.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"dependencies": {
|
||||
"expo": "36.0.0",
|
||||
"preact": "10.2.1",
|
||||
"preact-responder-event-plugin": "1.0.14",
|
||||
"react": "16.9.0",
|
||||
"react-dom": "16.9.0",
|
||||
"react-native": "https://github.com/expo/react-native/archive/sdk-36.0.0.tar.gz",
|
||||
"react-native-web": "0.11.7"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "7.0.0",
|
||||
"@expo/webpack-config": "0.10.11",
|
||||
"babel-preset-expo": "8.0.0"
|
||||
}
|
||||
}
|
||||
15
with-preact/webpack.config.js
Normal file
15
with-preact/webpack.config.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const createExpoWebpackConfigAsync = require('@expo/webpack-config');
|
||||
|
||||
module.exports = async function(env, argv) {
|
||||
const config = await createExpoWebpackConfigAsync({ ...env, report: true }, argv);
|
||||
|
||||
config.resolve.alias = {
|
||||
...config.resolve.alias,
|
||||
// Use Preact aliases
|
||||
react$: 'preact/compat',
|
||||
'react-dom$': 'preact/compat',
|
||||
// Fix the responder system which react-native-web depends on
|
||||
'react-dom/unstable-native-dependencies$': 'preact-responder-event-plugin',
|
||||
};
|
||||
return config;
|
||||
};
|
||||
Reference in New Issue
Block a user