Skip to content

Tiger Router v2.0.0 – TypeScript & Core Rewrite

Latest

Choose a tag to compare

@Fasping Fasping released this 04 Dec 13:15

Tiger Router v2.0.0 🐯

Complete rewrite of tiger-router with TypeScript, framework-agnostic core, and modern React bindings.


πŸš€ Highlights

  • βœ… Full TypeScript migration
  • βœ… Framework-agnostic core (history + route matcher)
  • βœ… Modern React bindings (Router, Route, Link)
  • βœ… New hooks: useLocation, useNavigate, useParams, useRouteMatch
  • βœ… Tiny bundle (~4.8 kB packed)
  • βœ… ESM + CJS + types ready for modern toolchains

🧠 TypeScript Migration

  • Rewritten entire codebase in TypeScript
  • Enabled strict mode for safer typings
  • Automatic .d.ts generation with tsup
  • tsconfig.json optimized for library usage

Result: first-class DX for TS projects, with full IntelliSense and type safety.


🧩 Framework-Agnostic Core

New core modules under src/core/:

  • history.ts

    • Minimal history wrapper
    • history and hash modes
    • Subscription mechanism for location changes
  • matcher.ts

    • Simple route pattern matching (/users/:id)
    • Parameter extraction: { id: "123" }
    • Used internally by React bindings and hooks

This keeps the router small and opens the door for potential non-React bindings in the future.


βš›οΈ Modern React Bindings

New React layer under src/react/:

  • <Router>

    • Provides routing context (location, navigate, params)
    • Supports mode="history" | "hash" (default: history)
  • <Route>

    • Renders when path matches current location
    • Uses element prop or children
  • <Link>

    • SPA navigation component
    • Prevents full page reloads
    • Uses router navigate under the hood

Hooks

  • useLocation() – get current location
  • useNavigate() – programmatic navigation
  • useParams() – access route parameters
  • useRouteMatch(path) – check if path matches current location

πŸ—οΈ Build System

  • Switched to tsup for bundling
  • Outputs:
    • dist/index.js (ESM)
    • dist/index.cjs (CJS)
    • dist/index.d.ts / dist/index.d.cts
  • package.json configured with:
    • "main", "module", "types" and "exports"
    • "files" whitelisted to keep package clean
  • prepare script now runs npm run build

Result: clean, modern, tree-shakeable bundle.


πŸ§ͺ Testing & Quality

  • Added tests for:
    • Core matcher
    • Core history navigation
    • React Router integration
  • Commands:
    • npm test – all tests passing
    • npx tsc --noEmit – no type errors
    • npm run build – successful

An integration test project was also used to verify:

  • Installation from packed tarball
  • TypeScript compatibility
  • React 18 usage
  • No peer dependency warnings

πŸ’₯ Breaking Changes

If you were using v1, please note:

  • <Router> no longer accepts a routes prop
  • <Route> now uses:
    • element (preferred) or children
      instead of older patterns
  • Components should now use useParams() hook
    instead of receiving routeParams via props