How to build bundle-stats.json in create react app? - reactjs

I need to build bundle-stats.json to work with webpack-bundle-analyzer.
Here how i'm trying build it , but it does not creating any file.
npm run build -- --stats
Could you please help me

The --stats flag was added back into CRA in this PR.
So you can use webpack-bundle-analyzer again.

stats have been remove from CRA see
It's recommended to use source-map-explorer
npm i -g source-map-explorer
source-map-explorer 'build/static/js/*.js'.

You can do it with #craco/craco which is a tool to use a custom webpack configuration with Create React App.
As explained on this article:
Install #craco/craco and webpack-bundle-analyzer
npm install #craco/craco webpack-bundle-analyzer --save-dev
Create a file named craco.config.js in the root of your project folder with this content:
const BundleAnalyzerPlugin =
require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
module.exports = function () {
return {
webpack: {
plugins: [new BundleAnalyzerPlugin({ analyzerMode: "server" })],
},
};
};
Add this "analyze" script to your package.json scripts sections:
{
...
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
+ "analyze": "craco build"
}
}
Now run:
npm run analyze
Because you set the analyzerMode to "server" in your craco config, you will automatically get your browser open with the results served as a webpage (you can use the "json" option if you want the output without involving the browser)

Firstly, add the webpack-bundle-analyzer to your dev dependecies:
yarn add -D webpack-bundle-analyzer
Then you can sequentially run commands:
yarn build -- --stats
yarn webpack-bundle-analyzer ./build/bundle-stats.json
Or, for your convenience, you can add the script to your package.json:
"scripts": {
...
"analyze": "yarn build -- --stats && yarn webpack-bundle-analyzer ./build/bundle-stats.json"
}
Instead of yarn you can use npm, just edit the command accordingly.
Then you can run the script using yarn analyze or the scripts runner UI available in VS Code like I do. You can also add a script for deleting the build folder if this already exists before creating a new one. For this, dependently on your platform, you can use the cmd or bash command:
cmd: if exist build\\ ( rmdir /s /q .\\build )
bash: [ -d 'build' ] && rm -r build
So the final solution will look like this:
"scripts": {
...
"analyze": "yarn remove_build && yarn build -- --stats && yarn webpack-bundle-analyzer ./build/bundle-stats.json",
"remove_build": "if exist build\\ ( rmdir /s /q .\\build )"
}

Related

ReactJS - $npm_package_version param not working on build version

I used this solution but it didn't work for me.
My React app(18.2.0v) was initialized with CRA(5.0.1v).
My dotenv version is 16.0.3.
On development version I get package.json file version successfully, but on build version I can't get it.
My .env.staging file:
NODE_ENV=development
REACT_APP_VERSION=$npm_package_version
My package.json scripts:
{
...
"version": "0.1.0",
...
"scripts":
{
...
"start": "react-scripts start",
...
"build:staging": "npm run clean && env-cmd -f .env.staging react-scripts build",
...
}
...
}
My Login.tsx display version this way:
...
const { REACT_APP_VERSION } = process.env;
...
return (
...
<h1>V{REACT_APP_VERSION}</h1>
...
)
...
Dev mode display print
Build mode display print
I have an .env.development with the same content as the current .env.staging quoted above that will be used for another build version.
Have someone any idea how to solve this issue, please?
I solved this when I installed dotenv-cli and changed package.json build:staging script to "npm run clean && dotenv -e .env.staging react-scripts build"

lerna run build command fails in lint-staged

I am working on a React monorepo and I have the below scripts in the root package.json:
"scripts": {
"build": "lerna run build",
},
"husky": {
"hooks": {
"pre-commit": "lint-staged",
}
},
lint-staged.config.js
module.exports = {
'**/*.*': 'yarn build',
};
When I commit the code, the commit fails with the below error:
✖ yarn build:
ERR! lerna Unknown arguments: /Users/xyz/lint-staged.config.js, /Users/xyz/package.json
error Command failed with exit code 1.
$ lerna run build /Users/xyz/lint-staged.config.js /Users/xyz/package.json
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
husky > pre-commit hook failed (add --no-verify to bypass)
In the monorepo packages folder, the package.json contains the scripts as below:
package/webApp/package.json
"scripts": {
"build": "run-s clean compile",
"clean": "rm -rf *.tsbuildinfo & rm -rf build && rm -rf tmp",
"compile": "run-p compile:*",
}
Can't we run the build command in lint-staged or is something missing in my implementation?
Thanks
lint-staged is passing filenames to the build script, whereas lerna thinks these are being arguments and can't resolve them. To avoid passing the filenames to the build script, try to change your lint-staged configuration to the following
// lint-staged.config.js
module.exports = {
'**/*.*': () => 'yarn build',
};
Here is the link to a similar example in the lint-staged - Link

Create-React-App .env file directory location (Non-root)

