Skip to content

Minify frontend/admin assets as an optional release build - #8

Merged
davidperezgar merged 4 commits into
mainfrom
feature/minify-assets
Sep 29, 2026
Merged

davidperezgar merged 4 commits into
mainfrom
feature/minify-assets

Conversation

@Castellon-ACM

Copy link
Copy Markdown
Contributor

Summary

  • Adds bin/build-assets.js, a small Node script (no bundler) that uses terser (JS) and clean-css (CSS) to produce .min.js/.min.css build artifacts for assets/cookie-notice/frontconsent-cookie-notice.js/.css and assets/admin/settings.js/.css.
  • CookieNotice::get_asset_url() and Settings::get_asset_url() enqueue the .min.* file when SCRIPT_DEBUG is off (undefined or false) and the file exists on disk, mirroring WordPress core's own suffix convention — otherwise they fall back to the plain file.
  • The plain, hand-commented files remain the source of truth; contributors keep editing them directly, exactly as AGENTS.md's "no build step" policy already states (amended with one clarifying sentence about the new optional release step).
  • .min.js/.min.css are gitignored — they're never committed to main, never required for local dev, and a fresh checkout that hasn't run the build simply falls back to the plain files (verified by tests).
  • Added npm run build as an optional local script, plus npm run test:build-assets (also covered by npm run test:js) smoke-testing the minifiers against the real source files: non-empty output, syntactically valid (new vm.Script(...)), and still containing a few behavior-relevant tokens/selectors.
  • Added PHPUnit tests (tests/Unit/CookieNoticeAssetMinificationTest.php, tests/Unit/SettingsAssetMinificationTest.php) covering: minified file preferred when present and SCRIPT_DEBUG is off, plain file used when the minified file is absent, and plain file forced when SCRIPT_DEBUG is on even if a minified file exists (run in a separate process so the constant doesn't leak into other tests).

Manual follow-up needed: .github/workflows/deploy.yml

The push token available to this automation lacks the workflow OAuth scope GitHub requires to update workflow files, so the actual CI wiring couldn't be pushed on this branch. Please apply this diff by hand (or ask me to redo it with proper scope) before merging, so tagged releases actually ship the minified assets:

     - name: Build
       run: composer install -o --no-dev --ignore-platform-reqs
 
+    - name: Setup Node
+      uses: actions/setup-node@v4
+      with:
+        node-version: '20'
+
+    - name: Install npm dependencies
+      run: npm ci
+
+    - name: Build minified assets
+      run: npm run build
+
     - name: WordPress Plugin Deploy
       uses: 10up/action-wordpress-plugin-deploy@stable

Insert it right after the existing - name: Build (composer install) step, before - name: WordPress Plugin Deploy, in .github/workflows/deploy.yml. .distignore already excludes bin/, node_modules/, and package.json/package-lock.json from the SVN package, and does not exclude *.min.*, so the generated files ship correctly once this step runs.

Test plan

  • composer lint — passes
  • composer phpstan — passes ("No errors")
  • npm run test:js (25 tests, includes new build-assets.test.js) — all pass
  • npm run build — verified it produces valid .min.js/.min.css (checked with node --check and manual inspection), then removed the artifacts again since they're gitignored dev-only output
  • composer test (PHPUnit) — not run in this sandbox: no MySQL/MariaDB or Docker available to stand up the WordPress test database (bin/install-wp-tests.sh requires it). The new tests follow the existing Yoast\WPTestUtils\WPIntegration\TestCase pattern exactly (see tests/Unit/CookieNoticePolicyPageTest.php) and were verified for syntax (php -l) and logic by hand; please run composer test in CI/locally to confirm.

Closes #4.

🤖 Generated with Claude Code

Castellon-ACM and others added 4 commits September 29, 2026 08:34
…requirement

Adds bin/build-assets.js (terser + clean-css, no bundler) producing
.min.js/.min.css build artifacts for the cookie-notice banner and admin
settings assets. The plain files stay the source of truth for contributors
(AGENTS.md's "no build step" policy is untouched); `npm run build` is
optional locally, and the release workflow (deploy.yml) now runs it before
packaging a tagged release, so the minified files ship in production
without ever being committed to main.

CookieNotice::get_asset_url() / Settings::get_asset_url() enqueue the
.min.* file when SCRIPT_DEBUG is off and it exists on disk, mirroring
WordPress core's own suffix convention, and fall back to the plain file
otherwise — a fresh checkout that never ran the build keeps working
exactly as before.

Closes #4.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
The push token available in this environment lacks the `workflow` scope
GitHub requires to update workflow files, so .github/workflows/deploy.yml
can't be pushed from here. See the PR description for the exact diff to
apply manually (adds a Node/npm setup + `npm run build` step before the
10up SVN deploy step, so tagged releases ship the minified assets).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
- Replace the real SCRIPT_DEBUG constant + @runInSeparateProcess in the
  new PHPUnit tests with a protected is_script_debug() seam on both
  CookieNotice and Settings, overridden via an anonymous subclass in
  tests. Process isolation was failing in CI with "Serialization of
  'Closure' is not allowed" (WP's global hook state holds closures), and
  defining SCRIPT_DEBUG for real would have leaked into every other test
  in the same PHPUnit process.
- Add pretest:js/pretest:build-assets npm hooks that install
  node_modules on demand (skipped if already present) — the CI "Run JS
  tests" job runs `npm run test:js` with no separate install step, and
  the workflow file itself can't be touched from this environment (see
  the open PR's note about the missing `workflow` OAuth scope), so the
  fix has to live in package.json instead.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
wp_register_script()/wp_register_style() are no-ops once a handle is
already registered — they never overwrite its `src` — so without
deregistering 'frontconsent-cookie-notice'/'frontconsent-settings' in
tear_down(), whichever `src` the first test in each new file enqueued was
"sticking" for every later test in the same PHPUnit process (WP core's
own test suite resets many globals between tests, but not $wp_scripts/
$wp_styles). This was failing 4 tests in CI with the wrong (stale) src.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@davidperezgar
davidperezgar merged commit 68e35ab into main Sep 29, 2026
6 checks passed
@davidperezgar
davidperezgar deleted the feature/minify-assets branch September 29, 2026 11:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Minify and version frontend assets (banner JS/CSS) to stop PageSpeed/Lighthouse flags

2 participants