react-hot-loader still reload not HMR - webpack-dev-server

I have webpack.config.js as shown below, running webpack-dev-server using npm run dev, whenever changes are detected it still results hard reloading not HMR
Please guide, what went wrong here, thanks
webpack.config.js
const path = require('path')
const webpack = require('webpack')
module.exports = {
entry: [
'webpack-dev-server/client?http://0.0.0.0:8080',
'webpack/hot/only-dev-server',
path.resolve(__dirname, 'src', 'entry.js')
],
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js'
},
devServer: {
contentBase: './dist',
hot: true,
},
module: {
loaders: [
{
test: /\.js?$/,
loader: 'babel-loader?cacheDirectory',
exclude: /(node_modules)/
}
]
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NamedModulesPlugin(),
]
}
.babelrc
{
presets: ['stage-0', 'es2015', 'react'],
plugins: ['react-hot-loader/babel']
}
package.json
{
"dependencies": {
"react-hot-loader": "3.0.0-beta.6",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.4.1"
},
"scripts": {
"dev": "webpack-dev-server"
}
}

Do you use module.hot.accept in entry.js file?:
import React from 'react';
import ReactDOM from 'react-dom'
import Navigation from './Navigation';
const rootEl = document.getElementById('root');
const render = Component => {
ReactDOM.render(
<Component/>,
rootEl
)
};
render(Navigation);
if (module.hot) {
module.hot.accept('./Navigation', () => {
render(Navigation)
})
}
WebPack HMR official docs
Interesting issue

Related

Jest webpack 4 babel 7 syntaxError 'import' inside .test.js files

Setup
react 16.4.2 (not cra, homebrewed)
babel v7
jest v24
webpack v4.17
PROBLEM
getting error below when I use 'import' syntax inside my test files (i.e. inside component.test.js).
How can I tell Jest to parse the test files using es6?
Have done a lot of googling but no joy
C:\repos\sandbox\my_react_starter\src\temp.test.js:2
import React from 'react';
^^^^^^
SyntaxError: Unexpected token import
at ScriptTransformer._transformAndBuildScript (node_modules/#jest/transform/build/ScriptTransformer.js:471:17)
at ScriptTransformer.transform (node_modules/#jest/transform/build/ScriptTransformer.js:513:25)
jest config:
"jest": {
"rootDir": ".",
"verbose": true,
"transform": {
"^.+\\.jsx?$": "babel-jest",
".+\\.(css|styl|less|sass|scss)$": "<rootDir>/node_modules/jest-css-modules-transform"
}
},
.babelrc
{
"presets": [
"#babel/preset-env",
"#babel/react"
],
"env": {
"test": {
"plugins": [
"transform-es2015-modules-commonjs"
]
}
}
}
webpack:
module.exports = {
entry: './src/index.js',
output: { path: __dirname + '/dist', publicPath: '/', filename: 'bundle.js' },
devServer: { contentBase: './dist' },
module:
{
rules:
[
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader',
options: { presets: ['es2015'] }
}
}
]
}
};

Webpack 4 and Babel 7 "import" Uncaught SyntaxError: Unexpected identifier in React

I've been trying to set up Webpack 4 and Babel 7 to work with ES6 "import" in React. I keep getting "Uncaught SyntaxError: Unexpected identifier" at "import React from 'react';" in Chrome 71. It worked in Webpack 3 and Babel 6, so I think I'm doing something wrong with the Babel setup.
These are my dev dependencies:
"#babel/core": "^7.1.6",
"#babel/plugin-proposal-class-properties": "^7.1.0",
"#babel/plugin-proposal-decorators": "^7.1.6",
"#babel/plugin-syntax-dynamic-import": "^7.0.0",
"#babel/preset-env": "^7.1.6",
"#babel/preset-react": "^7.0.0",
"#babel/register": "^7.0.0",
"babel-loader": "^8.0.4",
"webpack": "^4.26.0",
"webpack-dev-server": "^3.1.10"
My .babelrc looks like this:
{
"presets": ["#babel/preset-env", "#babel/preset-react"],
"plugins": [
["#babel/plugin-proposal-decorators", { "legacy": true }],
["#babel/plugin-proposal-class-properties", { "loose": true }],
["#babel/syntax-dynamic-import"]
]
}
I've been trying babel.config.js as well, to no prevail.
My webpack-config.js file:
const HTMLWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const webpack = require('webpack');
const HTMLWebpackPluginConfig = new HTMLWebpackPlugin({
template: __dirname + '/src/index.html',
filename: 'index.html',
injext: 'body'
})
module.exports = {
entry: ["babel-polyfill", __dirname + '/src/index.js'],
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
},
{
test: /\.scss$/,
loader: ExtractTextPlugin.extract('css-loader!sass-loader')
},
]
},
output: {
filename: 'transformed.js',
path: __dirname + 'build'
},
devServer: {
contentBase: './src/',
historyApiFallback: true,
hot: true,
port: 8000,
publicPath: "/src/public",
noInfo: false
},
plugins: [
HTMLWebpackPluginConfig,
new ExtractTextPlugin('public/css/style.css', {
allChunks: true
}),
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'production')
}),
]}
Okay, i seemed to have fixed to issue after spitting through my webpack config. The issue was caused by this:
devServer: {
contentBase: './src/',
historyApiFallback: true,
hot: true,
port: 8000,
publicPath: "/src/public",
noInfo: false
},
I'll see if i can find what exactly causes Webpack not to bundle with this. Thanks for all the help anyway!

webpack bundle file is not being generated with no errors

I am new to react, trying to learn webpack configuration. I am using webpack4 for my project, however, after setting up webpack, the bundle files are not generated, neither the js file nor the html file and there is no error.
The output on the console says "compiled successfully". How do i fix this. I have been struggling for about a week and no online resource seems to give me what i want.
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const port = process.env.PORT || 3000;
const paths = {
DIST: path.resolve(__dirname, 'dist'),
SRC: path.resolve(__dirname, 'src'),
JS: path.resolve(__dirname, 'src/js')
}
const htmlPlugin = new HtmlWebpackPlugin({
template: "./src/index.html",
filename: "./index.html"
});
module.exports = {
mode: 'development',
entry: path.join(paths.JS, 'index.js'),
output: {
path: paths.DIST,
filename: 'app.bundle.js'
},
module:{
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: "babel-loader"
}
},
{
test: /\.css$/,
use: [
{
loader: "style-loader"
},
{
loader: "css-loader",
options:{
modules: true,
importLoaders: 1,
localIndentName: "[name]_[local]_[hash:base64]",
sourceMap: true,
minimize: true,
camelCase:true
}
}
]
}
]
},
plugins: [htmlPlugin],
devServer: {
publicPath: paths.DIST,
host: 'localhost',
port: port,
historyApiFallback: true,
open:true,
hot: true
}
};
Make sure all path correctly, not have missing file.
src/js/index.js
src/index.html
make sure run webpack correctly to show log like:
package.json
"scripts": {
"start": "webpack --config webpack.config.js --bail --progress --profile"
},
and run by npm start, then will show the log.
if you want, you can use new babel version. change your bable with this:
"dependencies": {
"#babel/core": "^7.0.0-beta.42",
"#babel/preset-env": "^7.0.0-beta.42",
"babel-eslint": "^8.0.3",
"babel-loader": "^8.0.0-beta.0",
"css-loader": "^0.28.11",
"html-webpack-plugin": "^3.1.0",
...
"style-loader": "^0.20.3",
"webpack": "^4.4.1",
"webpack-cli": "^2.0.13"
},
.babelrc
{
"presets": ["#babel/preset-env"],
"plugins": ["#babel/plugin-proposal-object-rest-spread"]
}
then update your webpack rule:
{
test: /\.js$/,
use: {
loader: 'babel-loader',
options: {
presets: ['#babel/preset-env'],
plugins: [require('#babel/plugin-proposal-object-rest-spread')]
}
},
exclude: /node_modules/
},

why babel loader and jsx loader is not working in webpack version 3.x in react app

Package.json this is my package json file.
I have already install npm install --save-dev babel-loader babel-core in my app
/* Package.json*/
{
"name": "tripdetail",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dev": "webpack-dev-server",
"prod": "webpack -p"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-loader": "^7.1.1",
"babel-preset-env": "^1.6.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.4",
"extract-text-webpack-plugin": "^3.0.0",
"html-webpack-plugin": "^2.30.1",
"node-sass": "^4.5.3",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"sass-loader": "^6.0.6",
"style-loader": "^0.18.2",
"webpack": "^3.5.4",
"webpack-dev-server": "^2.7.1"
},
"dependencies": {
"eslint": "^4.4.1"
},
"description": ""
}
.babelrc this is my babelrc file. I have also configured it by this code -
/*.babelrc */
{
"presets": ["es2015", "es2016", "react"]
}
webpack.config.js this is my webpack config js file. I have also configure module and tested but it is not working.
/webconfig.js code/
'use strict';
var HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
var path = require('path');
module.exports = {
entry: './src/app.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.bundle.js'
},
module: {
rules: [{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ["css-loader", "sass-loader"],
publicPath: '/dist',
},
{
test: /\.jsx?$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel',
query: {
presets: ['react', 'es2015']
}
}
)
}]
},
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000,
stats: 'errors-only',
open: true
},
plugins: [new HtmlWebpackPlugin({
title: 'tripDetailPage',
hash: true,
minify: {
collapseWhitespace: false
},
template: './src/index.html'
}), new ExtractTextPlugin({
filename: "tripDetail.css",
disable: false,
allChunks: true
})]
}
app.js
const css = require('./app.scss');
import React from 'react';
import ReactDOM from 'react-dom';
ReactDOM.render(
<div>Hello</div>,
//e('div', null, 'Hello Worlddddd'),
document.getElementById('root')
);
Compiletion ERROR
ERROR in ./src/app.js
Module parse failed: E:\trip-react\src\app.js Unexpected token (10:3)
You may need an appropriate loader to handle this file type.
|
| ReactDOM.render(
| <div>Hello</div>,
| //e('div', null, 'Hello Worlddddd'),
| document.getElementById('root')
# multi (webpack)-dev-server/client?http://localhost:9000 ./src/app.js
webpack: Failed to compile.
ERROR in ./src/app.js
Have you tried renaming that file to /src/app.jsx ?
Check out resolve.extensions and module.loaders, in your webpack.config.js file -- both should refer to .jsx files.
This error is related to webpack.config.js. Rules was wrongly putted in this file. Here is my correct webconfig.
'use strict';
var HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require("extract-text-webpack-plugin");
var CleanWebpackPlugin = require('clean-webpack-plugin');
var path = require('path');
module.exports = {
entry: './src/app.js',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'app.bundle.js'
},
resolve: {
extensions: ['.js', '.jsx']
},
module: {
rules: [{
test: /\.scss$/,
use: ExtractTextPlugin.extract({
fallback: 'style-loader',
use: ["css-loader", "sass-loader"],
publicPath: '/dist',
})
},
{
test:/\.js$/,
use:'babel-loader',
exclude:/node_modules/
},
{
test:/\.jsx$/,
use:'babel-loader',
exclude:/node_modules/
}
]
},
devServer: {
contentBase: path.join(__dirname, 'dist'),
compress: true,
port: 9000,
stats: 'errors-only',
open: true
},
plugins: [new HtmlWebpackPlugin({
title: 'tripDetailPage',
hash: true,
minify: {
collapseWhitespace: false
},
template: './src/index.html'
}), new ExtractTextPlugin({
filename: "tripDetail.css",
disable: false,
allChunks: true
}),]
}

React Not Attaching DOM to the HTML

I am having some problem in my ReactJs code. Everything is compiling without any error using webpack. But after webpack dev server started and when I am browsing to localhost:9000 nothing is inserted into the DOM from ReactJs.
I am running npm start on terminal.
Here is all the code that i have written -
.babelrc
{
"presets": ["es2015", "react", "stage-0"]
}
.eslintrc
{
"ecmaFeatures": {
"jsx": true,
"modules": true
},
"env": {
"browser": true,
"node": true
},
"parser": "babel-eslint",
"rules": {
"quotes": [2, "single"],
"strict": [2, "never"],
"react/jsx-uses-react": 2,
"react/jsx-uses-vars": 2,
"react/react-in-jsx-scope": 2
},
"plugins": [
"react"
]
}
.jslintrc
{
"node": true,
"browser": true,
"esnext": true,
"newcap": false
}
index.html
<html>
<head>
<title>Forms in React Test</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
</head>
<body>
<div id='root' class="container">
</div>
</body>
<script src="/static/bundle.js" type="text/javascript"></script>
</html>
index.js
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
class IndexComponent extends React.Component{
render() {
return (
<div>
<input type="text" value="Shawn" />
</div>
);
}
}
ReactDOM.render(<IndexComponent />, document.getElementById('root'));
**package.json**
{
"name": "chapter4",
"version": "0.0.1",
"description": "ReactJS forms example",
"scripts": {
"start": "node server.js",
"lint": "eslint src"
},
"author": "",
"license": "MIT",
"homepage": "",
"devDependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.24.1",
"babel-core": "^6.24.1",
"babel-eslint": "^7.2.2",
"babel-loader": "^6.4.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"eslint": "^3.19.0",
"eslint-plugin-react": "^6.10.3",
"react-hot-loader": "^1.3.1",
"webpack": "^2.4.1",
"webpack-dev-server": "^2.4.2"
},
"dependencies": {
"react": "^15.5.4",
"react-dom": "^15.5.4"
}
}
webpack.config.js
var path = require('path');
var webpack = require('webpack');
module.exports = {
devtool: 'eval',
entry: [
'webpack-dev-server/client?http://localhost:9000',
'webpack/hot/only-dev-server',
'./src/index'
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
// new webpack.NoEmitOnErrorsPlugin()
],
resolve: {
extensions: ['.js', '.jsx']
},
module: {
rules: [
{
test: /\.jsx?$/,
include: [path.join(__dirname, 'src')],
},
{
use: [
{
loader: 'react-hot-loader'
},
{
loader: 'babel-loader',
options: {
// cacheDirectory: 'babel_cache',
presets: ['react', 'es2015']
}
}
]
}
]
}
};
Thank you all in advance.
In your webpack.confg.js you have the following:
module.exports = {
devtool: 'eval',
entry: [
'webpack-dev-server/client?http://localhost:9000',
'webpack/hot/only-dev-server',
'./src/index' // <-- no src dir in project
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
// new webpack.NoEmitOnErrorsPlugin()
],
resolve: {
extensions: ['.js', '.jsx']
},
module: {
rules: [
{
test: /\.jsx?$/,
include: [path.join(__dirname, 'src')], // <-- no src dir in project
},
{
use: [
{
loader: 'react-hot-loader'
},
{
loader: 'babel-loader',
options: {
// cacheDirectory: 'babel_cache',
presets: ['react', 'es2015']
}
}
]
}
]
}
};
You don't have a src directory in this project.
Fix: Make a src directory and move your index.js file in it. You also have a problem with the way you're trying to bring in react-hot-loader and not excluding node_modules. Try this for your module section:
module: {
loaders: [
{
test: /\.jsx?$/,
loaders: ['react-hot-loader', 'babel-loader?presets[]=es2015&presets[]=react&presets[]=stage-2&presets[]=stage-0'],
include: [path.join(__dirname, 'src')],
exclude: '/node_modules/'
}
]
}
Tested and it works on my local.
Got the solution.
In the rules part I am doing some mistake. Following will be the correct rules.
rules: [{
test: /\.jsx?$/,
include: [path.join(__dirname, 'src/')],
use: [
{
loader: 'react-hot-loader'
},
{
loader: 'babel-loader',
}
]
}]
This will be the correct webpack.config.js
var path = require('path');
var webpack = require('webpack');
module.exports = {
devtool: 'eval',
entry: [
'webpack-dev-server/client?http://localhost:9000',
'webpack/hot/only-dev-server',
path.resolve(__dirname, 'src/')
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
publicPath: '/static/'
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin()
],
resolve: {
extensions: ['.js', '.jsx']
},
module: {
rules: [{
test: /\.jsx?$/,
include: [path.join(__dirname, 'src/')],
use: [
{
loader: 'react-hot-loader'
},
{
loader: 'babel-loader',
}
]
}]
}
};
Thanks to all for the help.

Resources