Babel failing on Export default from even when enabled - reactjs

In my React app, I am getting the following error during compilation:
Support for the experimental syntax 'exportDefaultFrom' isn't currently enabled:
1 | export default from './CustomIcon'
|
I have already installed a couple of babel packages, but it did not help. Here is a part of my package.json:
...
"devDependencies": {
"#babel/plugin-proposal-export-default-from": "^7.12.1",
"#babel/plugin-syntax-export-default-from": "^7.12.1",
"#babel/plugin-syntax-jsx": "^7.12.1",
"#babel/preset-react": "^7.12.7",
"babel-cli": "^6.26.0",
"babel-core": "^6.26.3",
...
And this is the content of my babel.config.json file:
{
"presets": ["#babel/preset-react"],
"plugins": [
"#babel/plugin-syntax-jsx",
"#babel/plugin-proposal-export-default-from"
]
}
What am I doing wrong? Why is my babel configuration ignored? Are there any other steps I need to do?

In case of using create-react-app (CRA), you can't simply add the babel.config.js like that since it has been wrapped by CRA.
But we can achieve that by using package react-app-rewired which is able to modify configuration of CRA.
Here are a few steps you would follow up:
Install deps:
yarn add -D react-app-rewired customize-cra
Add the configuration file at root dir config-overrides.js:
const { useBabelRc, override } = require('customize-cra');
module.exports = override(
useBabelRc(),
);
Test your script by switching to react-app-rewired in your package.json:
{
"start": "react-app-rewired start", // Replace `react-scripts`
"build": "react-app-rewired build",
"test": "react-app-rewired test",
}
That's it!

Related

.less files not getting picked up by less-loader

I have created a react app using create-react-app and I am using less for css. My app builds correctly, but none of the less files are loaded in build/static folder
I am using customize-cra with react-app-rewired to add a loader for less files.
config-overrides.js file
const {
override,
addLessLoader
} = require('customize-cra');
const path = require('path');
module.exports = override(
addLessLoader({
paths: [path.resolve(__dirname, 'src/')],
javascriptEnabled: true,
sourceMap: true,
})
);
scripts in package.json
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test --passWithNoTests",
"eject": "react-scripts eject",
"release": "CI=true react-app-rewired test --coverage && npm run build && npm run package && npm run posttest",
},
dependencies
"less": "^4.1.1",
"less-loader": "^8.0.0",
"react-scripts": "4.0.2",
"webpack": "4.44.2",
"customize-cra": "^1.0.0"
The error that I was getting was
Module build failed (from ./node_modules/less-loader/dist/cjs.js): TypeError: this.getOptions is not a function
Referred this post and lowered the version of less-loader to 5.0.0 and it started working

Get code coverage from Cypress with babel, nyc and istanbul plugin?

For a React/Redux project I use Cypress to test the UI. I need to get code coverage from thoses tests to have the details in Sonar (but that's another story).
So I began reading the docs from Cypress: https://docs.cypress.io/guides/tooling/code-coverage.html
I followed the instructions and found the differents plugins but nothing work. After running Cypress tests I have no coverage results and when I try to run the server with instrumented code on transpilation and try to get window.__coverage__ it's undefined.
So here is my configuration.
My devDep and NYC conf in package.json:
"devDependencies": {
"#cypress/code-coverage": "^1.8.0",
"babel-cli": "^6.26.0",
"babel-plugin-istanbul": "^5.2.0",
"babel-plugin-react-intl": "^2.4.0",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-preset-react-app": "^3.1.2",
"cross-env": "^5.2.0",
"cypress": "3.1.0",
"istanbul-lib-coverage": "^2.0.5",
"jest-junit-reporter": "^1.1.0",
"junit": "^1.4.9",
"nyc": "^14.1.1",
"react-test-renderer": "^16.3.0"
[...]
},
"scripts": {
"start": "react-scripts start",
[...]
"test:unit:local": "react-scripts test --env=jsdom .*_test.js",
"test:unit": "npm run test:unit:local -- --ci --coverage --testResultsProcessor ./node_modules/jest-junit-reporter",
"test:integ:local": "cypress run --reporter junit",
"test:integ": "start-server-and-test start http://localhost:3000 test:integ:local",
"test": "cross-env NODE_ENV=test && npm run test:unit && npm run test:integ"
},
"nyc": {
"sourceMap": false,
"instrument": false
}
My .babelrc file:
{
"presets": ["#babel/preset-react"],
"plugins": [
[ "react-intl", {
"messagesDir": "./src/messages",
"extractSourceLocation": true
}],
"transform-class-properties",
"istanbul"
],
"env": {
"test": {
"plugins": [ "transform-class-properties", "istanbul" ]
}
}
}
My \cypress\support\index.js contains this line :
import '#cypress/code-coverage/support'
And here is my \cypress\plugins\index.js:
module.exports = (on, config) => {
on('task', require('#cypress/code-coverage/task'))
on('file:preprocessor', require('#cypress/code-coverage/use-babelrc'))
return config
}
So I expect to have a result like you can see on the Cypress docs.
The website is generated but the files containing coverage reports are empty.
And I don't understand what's wrong in my configuration.
Try the following line:
"start": "react-scripts -r #cypress/instrument-cra start",
I faced this issue too. Wasn't able to get code coverage. Downgrading nyc version from 14.x.x to 13.x.x fixed the problem, as mentioned here.

Webpack-dev-server bundling React JS but not SCSS

App was made by "create-react-app" command. Bundled (by webpack) JavaScript code is working properly in my React App, but it does not seem to get any styling from SASS. There are no errors in console or anywhere, it's just being ignored. To start webpack I am running "npm run start".
Repo just in case: https://github.com/ankeris/challengefriend
This is the code:
package.json
"scripts": {
"start": "webpack-dev-server --open --mode development --hot | react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"css-loader": "^1.0.0",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.9.3",
"sass-loader": "^7.1.0",
"style-loader": "^0.22.1",
"webpack": "^4.17.1",
"webpack-cli": "^3.1.0",
"webpack-dev-server": "^3.1.5"
}
webpack.config.js
module.exports = {
entry: './src/index.js',
output: {
path: path.resolve('dist'),
filename: 'bundle.js',
},
module: {
rules: [
{
test: /\.scss$/,
use: [
"style-loader", // creates style nodes from JS strings
"css-loader", // translates CSS into CommonJS
"sass-loader" // compiles Sass to CSS, using Node Sass by default
]
},
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader'
}
}
]
},
}
index.js
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
import './styles/scss/index.scss';
ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
To make it worked, I followed this link it tells you how to add css preprocessor in react-create-app, here are the commands to run :
npm install --save node-sass-chokidar
npm install --save npm-run-all
then add these lines in your package.json (in the scripts section) :
"build-css": "node-sass-chokidar src/ -o src/",
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive",
"start-js": "react-scripts start",
"start": "npm-run-all -p watch-css start-js",
"build-js": "react-scripts build",
"build": "npm-run-all build-css build-js",
in your file index.js replace import './styles/scss/index.scss'; by import './styles/scss/index.css';
Finally you can start your app by running npm start
As I said in comment, react-create-app uses its own webpack config, so your webpack.config.js is useless you can remove it from your project
If you really want to import the scss file, then you need to modify the default react-create-app webpack config, you need to run npm run eject to extract the config, then modify the files /config/webpack.config.{env}.js
It should be loaders because you are using Webpack 4.
Please add below rule in Webpack and try
{
test: /\.scss$/,
loaders: ["style-loader", "css-loader", "sass-loader"]
}

How can I add the CopyWebpackPlugin to create-react-app without ejecting?

I am building a react application and I want to copy all image files from the source destination to the build destination. I am following some tutorials and so far managed to use react-app-wired and the CopyWebpackPlugin
I am getting no errors and no files are being copied.
This is my config-overrides.js
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = function override(config, env) {
if (!config.plugins) {
config.plugins = [];
}
config.plugins.push(
new CopyWebpackPlugin(
[
{
from: 'src/images',
to: 'public/images'
}
])
);
return config;
};
This is my package.json
{
"name": "public",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"copy-webpack-plugin": "^4.5.2",
"css-loader": "^1.0.0",
"prop-types": "^15.6.2",
"react": "^16.4.2",
"react-app-rewired": "^1.5.2",
"react-axios": "^2.0.0",
"react-dom": "^16.4.2",
"react-router-dom": "^4.3.1",
"react-scripts": "1.1.4",
"react-slick": "^0.23.1",
"slick-carousel": "^1.8.1",
"typeface-montserrat": "0.0.54",
"webfontloader": "^1.6.28"
},
"scripts": {
"build-css": "node-sass-chokidar src/ -o src/",
"watch-css": "npm run build-css && node-sass-chokidar src/ -o src/ --watch --recursive",
"start-js": "react-app-rewired start",
"start": "npm-run-all -p watch-css start-js",
"build-js": "react-app-rewired build",
"build": "npm-run-all build-css build-js",
"test": "react-app-rewired test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"npm-run-all": "^4.1.3",
"style-loader": "^0.22.1"
}
}
My folder structure is
src/images
src/images/file.jpg
src/images/slider/1.png
src/images/slider/2.png
public/images (empty) so far
Assuming you just want to move files from -/src/<somewhere> to ./<somewhereElse> at build time.
You could just add an extra step to your build script in package.json. The step below is completely in your control and does not mess with any create-react-app scripts or configuration.
For example, I used the ncp package, but if you want more granular control, you can also create your own "step" with ncp or even with raw node fs api.
Here is a way to replicate what I did:
Setup
npx create-react-app img-upload
cd img-upload/
yarn add --dev ncp
Test Data
create src/images/ folder structure and put in files
ls -R src/images/
src/images/:
badge.jpg folder1
src/images/folder1:
applause.gif blank.png
Package.json
I only edited the part: "build": "ncp './src/images' './public/images' && react-scripts build",
{
"name": "img-upload",
"version": "0.1.0",
"private": true,
"dependencies": {
"react": "^16.4.2",
"react-dom": "^16.4.2",
"react-scripts": "1.1.5"
},
"scripts": {
"start": "react-scripts start",
"build": "ncp './src/images' './public/images' && react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"ncp": "^2.0.0"
}
}
Test:
Before running build:
ls public
favicon.ico index.html manifest.json
After running build: yarn build
ls public
favicon.ico images index.html manifest.json
ls -R public/images
public/images/:
badge.jpg folder1
public/images/folder1:
applause.gif blank.png
ls build
asset-manifest.json favicon.ico images index.html manifest.json service-worker.js static
ls -R build/images
build/images/:
badge.jpg folder1
build/images/folder1:
applause.gif blank.png
Suggestion
I am assuming you ultimately need these files in build directory. If so, you can just switch your build script.
"build": "react-scripts build && ncp './src/images' './build/images'",
That way you won't pollute your public folder, which you may want to keep in your source control.
It is definitely not recommended to modify the node_modules/react-scripts/config/webpack.config.dev.js file as this will break when this particular package is updated. Also most probably you would be having node_modules ignored in your version control system, meaning this config wont be shared across developers or backed up.
Although I haven't tried myself personally you could use something like this : https://github.com/timarney/react-app-rewired to override the Webpack config.
You can have a look at the tutorial here : https://medium.com/#timarney/but-i-dont-wanna-eject-3e3da5826e39
Mine worked like this:
const { override, addWebpackPlugin } = require('customize-cra');
module.exports = {
webpack: override(
addWebpackPlugin(
new CopyWebpackPlugin({
patterns: [
{ from: 'src/config', to: 'config' }
],
})
)
)
With that, I'm taking the contents of my config folder and creating an equal one in the build also called config.
I think the problem is because you are using webpack-dev-server together with copy-webpack-plugin. If that's true, the copy-webpack-plugin will copy files to virtual directory which webpack-dev-server works. So that, In this situation, you need to add one more plugin to make it happen
If you want webpack-dev-server to write files to the output directory
during development, you can force it with the
write-file-webpack-plugin.
import WriteFilePlugin from 'write-file-webpack-plugin';
webpack config:
config.plugins.push(
new CopyWebpackPlugin(
[
{
from: 'src/images',
to: 'public/images'
}
])
);
config.plugins.push(new WriteFilePlugin());
You can visit the official document for more information: https://webpack.js.org/plugins/copy-webpack-plugin/
Hope this works!

How to find .babelrc?

I want to create React app with Ant Design
In the docs, it says I need to change .babelrc to modularly load the files, also from https://medium.com/#GeoffMiller/how-to-customize-ant-design-with-react-webpack-the-missing-guide-c6430f2db10f..
But, I can't find any.. I'm very new to Webpack/Babel/other things...
I used create-react-app to make React app
Please help..
This is my package.json:
{
"name": "ant",
"version": "0.1.0",
"private": true,
"dependencies": {
"antd": "^3.0.3",
"jquery": "^3.2.1",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-scripts": "1.0.17"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
"devDependencies": {
"babel-plugin-import": "^1.6.3",
"css-loader": "^0.28.7",
"less": "^2.7.3",
"less-loader": "^4.0.5",
"less-vars-to-js": "^1.2.1",
"style-loader": "^0.19.1"
}
}
for anyone that ends up here in the future, some clarification: when you create your project with CRA, the babel configuration exists as part of the react-scripts setup (react-scripts is the package responsible for all the tooling magic of CRA). this means all babel related controls are nested somewhere inside of your project's node_modules directory. for all intents and purposes, they are not editable, and ejecting is required.
If you have created React app using create-react-app, first you need to eject from the app so that you can use custom configuration. Check this official doc about custom setup : link
Note: this is a one-way operation. Once you eject, you can’t go back!
Once you run "npm run eject" when you are in a react app, you can then use custom babel presets. After ejecting from project, go to package.json and you will find babel presets.
There are two ways to edit your Babel configuration in your react app
1) Edit directly using package.json :
{
"babel": { // nest config under "babel"
"presets": [
"es2015",
],
"plugins": ["transform-class-properties"]
}
}
2) Create new .babelrc file and edit the configuration :
//.babelrc
{
"presets": [
"es2015",
],
"plugins": ["transform-class-properties"]
}

Resources