how to package the project after run the react-app-rewired eject - javascript

I am using the react-app-rewired to package the project, now I did not want to use react-app-rewired. I have run this command to eject the react-app-rewired:
yarn eject
this is the legacy package.json config:
"scripts": {
"start": "react-app-rewired start --host 0.0.0.0",
"build": "CI=false && react-app-rewired build",
"test": "react-app-rewired test"
},
after eject, I could not use the legacy command to package the project now. what should I do to package the eject project? config then webpack again? install the webpack-cli?

Related

How to update webpack config for a react project created using create-react-app?

I have created a react project using create-react-app. Now I need to update the webpack config, but I don't find the file anywhere.
Do I need to create this file myself or what is the process?
I am new to react and not really sure how to proceed from here.
No need to run npm run eject
Step 1
npm install react-app-rewired --save-dev
Step 2
Add config-overrides.js to the project root directory.(NOT ./src)
// config-overrides.js
module.exports = function override(config, env) {
// New config, e.g. config.plugins.push...
return config
}
Step 3
'Flip' the existing calls to react-scripts in npm scripts for start, build and test
/* package.json */
"scripts": {
- "start": "react-scripts start",
+ "start": "react-app-rewired start",
- "build": "react-scripts build",
+ "build": "react-app-rewired build",
- "test": "react-scripts test",
+ "test": "react-app-rewired test",
"eject": "react-scripts eject"
}
Step 4
Restart your app. Done
Option 1 - Eject your CRA
If you've just created your app using CRA, and haven't made big changes to it, you could use npm run eject - more about it here
Keep in mind that there is no going back (except by commits, of course) after doing this. This will basically provide you with webpack file and other files which are currently 'hidden' in CRA
Some critiques and second thoughts about this method here
Option 2 - React App Rewired
This might be the right choice for you. This allows you to extend your current webpack without ejecting, or messing up / making too many changes at your project as npm run eject will. Take a look at the package here
A great tutorial by Egghead.io using react-app-rewired here
I solved this problem by running a script between yarn install and yarn build. After yarn install the webpack.config.json file is generated, then immediately run a script on Node that modifies it, and then run the build.
My code:
custom.webpack.config.js
const fs = require('fs')
// WebPack.config File
const fileConfig = 'node_modules/react-scripts/config/webpack.config.js'
new Promise((resolve) => {
fs.readFile(fileConfig, 'utf8', function (err, data) {
if (err) {
return console.log(err)
}
resolve(data)
})
}).then((file) => {
let CodeAsString = "Code as String to save"
let regexCode = /YourCodeRegex}/g
let result = file.replace(regexCode, CodeAsString)
fs.writeFile(fileConfig, result, function (err) {
if (err) return console.log(err)
console.log('The webpack.config file was modifed!')
})
})
So, now do you need to edit the package.json to include this code in the process:
"scripts": {
"prestart": "node custom.webpack.config.js",
"prebuild": "node custom.webpack.config.js",
"start": "react-scripts start",
"build": "react-scripts build"
}
Done! :)
https://www.npmjs.com/package/react-app-rewired
Complete answer is :
How to rewire your create-react-app project
Create your app using create-react-app and then rewire it.
Install react-app-rewired
For create-react-app 2.x with Webpack 4:
npm install react-app-rewired --save-dev
For create-react-app 1.x or react-scripts-ts with Webpack 3:
npm install react-app-rewired#1.6.2 --save-dev
Create a config-overrides.js file in the root directory
/* config-overrides.js */
module.exports = function override(config, env) {
//do stuff with the webpack config...
return config;
}
like this:
+-- your-project
| +-- config-overrides.js
| +-- node_modules
| +-- package.json
| +-- public
| +-- README.md
| +-- src
for example :
module.exports = function override(config, env) {
// New config, e.g. config.plugins.push...
config.module.rules = [...config.module.rules,
{
test: /\.m?js/,
resolve: {
fullySpecified: false
}
}
]
return config
}
'Flip' the existing calls to react-scripts in npm scripts for start, build and test
from:
/* package.json */
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
}
To:
/* package.json */
"scripts": {
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
}
Note: Do NOT flip the call for the eject script. That gets run only once for a project, after which you are given full control over the webpack configuration making react-app-rewired no longer required. There are no configuration options to rewire for the eject script.
Start the Dev Server
npm start
Build your app
npm run build
Webpack configuration is being handled by react-scripts. I assume that you don't want to eject and just want to look at the config, you will find them in /node_modules/react-scripts/config
webpack.config.dev.js. //used by `npm start`
webpack.config.prod.js //used by `npm run build`
You can modify your webpack config or any other node_module using patch-package https://www.npmjs.com/package/patch-package
Install the version of react-scripts you want with npm i react-scripts#5.0.0
Then go into node_modules/react-scripts/webpack/webpack.config.js. Make the changes you need and then npx patch-package react-scripts
patch-package will generate a file in your project root like patches/react-scripts+5.0.0.patch
Add post-install script to package.json with
"scripts": {
"postinstall": "patch-package",
...
}
Now whenever you run npm i you will automatically get this patch included with the installed library.
The best way is:
Install react-app-rewired and customize-cra
Create a config-overrides.js file in your root folder. Here is an example Webpack override file gist: https://gist.github.com/Nagibaba/209c1bddcc39ff0686a820806cfa8ee3
Also, consider that you need to change react-scripts inside your package.json to react-app-rewired like:
"start": "react-app-rewired start",
"build": "react-app-rewired build",
"test": "react-app-rewired test",
"eject": "react-scripts eject"
You are ready to go

How to configure mocha to support ES6 with node v13 and bable v6?

How to configure test setup with mocha to support es6 with babel 6?
I tried to configure with --require option with mocha as as mentioned here but it does not resolve my problem
I have also tried with --compiler option but it does not support with mocha v7
Node v13.13.0
Babel v6.26
mocha v7.1.1
> mocha './test/**/*.js' --require babel-core/register
import module from '../models/module';
^^^^^^
SyntaxError: Cannot use import statement outside a module
Module is a .js files
babelConfig.babelrc file contains
{
"presets": ["env"]
}
pakcage.json configrations of scripts section:
"scripts": {
"start": "nodemon server.js --exec babel-node --presets env",
"release": "npm run clean && npm run build && npm run serve",
"clean": "rm -rf dist && mkdir dist",
"build": "babel . -s -D -d dist --presets env --ignore node_modules",
"serve": "node dist/server.js",
"test": "mocha './test/**/*.js' --require babel-core/register"
}

React Application to have a staging environment and deploying with Docker on AWS Elastic BeanStalk

I am working on a react application created by create-react-app. I have two environments on AWS Elastic BeanStalk, staging and production. I am deploying my application through Docker. I am using CircleCI for automated deployments.
My problem is I want to change the endpoint url while building the react application.
I am using cross-env for setting a variable REACT_APP_API_HOST but i have to run a command build:staging for building it.
I am not sure how to do it with this docker.
Docker file
FROM node:12
WORKDIR /app
COPY . /app
RUN npm install
RUN npm run build
RUN npm install -g serve
EXPOSE 3000
ENTRYPOINT ["serve", "-l", "3000", "-s", "build", "-d"]
And the scripts part of package.json file
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"build:staging": "cross-env REACT_APP_API_HOST=staging react-scripts build",
"build:prod": "cross-env REACT_APP_API_HOST=production react-scripts build"
},
So after a long R&D, the answer I find with my friend is do run a intermediate script before deploying it to Elastic Beanstalk.
buildDocker.sh
#!/bin/bash
branch_name=$(git symbolic-ref --short -q HEAD)
if [ "$branch_name" == "develop" ]; then
result_sed=$(sed 's/RUN npm run build/RUN npm run build:staging/g' Dockerfile)
echo "$result_sed" > Dockerfile
elif [ "$branch_name" == "master" ]; then
result_sed=$(sed 's/RUN npm run build/RUN npm run build:prod/g' Dockerfile)
echo "$result_sed" > Dockerfile
fi

