Use alt with react in ES6 - reactjs

I'm trying to use alt with React and code an Action in ES6 style:
import alt from '../alt';
class LoginActions {
login() {
alert('oi');
}
}
export default alt.createActions(LoginActions);
My .babelrc is using class transformation plugin:
// without options
{
"plugins": ["transform-class-properties"]
}
My package.json is configured with babel and webpack
{
"name": "npm-yarn-babel-webpack",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"devDependencies": {
"babel-core": "6",
"babel-loader": "7",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"css-loader": "^1.0.1",
"node-sass": "^4.10.0",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"webpack": "^4.25.1",
"webpack-cli": "^3.1.2",
"webpack-dev-server": "^3.1.10"
},
"dependencies": {
"alt": "^0.18.6",
"react": "^16.6.3",
"react-dom": "^16.6.3"
},
"scripts": {
"build": "webpack -p",
"dev": "webpack-dev-server --hot --inline"
}
I Have a component Hello World:
import React, { Component } from 'react';
import LoginActions from './LoginActions';
export class HelloWorld extends Component {
render() {
return (
<div className="hello-world">
<h1>Hello World</h1>
</div>
);
}
}
export default HelloWorld;
I'm running as:
yarn run dev
If I do not import LoginActions, it works, when I import, it compiles, but it does not work.
If I a run with react-scripts it show me an error that alt can understand ES6 class definition.
What Am I doing wrong?

Your babel-loader is only configured for .jsx files. You need to configure it for .js file too which alt.js is. To do that use the regex /\.jsx?/
{
test: /\.jsx?/,
use: {
loader: 'babel-loader',
options: { presets: ['react', 'es2015'] }
}
},

Related

SVG Next Error You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file

I have an issue with my app, everything was working fine until i ran npm run audit fix force then when i ran my app after this i am getting the error message below related to svg file.
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file.
i don't understand what went wrong.
Can someone help me, please?
package.json
{
"name": "myskillreactapp",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint"
},
"dependencies": {
"#babel/preset-env": "^7.15.8",
"bootstrap": "^5.0.0-beta3",
"bootstrap-dark-5": "^1.1.3",
"font-awesome": "^4.7.0",
"next": "^10.0.4",
"next-images": "^1.8.1",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-icons": "^4.3.1",
"react-placeholder": "^4.1.0"
},
"devDependencies": {
"babel-plugin-inline-react-svg": "^2.0.1",
"eslint": "7.32.0",
"eslint-config-next": "^0.2.4",
"file-loader": "^6.2.0",
"sass": "^1.43.3"
}
}
next-config.js
const webpack = require('webpack');
module.exports = {
reactStrictMode: true,
entry: './src/index.js',
module: {
rules: [
//...
{
test: /\.(png|jp(e*)g|svg|gif)$/,
use: [
{
loader: 'file-loader',
options: {
name: 'images/[hash]-[name].[ext]',
},
},
],
},
],
},
//...
};
_app.js
//import 'bootstrap/dist/css/bootstrap.css';
//import '/node_modules/bootstrap/scss/bootstrap.scss';
import '../styles/globals.scss';
import '../styles/Home.module.scss';
import 'font-awesome/css/font-awesome.min.css';
function MyApp({ Component, pageProps }) {
return <Component {...pageProps} />
}
export default MyApp
The got solved by running npm run audit fix force again and it reverted back to normal. hope it helps someone. Thx
Leo

ReactJS Error: Module Build failed: SyntaxError: Unexpected token '<'

