From 7a31c36c10faf4123ae551609cfd5c30947bbc01 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Sun, 4 Dec 2022 19:51:06 +0800 Subject: [PATCH] Replace react with preact reduce js file bundle to 20% --- web/README.md | 71 +++---------------------------------------- web/build.sh | 4 ++- web/package-lock.json | 38 +++++++++++++++++++++-- web/package.json | 2 +- 4 files changed, 44 insertions(+), 71 deletions(-) diff --git a/web/README.md b/web/README.md index 0c83cde..7c7774f 100644 --- a/web/README.md +++ b/web/README.md @@ -1,70 +1,9 @@ -# Getting Started with Create React App +# MSW Open Music Web Frontend -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). +This is a React single page application. And use Preact instead of React to achieve a smaller file size. -## Available Scripts +`node_modules` only has 19M. We uses esbuild and shell scripts and build only takes a milliseconds! -In the project directory, you can run: +## How to build -### `npm start` - -Runs the app in the development mode.\ -Open [http://localhost:3000](http://localhost:3000) to view it in the browser. - -The page will reload if you make edits.\ -You will also see any lint errors in the console. - -### `npm test` - -Launches the test runner in the interactive watch mode.\ -See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. - -### `npm run build` - -Builds the app for production to the `build` folder.\ -It correctly bundles React in production mode and optimizes the build for the best performance. - -The build is minified and the filenames include the hashes.\ -Your app is ready to be deployed! - -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. - -### `npm run eject` - -**Note: this is a one-way operation. Once you `eject`, you can’t go back!** - -If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project. - -Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own. - -You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it. - -## Learn More - -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). - -To learn React, check out the [React documentation](https://reactjs.org/). - -### Code Splitting - -This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting) - -### Analyzing the Bundle Size - -This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size) - -### Making a Progressive Web App - -This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app) - -### Advanced Configuration - -This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration) - -### Deployment - -This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment) - -### `npm run build` fails to minify - -This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify) +Simple run `./build.sh`, then all output files are under `./build/` directory. diff --git a/web/build.sh b/web/build.sh index fb99515..2eda74b 100755 --- a/web/build.sh +++ b/web/build.sh @@ -1,4 +1,6 @@ rm -rf build cp -raf public build -./node_modules/.bin/esbuild src/index.jsx --bundle --minify --outfile=build/msw-open-music.js --sourcemap +./node_modules/.bin/esbuild src/index.jsx --bundle --outfile=build/msw-open-music.js --alias:react=preact/compat --alias:react-dom=preact/compat --minify --analyze cat public/index.html | sed "s/%PUBLIC_URL%/$PUBLIC_URL/" > build/index.html + +echo "Build done, output files under ./build directory" diff --git a/web/package-lock.json b/web/package-lock.json index 3d1b15c..e9f950a 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -8,8 +8,8 @@ "name": "msw-open-music-react", "version": "1.2.0", "dependencies": { + "@preact/compat": "^17.1.2", "esbuild": "^0.15.17", - "react": "^18.2.0", "react-router-dom": "^6.4.4", "water.css": "^2.1.1" } @@ -44,6 +44,14 @@ "node": ">=12" } }, + "node_modules/@preact/compat": { + "version": "17.1.2", + "resolved": "https://registry.npmmirror.com/@preact/compat/-/compat-17.1.2.tgz", + "integrity": "sha512-7pOZN9lMDDRQ+6aWvjwTp483KR8/zOpfS83wmOo3zfuLKdngS8/5RLbsFWzFZMGdYlotAhX980hJ75bjOHTwWg==", + "peerDependencies": { + "preact": "*" + } + }, "node_modules/@remix-run/router": { "version": "1.0.4", "resolved": "https://registry.npmmirror.com/@remix-run/router/-/router-1.0.4.tgz", @@ -391,12 +399,14 @@ "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmmirror.com/js-tokens/-/js-tokens-4.0.0.tgz", - "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", + "peer": true }, "node_modules/loose-envify": { "version": "1.4.0", "resolved": "https://registry.npmmirror.com/loose-envify/-/loose-envify-1.4.0.tgz", "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", + "peer": true, "dependencies": { "js-tokens": "^3.0.0 || ^4.0.0" }, @@ -404,10 +414,17 @@ "loose-envify": "cli.js" } }, + "node_modules/preact": { + "version": "10.11.3", + "resolved": "https://registry.npmmirror.com/preact/-/preact-10.11.3.tgz", + "integrity": "sha512-eY93IVpod/zG3uMF22Unl8h9KkrcKIRs2EGar8hwLZZDU1lkjph303V9HZBwufh2s736U6VXuhD109LYqPoffg==", + "peer": true + }, "node_modules/react": { "version": "18.2.0", "resolved": "https://registry.npmmirror.com/react/-/react-18.2.0.tgz", "integrity": "sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==", + "peer": true, "dependencies": { "loose-envify": "^1.1.0" }, @@ -486,6 +503,12 @@ "integrity": "sha512-IA1O7f7qxw2DX8oqTpugHElr926phs7Rq8ULXleBMk4go5K05BU0mI8BfCkWcYAvcmVaMc13bv5W3LIUlU6Y9w==", "optional": true }, + "@preact/compat": { + "version": "17.1.2", + "resolved": "https://registry.npmmirror.com/@preact/compat/-/compat-17.1.2.tgz", + "integrity": "sha512-7pOZN9lMDDRQ+6aWvjwTp483KR8/zOpfS83wmOo3zfuLKdngS8/5RLbsFWzFZMGdYlotAhX980hJ75bjOHTwWg==", + "requires": {} + }, "@remix-run/router": { "version": "1.0.4", "resolved": "https://registry.npmmirror.com/@remix-run/router/-/router-1.0.4.tgz", @@ -643,20 +666,29 @@ "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmmirror.com/js-tokens/-/js-tokens-4.0.0.tgz", - "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==" + "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", + "peer": true }, "loose-envify": { "version": "1.4.0", "resolved": "https://registry.npmmirror.com/loose-envify/-/loose-envify-1.4.0.tgz", "integrity": "sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==", + "peer": true, "requires": { "js-tokens": "^3.0.0 || ^4.0.0" } }, + "preact": { + "version": "10.11.3", + "resolved": "https://registry.npmmirror.com/preact/-/preact-10.11.3.tgz", + "integrity": "sha512-eY93IVpod/zG3uMF22Unl8h9KkrcKIRs2EGar8hwLZZDU1lkjph303V9HZBwufh2s736U6VXuhD109LYqPoffg==", + "peer": true + }, "react": { "version": "18.2.0", "resolved": "https://registry.npmmirror.com/react/-/react-18.2.0.tgz", "integrity": "sha512-/3IjMdb2L9QbBdWiW5e3P2/npwMBaU9mHCSCUzNln0ZCYbcfTsGbTJrU/kGemdH2IWmB2ioZ+zkxtmq6g09fGQ==", + "peer": true, "requires": { "loose-envify": "^1.1.0" } diff --git a/web/package.json b/web/package.json index e6e9f20..3dd175f 100644 --- a/web/package.json +++ b/web/package.json @@ -3,8 +3,8 @@ "version": "1.2.0", "private": true, "dependencies": { + "@preact/compat": "^17.1.2", "esbuild": "^0.15.17", - "react": "^18.2.0", "react-router-dom": "^6.4.4", "water.css": "^2.1.1" },