-
-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document CLI and plugin APIs (#1518)
- Loading branch information
Showing
11 changed files
with
201 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Fixture plugins | ||
|
||
While there's no formal way to package renderer plugins (like with server and UI plugins), you can tap into the fixture context to read and write fixture state that is synchronized between the renderer and the Cosmos UI. | ||
|
||
The [Viewport](../usage/fixtures.md#viewport) decorator is a good example. | ||
|
||
## `FixtureContext` | ||
|
||
```jsx | ||
import { FixtureContext } from 'react-cosmos/client'; | ||
|
||
export function MagicDecorator({ children }) { | ||
const { fixtureState, setFixtureState } = React.useContext(FixtureContext); | ||
|
||
// Read or write the fixture state based on user events or other side effects. | ||
|
||
return children; | ||
} | ||
``` | ||
|
||
- The standard fixture state object contains the `props`, `classState` and `controls` fields. They're used to construct the Props, Class State and Control panels in the Cosmos UI. | ||
- You can extend the fixture state with extra fields. For example the [Viewport](../usage/fixtures.md#viewport) decorator sets the `fixtureState.viewport` field used by the responsive preview plugin in the Cosmos UI. | ||
- Generally a fixture plugin will pair with a Cosmos UI plugin to syncronize data between the renderer, which runs inside the user's code, and the Cosmos UI. | ||
|
||
--- | ||
|
||
[Join us on Discord](https://discord.gg/3X95VgfnW5) for feedback, questions and ideas. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# Plugin config | ||
|
||
This is a guide for creating Cosmos plugins. | ||
|
||
Create a `cosmos.plugin.json` file: | ||
|
||
```json | ||
{ | ||
"name": "Magic plugin", | ||
"server": "serverPlugin.js", | ||
"ui": "uiPlugin.js" | ||
} | ||
``` | ||
|
||
This JSON config is the plugin's entry point. | ||
|
||
A Cosmos plugin can contain a **server plugin** and a **UI plugin**. At least one is required, along with a plugin name. The `server` and `ui` paths are resolved relative to the config's parent directory. | ||
|
||
To enable a Cosmos plugin add it to the `plugins` option in the `cosmos.config.json` of the host project: | ||
|
||
```json | ||
{ | ||
"plugins": ["../packages/magic-plugin/cosmos.plugin.json"] | ||
} | ||
``` | ||
|
||
If the Cosmos plugin is an NPM package, add the name of the package to `plugins` instead: | ||
|
||
```json | ||
{ | ||
"plugins": ["react-cosmos-plugin-magic"] | ||
} | ||
``` | ||
|
||
To publish a Cosmos plugin as an NPM package, set the `main` field in its `package.json` to `"cosmos.plugin.json"` (or a different path where the Cosmos plugin config is located): | ||
|
||
```json | ||
{ | ||
"name": "react-cosmos-plugin-magic", | ||
"version": "1.0.0", | ||
"main": "cosmos.plugin.json" | ||
} | ||
``` | ||
|
||
See the individual guides for each plugin type: | ||
|
||
- [Server plugins](server-plugins.md) | ||
- [UI plugins](ui-plugins.md) | ||
|
||
--- | ||
|
||
[Join us on Discord](https://discord.gg/3X95VgfnW5) for feedback, questions and ideas. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# Server plugins | ||
|
||
This is a guide for creating Cosmos server plugins. | ||
|
||
## Boilerplate | ||
|
||
The `server` field in [`cosmos.plugin.json`](./plugin-config.md) points to a module like this: | ||
|
||
```js | ||
export default { | ||
name: 'magic-plugin', | ||
|
||
async config({ cosmosConfig }) { | ||
// An opportunity to alter the user's Cosmos config | ||
return cosmosConfig; | ||
}, | ||
|
||
async devServer({ cosmosConfig, platform, httpServer, expressApp }) { | ||
// Dev server plugin | ||
}, | ||
|
||
async export({ cosmosConfig }) { | ||
// Static export plugin | ||
}, | ||
}; | ||
``` | ||
|
||
### `config` | ||
|
||
| Argument | Description | | ||
| -------------- | ----------------------------------------------------------------------- | | ||
| `cosmosConfig` | The user's [Cosmos config](../usage/configuration.md#cosmosconfigjson). | | ||
| `command` | `"dev"` or `"export"`. | | ||
| `platform` | `"web"` or `"native"`. | | ||
|
||
The `config` hook is called before both `devServer` and `export` hooks. It allows overriding the user's Cosmos config. Setting the `rendererUrl` config option is a common use case. | ||
|
||
### `devServer` | ||
|
||
| Argument | Description | | ||
| -------------- | --------------------------------------------------------------------------------------------- | | ||
| `cosmosConfig` | The user's [Cosmos config](../usage/configuration.md#cosmosconfigjson). | | ||
| `platform` | `"web"` or `"native"`. | | ||
| `httpServer` | The [http.Server](https://nodejs.org/api/http.html#class-httpserver) instance used by Cosmos. | | ||
| `expressApp` | The [Express App](https://expressjs.com/en/4x/api.html#app) instance used by Cosmos. | | ||
| `sendMessage` | Send a message to the Cosmos UI. | | ||
|
||
A hook for starting the renderer dev server alongside the Cosmos server. | ||
|
||
For example in the Webpack plugin the Webpack compiler is attached to Cosmos' internal Express app, having the renderer run on the same port as the Cosmos server. In contract, the Vite plugin starts the Vite dev server independently and in this case you end up using two ports—one for the Cosmos server and one for the Vite renderer. | ||
|
||
### `export` | ||
|
||
| Argument | Description | | ||
| -------------- | ----------------------------------------------------------------------- | | ||
| `cosmosConfig` | The user's [Cosmos config](../usage/configuration.md#cosmosconfigjson). | | ||
|
||
A hook for exporting the user's fixtures and decorators into a static Cosmos renderer that the static Cosmos UI connects to. | ||
|
||
--- | ||
|
||
[Join us on Discord](https://discord.gg/3X95VgfnW5) for feedback, questions and ideas. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# UI plugins | ||
|
||
This is a guide for creating Cosmos UI plugins. | ||
|
||
WIP. | ||
|
||
--- | ||
|
||
[Join us on Discord](https://discord.gg/3X95VgfnW5) for feedback, questions and ideas. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters