A React project built with webpack. It features TypeScript and ts-loader
.
The surface area of popular software composition options and tooling (e.g. runtime libraries, programming languages, and build tools) for web applications is perhaps the largest across all types of software projects in the world. There is a combinatorial explosion of options: JS/TS, JSX, webpack/esbuild/babel/vite etc. What's incredible is how all of these options are viable and sometimes mainstream! In other software ecosystems, there are only a couple dominant options and other niche options.
This subproject serves as an executable example and knowledge reference for my own sake so that I have at least one "working and well-understood" setup for a React project. I'm eager to explore many of the different options, and this is just one of them.
Here are some elements I need to remember about "how to create a React + TypeScript project":
- Install the DefinitelyTyped type definitions for each library
-
npm install --save-dev @types/react @types/react-dom
-
- Use
allowSyntheticDefaultImports: true
in the TypeScript config- StackOverflow answer: use
allowSyntheticDefaultImports: true
in React projects - I don't know how this works, so I really need the reminder.
- StackOverflow answer: use
- webpack is probably the safest option.
- webpack is safest because it's "Google-ability" is strongest, the docs are extensive, and the feature set is huge.
Follow these instructions to build and run the project.
- Pre-requisite: Node.js
- I used version 20.17.0
- Install dependencies
-
npm install
-
- Build the project:
-
npm run build
-
- Alternatively, build the project continuously and serve the output:
-
npm start
- The project is ready to be viewed in the browser! The browser should open automatically.
-
General clean-ups, todos and things I wish to implement for this project:
- DONE Actually implement some React
- DONE Do something with hooks
- Get the tsconfig.json right. I need to use the React preset, or whatever.
- What is the idiomatic way to do CSS in React? Do you just import the "styles.css" file? Is there not really a react way to do it? Do you somehow scope the CSS to the React element, like you would with web components?
- DONE (it's what I thought, ideally you should express the dependencies correctly. But technically it doesn't matter if you aren't publishing anything) What's the deal with
devDependencies
the block vs thedependencies
block these days? I noticed acreate-react-app
scaffolded repo only usesdependencies
but I thought build tools and test frameworks were supposed to go indevDependencies
?- I needed this:
npm install --save-dev @types/react @types/react-dom
- I needed this:
- Pare down
webpack.config.js
to the required minimum. I'm not sure theresolve
andmodule.rules
are correct. - Pare down
tsconfig.json
to the required minimum. I'd like to understandallowSyntheticDefaultImports
. - DONE Use async/await instead of promises where feasible.
- Use React "suspense" for the mocked data loading.
Note, React doesn't actually support suspense for data fetchin yet. The docs read:
In the future we plan to let Suspense handle more scenarios such as data fetching.
- DONE Upgrade to latest webpack and other deps
- DONE Debug logging.
- Another "webpack" playground project of mine
- I mostly copied
react-playground/webpack
from the above project.
- I mostly copied
- TypeScript "Module Resolution"
- StackOverflow answer: use
allowSyntheticDefaultImports: true
in React projects- I don't know how this works exactly. But people seem to need this in TypeScript + React projects. I certainly did.