I have a React app and I want to make the build folder its own Git repository, but every time I run npm run build, the .git folder inside the regenerated /build directory disappears. How can I resolve this?
CodePudding user response:
I think you can use postbuild command in script in your package.json, which will run immediately after build. It will run automaticaly.
Example:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",
"postbuild": "copy .git .\\build\\.git"
},
CodePudding user response:
In your package.json, change the build script to this:
"scripts": {
[...]
"build": "rm -rf build_old/.git && cp -R build/.git build_old/.git && react-scripts build && cp -R build_old/.git build/.git"
}
It does the following:
- Deletes the build_old/.git folder (make sure to create the build_old folder first).
- Copies your original .git folder to the build_old directory.
- Runs
react-scripts build. - Pastes the .git directory back.