After creating a new web app with create-react-app (CRA), I need to include some environment files for configuring various endpoints. Noticed that CRA comes with the cool dotenv package all ready to go. There's only one problem with that - I would like to have dotenv read these files from within my ./environments directory and not the root directory. Is there any way to load the .env, .env.local, .env.test, etc... files in a directory separate from the root directory?
Noticing I can achieve this in my express backend server.js by simply importing like so:
require('dotenv').config({ path: `./environments/` })
Can I do the same with my client-side code in React? If so, where should I put this import? Doesn't seem to work for me.
If you created your project using CRA and want to configure dotenv to change the path from where it loads the env files, you will have to do npm eject.
Alternatively, you can use env-cmd to achieve the same:
Installation:
npm i env-cmd
Add scripts in package.json, for example:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"dev": "env-cmd -f envs/.env.dev react-scripts build",
"qa": "env-cmd -f envs/.env.qa react-scripts build",
"demo": "env-cmd -f envs/.env.demo react-scripts build"
},
After creating .env.dev, .env.qa, .env.demo in envs/ directory.
Now, you can run:
$ npm run dev // it will use envs/.env.dev file
$ npm run qa // it will use envs/.env.qa file
$ npm run demo // it will use envs/.env.demo file

How to run tests before commit in create react app?

How to run jest in create react app before commit? I have found couple articles but they are too complicated , without explaining what to do step by step. Could you please explain me how to make it work
You need to use husky package. Here is basic configuration (put it in package.json). It adds pre-commit hook to your git configuration.
"husky": {
"hooks": {
"pre-commit": "CI=true npm run test",
}
}
You can also consider using lint-staged to lint files which you commit. You can see full configuration here.
Maybe you can utilise some npm packages for this.
So, this is not a direct solution but you can include as many things you want instead of using git commands, shells and yml files
Install package Pre-Commit and install git-cz from npm. Using these you can make use of pre-commit and commit in package.json and desired things to them.
Now you can make use of these packages in your packages.json like below
{
"start": "node index.js",
"pre-commit": "lint-staged",
"commit": "git-cz",
"lint": "eslint . --ext .js,.jsx",
}
For example you want to run test cases then pre-commit: npm run test && lint-staged
Because in our project we needed to update documents, checking style-lint, eslint and test cases, so we were using these combination.
But you should not commit directly using git commit -m "message" but with npm run commit.
Hope, this helps.
To do locally you can just write a script which first runs the tests and pipes the exit code and decides whether to really push or not based on the exit code.
By default npm test runs in interactive mode
To exit after running the tests use CI=true
i.e. CI=true npm test
This worked for me:
npm install --save-dev pre-commit
https://www.npmjs.com/package/pre-commit
Then, in package.json:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "CI=true react-scripts test", //<-- update
"eject": "react-scripts eject",
}
Adding CI=true runs all tests without making it interactive
Finally,
"devDependencies": {
"pre-commit": "test"
}

How to create and run a development build of an application using create-react-app configuration

npm run build creates production build of the project. How do I create development build? I'm using gradle-node-plugin to build the artifact. The project has standard create-react-app configuration.
This is not really doable with just create-react-app, there is an open issue Here and it doesn't look like it will be added anytime soon.
However, you can use a package called dotenv for that, following are the steps you should take:
Install dotenv (make sure to add save dev) npm install dotenv-cli --save-dev
In package.json scripts section add new script: "build:dev": "dotenv -e .env.development react-scripts build",
And you can build for development with npm run build:dev
PS: if you want to avoid mistakenly deploying dev builds to production (as mentioned here) you can add build:prod to package.json and disable the regular build command, see code:
"build:dev": "dotenv -e .env.development react-scripts build",
"build:prod": "dotenv -e .env.production react-scripts build",
"build": "echo \"Please use build:dev or build:prod \" && exit 1",
Also note that process.env.NODE_ENV will still be production but it'll load your .env.development file
Thanks, #Moses. This is an extension to the answer posted by Moses Schwartz. You can also make the build pick the environment files dynamically by exporting the value of the environment(development, test, production) in the bash shell. And then you don't have to have different build commands for different environments.
You can use this in your package.json
"start": "dotenv -e .env react-scripts start",
"build": "dotenv -e .env.${REACT_APP_ENVIRONMENT} react-scripts build",
So when your run npm start, it will pick the environment values from .env
and when you run npm run build, it will pick the environment values from .env.{REACT_APP_ENVIRONMENT}
To define the REACT_APP_ENVIRONMENT, you can do:
export REACT_APP_ENVIRONMENT="development" or
export REACT_APP_ENVIRONMENT="test" or
export REACT_APP_ENVIRONMENT="production"
Hope this helps. This will help in staging the react application for multiple environments.
Thanks to #Tx_monster comment
github.com/Nargonath/cra-build-watch
A script for create-react-app that writes development builds to the disk
npm install -D cra-build-watch
package.json:
{
"scripts": {
"watch": "cra-build-watch"
}
}
npm run watch

Resources