icejs Router Plugin
build-plugin-ice-router provides comprehensive routing capabilities for the icejs framework. At its current stable version 2.1.3, it supports both convention-based routing, automatically generating routes from the `src/pages` directory structure, and configuration-based routing, allowing developers to define routes explicitly in `src/routes.[ts|js]`. Key differentiators include built-in support for nested layouts (using `_layout.tsx`), dynamic route parameters (e.g., `/$uid.tsx` for `/app/:uid`), and optional dynamic parameters (`/$uid$.tsx` for `/app/:uid?`). It also offers dedicated features for global layouts via `src/layouts/index.tsx` and 404 error pages via `src/pages/404.tsx`. This plugin is primarily associated with the icejs v2 ecosystem. The broader Alibaba/ICE framework has evolved to `@ice/app` in v3, which introduces a potentially different plugin system and core architecture. Users should be aware of this version distinction when integrating, as `build-plugin-ice-router` targets icejs v2.
Common errors
-
Error: Not Found: [route path]
cause The requested route path does not match any defined convention-based or configuration-based route, or the 404 page is not correctly placed/configured.fixVerify that your page components are correctly placed in `src/pages` for convention-based routing, or that your `src/routes.ts` file accurately defines the path. Ensure `src/pages/404.tsx` (or `src/pages/404/index.tsx`) exists for a custom 404 page. -
TypeError: Cannot read properties of undefined (reading 'router') when calling runApp
cause The router configuration object (`router` property) passed to `runApp` in `src/app.ts` is either missing or malformed, preventing the router from initializing correctly.fixEnsure `src/app.ts` includes an `appConfig` object with a properly structured `router` property, for example: `{ router: { type: 'browser', basename: '/' } }`. -
Error: Cannot find module 'ice'
cause The `ice` core framework package is not installed, or there's an issue with module resolution in your project, often due to an incorrect `package.json` setup or `node_modules` state.fixInstall the `ice` package via `npm install ice` or `yarn add ice`. If already installed, try clearing your package manager cache and reinstalling dependencies (`npm cache clean --force && rm -rf node_modules && npm install`).
Warnings
- breaking This plugin (`build-plugin-ice-router` v2.x) is strictly designed for icejs v2 applications. Migrating to `@ice/app` (v3.x) will require replacing this plugin with `@ice/plugin-router` or leveraging the built-in routing solution provided by the new `@ice/app` framework. Direct compatibility is not guaranteed.
- gotcha When both convention-based routing (pages in `src/pages`) and configuration-based routing (an explicit `src/routes.[ts|js]` file) are detected, the configuration file takes full precedence. Convention-based routes will be ignored.
- gotcha By default, route paths generated from the file system are case-insensitive. If strict case matching is required, you must enable the `caseSensitive` option in your `build.json`.
- gotcha Dynamic routes use specific naming conventions: `src/pages/$param.tsx` for a mandatory parameter (`/:param`), and `src/pages/$param$.tsx` for an optional parameter (`/:param?`). Incorrect use of `$` will lead to routes not being generated or matched as expected.
Install
-
npm install build-plugin-ice-router -
yarn add build-plugin-ice-router -
pnpm add build-plugin-ice-router
Imports
- runApp
import { runApp } from '@ice/app';import { runApp } from 'ice'; - routes
import routes from './routes';
- IAppRouterConfig
import type { IAppRouterConfig } from 'ice';
Quickstart
/* src/app.ts */
import { runApp } from 'ice';
import type { IAppRouterConfig } from 'ice';
const appConfig: { router?: IAppRouterConfig } = {
router: {
type: 'browser',
basename: '/',
modifyRoutes: (routes) => {
console.log('Routes generated:', routes);
return routes;
}
}
};
runApp(appConfig);
/* src/pages/index.tsx */
import React from 'react';
import { Link } from 'ice'; // Example of a router-provided component
const Home = () => {
return (
<div>
<h1>Welcome to icejs Home!</h1>
<p>This is a convention-based route generated by build-plugin-ice-router.</p>
<Link to="/about">Go to About</Link>
</div>
);
};
export default Home;
/* src/pages/About/index.tsx */
import React from 'react';
const About = () => {
return (
<div>
<h2>About Page</h2>
<p>Another convention-based route from `src/pages/About/index.tsx`.</p>
</div>
);
};
export default About;
/* build.json */
// Optional: Configure build-time router options, e.g., to ignore specific paths.
// {
// "router": {
// "ignorePaths": ["stores", "components"]
// }
// }