- Overview
- Prerequisites
- Installing & getting started
- Windows subsystem for Linux (WSL2)
- Available React configuration
- Available Jest mocks
- Available aliases
- Environments
- Enable CSS modules
- Enable HTTPS in development
- Start the development server without opening the app in the browser
- Enable PWA (production only)
- Configuring prettier
- Site meta tags
- Add Cypress
- Extras
- Code generator
- Available scripts
- Testing using jest, react-testing-library
- SCSS preprocessor
- Autoprefixer for CSS (it supports IE >= 11)
- Hot reloading for JS & CSS and redux store (in development)
- Prettier (for code format)
- Docker setup for development
- Docker setup for production (basic setup using nginx)
- nodeJS > 17.X.X or Docker
- Clone repo =>
git clone git@github.com:DonAdam2/webpack-react-boilerplate.git
- Navigate to project directory
cd webpack-react-boilerplate
- Install dependencies =>
pnpm install
- Start the development server =>
pnpm start
- Clone repo =>
git clone git@github.com:DonAdam2/webpack-react-boilerplate.git
- Navigate to project directory
cd webpack-react-boilerplate
- Install dependencies (required for prettier) =>
pnpm install
- Start the development server =>
docker-compose up web-dev
- If you are using Windows 11 or 10, it's recommended to use WSL2:
- Install WSL on windows
- Install docker on windows
- Open docker desktop app:
- settings => resources => WSL integration => enable required Ubuntu integration
- Setup SSH keys (if you want) on windows and share it with WSL
- Create a new directory in ubuntu:
mkdir workspace cd workspace
- Clone this repo into the selected Ubuntu:
git clone git@github.com:DonAdam2/webpack-react-boilerplate.git
- Install curl in Ubuntu:
sudo apt-get install curl
- Install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
- Install required node version:
nvm install --lts
- Install pnpm globally:
npm install -g pnpm
- Update permissions to be able to install NPM packages
sudo chown -R $(whoami) ~/workspace/webpack-react-boilerplate
- Install packages (for linting purposes):
pnpm install
- Run docker for development:
docker-compose up web-dev
- Update the production section of the Dockerfile to meet your needs
- Run the following command to build your image =>
docker-compose up web-prod
- React router dom v6
- Redux toolkit with Redux logger & Redux devTool
- 2 environments {production: .env, development: .env.development}
- RenderWithRedux => overrides the render method of RTL with redux
- RenderWithReactIntl => overrides the render method of RTL with react-intl provider (if you are using it you need to download it)
- RenderWithRouter => overrides the render method of RTL with react router
- RenderWithProviders => overrides the render method of RTL with all providers
- @/jest => for the jest directory
- @/js => for the JS directory
- @/scss => for the SCSS directory
- @/public => for the public directory, (don't forget to prepend the tilde symbol in scss files):
background-image: url('~@/public/asstes/images/favicon.png');
@font-face { font-family: 'Roboto'; src: url('~@/public/asstes/fonts/Roboto-Regular.ttf'); font-weight: normal; font-style: normal; font-display: swap; }
- Development => .env.development
- Production => .env
- Create a file with the chosen env extension in /environments directory, .e.g
.env.local
- Install env-cmd package
- Update your start script
"start": "env-cmd -f environments/.env.local node scripts/start.js",
- Create a file with the chosen env extension in /environments directory, .e.g
.env.prod
- Install env-cmd package
- Update your build script
"build": "env-cmd -f environments/.env.prod webpack --config buildTools/webpack.prod.js --progress --color"
- Create a file with the chosen env extension in /environments directory, .e.g
.env.staging
- Install env-cmd package
- Add a new build script
"build:staging": "env-cmd -f environments/.env.staging webpack --config buildTools/webpack.prod.js --progress --color"
Please keep in mind that environment variables configured using webpack which means that you need to re-run the corresponding environment script (pnpm start, pnpm build) if you update the environment file.
- Open /buildTools/constants.js and set isCssModules to true
- Then in every component add the required import as follows:
import classes from './requiredStyles.scss'
- Use it as follows:
<div className={classes.wrapper} />
- Note:
- In development class names will be in (componentName_class-name) format => e.g.
box_box-wrapper
- In production class names will be in (letter_letter) unique format to reduce the bundle size => e.g.
a_i
- In development class names will be in (componentName_class-name) format => e.g.
- Note:
Add set HTTPS=true
to pnpm start
script => "start": "set HTTPS=true && node scripts/start.js"
Add set BROWSER=none
to pnpm start
script => "start": "set BROWSER=none && node scripts/start.js"
-
Run the given script to add the required files for progressive web app
-
Install the following packages:
pnpm add -D workbox-webpack-plugin workbox-cacheable-response workbox-core workbox-expiration workbox-precaching workbox-routing workbox-strategies workbox-window webpack-pwa-manifest
-
Add pwa directory
/public/assets/images/pwa
, then add your PWA images using the following names:- icon_192x192.png
- icon_256x256.png
- icon_384x384.png
- icon_512x512.png
-
Open
/buildTools/webpack.prod.js
file and update manifest data inWebpackPwaManifest
plugin:- start_url (indicates what page should launch when someone opens your progressive web app.)
- theme_color (This sometimes affects how the OS displays the site (e.g., on Android's task switcher, the theme color surrounds the site).)
- background_color (tells the browser what color to use on the startup splash screen that users will see when they launch your app.)
- name
- short_name
- description
- orientation (you can enforce the orientation of your app, it can be ommited.)
- categories (This is intended to be used by app stores to categorize your app.)
-
Update cache APIs section as needed in
/src/serviceWorker/swSource
file -
You are good to go.
Notes:
- If you get the following error in production:
Uncaught (in promise) bad-precaching-response: bad-precaching-response :: [{"url":"https:
- Add the given package or file in exclude array of InjectManifest plugin in
/buildTools/webpack.prod.js
as follows:
new InjectManifest({ //this is the source of your service worker setup swSrc: \`\${PATHS.src}/serviceWorker/swSource\`, dontCacheBustURLsMatching: ${dontCacheBustURLsMatching}, // Bump up the default maximum size (2mb) to (5mb) that's precached, // to make lazy-loading failure scenarios less likely. maximumFileSizeToCacheInBytes: 5 * 1024 * 1024, //this is the output name of your service worker file swDest: 'serviceWorker.js', exclude: ['fileName'], }),`,
- Add the given package or file in exclude array of InjectManifest plugin in
This build relies on Prettier formatter to enforce code style. And ESLint for identifying problematic patterns found in the code.
-
Setting up prettier:
1- You can find steps on how to set up prettier formatter with WebStorm/PhpStorm here.
Notes:
- It's better to use the local
node_modules
version of prettier instead of a global one, to avoid version conflicts (in case the globally installed version does not match the version specified inpackage.json
).
2- Follow the next steps to set up prettier and eslint in VS Code:
-
Install
prettier
plugin -
Install
eslint
plugin -
Open VS Code settings
CTRL + ,
:a- Search for
formatter
=> check Format on saveb- Search for
prettier
=> add.prettierrc
in Prettier: Config Path section && check Prettier: Require Config
3- Please refer to other tutorials if you are using a different IDE.
- It's better to use the local
- This app includes search engines, Facebook, Twitter and regular meta tags
-
Open /buildTools/constants.js file and update metaInfo object
-
Open public/assets/images and replace (favicon.png, metaImage.jpg) with your images but using the same name
Notes:
- It's very important to set
PRODUCTION_DOMAIN
in any production environment file, to generate site meta tags correctly PRODUCTION_DOMAIN
is the domain of your deployed app
- It's very important to set
-
Install the following packages:
pnpm add -D cypress start-server-and-test
-
Add the following scripts in
package.json
:"cypress:prepare": "set BROWSER=none && set ENV=test && pnpm start", //starts the app without opening it in the browser "cypress:start": "start-server-and-test cypress:prepare 3000", //Runs the previous command and waits for localhost to start up "cypress:open": "pnpm cypress:start \"cypress open\"", //Runs cypress tests in electron browser (recommended by cypress) "cypress:run": "pnpm cypress:start \"cypress run\"" //Runs cypress tests in headless mode (no browser)
- Note:
- If you would like to start the E2E tests automatically you can update
cypress:open
command to the following, but after running it at least once:
"cypress:open": "pnpm cypress:start \"cypress open --e2e -b electron\""
- If you would like to start the E2E tests automatically you can update
- Note:
-
Run
cypress:open
command and choose E2E to configure it -
Open
cypress.config.js
file and add the baseUrl:baseUrl: 'http://localhost:3000/',
-
Add e2e directory in cypress directory then add your test files inside it using the following convention
fileName.cy.js
-
If you would like to add code coverage for E2E, do the following Documentations:
- Install the following package:
pnpm add -D @cypress/code-coverage
- Import the installed package in
cypress.config.js
file:setupNodeEvents(on, config) { require('@cypress/code-coverage/task')(on, config); // include any other plugin code... // It's IMPORTANT to return the config object // with any changed environment variables return config; },
- Add the following import in
cypress/support/e2e.js
file:import '@cypress/code-coverage/support';
- Add the following to
babel.config.js
file before the return:if (process.env['ENV']?.trim() === 'test') { plugins.push('istanbul'); }
- Note:
- To open the coverage report go to
/coverage/lcov-report/index.html
- To open the coverage report go to
- Note:
- Install the following package:
- Private route guard => protect the given route based on a token.
- Public route guard => used for public routes and authentication routes (ex: login, signup, ...etc) {if authenticated it will redirect the user to the home page}
- Restricted route guard => protect the given route based on a token, and a list of permissions (can be an array or a string).
- Restricted section => protect the given section based on a list of permissions (can be an array or a string).
- Generic error boundary fallback component (you can customize it)
- Cookies, local storage and sessions storage mangers to store data in the browser. (Note: the data is encrypted using crypto-js package before storing it.)
- Basic mixins
(scss/generic/_mixins.scss)
- Normalize styles
(scss/generic/_normalize.scss)
- App typography styles
(scss/generic/_typography.scss)
- 4 break points
(scss/generic/_variables.scss)
- Components
- Containers
- Pages
- Custom hooks
- Services
- Reducers
- progressiveWebApp
In the project directory, you can run:
Runs the app in the development mode.
It will open http://localhost:3000 automatically in the browser to see your app.
All changes will be injected automatically without reloading the page.
You will see in the console the following:
- All redux store related changes
- Any of the following errors:
- Linting errors.
- Code format errors (because of prettier)
Builds the app for production to the dist
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!
Serves the app on http://localhost:8080/
from the dist
folder to check the production version.
Note: Use this script only if you ran the build script pnpm build
.
It allows you to analyze the bundle size.
It runs all test files.
- It runs all unit test files in
/src
directory using watch mode. - Will run all your tests once then again on every change of your source code
It runs test coverage.
Clears test cache.
- It creates a boilerplate for component, container, page, custom hook, service, reducer or progressive web app setup.