Template for a project with React, TypeScript, SSR, and HMR. The template implies the use of CSS Modules and Stylus.
To copy the template in an empty local folder without creating of a repo or in an existing repo use the following:
npx degit Kallenju/template-for-react-ts-ssr-hmr
The ./dist/
folder is the root of the site. It is created by Webpack and contains the result of the build process. To start the development servers, run the following command:
npm install
npm run dev
- __tests__
- bin
- config
- dist
- src
- client
- fonts
- images
- styles
- server
- shared
- Header
- Layout
- assets
- styles
- body
- html
- root
- styles
- client
- server
- shared
- Header
- Layout
- static
- images
- fonts
- styles
- types
- views
./src
contains App.tsx
, which is the entry point of the React app. It imports components from ./src/shared
and global styles from ./assets/styles/
. App.tsx
is in turn imported by server (server.tsx
) and client (index.tsx
) files in ./src/server/
and ./src/client/
, respectively.
The app.get()
method in server code (./src/server/server.tsx
) specifies a callback function that will render to string React component from App.tsx
and past it in a template (./src/views/index.tsx
), whenever there is an HTTP GET request with a path (/
) relative to the site root.
The server code use modern React method renderToPipeableStream
to render React components to a stream. It is similar to renderToString
, but it returns a Node.js Readable stream instead of a string. This is useful for sending the rendered HTML to the client in chunks.
The client code hydrate
obtained React root. The hydrate
method is used to render React components on the client side. It is similar to render
, but it will attach event listeners to the existing markup instead of replacing it.
In the App.tsx
component, React.Suspense
component and React.lazy
method can be used to load components asynchronously. The React.Suspense
component is used to display a fallback component while waiting for the asynchronous component to load. The React.lazy
function is used to load components asynchronously.
Hot Module Replacement is one of the most useful features offered by webpack. It allows all kinds of modules or files to be updated at runtime without the need for a full refresh. This includes CSS and JS files, as well as static assets that are part of the dependency graph. When a file is changed, the module is replaced, and the application code is re-executed. During such module replacement a component's state is preserved.
After each re-building webpack plugin fork-ts-checker-webpack-plugin
checks the typescript code for errors.
All files in ./src/static/
are copied to the ./dist/client
folder without changes. For example, the ./src/static/images
folder will be copied to ./dist/client/images
. All files in ./src/static
are accessible through the virtual prefix /static
. For example, the ./src/static/images/raster/favicon/favicon.png
file will be available at the address http://localhost:3000/static/images/raster/favicon/favicon.png
=== /static/images/raster/favicon/favicon.png
.
Images in ./src/static/images
are optimized by Webpack. For images with jpeg
or jpg
or png
extentions, the image-minimizer-webpack-plugin
and sharp
generate a WebP version of the images.
The browserslist
is used by babel and postcss to compile code for the specified browsers. The list of browsers is specified in the package.json
file.
It is possible to write snapshot tests (using jest
and react-test-renderer
) and tests for React components (using the react-testing-library
library).
The template contains a simple tests for the Header
component. The test is located in the ./src/__tests__/Header.test.tsx
file.
The dotenv
package is used to load environment variables from a .env
file into process.env
. The .env
files is located in the root folder of the project.
- Express server
- Nodemon
- Dotenv
- Express server
- react-refresh-webpack-plugin
- React Fast Refresh
- Webpack modules and plugins (webpack-dev-middleware, webpack-hot-middleware)
- Webpack plugins (webpack.HotModuleReplacementPlugin)
- Dotenv
- Webpack loader (babel-loader)
- Webpack plugin (fork-ts-checker-webpack-plugin)
- browserslist
- Dotenv
- Stylus
- postcss
- Webpack loaders (mini-css-extract-plugin, css-loader, postcss-loader, style-loader)
- browserslist
- Dotenv
- Webpack plugin (image-minimizer-webpack-plugin)
- Sharp
- Prettier
- ESLint
- Jest
- React Test Renderer
- React Testing Library
- Dotenv