How to set NODE_ENV from package.json for react

I am trying to target multiple environments from local while executing React app.
1. Development
2. Staging
3. Production
I am also trying to test for offline mode in any of the environments. So, the scripts what I have configured is as follows:
"staging-server": "nodemon server.js --environment=staging",
"staging": "concurrently -k \"npm:staging-server\" \"NODE_ENV='staging' PORT=3003 react-scripts start\"",
"prod": "npm run build && forever server.js --environment=production"
I am able to fetch environment arg using args inside my Express, but my local ui app is still showing development only when I console for process.env.NODE_ENV. I am also trying to set NODE_ENV with same line for staging, but still no luck. PORT setting is working but, the app is running in 3000 and 3003 both ports.
How to get rid of this? I would like to understand the staging configuration as well.
As per the docs, we cannot override NODE_ENV, but there is a room to create our own custom variables starting with REACT_APP_. So i configured to look as below:
Reference: https://facebook.github.io/create-react-app/docs/adding-custom-environment-variables
"staging": "concurrently -k \"npm:staging-server\" \"cross-env REACT_APP_ENVIRONMENT='staging' PORT=3003 react-scripts start\"",
And inside my UI application, I can fetch its value by consoling it like this:
console.log('REACT_APP_ENVIRONMENT => ', process.env.REACT_APP_ENVIRONMENT);
I build the build with REACT_APP_STAGE and use it in my application as process.env.REACT_APP_STAGE.
"scripts": {
"analyze": "source-map-explorer 'build/static/js/*.js'",
"build-css": "node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/",
"watch-css": "npm run build-css && node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/ --watch --recursive",
"start-js": "react-scripts start",
"start": "REACT_APP_STAGE=local npm-run-all -p watch-css start-js",
"build": "npm run build-css && react-scripts build",
"build-dev": "REACT_APP_STAGE=dev react-scripts build",
"build-prod": "REACT_APP_STAGE=prod react-scripts build",
"build-qa": "REACT_APP_STAGE=qa react-scripts build",
"test": "react-scripts test --env=jsdom",
"eject": "react-scripts eject"
},
Use cross-env in front of NODE_ENV.
npm i -g cross-env
"staging": "concurrently -k \"npm:staging-server\" \"cross-env NODE_ENV='staging' PORT=3003 react-scripts start\"",
Easiest approach is to add it directly in your command:
"scripts": {
"start": "./node_modules/.bin/nodemon server.js",
"start:prod": "NODE_ENV=prod node server.js",
},

how build the webpack.config.prod.js?

I use React.js, Windows in my webpage project and want to apply the settings of webpack.config.prod.js to the project.
So I used command below at Webstorm terminal.
set NODE_ENV=production
.\node_modules\.bin\webpack --config webpack.config.prod.js
yarn build
serve -p 3000 -s build
This below is part of package.json
"scripts": {
"start": "cross-env NODE_PATH=src node scripts/start.js",
"build": "node scripts/build.js",
"test": "node scripts/test.js --env=jsdom",
"development": "cross-env NODE_PATH=src node scripts/start.js"
},
Is it correct command to apply the webpack.config.prod.js to my create-react-app? I don't know how to check that the configuration of webpack.config.prod.js has been applied.
I also wonder if I need webpack-dev-server to apply webpack.config.prod.js.
Any help will be thankful. Thanks for reading.
"build": "node scripts/build.js",
instead of this
use
NODE_ENV=production .\node_modules\.bin\webpack --config webpack.config.prod.js
It will build with production config
Now run yarn run build

Categories