Skip to content

Instantly share code, notes, and snippets.

@cngondo
Last active March 16, 2022 20:50
Show Gist options
  • Save cngondo/cec56758c2ae78591f251e0f688b9c81 to your computer and use it in GitHub Desktop.
Save cngondo/cec56758c2ae78591f251e0f688b9c81 to your computer and use it in GitHub Desktop.
SIMPLE WEBPACK 5 SETTINGS WITH CORRESPONDING package.json FOR REACT
{
"name": "react-webpack-setup",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"react-dev": "webpack serve"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@babel/core": "^7.12.16",
"@babel/plugin-proposal-class-properties": "^7.13.0",
"@babel/preset-env": "^7.12.16",
"@babel/preset-react": "^7.12.13",
"babel-loader": "^8.2.2",
"clean-webpack-plugin": "^3.0.0",
"css-loader": "^5.0.2",
"html-webpack-plugin": "^5.2.0",
"sass-loader": "^11.0.1",
"webpack": "^5.22.0",
"webpack-cli": "^4.5.0",
"webpack-dev-server": "^3.11.2"
},
"dependencies": {
"react": "^17.0.1",
"react-dom": "^17.0.1"
}
}
const dev = process.env.NODE_ENV !== 'production' // Loads the environment that you're running on
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');
module.exports = {
mode:'development',
devtool: dev ? 'inline-source-map': 'eval-source-map', // Maps the line numbers of jsx files to the ones in the bundle
entry: path.join( __dirname, 'client/src/index.jsx'),
module: { // All modules to be used in the app
rules: [
{
test: [/\.(js|jsx)$/], // Regex for parsing both js and jsx files
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: {
presets: ['@babel/preset-env', '@babel/preset-react'],
plugins: ['@babel/plugin-proposal-class-properties'] // For class based react components
}
}
},
{
test: /\.(woff(2)?|eot|ttf|otf|svg|)$/, // Regex for parsing all the font types
type: 'asset/inline',
},
{
test: /\.(scss|css)$/, // Regex for parsing css and sass files
use: ['css-loader', 'sass-loader']
},
{
test: /\.(?:ico|gif|png|jpg|jpeg)$/i, // Regex for parsing image files
type: 'asset/resource'
},
]
},
output: {
filename: 'bundle.js',
path: path.join(__dirname, 'client/dist/')
},
devServer: { // Serves the bundle for development
contentBase: path.join(__dirname, 'client/dist'),
compress: true,
hot: true,
port: 8080,
writeToDisk: true // Makes the bundle available on disk. The serving happens in memory.
},
plugins:[
new HtmlWebpackPlugin({ // Creates the required html file on every build
title: 'React Webpack Boilerplate',
inject: 'body',
template: path.join(__dirname, 'client/src/template.html'), // Include your app's target node here.
filename: 'index.html', // output file
}),
new CleanWebpackPlugin({ cleanStaleWebpackAssets: false }) // Cleans up all unwanted bundles
]
};
@cngondo
Copy link
Author

cngondo commented Feb 24, 2021

Check full repo here: react webpack setup

This mostly focussed on developing on the frontend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment