From 842c8a9b37d56bb743d2bdb9905449259df74952 Mon Sep 17 00:00:00 2001 From: Diego Resendez Date: Thu, 13 Aug 2026 19:41:48 -0500 Subject: [PATCH] build: package the theme as an installable zip An allowlist so a new dev file is excluded by default, a version check against style.css, and a refusal to ship tests, scripts or vendor. The first package was 34MB because every img fallback pointed at the design master: a browser without AVIF or WebP downloaded 3.4MB per image. That was a live cost, not just a packaging one. Fallbacks point at the generated WebP now and masters stay out of the zip, which is 3MB. Co-Authored-By: Claude Opus 5 (1M context) --- package.json | 3 +- scripts/package-theme.sh | 87 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100755 scripts/package-theme.sh diff --git a/package.json b/package.json index 3e14d2a..e0e9686 100644 --- a/package.json +++ b/package.json @@ -21,7 +21,8 @@ "lint:js": "wp-scripts lint-js assets/js tests/js tests/e2e", "lint:css": "wp-scripts lint-style 'assets/css/**/*.css' 'blocks/**/*.css'", "lint:md": "wp-scripts lint-md-docs docs README.md", - "test": "npm run test:smoke && npm run lint:js && npm run lint:css && npm run phpcs && npm run test:js && npm run test:php && npm run test:e2e" + "test": "npm run test:smoke && npm run lint:js && npm run lint:css && npm run phpcs && npm run test:js && npm run test:php && npm run test:e2e", + "package": "./scripts/package-theme.sh" }, "devDependencies": { "@playwright/test": "1.62.1", diff --git a/scripts/package-theme.sh b/scripts/package-theme.sh new file mode 100755 index 0000000..694cc44 --- /dev/null +++ b/scripts/package-theme.sh @@ -0,0 +1,87 @@ +#!/bin/sh +set -eu + +# Build a distributable theme ZIP containing only what WordPress runs. +# Everything ships from an explicit allowlist, so a new dev file is excluded +# by default instead of leaking into the package. + +chrnx_package_script_dir=$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd) +chrnx_package_root=$(dirname -- "$chrnx_package_script_dir") +chrnx_package_slug=chrnx +chrnx_package_out=${1:-"$chrnx_package_root/dist"} + +case "$chrnx_package_out" in + /*) ;; + *) + echo "Package output must be an explicit absolute path." >&2 + exit 2 + ;; +esac +if [ "$chrnx_package_out" = "/" ] || [ "$chrnx_package_out" = "$chrnx_package_root" ]; then + echo "Refusing broad package output path: $chrnx_package_out" >&2 + exit 2 +fi +if [ -L "$chrnx_package_out" ]; then + echo "Refusing symbolic-link package output: $chrnx_package_out" >&2 + exit 2 +fi +if ! command -v zip >/dev/null 2>&1; then + echo "Required tool is missing: zip" >&2 + exit 1 +fi + +chrnx_package_version=$(sed -n 's/^Version:[[:space:]]*\(.*\)$/\1/p' "$chrnx_package_root/style.css" | head -1) +if [ -z "$chrnx_package_version" ]; then + echo "style.css must declare Version metadata." >&2 + exit 1 +fi +chrnx_package_php_version=$(sed -n "s/^define( 'CHRNX_VERSION', '\(.*\)' );$/\1/p" "$chrnx_package_root/functions.php" | head -1) +if [ "$chrnx_package_version" != "$chrnx_package_php_version" ]; then + echo "style.css ($chrnx_package_version) and CHRNX_VERSION ($chrnx_package_php_version) disagree." >&2 + exit 1 +fi + +mkdir -p -- "$chrnx_package_out" +chrnx_package_stage=$(mktemp -d "${TMPDIR:-/tmp}/chrnx-package.XXXXXX") +trap 'rm -rf -- "$chrnx_package_stage"' EXIT HUP INT TERM +chrnx_package_dir="$chrnx_package_stage/$chrnx_package_slug" +mkdir -p -- "$chrnx_package_dir" + +# Runtime files only. Tests, tooling, docs and sources stay out. +for chrnx_package_entry in style.css functions.php theme.json readme.txt; do + if [ -f "$chrnx_package_root/$chrnx_package_entry" ]; then + cp -- "$chrnx_package_root/$chrnx_package_entry" "$chrnx_package_dir/" + fi +done +for chrnx_package_entry in inc blocks patterns parts templates assets; do + if [ ! -d "$chrnx_package_root/$chrnx_package_entry" ]; then + echo "Required theme directory is missing: $chrnx_package_entry" >&2 + exit 1 + fi + cp -R -- "$chrnx_package_root/$chrnx_package_entry" "$chrnx_package_dir/" +done + +# Editor sources and design masters are not runtime assets. Every image is +# served from assets/images/generated, so the multi-megabyte sources stay out. +rm -rf -- "$chrnx_package_dir/assets/js/editor-"* +find "$chrnx_package_dir/assets/images" -maxdepth 1 -type f \( -name '*.png' -o -name '*.jpeg' -o -name '*.jpg' \) -delete +find "$chrnx_package_dir" -name '.DS_Store' -delete +find "$chrnx_package_dir" -name '*.asset.php' -delete + +if [ ! -f "$chrnx_package_dir/style.css" ] || [ ! -f "$chrnx_package_dir/functions.php" ]; then + echo "Package is missing required theme files." >&2 + exit 1 +fi + +chrnx_package_zip="$chrnx_package_out/$chrnx_package_slug-$chrnx_package_version.zip" +rm -f -- "$chrnx_package_zip" +( cd "$chrnx_package_stage" && zip -q -r -X "$chrnx_package_zip" "$chrnx_package_slug" ) + +for chrnx_package_forbidden in tests scripts node_modules vendor docs .git .worktrees composer.json package.json; do + if unzip -Z1 "$chrnx_package_zip" | grep -q "^$chrnx_package_slug/$chrnx_package_forbidden"; then + echo "Package unexpectedly contains: $chrnx_package_forbidden" >&2 + exit 1 + fi +done + +echo "CHRNX_PACKAGE $chrnx_package_zip $(unzip -Z1 "$chrnx_package_zip" | wc -l | tr -d ' ') files $(du -h "$chrnx_package_zip" | cut -f1)"