Skip to content

Commit

Permalink
Merge pull request #68 from sky172839465/pkg
Browse files Browse the repository at this point in the history
chore: upgrade from CRA to vite react
  • Loading branch information
sky172839465 authored Nov 17, 2024
2 parents 194c988 + c09e90d commit 75dafd1
Show file tree
Hide file tree
Showing 7 changed files with 9,053 additions and 8,233 deletions.
2 changes: 1 addition & 1 deletion public/index.html → index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<script type="module" src="src/index"></script></head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
Expand Down
17,032 changes: 8,828 additions & 8,204 deletions package-lock.json

Large diffs are not rendered by default.

63 changes: 35 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,49 @@
"dependencies": {
"@babel/plugin-proposal-private-property-in-object": "^7.21.11",
"lodash": "^4.17.21",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
"web-vitals": "^4.2.4"
},
"devDependencies": {
"@codeceptjs/allure-legacy": "^1.0.2",
"@flydotio/dockerfile": "^0.5.0",
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"@wdio/sauce-service": "^9.1.1",
"allure-commandline": "^2.13.8",
"@flydotio/dockerfile": "^0.5.9",
"@testing-library/jest-dom": "^6.6.3",
"@testing-library/react": "^16.0.1",
"@testing-library/user-event": "^14.5.2",
"@wdio/sauce-service": "^9.2.14",
"allure-commandline": "^2.32.0",
"codeceptjs": "^3.6.7",
"cross-env": "^7.0.3",
"debug": "^4.3.1",
"eslint": "^7.24.0",
"eslint-plugin-jsx-a11y": "^6.4.1",
"eslint-plugin-react": "^7.23.2",
"husky": "^6.0.0",
"jest-junit": "^12.0.0",
"lint-staged": "^10.5.4",
"mocha-multi": "^1.1.3",
"mochawesome": "^4.1.0",
"debug": "^4.3.7",
"eslint": "^9.15.0",
"eslint-plugin-jsx-a11y": "^6.10.2",
"eslint-plugin-react": "^7.37.2",
"husky": "^9.1.6",
"jest-junit": "^16.0.0",
"lint-staged": "^15.2.10",
"mocha-multi": "^1.1.7",
"mochawesome": "^7.1.3",
"npm-check-updates": "^17.1.11",
"npm-run-all": "^4.1.5",
"prettier": "^2.2.1",
"prettier-plugin-sh": "^0.6.1",
"prettier": "^3.3.3",
"prettier-plugin-sh": "^0.14.0",
"saucelabs": "^8.0.0",
"source-map-explorer": "^2.5.2",
"stylelint": "^13.12.0",
"stylelint-config-standard": "^21.0.0",
"webdriverio": "^6.12.1",
"xml2js": "^0.5.0"
"source-map-explorer": "^2.5.3",
"stylelint": "^16.10.0",
"stylelint-config-standard": "^36.0.1",
"webdriverio": "^9.2.14",
"xml2js": "^0.6.2",
"@svgr/core": "^8.1.0",
"@svgr/plugin-jsx": "^8.1.0",
"@vitejs/plugin-react": "^4.3.1",
"vite": "^5.2.13",
"vite-tsconfig-paths": "^4.3.2"
},
"scripts": {
"start": "REACT_APP_VERSION=$(npm pkg get version) react-scripts start",
"build": "REACT_APP_VERSION=$(npm pkg get version) react-scripts build",
"start": "REACT_APP_VERSION=$(npm pkg get version) vite",
"build": "REACT_APP_VERSION=$(npm pkg get version) vite build",
"eject": "react-scripts eject",
"prepare": "husky install",
"\n# OUTPUT CLEAN": "",
Expand Down Expand Up @@ -78,7 +84,8 @@
"\n# CI SCRIPTS": "",
"ci-lint": "npm run all-lint > lint.log",
"ci-test": "npm run test:report",
"ci-functional": "cross-env IS_GLOBAL_WEBSITE=1 npm run functional:sauce"
"ci-functional": "cross-env IS_GLOBAL_WEBSITE=1 npm run functional:sauce",
"dev": "vite"
},
"lint-staged": {
"**/*.{js,jsx}": [
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
189 changes: 189 additions & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
import { resolve } from "node:path";
import { readFileSync } from "node:fs";
import { defineConfig, loadEnv, createFilter, transformWithEsbuild } from "vite";
import react from "@vitejs/plugin-react";
import tsconfigPaths from "vite-tsconfig-paths";
// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
setEnv(mode);
return {
plugins: [
react(),
tsconfigPaths(),
envPlugin(),
devServerPlugin(),
sourcemapPlugin(),
buildPathPlugin(),
basePlugin(),
importPrefixPlugin(),
htmlPlugin(mode),
svgrPlugin(),
],
};
});
function setEnv(mode) {
Object.assign(process.env, loadEnv(mode, ".", ["REACT_APP_", "NODE_ENV", "PUBLIC_URL"]));
process.env.NODE_ENV ||= mode;
const { homepage } = JSON.parse(readFileSync("package.json", "utf-8"));
process.env.PUBLIC_URL ||= homepage
? `${homepage.startsWith("http") || homepage.startsWith("/")
? homepage
: `/${homepage}`}`.replace(/\/$/, "")
: "";
}
// Expose `process.env` environment variables to your client code
// Migration guide: Follow the guide below to replace process.env with import.meta.env in your app, you may also need to rename your environment variable to a name that begins with VITE_ instead of REACT_APP_
// https://vitejs.dev/guide/env-and-mode.html#env-variables
function envPlugin() {
return {
name: "env-plugin",
config(_, { mode }) {
const env = loadEnv(mode, ".", ["REACT_APP_", "NODE_ENV", "PUBLIC_URL"]);
return {
define: Object.fromEntries(Object.entries(env).map(([key, value]) => [
`process.env.${key}`,
JSON.stringify(value),
])),
};
},
};
}
// Setup HOST, SSL, PORT
// Migration guide: Follow the guides below
// https://vitejs.dev/config/server-options.html#server-host
// https://vitejs.dev/config/server-options.html#server-https
// https://vitejs.dev/config/server-options.html#server-port
function devServerPlugin() {
return {
name: "dev-server-plugin",
config(_, { mode }) {
const { HOST, PORT, HTTPS, SSL_CRT_FILE, SSL_KEY_FILE } = loadEnv(mode, ".", ["HOST", "PORT", "HTTPS", "SSL_CRT_FILE", "SSL_KEY_FILE"]);
const https = HTTPS === "true";
return {
server: {
host: HOST || "0.0.0.0",
port: parseInt(PORT || "3000", 10),
open: true,
...(https &&
SSL_CRT_FILE &&
SSL_KEY_FILE && {
https: {
cert: readFileSync(resolve(SSL_CRT_FILE)),
key: readFileSync(resolve(SSL_KEY_FILE)),
},
}),
},
};
},
};
}
// Migration guide: Follow the guide below
// https://vitejs.dev/config/build-options.html#build-sourcemap
function sourcemapPlugin() {
return {
name: "sourcemap-plugin",
config(_, { mode }) {
const { GENERATE_SOURCEMAP } = loadEnv(mode, ".", [
"GENERATE_SOURCEMAP",
]);
return {
build: {
sourcemap: GENERATE_SOURCEMAP === "true",
},
};
},
};
}
// Migration guide: Follow the guide below
// https://vitejs.dev/config/build-options.html#build-outdir
function buildPathPlugin() {
return {
name: "build-path-plugin",
config(_, { mode }) {
const { BUILD_PATH } = loadEnv(mode, ".", [
"BUILD_PATH",
]);
return {
build: {
outDir: BUILD_PATH || "build",
},
};
},
};
}
// Migration guide: Follow the guide below and remove homepage field in package.json
// https://vitejs.dev/config/shared-options.html#base
function basePlugin() {
return {
name: "base-plugin",
config(_, { mode }) {
const { PUBLIC_URL } = loadEnv(mode, ".", ["PUBLIC_URL"]);
return {
base: PUBLIC_URL || "",
};
},
};
}
// To resolve modules from node_modules, you can prefix paths with ~
// https://create-react-app.dev/docs/adding-a-sass-stylesheet
// Migration guide: Follow the guide below
// https://vitejs.dev/config/shared-options.html#resolve-alias
function importPrefixPlugin() {
return {
name: "import-prefix-plugin",
config() {
return {
resolve: {
alias: [{ find: /^~([^/])/, replacement: "$1" }],
},
};
},
};
}
// In Create React App, SVGs can be imported directly as React components. This is achieved by svgr libraries.
// https://create-react-app.dev/docs/adding-images-fonts-and-files/#adding-svgs
function svgrPlugin() {
const filter = createFilter("**/*.svg");
const postfixRE = /[?#].*$/s;
return {
name: "svgr-plugin",
async transform(code, id) {
if (filter(id)) {
const { transform } = await import("@svgr/core");
const { default: jsx } = await import("@svgr/plugin-jsx");
const filePath = id.replace(postfixRE, "");
const svgCode = readFileSync(filePath, "utf8");
const componentCode = await transform(svgCode, undefined, {
filePath,
caller: {
previousExport: code,
defaultPlugins: [jsx],
},
});
const res = await transformWithEsbuild(componentCode, id, {
loader: "jsx",
});
return {
code: res.code,
map: null,
};
}
},
};
}
// Replace %ENV_VARIABLES% in index.html
// https://vitejs.dev/guide/api-plugin.html#transformindexhtml
// Migration guide: Follow the guide below, you may need to rename your environment variable to a name that begins with VITE_ instead of REACT_APP_
// https://vitejs.dev/guide/env-and-mode.html#html-env-replacement
function htmlPlugin(mode) {
const env = loadEnv(mode, ".", ["REACT_APP_", "NODE_ENV", "PUBLIC_URL"]);
return {
name: "html-plugin",
transformIndexHtml: {
order: "pre",
handler(html) {
return html.replace(/%(.*?)%/g, (match, p1) => env[p1] ?? match);
},
},
};
}

0 comments on commit 75dafd1

Please sign in to comment.