diff --git a/with-preact/App.js b/with-preact/App.js new file mode 100644 index 0000000..fd92fe8 --- /dev/null +++ b/with-preact/App.js @@ -0,0 +1,19 @@ +import React from 'react'; +import { StyleSheet, Text, View } from 'react-native'; + +export default function App() { + return ( + + Demo Preact + Expo web + + ); +} + +const styles = StyleSheet.create({ + container: { + flex: 1, + backgroundColor: '#fff', + alignItems: 'center', + justifyContent: 'center', + }, +}); diff --git a/with-preact/README.md b/with-preact/README.md new file mode 100644 index 0000000..404d142 --- /dev/null +++ b/with-preact/README.md @@ -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** + +![expo web bundle without preact](.gh-assets/before-preact.png "Expo web bundle without Preact") + +> 💡 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** + +![expo web bundle with preact](.gh-assets/after-preact.png "Expo web bundle with Preact") + +### ⚽️ 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. diff --git a/with-preact/app.json b/with-preact/app.json new file mode 100644 index 0000000..eff109c --- /dev/null +++ b/with-preact/app.json @@ -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" + } + } +} diff --git a/with-preact/babel.config.js b/with-preact/babel.config.js new file mode 100644 index 0000000..2900afe --- /dev/null +++ b/with-preact/babel.config.js @@ -0,0 +1,6 @@ +module.exports = function(api) { + api.cache(true); + return { + presets: ['babel-preset-expo'], + }; +}; diff --git a/with-preact/package.json b/with-preact/package.json new file mode 100644 index 0000000..45a7cf2 --- /dev/null +++ b/with-preact/package.json @@ -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" + } +} diff --git a/with-preact/webpack.config.js b/with-preact/webpack.config.js new file mode 100644 index 0000000..8ee966d --- /dev/null +++ b/with-preact/webpack.config.js @@ -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; +};