My ReactJS code throws an error when I try to compile it. I used ReactJS before but I never ran into this error (see below)
ERROR in ./kapiche/#kapiche_react/teacher/login.jsx
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /mnt/d/src/kapiche/#kapiche_react/teacher/login.jsx: Unexpected token (59.6)
57 | render() {
58 | return (
> 59 | <div>
| ^
60 | Hello world
61 | </div>
62 | );
at Parser.raise (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:6420:17)
at Parser.unexpected (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:7773:16)
at Parser.parseExprAtom (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8996:20)
at Parser.parseExprSubscripts (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8556:23)
at Parser.parseMaybeUnary (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8536:21)
at Parser.parseExprOps (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8402:23)
at Parser.parseMaybeConditional (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8375:23)
at Parser.parseMaybeAssign (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8325:21)
at Parser.parseParenAndDistinguishExpression (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:9133:28)
at Parser.parseExprAtom (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8917:21)
at Parser.parseExprSubscripts (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8556:23)
at Parser.parseMaybeUnary (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8536:21)
at Parser.parseExprOps (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8402:23)
at Parser.parseMaybeConditional (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8375:23)
at Parser.parseMaybeAssign (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8325:21)
at Parser.parseExpression (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:8275:23)
at Parser.parseReturnStatement (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10378:28)
at Parser.parseStatementContent (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10057:21)
at Parser.parseStatement (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10009:17)
at Parser.parseBlockOrModuleBlockBody (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10585:25)
at Parser.parseBlockBody (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10572:10)
at Parser.parseBlock (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10556:10)
at Parser.parseFunctionBody (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:9584:24)
at Parser.parseFunctionBodyAndFinish (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:9554:10)
at Parser.parseMethod (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:9508:10)
at Parser.pushClassMethod (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10987:30)
at Parser.parseClassMemberWithIsStatic (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10912:12)
at Parser.parseClassMember (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10851:10)
at /mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10806:14
at Parser.withTopicForbiddingContext (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:9884:14)
at Parser.parseClassBody (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10783:10)
at Parser.parseClass (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10757:22)
at Parser.parseStatementContent (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10051:21)
at Parser.parseStatement (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10009:17)
at Parser.parseBlockOrModuleBlockBody (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10585:25)
at Parser.parseBlockBody (/mnt/d/src/kapiche/node_modules/#babel/parser/lib/index.js:10572:10)
This is my webpack.config.js where I define my entry and output points.
// create proper string representing path to file
const path = require('path');
// files to export
let export_list = [];
// helpful log message for debugging
console.log('webpack watch starting for the files:\n');
// general files
const export_dict = {
'student': ['login'],
'teacher': ['login']
};
// loop through all directories and files in export dictionary
for (let [key, value] of Object.entries(export_dict)) {
for (let i = 0; i < export_dict[key].length; i++) {
// helpful log message for debugging
console.log(`entry:\t/kapiche/#kapiche_react/${key}/${value[i]}.jsx`);
console.log(`output:\t/kapiche/static/js/${key}/${value[i]}_compiled.js\n`);
// add object to export list
export_list.push(
{
entry: `./kapiche/#kapiche_react/${key}/${value[i]}.jsx`,
output: {
path: path.join(__dirname, `/kapiche/static/js/${key}/`),
filename: `${value[i]}_compiled.js`,
},
module: {
rules: [
{
test: /\.(js|jsx)$/,
exclude: /node_modules/,
use: ['babel-loader']
}
]
},
resolve: {
extensions: ['*', '.js', '.jsx']
},
}
)
}
}
console.log(export_list);
// list of exports
module.exports = export_list;
Here is the ReactJS file I am trying to compile (in jsx)
import React from 'react';
import ReactDOM from 'react-dom';
class LoginTeacher extends React.Component {
constructor(props) {
super(props);
this.state = { };
}
componentDidMount() {
}
render() {
return (
<div>
Hello world
</div>
);
}
}
ReactDOM.render(
<LoginTeacher/>,
document.getElementById('login'),
);
And finally, here is my package.json
{
"name": "kapiche",
"version": "1.0.0",
"description": "Promoting classroom communication",
"main": "webpack.config.js",
"scripts": {
"watch": "webpack --watch --mode production",
"start": "webpack-dev-server --open --hot --mode development",
"build": "webpack --mode production"
},
"repository": {
"type": "git",
"url": "git+https://github.com/jbseg/BlueSlide.git"
},
"author": "Joshua S, Riley C, Hannah Z, Xiangliang N",
"license": "MIT",
"bugs": {
"url": "https://github.com/jbseg/BlueSlide/issues"
},
"homepage": "https://github.com/jbseg/BlueSlide#readme",
"devDependencies": {
"#babel/core": "^7.6.4",
"#babel/preset-env": "^7.6.3",
"#babel/preset-react": "^7.6.3",
"babel-loader": "^8.0.6",
"css-loader": "^3.2.0",
"html-loader": "^0.5.5",
"html-webpack-plugin": "^3.2.0",
"sass": "^1.23.3",
"style-loader": "^1.0.0",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",
"webpack-dev-server": "^3.9.0"
},
"dependencies": {
"#material-ui/core": "^4.5.2",
"#material-ui/icons": "^4.5.1",
"chart.js": "^2.9.2",
"react": "^16.11.0",
"react-chartjs-2": "^2.8.0",
"react-dom": "^16.11.0",
"react-tabs": "^3.0.0",
"socket.io": "^2.3.0"
}
}
Any ideas what might be causing this error?
EDIT: Here is my .babelrc as well
{
"presets": ["#babel/preset-env", "#babel/preset-react"]
}
After a few hours of trying to figure this one out, I finally managed to catch the bug. It was a typo... yep, a programmer's worst enemy. I misspelled my .babelrc as .bablerc and that was the root of my issue. Thank you everyone for the feedback, I am going to go be embarrassed now...
Make sure you have imported the right thing !
the vs code has imported
import { Button , Modal } from "bootstrap";
You should import
import { Button , Modal } from "react-bootstrap";

SyntaxError: Unexpected token import at ScriptTransformer._transformAndBuildScript [duplicate]

I'm trying to learn how to do unit testing with React and jest. I've run into the following error:
Unexpected token import
Here are my babel presets:
{
"presets": ["es2015", "stage-0", "react"],
"env": {
"test": {
"plugins": ["transform-es2015-modules-commonjs"]
}
}
}
Here is the file i'm trying to test:
import jest from 'jest';
import React from 'react';
import { shallow } from 'enzyme';
import Step from '../src/components/step.js';
const wrapper = shallow(<Step />);
wrapper.find('a').simulate('click', { preventDefault() {} });
and here is my package.json:
{
"name": "react-simple-boilerplate",
"version": "1.0.0",
"description": "Simple and lightweight Boilerplate for ReactJS projects",
"scripts": {
"lint": "eslint src",
"start": "node server.js",
"test": "jest"
},
"devDependencies": {
"babel-core": "^6.24.1",
"babel-loader": "^7.0.0",
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"css-loader": "^0.28.0",
"enzyme": "^3.2.0",
"enzyme-adapter-react-16": "^1.1.0",
"eslint": "^3.19.0",
"eslint-plugin-react": "^6.10.3",
"jest": "^21.2.1",
"node-sass": "^4.5.2",
"react-addons-test-utils": "^15.6.2",
"sass-loader": "^6.0.3",
"style-loader": "^0.16.1",
"webpack": "^2.4.1",
"webpack-dev-server": "^2.4.4"
},
"dependencies": {
"babel-loader": "^7.0.0",
"react": "^15.5.4",
"react-dom": "^15.5.4"
},
"main": "server.js"
}
Have no idea why its giving me this message?
It looks like babel-jest is missing among your dependencies. That's why jest is not running babel on your ES6+ code before executing tests.
I'm pretty sure you need to add your babel presets to test as well, jest sets the env variable to test.
So...
"test": {
"plugins": ["transform-es2015-modules-commonjs"],
"presets": ["es2015", "react", "stage-0"]
}

babel-plugin-syntax-dynamic-import does not transpile

I think I correctly configured babel-plugin-syntax-dynamic-import but it would still not transpile my dynamic import.
Node: v8.11.3
package.json:
...
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-loader": "^7.1.5",
"babel-plugin-syntax-dynamic-import": "^6.18.0",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-3": "^6.24.1",
...
"webpack": "^4.16.5",
"webpack-bundle-analyzer": "^2.13.1",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
},
"scripts": {
"dev": "webpack-dev-server --config webpack.config.js --open --hot --progress",
...
},
"dependencies": {
"babel-core": "^6.26.3",
"babel-polyfill": "^6.26.0",
...
"react-loadable": "^5.5.0",
"whatwg-fetch": "^2.0.4"
}
webpack.config.js:
...
{
test: [/\.js$/, /\.jsx?$/],
exclude: /node_modules/,
use: {
loader: 'babel-loader',
},
},
...
.babelrc
{
"presets": ["env", "react", "stage-3"],
"plugins": ["syntax-dynamic-import"]
}
AsyncComp.jsx:
import React from 'react';
import Loadable from 'react-loadable';
const Loading = () => (<div>Loading...</div>);
const LoadableComponent = Loadable({
loader: () => import('./CompRaw'),
loading: Loading,
})
export default class AsyncComp extends React.Component {
render() {
return <LoadableComponent />;
}
}
executing yarn dev results in:
/home/somepath/src/components/AsycnComp.jsx
7:17 error Parsing error: Unexpected token import
Why is the dynamic import() not transpiled? How can I fix it?
Ah okay! I think you also need react-loadable/babel. See the discussion here: https://github.com/jamiebuilds/react-loadable/pull/35#issuecomment-337156641
The configuration is good and the error originates from using the eslint-loader but not installing babel-eslint plugin which would enable support for dynamic imports.
This solved the issue
yarn add babel-eslint -D
.eslintrc
{
...
"parser": "babel-eslint"
}

How do I properly set up my Babel plugins and presets to make es6 decorators work in my React app?

I have a React app going, and I'm trying to get the hang of decorators. They're causing unexpected token errors.
I'm using the transform-decorators-legacy and transform-class-properties babel plugins.
I've tried both the stage-0 and stage-2 presets. All the related posts on SO and elsewhere seem to think that the plugins should make it work. Some say to use the stage-0 preset, while others recommend stage-2.
Here's the file with the decorator:
import React, {Component} from 'react';
import Actions from '../firebase/actions';
import RecipeList from '../Recipe/recipe-list';
import connectToStores from 'alt-utils/lib/connectToStores';
import RecipeStore from '../../store/recipe-store';
#connectToStores
class Main extends Component {
constructor() {
super();
Actions.getRecipes();
}
static getStores() {
return [RecipeStore];
}
static getPropsFromStores() {
return RecipeStore.getState();
}
render() {
return (
<section>
<section className="container">
{
this.props.recipes
?
<RecipeList recipeList={this.props.recipes}/>
:
null
}
</section>
</section>
);
}
}
export default Main;
Here's the package.json file:
{
"name": "glutenfreehacker",
"version": "0.1.0",
"private": true,
"dependencies": {
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-decorators-legacy": "^1.3.4",
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"babel-preset-stage-0": "^6.24.1",
"babel-preset-stage-2": "^6.24.1",
"firebase": "^4.12.1",
"react": "^16.3.2",
"react-dom": "^16.3.2",
"react-redux": "^5.0.7",
"react-router-dom": "^4.2.2",
"react-scripts": "1.1.4",
"recompose": "^0.26.0",
"redux": "^4.0.0"
},
"babel": {
"presets": [
"env",
"stage-2",
"react"
],
"plugins": [
"transform-decorators-legacy",
"transform-class-properties"
]
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
}
}
I understand this has been discussed here and elsewhere. However, the solutions I've seen suggest using the above plugins and presets, and I still can't get it to work.
Thanks for reading, and any help is greatly appreciated.
For decorators, use the babel-plugin-transform-decorators.
Installation
npm install --save-dev babel-plugin-transform-decorators
Usage
Via .babelrc:
{
"plugins": ["transform-decorators"]
}

Resources