Skip to content

feat(page-compiler): improve Transform API for dual-mode usage#64

Merged
YusukeHirao merged 10 commits intomainfrom
feat/improve-transform-api
Jan 29, 2026
Merged

feat(page-compiler): improve Transform API for dual-mode usage#64
YusukeHirao merged 10 commits intomainfrom
feat/improve-transform-api

Conversation

@YusukeHirao
Copy link
Copy Markdown
Member

Summary

Improves the Transform API to support both development and build modes by adding TransformContext to compiler hooks and renaming the directory structure to reflect dual-mode capabilities.

Key Changes

1. Directory Rename

  • Renamed dev-transform/transform/ to reflect dual-mode usage
  • Maintained backward compatibility in package.json exports
  • Updated all documentation references

2. TransformContext Integration

  • Added TransformContext as 3rd parameter to beforeSerialize hook
  • Added TransformContext as 4th parameter to afterSerialize hook
  • Provides access to path, inputPath, outputPath, isServe, and full context

3. Dual-Mode Support

  • Transform utilities (injectToHead, createSSIShim) can now be used in both dev and build contexts
  • devServer.transforms continues to apply only in serve mode (unchanged)
  • Users can manually call transform utilities in beforeSerialize for build-time transformations

4. Comprehensive Documentation

  • Added dedicated TransformContext section explaining all properties
  • Added advanced usage examples (conditional processing, file operations)
  • Clarified that devServer.transforms is serve-mode only
  • Updated all signatures in Options sections
  • Updated ARCHITECTURE.md to reflect current implementation

Test Coverage

  • Added 6 new tests for TransformContext in hooks
  • All 156 tests passing
  • Build verification successful

Breaking Changes

⚠️ beforeSerialize and afterSerialize signatures have changed:

Before:

beforeSerialize?: (content: string, isServe: boolean) => Promise<string> | string
afterSerialize?: (elements: readonly Element[], window: Window, isServe: boolean) => Promise<void> | void

After:

beforeSerialize?: (content: string, isServe: boolean, context: TransformContext) => Promise<string> | string
afterSerialize?: (elements: readonly Element[], window: Window, isServe: boolean, context: TransformContext) => Promise<void> | void

Existing code that doesn't use the new parameter will continue to work (JavaScript ignores extra parameters).

Migration Guide

Using Transform Utilities in Build Time

import { createInjectToHeadTransform } from '@kamado-io/page-compiler/transform/inject-to-head';

const pageCompilerOptions: PageCompilerOptions = {
    beforeSerialize: async (content, isServe, context) => {
        const injectTransform = createInjectToHeadTransform({
            content: '<meta name="generator" content="Kamado">',
        });
        return await injectTransform(content, context);
    },
};

Conditional Processing Based on Path

beforeSerialize: async (content, isServe, context) => {
    if (context.path.startsWith('admin/')) {
        // Special processing for admin pages
    }
    return content;
};

🤖 Generated with Claude Code

YusukeHirao and others added 10 commits January 29, 2026 19:15
Renames dev-transform/ to transform/ to reflect that transform utilities
can be used in both dev and build modes. Maintains backward compatibility
by keeping old paths in package.json exports.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Restores the serve-only mode guard in applyTransforms to maintain the
original behavior where transforms only apply during development.
Transform utilities remain usable in both modes when called manually.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…tions

Removes the incorrectly added transforms option and reverts beforeSerialize
signature to original form. Transform utilities should be called manually
within beforeSerialize hook, not applied automatically via options.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Extends beforeSerialize hook to receive TransformContext as third parameter,
enabling users to call transform utilities manually within the hook.
Transform utilities can now be used in both dev and build contexts.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Adds comprehensive tests verifying that beforeSerialize receives
TransformContext as third parameter with correct path, inputPath,
outputPath, isServe, and context values.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…sage

Updates JSDoc comments in inject-to-head and ssi-shim to reflect that
transform utilities can be used in both development and build contexts.
Updates import paths in examples from dev-transform to transform.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Adds comprehensive examples showing how to use transform utilities
(injectToHead, createSSIShim) in the beforeSerialize hook for build-time
transformations. Updates description to reflect dual-mode usage.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Extends afterSerialize hook to receive TransformContext as fourth parameter
for consistency with beforeSerialize. Adds comprehensive tests verifying the
context is passed correctly with path, inputPath, outputPath, isServe values.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Comprehensively updates all documentation to reflect TransformContext
implementation:

page-compiler/README.md:
- Update beforeSerialize/afterSerialize signatures in Options section
- Add dedicated TransformContext section with all properties explained
- Add note that devServer.transforms only applies in serve mode
- Add advanced examples using TransformContext properties
- Update FormatHtmlOptions section with correct signatures

kamado/README.md:
- Update afterSerialize example to include context parameter
- Add full signature in options list

kamado/ARCHITECTURE.md & ARCHITECTURE.ja.md:
- Remove outdated contentType property from TransformContext
- Update comments to reflect actual implementation
- Remove Content-Type filtering from execution flow
- Add note about dual-mode usage of transform utilities

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Replaces try-catch block with existsSync check in file operations example
for clearer code style.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
@cursor
Copy link
Copy Markdown

cursor bot commented Jan 29, 2026

You have run out of free Bugbot PR reviews for this billing cycle. This will reset on February 26.

To receive reviews on all of your PRs, visit the Cursor dashboard to activate Pro and start your 14-day free trial.

@YusukeHirao YusukeHirao merged commit a6f888b into main Jan 29, 2026
1 check passed
@YusukeHirao YusukeHirao deleted the feat/improve-transform-api branch January 29, 2026 11:53
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.

1 participant