Skip to content

Migrate announcement banner block to TypeScript with build tooling - #50

Open
markwong167 wants to merge 2 commits into
mainfrom
claude/canopy-blocks-jsx-to-tsx-migration
Open

markwong167 wants to merge 2 commits into
mainfrom
claude/canopy-blocks-jsx-to-tsx-migration

Conversation

@markwong167

Copy link
Copy Markdown
Contributor

Converts the announcement banner block from vanilla JavaScript to TypeScript and integrates it with the WordPress build pipeline (@wordpress/scripts).

Summary

Modernizes the canopy-blocks plugin by:

  • Converting the announcement banner block implementation from ES5 JavaScript to TypeScript with JSX
  • Establishing a monorepo structure with pnpm workspaces
  • Adding build tooling via @wordpress/scripts to compile TypeScript sources
  • Moving block metadata to block.json (WordPress 5.8+ standard)
  • Introducing type safety with TypeScript interfaces for block attributes

Key Changes

  • Removed: src/announcement-banner/edit.js (ES5 implementation with manual wp.blocks.registerBlockType)
  • Added: TypeScript sources:
    • src/announcement-banner/index.tsx — block registration using registerBlockType from @wordpress/blocks
    • src/announcement-banner/edit.tsx — editor component with proper React/JSX syntax and type annotations
    • src/announcement-banner/attributes.ts — TypeScript interface for block attributes
    • src/announcement-banner/block.json — block metadata (replaces inline registration)
  • Added: Build configuration:
    • web/app/plugins/canopy-blocks/package.json — plugin-level dependencies and build scripts
    • web/app/plugins/canopy-blocks/tsconfig.json — TypeScript configuration
    • Root package.json and pnpm-workspace.yaml — monorepo setup
    • .npmrc — pnpm configuration
  • Updated: canopy-blocks.php — simplified to call register_block_type() with the built output directory instead of manual script registration
  • Generated: build/announcement-banner/ — compiled output (index.js, index.asset.php, render.php)

Implementation Details

  • Block attributes are now defined in block.json following WordPress standards
  • The render function is delegated to the existing PHP handler via render.php
  • TypeScript strict mode enabled for type safety
  • Build artifacts are committed to build/ for deployment without requiring build steps in production

https://claude.ai/code/session_01LSUtqpCkbz5v2F5dooi4TF

@markwong167
markwong167 marked this pull request as draft August 21, 2026 22:31
@markwong167
markwong167 changed the base branch from main to claude/canopy-blocks-typescript-migration August 21, 2026 22:32
@markwong167
markwong167 force-pushed the claude/canopy-blocks-typescript-migration branch from e0b9f09 to 51183bc Compare August 31, 2026 13:53
@markwong167
markwong167 force-pushed the claude/canopy-blocks-jsx-to-tsx-migration branch from 188ed3b to 6ac8bae Compare August 31, 2026 13:56
Base automatically changed from claude/canopy-blocks-typescript-migration to main September 1, 2026 20:17
Converts edit.js (a global-IIFE script depending on window.wp.*) to a
proper wp-scripts entry: index.tsx registers the block, edit.tsx is a
typed component using ES module imports and BlockEditProps for the
block's attributes.

This requires wiring the build to actually work, not just type-check:
- block.json now declares editorScript so wp-scripts builds an entry
  and generates the dependency manifest (index.asset.php) instead of
  the plugin hardcoding a dependency array by hand.
- canopy-blocks.php now registers the block from build/, not src/, and
  drops the manual wp_register_script call entirely — WP core reads
  the built block.json + index.asset.php on its own.
- build/ is committed. There's no CI or Docker step that runs `pnpm
  build` yet (tracked separately), so without committing it the block
  would silently stop rendering in the editor.

Also pins typescript to ^5.9.3 instead of the latest ^7: @typescript-
eslint@6.21.0 (pulled in by @wordpress/eslint-plugin) crashes under
TypeScript 7 — ts-api-utils reads an internal ts.SyntaxKind shape that
7.x changed. `pnpm lint:js` failed outright before this pin.
It's the block's attribute schema (mirrors block.json, shared by
edit.tsx and index.tsx), not a component-local prop type, so it
shouldn't live inside edit.tsx. Kept scoped to the block's own folder
rather than a plugin-wide types/ directory — each src/<block-name>/
folder is the self-contained, independently-reusable unit in this
codebase, so a shared type belongs next to the block.json it
describes, not pulled out into a cross-block bucket.
@markwong167
markwong167 force-pushed the claude/canopy-blocks-jsx-to-tsx-migration branch from 6ac8bae to 9a1d0b5 Compare September 1, 2026 20:25
@markwong167

Copy link
Copy Markdown
Contributor Author

Actual changes:

  • 4 files are generated build output: build/announcement-banner/{block.json,index.asset.php,index.js,render.php}
  • pnpm.yaml auto-generated
  • split edit.js into edit.tsx + index.tsx + attributes.ts, +block.json
  • canopy-blocks.php updated
  • package.json version updated

@markwong167
markwong167 marked this pull request as ready for review September 1, 2026 20:33
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.

2 participants