Missing class properties transform when Webpack presets:"env" - reactjs

I'm testing React project on webpack-dev-server. I want to use classfield syntax but got an error at state init.
client:162 ./src/containers/App.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: Missing class properties transform.
2 |
3 | class App extends Component {
4 | state = {
| ^
5 | count: 0
6 | }
7 | // constructor(props) {
I found solution that presets to be this "presets": ["es2015", "stage-0", "react"]. But I'm using "env" presets. I thought "env" presets support all babel-stages. Do I have to change presets?
App.js
class App extends Component {
state = {
count: 0
}
render() {
return (
<div>
<input defaultValue={this.state.count} />
</div>
);
}
}
export default App;
package.json
{
"name": "redux",
"version": "1.0.0",
"description": "",
"main": "counter.js",
"dependencies": {
"#types/react": "^16.4.14",
"babel": "^6.23.0",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-register": "^6.26.0",
"http": "0.0.0",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"webpack": "^4.19.1",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.9"
},
"scripts": {
"devserver": "webpack-dev-server --open --progress"
},
"author": "",
"license": "ISC",
"devDependencies": {
"babel-plugin-transform-class-properties": "^6.24.1"
}
}
.babelrc
{
"presets": ["env", "react"],
"plugins": ["transform-class-properties"]
}
.webpack.config.js
const webpack = require('webpack');
const path = require('path');
module.exports = {
mode: 'development',
entry: './src/index.js',
output: {
path: path.resolve(__dirname, 'dist'),
publicPath: '/dist/',
filename: 'bundle.js'
},
module: {
rules: [
{
test: /\.js$/,
include: path.join(__dirname),
exclude: /(node_modules)|(dist)/,
use: {
loader: 'babel-loader',
options: {
presets: ['env']
}
}
}
]
}
}

Try this:
.babelrc
{
"presets": ["env", "react", "stage-2"]
}
package.json
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^7.1.4",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
...
When using the .babelrc file, in the webpack.config.js, we can simply config like this:
{
test: /\.js$/,
use: ['babel-loader']
}
Hopefully, that helps.

Related

Babel-Loader not parsing ReactDom.render

I'm working on a React tutorial via Youtube and have matched his webpack.config and package.json files but I am getting a strange error when running webpack-dev-server
package.json
{
"name": "react-tutorials",
"version": "0.0.0",
"description": "",
"main": "webpack.config.js",
"dependencies": {
"babel-plugin-add-module-exports": "^0.1.2",
"babel-plugin-react-html-attrs": "^2.0.0",
"babel-plugin-transform-class-properties": "^6.3.13",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-stage-0": "^6.3.13",
"bootstrap-without-jquery": "^1.0.5",
"history": "^1.17.0",
"react": "^0.14.6",
"react-dom": "^0.14.6",
"react-router": "^1.0.3",
"webpack-dev-server": "^1.14.1"
},
"devDependencies": {
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
"babel-loader": "^6.4.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"webpack": "^1.15.0"
},
"scripts": {
"dev": "webpack-dev-server --content-base src --inline --hot"
},
"author": "",
"license": "ISC"
}
webpack.config
var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var path = require('path');
module.exports = {
context: path.join(__dirname, "src"),
devtool: debug ? "inline-sourcemap" : null,
entry: "./js/client.js",
mode: "development",
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /(node_modules|bower_components)/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015', 'stage-0'],
plugins: ['react-html-attrs', 'transform-class-properties', 'transform-decorators-legacy'],
}
}
]
},
output: {
path: __dirname + "/src/",
filename: "client.min.js"
},
plugins: debug ? [] : [
new webpack.optimize.DedupePlugin(),
new webpack.optimize.OccurenceOrderPlugin(),
new webpack.optimize.UglifyJsPlugin({ mangle: false, sourcemap: false }),
],
};
./src/js/client.js
import React from "react";
import ReactDOM from "react-dom";
import Bootstrap from 'bootstrap-without-jquery';
import Layout from "./pages/Layout";
const app = document.getElementById('app');
ReactDOM.render(<div>/Hello</div>, app);
The file structure matches the tutorial source code so I don't think that is an issue. However when I run npm run dev I get the following error
Webpack Error
If I comment out line 10 of client.js webpack compiles with no issue. (ReactDOM.render function)
You have Webpack 1 installed, which has all the loader configurations in config.module.loaders, but your config uses config.module.rules which was introduces in Webpack 2.
Installing Webpack 4 should resolve your issue.
npm install -D webpack#latest

React - Chrome does not report undefined JavaScript object property. Crashes

Since im building my webapp with React, when i try to set something into an undefined object property, the code processing crashes (reproducible with breakpoint debugging) and no errors are shown in the console log. Also the chrome react extension does not give me any errors.
So to finding a bug is more time consuming that it should be.
let changedJsonResponse = jsonResponse;
changedJsonResponse.data = newTableData; // changedJsonResponse.data is undefined
// throws no error & code proccess crashes
webpack.config.js
const webpack = require('webpack');
const path = require('path');
let config = {
devtool: 'eval-source-map',
entry: {
FooManagement: ['babel-polyfill', './domains/FooManagement/entry.jsx'],
},
output: {
path: path.resolve(__dirname, '../web/react/'),
filename: '[name].js'
},
externals: {
'jquery': 'window.jQuery',
'bootbox': 'window.bootbox',
},
resolve: {
alias: {
alias_shared: path.resolve(__dirname, 'shared'),
},
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: [/node_modules/],
loader: 'babel-loader',
options: {presets: ['es2015', 'react']}
},
{
test: /\.css/,
loader: 'style-loader!css-loader'
}
]
}
};
module.exports = config;
package.json
{
"author": "myself",
"version": "0.0.1",
"description": "",
"main": "index.jsx",
"scripts": {
"watch": "webpack -d --watch",
"build": "webpack -p"
},
"devDependencies": {
"babel-core": "^6.26.0",
"babel-loader": "^7.1.2",
"babel-polyfill": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.7",
"style-loader": "^0.19.0",
"webpack": "^3.8.1"
},
"dependencies": {
"axios": "^0.16.2",
"dateformat": "^3.0.2",
"is-uuid": "^1.0.2",
"lodash": "^4.17.4",
"react": "^16.0.0",
"react-cropper": "^1.0.1",
"react-dom": "^16.0.0",
"react-dropzone": "^4.2.3",
"react-router-dom": "^4.2.2",
"react-switchery-component": "0.0.7",
"uuid": "^3.1.0"
}
}
Chrome version: 62.0.3202.94
I was able to catch those errors in Axios .catch.
After a while i found the cause of the problem.

Webpack with babel-loader doesn't recognize Typescript

I am using webpack and trying to leverage an existing React component library written in TS.
However, with the settings below, the webpack is giving 'unexpected Token' error. I copied the problematic code to babel online transpiler https://babeljs.io/repl/ but it is not showing any error.
I am very new to React development, any criticism/questions/answers are welcomed.
Thanks!
This is my webpack.config.js
var path = require("path")
var webpack = require('webpack')
var BundleTracker = require('webpack-bundle-tracker')
module.exports = {
context: __dirname,
entry: [
'webpack-dev-server/client?http://localhost:3000',
'webpack/hot/only-dev-server',
'./app/static/js/index'
],
output: {
path: path.resolve('./app/static/bundles/'),
filename: '[name]-[hash].js',
publicPath: 'http://localhost:3000/static/bundles/',
},
plugins: [
new webpack.HotModuleReplacementPlugin(),
new webpack.NoEmitOnErrorsPlugin(),
new BundleTracker({filename: './webpack-stats.json'}),
],
module: {
loaders: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
loader: ["babel-loader?presets[]=es2015,presets[]=react"],
},
{
test: /\.tsx?$/,
loader: 'ts-loader',
options: {
configFileName: './tsconfig.json'
},
exclude: /node_modules/
},
{
test: /\.css$/,
loader: 'style-loader!css-loader'
}
]
},
resolve: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.css']
}
}
My entry point file index.js:
import React from 'react';
import ReactDOM from 'react-dom';
import * as PropTypes from 'prop-types';
interface IDemoState {
value ?: string;
}
class Test extends React.Component<{}, IDemoState> {
state: IDemoState = {
value : 'general'
}
}
ReactDOM.render(<Test />, document.getElementById('tabnav'));
Package.json
{
"name": "djangoproject",
"version": "1.0.0",
"description": "Django Project",
"main": "index.js",
"scripts": {
"build": "webpack --config webpack.config.js --progress --colors",
"build-production": "webpack --config webpack.prod.config.js --progress --colors",
"watch": "node server.js"
},
"keywords": [
"Django"
],
"license": "ISC",
"devDependencies": {
"adp-css-framework": "^1.5.3",
"adp-react-components": "^1.4.3",
"adp-react-icons": "^1.16.0",
"babel": "^6.23.0",
"babel-cli": "^6.24.1",
"babel-core": "^6.25.0",
"babel-loader": "^7.1.0",
"babel-preset-env": "^1.5.2",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^0.28.4",
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-hot-loader": "^1.3.1",
"style-loader": "^0.18.2",
"ts-loader": "^2.2.1",
"typescript": "^2.4.1",
"webpack": "^3.0.0",
"webpack-bundle-tracker": "^0.2.0",
"webpack-dev-server": "^2.5.0"
}
}
If I run npm run watch, I got this error:
ERROR in ./app/static/js/index.js
Module build failed: SyntaxError: xxxxx./app/static/js/index.js: Unexpected token (10:20)
8 |
9 | class Test extends React.Component<{}, IDemoState> {
> 10 | state: IDemoState = {
| ^
11 | value : 'general'
12 | }
13 | }
# multi webpack-dev-server/client?http://localhost:3000 webpack/hot/only-dev-se
rver ./app/static/js/index
The problem finally goes away after I changed the ts-loader to awesome-typescript-loader and write everything in a tsx file.

Webpack producing no input

