This gist is about configure React with webpack and babel without npx create-react-app.
- Commands and how to for this repo
- Comands used in this repository
$ yarn add @babel/core @babel/preset-env @babel/preset-react webpack webpack-cli -D
$ yarn add react react-dom
$ yarn add babel-loader -D
For webpack configuration we need to indicate dir locales for transpilation input and transpilation output:
const path = require('path');
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
}
};To configure loaders in webpack just add this:
// ...
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
}
}
]
}
// ...Each rule need some object scope rule: [{a},{b}...]. At exemple above we
transpile all js modules in repo.
"scripts": {
"build": "webpack --mode production", // minified output
"start": "webpack --mode development",
}In this repo I didn't use npx create-react-app in this case I will create html
for transpiled script:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tech Lists</title>
</head>
<body>
<script src="./bundle.js"></script>
</body>
</html>For automate bundle use this:
$ yarn add webpack-dev-server -D
Add in webpack.config file:
// ...
devServer: {
contentBase: resolve(__dirname, 'dist')
},"scripts": {
"build": "webpack --mode production",
"start": "webpack-dev-server --mode development"
}$ yarn add style-loader css-loader -D
$ yarn add file-loader -D
$ yarn add @babel/plugin-proposal-class-properties -D
$ yarn add prop-types