Skip to content

Add TypeScript support and improve project configuration for opencc-wasm#3

Merged
frankslin merged 1 commit intomasterfrom
claude/review-opencc-wasm-NHpXU
Jan 1, 2026
Merged

Add TypeScript support and improve project configuration for opencc-wasm#3
frankslin merged 1 commit intomasterfrom
claude/review-opencc-wasm-NHpXU

Conversation

@frankslin
Copy link
Owner

@frankslin frankslin commented Jan 1, 2026

This commit enhances the opencc-wasm library with TypeScript support and
implements a cleaner build architecture with semantic separation between
intermediate build artifacts and publishable distribution.

TypeScript Support:

  • Add comprehensive type definitions (index.d.ts) with full JSDoc documentation
  • Define interfaces: ConverterOptions, ConverterFunction, OpenCCNamespace, etc.
  • Provide complete type safety for better IDE support and developer experience

Build Architecture Redesign (semantic separation):

  • build/ - Intermediate WASM artifacts (gitignored, for tests/development)
    • build/opencc-wasm.esm.js - ESM WASM glue
    • build/opencc-wasm.cjs - CJS WASM glue
    • build/opencc-wasm.wasm - WASM binary
  • dist/ - Publishable distribution (committed, for npm)
    • dist/esm/ - ESM package entry
    • dist/cjs/ - CJS package entry
    • dist/data/ - OpenCC config and dictionary files

Invariants and Semantics:

  • Tests import source (index.js) → loads from build/
  • Published package exports dist/ only
  • build/ = internal intermediate artifacts
  • dist/ = publishable artifacts
  • Clear separation ensures tests validate actual build output

Enhanced .gitignore:

  • Add build/ to gitignore (intermediate artifacts)
  • Add node_modules/, logs, OS-specific files (.DS_Store, Thumbs.db)
  • Exclude editor configurations (.vscode/, .idea/)
  • Add cache and temporary file exclusions

Two-Stage Build Process:
Stage 1 (build.sh):

  • Compiles C++ to WASM using Emscripten
  • Outputs to build/ directory

Stage 2 (build-api.js):

  • Copies WASM artifacts from build/ to dist/
  • Transforms source paths for production
  • Generates API wrappers for ESM and CJS
  • Copies data files

Package Configuration (package.json):

  • Add "types" field pointing to index.d.ts
  • Update "main" and "module" to point to API wrappers in dist/
  • Add comprehensive "exports" map:
    • "." - Main API (ESM/CJS wrappers)
    • "./wasm" - Direct access to WASM glue for advanced users
    • "./dist/*" - Wildcard for flexible file access
  • Include LICENSE and NOTICE in published files

Documentation:

  • Add comprehensive README section explaining build architecture
  • Document project structure with invariants
  • Explain semantic separation between build/ and dist/

Benefits:

  • Better TypeScript integration and IDE autocomplete
  • Cleaner, more maintainable directory structure
  • Tests validate actual build output, not stale dist files
  • Clear semantic separation between internal and publishable artifacts
  • Professional project setup following modern npm best practices
  • Long-term maintainability through clear invariants

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds comprehensive TypeScript support and improves the project configuration for the opencc-wasm library. The changes include type definitions, enhanced gitignore patterns, and updated package.json to properly reference the new TypeScript types and build outputs.

  • Added complete TypeScript type definitions with JSDoc documentation
  • Updated package.json to reference new ESM/CJS build locations and include TypeScript types
  • Enhanced .gitignore with comprehensive exclusions for node_modules, logs, OS files, and editor configurations

Reviewed changes

Copilot reviewed 2 out of 3 changed files in this pull request and generated no comments.

File Description
wasm-lib/package.json Updated module entry points to new dist/esm and dist/cjs paths, added TypeScript types field, and included LICENSE/NOTICE in published files
wasm-lib/index.d.ts New TypeScript type definitions file providing complete type safety with interfaces for ConverterOptions, ConverterFunction, OpenCCNamespace, and related types with JSDoc examples
wasm-lib/.gitignore Expanded from single line to comprehensive exclusions covering node_modules, logs, OS files, editor configs, and cache directories

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@frankslin frankslin force-pushed the claude/review-opencc-wasm-NHpXU branch 2 times, most recently from c63da4c to 37fc54d Compare January 1, 2026 19:23
…eparation

This commit enhances the opencc-wasm library with TypeScript support and
implements a cleaner build architecture with semantic separation between
intermediate build artifacts and publishable distribution.

TypeScript Support:
- Add comprehensive type definitions (index.d.ts) with full JSDoc documentation
- Define interfaces: ConverterOptions, ConverterFunction, OpenCCNamespace, etc.
- Provide complete type safety for better IDE support and developer experience

Build Architecture Redesign (semantic separation):
- build/ - Intermediate WASM artifacts (gitignored, for tests/development)
  * build/opencc-wasm.esm.js - ESM WASM glue
  * build/opencc-wasm.cjs - CJS WASM glue
  * build/opencc-wasm.wasm - WASM binary
- dist/ - Publishable distribution (committed, for npm)
  * dist/esm/ - ESM package entry
  * dist/cjs/ - CJS package entry
  * dist/data/ - OpenCC config and dictionary files

Invariants and Semantics:
- Tests import source (index.js) → loads from build/
- Published package exports dist/ only
- build/ = internal intermediate artifacts
- dist/ = publishable artifacts
- Clear separation ensures tests validate actual build output

Enhanced .gitignore:
- Add build/ to gitignore (intermediate artifacts)
- Add node_modules/, logs, OS-specific files (.DS_Store, Thumbs.db)
- Exclude editor configurations (.vscode/, .idea/)
- Add cache and temporary file exclusions

Two-Stage Build Process:
Stage 1 (build.sh):
  - Compiles C++ to WASM using Emscripten
  - Outputs to build/ directory

Stage 2 (build-api.js):
  - Copies WASM artifacts from build/ to dist/
  - Transforms source paths for production
  - Generates API wrappers for ESM and CJS
  - Copies data files

Package Configuration (package.json):
- Add "types" field pointing to index.d.ts
- Update "main" and "module" to point to API wrappers in dist/
- Add comprehensive "exports" map:
  * "." - Main API (ESM/CJS wrappers)
  * "./wasm" - Direct access to WASM glue for advanced users
  * "./dist/*" - Wildcard for flexible file access
- Include LICENSE and NOTICE in published files

Documentation:
- Add comprehensive README section explaining build architecture
- Document project structure with invariants
- Explain semantic separation between build/ and dist/

Benefits:
- Better TypeScript integration and IDE autocomplete
- Cleaner, more maintainable directory structure
- Tests validate actual build output, not stale dist files
- Clear semantic separation between internal and publishable artifacts
- Professional project setup following modern npm best practices
- Long-term maintainability through clear invariants
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 6 out of 9 changed files in this pull request and generated 1 comment.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

# Build intermediates (not committed)
build/

# Dist outputs (keep in git for npm package)
Copy link

Copilot AI Jan 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generated dist files (opencc-wasm.js and opencc-wasm.cjs) are minified production bundles that should not be committed to git as they are binary-like artifacts that create merge conflicts and bloat the repository. According to the PR description, build/ is gitignored for intermediate artifacts, but dist/ should contain publishable outputs. However, these specific Emscripten-generated WASM glue files are large minified files (likely auto-generated) that should be treated as build outputs.

Consider either:

  1. Adding dist/esm/opencc-wasm.js and dist/cjs/opencc-wasm.cjs to .gitignore (they can be generated during npm prepare/publish hooks)
  2. If they must be committed, document why in the README
Suggested change
# Dist outputs (keep in git for npm package)
# Dist outputs (keep in git for npm package)
# (except specific Emscripten-generated WASM glue files)
dist/esm/opencc-wasm.js
dist/cjs/opencc-wasm.cjs

Copilot uses AI. Check for mistakes.
@frankslin frankslin merged commit 114648d into master Jan 1, 2026
29 checks passed
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.

3 participants