I am aware that this is quite a common problem with Webpack, but I am a total newbie and the solutions I've found so far seem to be very specific to the single project. However, the scenario is the same: Webpack compiling the project correctly and then showing a blank page, without reporting any errors.
Here is my webpack.config.js (I made it following tutorials):
import path from 'path';
import HtmlWebpackPlugin from 'html-webpack-plugin';
export default () => ({
entry: [
path.join(__dirname, 'src/index.jsx'),
],
output: {
path: path.join(__dirname, 'dist'),
filename: 'bundle.js',
},
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: './App.html'
}),
],
module: {
rules: [
{
test: /.jsx?$/,
exclude: /node_modules/,
include: path.join(__dirname, 'src'),
use: [
{
loader: 'babel',
options: {
babelrc: false,
presets: [
['es2015', { modules: false }],
'react',
],
}
}
]
},
{
test: /\.(css|scss|sass)$/,
loader: 'style!css!sass',
},
]
},
});
module.exports = config;
Here my package.json:
{
"name": "smart-fit",
"version": "1.0.0",
"description": "",
"main": "src/index.js",
"dependencies": {
"chartjs": "^0.3.24",
"interactjs": "^1.2.8",
"jquery": "^3.2.1",
"lodash": "^4.17.4",
"moment": "^2.18.1",
"react": "^15.5.4",
"react-dom": "^15.5.4",
"react-hot-loader": "^3.0.0-beta.3",
"react-table": "^5.6.0"
},
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^6.4.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-register": "^6.24.1",
"css-loader": "^0.28.1",
"extract-text-webpack-plugin": "^2.0.0-beta.4",
"html-webpack-plugin": "^2.28.0",
"loader-utils": "^1.1.0",
"node-sass": "^4.5.2",
"sass-loader": "^6.0.3",
"style-loader": "^0.17.0",
"webpack": "^2.1.0-beta.25",
"webpack-dev-server": "^2.1.0-beta.2"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
Webpack version is the lastest and the project is written using React and ES5.

Can't get arrow functions to work (React, Babel, Webpack, ES6) [duplicate]

This question already has answers here:
How to use arrow functions (public class fields) as class methods?
(4 answers)
Closed 6 years ago.
Desperately hoping i'll be able to find someone who can tell why i can't get arrow functions to work with my Webpack / Babel setup, been trying a bunch of stuff and nothing has worked so far. Current state of the project:
Package.json:
{
"name": "..",
"version": "1.0.0",
"description": "..",
"main": "app.jsx",
"author": "..",
"license": "ISC",
"devDependencies": {
"babel-core": "^6.22.1",
"babel-loader": "^6.2.10",
"babel-plugin-transform-class-properties": "^6.22.0",
"babel-plugin-transform-react-jsx": "^6.22.0",
"babel-preset-es2015": "^6.22.0",
"babel-preset-react": "^6.22.0",
"babel-preset-stage-2": "^6.22.0",
"css-loader": "^0.26.1",
"node-sass": "^4.5.0",
"sass-loader": "^4.1.1",
"style-loader": "^0.13.1",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.3.0"
},
"dependencies": {
"lodash": "^4.17.4",
"react": "^15.4.2",
"react-dom": "^15.4.2",
"react-router": "^3.0.2"
}
}
webpack.config.js:
var path = require('path');
module.exports = {
entry: './src/router.jsx',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'public')
},
watch: true,
module: {
loaders: [
{
test: /\.jsx$/,
exclude: /(node_modules)/,
loader: 'babel-loader',
query: {
presets: ['react', 'es2015']
}
},
{
test: /\.scss$/,
loader: 'style-loader!css-loader!sass-loader'
},
]
}
};
.babelrc
{
"plugins": [
"transform-react-jsx",
"transform-class-properties"
],
"presets": [
"es2015",
"react",
"stage-2"
],
}
Component:
import React from 'react'
import './styles.scss'
class Button extends React.Component {
asdf = () => {
return ['btn', this.props.size].join(' ')
}
render() {
return (
<button className={this.asdf}>
{this.props.children}
</button>
)
}
}
export default Button
Error:
ERROR in ./src/app/ui-kit/button.jsx
Module build failed: SyntaxError: Missing class properties transform.
4 |
5 | class Button extends React.Component {
> 6 | asdf = () => {
| ^
7 | return ['btn', this.props.size].join(' ')
8 | }
9 |
# ./src/app/components/signup.jsx 13:14-45
# ./src/app/app.jsx
# ./src/router.jsx
Checkout my babel packages and .babelrc is working in my current project and compare with yours:
.babelrc:
{
"presets": ["react", "es2015" , "stage-0"],
"plugins": [
["transform-decorators-legacy"]
]
}
packages.json
"babel-core": "^6.4.5",
"babel-eslint": "^6.1.2",
"babel-loader": "^6.2.1",
"babel-plugin-react-transform": "^2.0.0",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-polyfill": "^6.3.14",
"babel-preset-es2015": "^6.3.13",
"babel-preset-react": "^6.3.13",
"babel-preset-react-hmre": "^1.0.1",
"babel-preset-stage-0": "^6.3.13",

Resources