Custom Build folder for React

In this article, we will learn how to change the Build path of a React application. Here I’m using a shell script for moving the contents inside the build folder to another location.

Create a {file-name}.sh file inside the root directory of your rea…


This content originally appeared on DEV Community and was authored by Arjun Santhosh

In this article, we will learn how to change the Build path of a React application. Here I'm using a shell script for moving the contents inside the build folder to another location.

  1. Create a {file-name}.sh file inside the root directory of your react app.

  2. Open to your package.json file.

  3. Just beneath the build script, add a new one called "postbuild" with a value "./{file-name}.sh".

if you are using Windows, use bash before executing the script.

Image description

This shell script is executed right after the build process, and the actual magic happens inside the shell script.

#If the folder exists, remove it first, then copy the files
if [ -d "./../server/public/${APP_NAME}/" ]; then

    rm -rv ./../server/public/${APP_NAME}/*
    mv -v build/* ./../server/public/${APP_NAME}/

#Else make a new folder and then move the files.
else
    mkdir -p ./../server/public/${APP_NAME}
    mv -v build/* ./../server/public/${APP_NAME}/
fi

In my case, I just wanted to move the build folder contents to my server folder.
Before moving the content inside the build folder, I checked whether the output folder was already exiting. If there is a folder, I want to clean it first and move the contents.

This script can be customized to your need. Just go through some simple shell-script tutorials. Here what is helping us is the 'postbuild' script that will be automatically executed after the build command.

Happy Coding 🥳


This content originally appeared on DEV Community and was authored by Arjun Santhosh


Print Share Comment Cite Upload Translate Updates
APA

Arjun Santhosh | Sciencx (2021-12-24T23:48:56+00:00) Custom Build folder for React. Retrieved from https://www.scien.cx/2021/12/24/custom-build-folder-for-react/

MLA
" » Custom Build folder for React." Arjun Santhosh | Sciencx - Friday December 24, 2021, https://www.scien.cx/2021/12/24/custom-build-folder-for-react/
HARVARD
Arjun Santhosh | Sciencx Friday December 24, 2021 » Custom Build folder for React., viewed ,<https://www.scien.cx/2021/12/24/custom-build-folder-for-react/>
VANCOUVER
Arjun Santhosh | Sciencx - » Custom Build folder for React. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/24/custom-build-folder-for-react/
CHICAGO
" » Custom Build folder for React." Arjun Santhosh | Sciencx - Accessed . https://www.scien.cx/2021/12/24/custom-build-folder-for-react/
IEEE
" » Custom Build folder for React." Arjun Santhosh | Sciencx [Online]. Available: https://www.scien.cx/2021/12/24/custom-build-folder-for-react/. [Accessed: ]
rf:citation
» Custom Build folder for React | Arjun Santhosh | Sciencx | https://www.scien.cx/2021/12/24/custom-build-folder-for-react/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.