|
1 |
| -# Reactmos |
| 1 | +# Reactmos (REACT MOduleS) |
2 | 2 |
|
3 |
| -Create extendable React SPA projects! |
| 3 | +Reactmos is a small project, primarily a conceptual approach, inspired by [Nuxt Layers](https://nuxt.com/docs/getting-started/layers) for React. |
4 | 4 |
|
5 |
| -## Usage |
| 5 | +> [!NOTE] |
| 6 | +> This project is a Work In Progress and currently supports only Single Page Applications (SPA). |
6 | 7 |
|
| 8 | +See [documentation](https://reactmos.dev) |
| 9 | + |
| 10 | +## CLI |
| 11 | + |
| 12 | +We provide a CLI to help run the development server and build the final bundle. |
| 13 | + |
| 14 | +## Reactmos |
| 15 | + |
| 16 | +Reactmos serves as the core of the application and acts as the entry point for Vite. |
| 17 | + |
| 18 | +### Modules |
| 19 | + |
| 20 | +This is where you will develop your application and extend other modules. |
| 21 | + |
| 22 | +To create a new module, simply run: |
| 23 | + |
| 24 | +```sh |
| 25 | +pnpm create reactmos <module-name> |
| 26 | +``` |
| 27 | + |
| 28 | +#### Configuration |
| 29 | + |
| 30 | +See more in `packages/cli/src/types.ts` |
| 31 | + |
| 32 | +```ts |
| 33 | +const moduleConfig: ModuleConfig = { |
| 34 | + moduleName: 'module-boilerplate', |
| 35 | + /** |
| 36 | + * Root component of the application. |
| 37 | + * If not provided, Reactmos will use the root component |
| 38 | + * from the first extended module that defines one. |
| 39 | + * If no extended module provides a root component, |
| 40 | + * the default from the entry point will be used. |
| 41 | + */ |
| 42 | + root: App, |
| 43 | + routes: () => { |
| 44 | + return [ |
| 45 | + { |
| 46 | + path: '/welcome', |
| 47 | + Component: Welcome, |
| 48 | + }, |
| 49 | + ]; |
| 50 | + }, |
| 51 | + hooks: { |
| 52 | + 'app:beforeRender': () => { |
| 53 | + console.log('Before render') |
| 54 | + } |
| 55 | + } |
| 56 | +} |
| 57 | +``` |
| 58 | + |
| 59 | +### Extending Modules |
| 60 | + |
| 61 | +To extend another module, just add its package name to the `extends` field in `module.config.ts`: |
| 62 | + |
| 63 | +```ts |
| 64 | +export default { |
| 65 | + extends: ['module-to-extend'] |
| 66 | +} |
7 | 67 | ```
|
8 |
| -pnpm create reactmos <project-name> |
| 68 | + |
| 69 | +You can also use relative path to other module directory |
| 70 | + |
| 71 | +```ts |
| 72 | +export default { |
| 73 | + extends: ['../module-to-extend'] |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Lifecycle Hooks |
| 78 | + |
| 79 | +Reactmos provides several lifecycle hooks: |
| 80 | + |
| 81 | +1. **`app:afterBoot`** - Called after the CLI registers all module routes and hooks. Used by the entry point to mount the application. |
| 82 | +2. **`app:init`** - Called before the entry point retrieves `App.tsx`, which serves as the root of the application. |
| 83 | +3. **`app:beforeRender`** - Called after `App.tsx` is retrieved but before calling `render` from `createRoot`. |
| 84 | +4. **`app:afterRender`** - Called after `createRoot` executes `render`. |
| 85 | + |
| 86 | +You can use `app:afterBoot` to create new hooks within your application: |
| 87 | + |
| 88 | +```ts |
| 89 | +import { hooks } from 'reactmos'; |
| 90 | + |
| 91 | +// Register a new hook called 'hello' |
| 92 | +hooks.hook('hello', () => { |
| 93 | + console.log('Hello, World!'); |
| 94 | +}); |
| 95 | + |
| 96 | +// Call the 'hello' hook |
| 97 | +hooks.callHook('hello'); |
9 | 98 | ```
|
10 | 99 |
|
| 100 | +For more details, see [hookable](https://github.com/unjs/hookable). |
| 101 | + |
| 102 | +### Components |
| 103 | + |
| 104 | +Reactmos provides a `<Pages />` component that represents all registered routes. You can use it in your `App.tsx` root component. |
| 105 | + |
| 106 | +### Utility Functions |
| 107 | + |
| 108 | +Reactmos also provides the following functions: |
| 109 | + |
| 110 | +1. **`getRoutes`** - Retrieves all registered routes. |
| 111 | +2. **`getRoot`** - Returns the `App.tsx` root component. |
| 112 | +3. **`getExtras(moduleName)`** - Returns extra configuration in module configs |
| 113 | + |
| 114 | +## Roadmap |
11 | 115 |
|
| 116 | +- [x] Support for extending routes/pages |
| 117 | +- [x] Support for extending the `public` directory |
| 118 | +- [x] Share custom configs with other modules |
| 119 | +- [ ] Additional extension capabilities? |
0 commit comments