Skip to content
This repository was archived by the owner on Dec 26, 2022. It is now read-only.

Commit f49216b

Browse files
committed
📦 Webpack Code-Spiltting and Tree Shaking
Added Code Spilting optimizations/splitChunks and Tree Shaking with Terser Plugin
1 parent 5cd6444 commit f49216b

File tree

4 files changed

+24
-10
lines changed

4 files changed

+24
-10
lines changed

‎README.md‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ This Template is for building React efficient React Frontend Apps. It uses moder
3131
- .env.local - varialbles with secrets (should include in .gitignore)
3232
- .env.development - variables required while development
3333
- .env.production - variables required for production
34+
- Code Spiltting : 🖖 Webpack Split Chunks (basic)
35+
- Tree Shaking : 🌲 Webpack Terser Plugin (basic)
36+
- Clone Branch react-router if you want to use [react-router-dom](https://reactrouter.com/)
3437

35-
- Clone Branch react-router if you want to use [react-router-dom](https://reactrouter.com/)
36-
# Future Implementations
37-
- Code Spiltting
38-
- Tree Shaking
39-
- cross-env

‎package.json‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
"license": "ISC",
88
"scripts": {
99
"start": "webpack serve --config webpack/webpack.config.js --env env=dev",
10-
"build": "yarn clean && webpack --config webpack/webpack.config.js --env env=prod",
11-
"analyze": "yarn clean && webpack --config webpack/webpack.config.js --env env=prod --analyze",
12-
"clean": "rimraf build",
10+
"build": "webpack --config webpack/webpack.config.js --env env=prod",
11+
"analyze": "webpack --config webpack/webpack.config.js --env env=prod --analyze",
1312
"lint": "eslint --fix \"./src/**/*.{js,jsx,ts,tsx,scss,css,json}\"",
1413
"format": "prettier --write \"./src/**/*.{js,jsx,ts,tsx,scss,css,json,md}\"",
1514
"test": "jest -c jest.config.ts",
@@ -45,7 +44,6 @@
4544
"html-webpack-plugin": "^5.3.1",
4645
"react": "^17.0.2",
4746
"react-dom": "^17.0.2",
48-
"rimraf": "^3.0.2",
4947
"style-loader": "^2.0.0",
5048
"styled-components": "^5.3.0",
5149
"typescript": "^4.2.4",

‎webpack/webpack.common.js‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module.exports = {
3232
utils: path.resolve(__dirname, '..', 'src', 'utils'),
3333
},
3434
},
35+
devServer: { port: 3000 },
3536
module: {
3637
rules: [
3738
{
@@ -63,8 +64,14 @@ module.exports = {
6364
],
6465
},
6566
output: {
67+
clean: true,
6668
path: path.resolve(__dirname, '..', './build'),
67-
filename: 'bundle.js',
69+
filename: '[name].[chunkhash].js',
70+
},
71+
optimization: {
72+
splitChunks: {
73+
chunks: 'all',
74+
},
6875
},
6976
plugins: [
7077
new HtmlWebpackPlugin({

‎webpack/webpack.prod.js‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
const dotenv = require('dotenv');
33
const path = require('path');
44
const { DefinePlugin } = require('webpack');
5+
const TerserPlugin = require('terser-webpack-plugin');
56

67
let envKeys = {};
78
try {
@@ -20,5 +21,15 @@ module.exports = {
2021
mode: 'production',
2122
devtool: 'source-map',
2223
stats: 'normal',
24+
optimization: {
25+
minimize: true,
26+
minimizer: [
27+
new TerserPlugin({
28+
terserOptions: {
29+
compress: true,
30+
},
31+
}),
32+
],
33+
},
2334
plugins: [new DefinePlugin(envKeys)],
2435
};

0 commit comments

Comments
 (0)