Last active
January 7, 2023 08:54
-
-
Save dreamyguy/358456085528c186124d8908a6927873 to your computer and use it in GitHub Desktop.
Custom build output folder when using create-react-app with postbuild
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "your-project", | |
"version": "0.0.1", | |
[...] | |
"scripts": { | |
"build": "react-scripts build", | |
"postbuild": "./postbuild.sh", | |
[...] | |
}, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# The purpose of this script is to do things with files generated by | |
# 'create-react-app' after 'build' is run. | |
# 1. Move files to a new directory called 'user' | |
# The resulting structure is 'build/user/static/<etc>' | |
# 2. Update reference on generated files from | |
# static/<etc> | |
# to | |
# user/static/<etc> | |
# | |
# More details on: https://github.com/facebook/create-react-app/issues/3824 | |
# Browse into './build/' directory | |
cd build | |
# Create './user/' directory | |
echo '1/4 Create "user" directory' | |
mkdir user | |
# Find all files, excluding (through 'grep'): | |
# - '.', | |
# - the newly created directory './user/' | |
# - all content for the directory'./static/' | |
# Move all matches to the directory './user/' | |
echo '2/4 Move relevant files' | |
find . | grep -Ev '^.$|^.\/user$|^.\/static\/.+' | xargs -I{} mv -v {} user | |
# Browse into './user/' directory | |
cd user | |
# Find all files within the folder (not subfolders) | |
# Replace string 'static/' with 'user/static/' on all files that match the 'find' | |
# ('sed' requires one to create backup files on OSX, so we do that) | |
echo '3/4 Replace file references' | |
find . -type f -maxdepth 1 | LC_ALL=C xargs -I{} sed -i.backup -e 's,static/,user/static/,g' {} | |
# Delete '*.backup' files created in the last process | |
echo '4/4 Clean up' | |
find . -name '*.backup' -type f -delete | |
# Done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment