BASH

Automate Frontend Build Workflow

Streamline your web development workflow by automating the creation of output directories and execution of frontend build commands (e.g., Webpack, Vite, Gulp).

#!/bin/bash

# --- Configuration ---
BUILD_DIR="dist"        # The directory for compiled assets
FRONTEND_SOURCE_DIR="src" # Your frontend source code directory
BUILD_COMMAND="npm run build" # The command to build your frontend

# --- Script Start ---
echo "Starting frontend build process..."

# 1. Clean previous build (optional but good practice)
if [ -d "$BUILD_DIR" ]; then
  echo "Cleaning previous build directory: $BUILD_DIR"
  rm -rf "$BUILD_DIR"
fi

# 2. Create the build directory if it doesn't exist
echo "Creating build directory: $BUILD_DIR"
mkdir -p "$BUILD_DIR" || { echo "Error: Failed to create build directory."; exit 1; }

# 3. Navigate to frontend source (if your build command expects it)
# cd "$FRONTEND_SOURCE_DIR" || { echo "Error: Frontend source directory '$FRONTEND_SOURCE_DIR' not found."; exit 1; }

# 4. Run the frontend build command
echo "Running frontend build command: $BUILD_COMMAND"
if ! eval "$BUILD_COMMAND"; then
  echo "ERROR: Frontend build failed. Please check your '$BUILD_COMMAND' command."
  exit 1
fi

# 5. (Optional) Copy additional static assets if not handled by build tool
# echo "Copying static assets to $BUILD_DIR/static"
# mkdir -p "$BUILD_DIR/static"
# cp -r public/* "$BUILD_DIR/static/"

echo "Frontend build process completed successfully."
exit 0
How it works: This script orchestrates a common frontend build process. It starts by optionally cleaning a previous build directory, then creates a fresh one. It then executes a specified build command (e.g., `npm run build` for Webpack or Vite projects). Error handling is included to ensure the process stops if the build fails, providing clear feedback. This is invaluable for CI/CD pipelines or local development workflows.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs