|
| 1 | +#!/bin/bash |
| 2 | +# ============================================================================ |
| 3 | +# Rica - Production Build Script |
| 4 | +# ============================================================================ |
| 5 | +# |
| 6 | +# Purpose: |
| 7 | +# Creates an optimized production build of rica with optional asset inlining |
| 8 | +# for single-file HTML distribution (suitable for embedding in tedana outputs). |
| 9 | +# |
| 10 | +# Expected Environment: |
| 11 | +# - Node.js v16+ installed |
| 12 | +# - Dependencies installed via npm install |
| 13 | +# |
| 14 | +# Build Output: |
| 15 | +# - ./build/ - Standard React production build |
| 16 | +# - ./build/index.html - With --inline: single-file with inlined JS/CSS |
| 17 | +# |
| 18 | +# Usage: |
| 19 | +# ./scripts/build.sh # Standard production build |
| 20 | +# ./scripts/build.sh --inline # Build + inline assets (single HTML file) |
| 21 | +# ./scripts/build.sh --analyze # Build + analyze bundle size |
| 22 | +# ./scripts/build.sh --check # Build + verify output integrity |
| 23 | +# |
| 24 | +# Notes: |
| 25 | +# - The --inline option uses Gulp to inline JS/CSS into index.html |
| 26 | +# - This is useful for distributing rica as a single file with tedana outputs |
| 27 | +# - Build artifacts are placed in ./build/ directory |
| 28 | +# |
| 29 | +# ============================================================================ |
| 30 | + |
| 31 | +set -e |
| 32 | + |
| 33 | +# Colors for output |
| 34 | +RED='\033[0;31m' |
| 35 | +GREEN='\033[0;32m' |
| 36 | +YELLOW='\033[1;33m' |
| 37 | +NC='\033[0m' |
| 38 | + |
| 39 | +# Navigate to project root |
| 40 | +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
| 41 | +PROJECT_ROOT="$(dirname "$SCRIPT_DIR")" |
| 42 | +cd "$PROJECT_ROOT" |
| 43 | + |
| 44 | +# Default values |
| 45 | +INLINE=false |
| 46 | +ANALYZE=false |
| 47 | +CHECK=false |
| 48 | + |
| 49 | +# Parse arguments |
| 50 | +while [[ $# -gt 0 ]]; do |
| 51 | + case $1 in |
| 52 | + --inline) |
| 53 | + INLINE=true |
| 54 | + shift |
| 55 | + ;; |
| 56 | + --analyze) |
| 57 | + ANALYZE=true |
| 58 | + shift |
| 59 | + ;; |
| 60 | + --check) |
| 61 | + CHECK=true |
| 62 | + shift |
| 63 | + ;; |
| 64 | + *) |
| 65 | + echo "Unknown option: $1" |
| 66 | + echo "Usage: ./scripts/build.sh [--inline] [--analyze] [--check]" |
| 67 | + exit 1 |
| 68 | + ;; |
| 69 | + esac |
| 70 | +done |
| 71 | + |
| 72 | +# Check if node_modules exists |
| 73 | +if [ ! -d "node_modules" ]; then |
| 74 | + echo -e "${RED}Error: node_modules not found. Run ./scripts/init_env.sh first.${NC}" |
| 75 | + exit 1 |
| 76 | +fi |
| 77 | + |
| 78 | +echo "============================================" |
| 79 | +echo "Rica Production Build" |
| 80 | +echo "============================================" |
| 81 | +echo "" |
| 82 | + |
| 83 | +# Clean previous build |
| 84 | +if [ -d "build" ]; then |
| 85 | + echo "Cleaning previous build..." |
| 86 | + rm -rf build |
| 87 | +fi |
| 88 | + |
| 89 | +# Run production build |
| 90 | +echo "Creating production build..." |
| 91 | +if [ "$ANALYZE" = true ]; then |
| 92 | + # Build with source maps for analysis |
| 93 | + GENERATE_SOURCEMAP=true npm run build |
| 94 | +else |
| 95 | + npm run build |
| 96 | +fi |
| 97 | + |
| 98 | +# Verify build output |
| 99 | +if [ ! -f "build/index.html" ]; then |
| 100 | + echo -e "${RED}Error: Build failed - index.html not found${NC}" |
| 101 | + exit 1 |
| 102 | +fi |
| 103 | + |
| 104 | +echo -e "${GREEN}React build complete${NC}" |
| 105 | + |
| 106 | +# Inline assets if requested |
| 107 | +if [ "$INLINE" = true ]; then |
| 108 | + echo "" |
| 109 | + echo "Inlining JS and CSS assets..." |
| 110 | + |
| 111 | + # Check if gulp is available |
| 112 | + if [ ! -f "node_modules/.bin/gulp" ]; then |
| 113 | + echo -e "${YELLOW}Warning: Gulp not found. Installing...${NC}" |
| 114 | + npm install --save-dev gulp gulp-inline-source gulp-replace |
| 115 | + fi |
| 116 | + |
| 117 | + # Run gulp task |
| 118 | + npx gulp |
| 119 | + |
| 120 | + if [ $? -eq 0 ]; then |
| 121 | + echo -e "${GREEN}Assets inlined successfully${NC}" |
| 122 | + |
| 123 | + # Check file size |
| 124 | + INLINE_SIZE=$(wc -c < "build/index.html" | tr -d ' ') |
| 125 | + INLINE_SIZE_MB=$(echo "scale=2; $INLINE_SIZE / 1048576" | bc) |
| 126 | + echo "Inlined index.html size: ${INLINE_SIZE_MB}MB" |
| 127 | + else |
| 128 | + echo -e "${RED}Warning: Asset inlining failed${NC}" |
| 129 | + fi |
| 130 | +fi |
| 131 | + |
| 132 | +# Verify build integrity if requested |
| 133 | +if [ "$CHECK" = true ]; then |
| 134 | + echo "" |
| 135 | + echo "Verifying build integrity..." |
| 136 | + |
| 137 | + # Check for expected files |
| 138 | + EXPECTED_FILES=("build/index.html" "build/static/js" "build/static/css") |
| 139 | + ALL_PRESENT=true |
| 140 | + |
| 141 | + for file in "${EXPECTED_FILES[@]}"; do |
| 142 | + if [ -e "$file" ] || [ -d "$file" ]; then |
| 143 | + echo -e " ${GREEN}[OK]${NC} $file" |
| 144 | + else |
| 145 | + echo -e " ${RED}[MISSING]${NC} $file" |
| 146 | + ALL_PRESENT=false |
| 147 | + fi |
| 148 | + done |
| 149 | + |
| 150 | + if [ "$ALL_PRESENT" = true ]; then |
| 151 | + echo -e "${GREEN}Build verification passed${NC}" |
| 152 | + else |
| 153 | + echo -e "${YELLOW}Warning: Some expected files are missing${NC}" |
| 154 | + fi |
| 155 | +fi |
| 156 | + |
| 157 | +# Print build summary |
| 158 | +echo "" |
| 159 | +echo "============================================" |
| 160 | +echo -e "${GREEN}Build complete!${NC}" |
| 161 | +echo "============================================" |
| 162 | +echo "" |
| 163 | +echo "Output directory: ./build/" |
| 164 | + |
| 165 | +if [ "$INLINE" = true ]; then |
| 166 | + echo "Single-file distribution: ./build/index.html" |
| 167 | +fi |
| 168 | + |
| 169 | +echo "" |
| 170 | +echo "To preview the build locally:" |
| 171 | +echo " npx serve -s build" |
| 172 | +echo "" |
0 commit comments