Rewrite React and ReactDOM in Typescript.
This project was bootstrapped with Create React App.
- Uninstall
react-dom
because I will use my own implementation. I tried to uninstallreact
, but I added it back because I decided to use react types for API requirements. For example, I simply reuse the types ofReact.ReactElement
,React.ReactNode
,React.JSX.IntrinsicElements
, rather than define my own versions. This allows me to focus on the core implementation and also guarantees that my code is compatible withreact
. - In this project, we need to transform JSX into JS and replace the default
React.createElement
withMyReact.createElement
. Here I use babel-plugin-transform-react-jsx. To customize babel beyond the default config ofcreate-react-app
, I found a workaround here.- First
yarn eject
. - Then edit
config/webpack.config.js
by adding the plugin into the the following section (around line 390).
- First
{
test: /\.(js|mjs|jsx|ts|tsx)$/,
include: paths.appSrc,
loader: require.resolve('babel-loader'),
options: {
customize: require.resolve(
'babel-preset-react-app/webpack-overrides'
),
plugins: [
[
require.resolve('babel-plugin-named-asset-import'),
{
loaderMap: {
svg: {
ReactComponent:
'@svgr/webpack?-svgo,+titleProp,+ref![path]',
},
},
},
],
[ // Add plugin here
"transform-react-jsx",
{
"pragma": "MyReact.createElement" // default pragma is React.createElement
}
],
isEnvDevelopment &&
shouldUseReactRefresh &&
require.resolve('react-refresh/babel'),
].filter(Boolean),
-
Similarly, in the testing, we also need to transform JSX with JS and use
MyReact.createElement
. Since this project is usingjest
withbabel-jest
, I was inspired by this issue, and created my own transformer forbabel-jest
. The code is put underconfig/jest/jsxTransform.js
. Then, insidepackage.json
, replace thebabel-jest
transform as below:"jest": { ... "transform": { // "^.+\\.(js|jsx|mjs|cjs|ts|tsx)$": "<rootDir>/node_modules/babel-jest", "^.+\\.(js|jsx|ts|tsx)$": "<rootDir>/config/jest/jsxTransform.js", ... } ... }
-
The webpage does not reload on file change in Windows Linux. I checked this StackOverflow and this Reddit Create React App live reload not working on Ubuntu-18.04, none was working. Then I updated the
injectClient
from false to true inconfig/webpackDevServer.config.js
. Now auto-reload is working. When I checked the compiled code, I noticed that0.chunk.js
has added about 6000 lines of code ofWebsocketClient.js
, compared to the old version when theinjectClient
is false.
In the project directory, you can run:
Runs the app in the development mode.
Open 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.
Launches the test runner in the interactive watch mode.
See the section about running tests for more information.
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 for more information.