docs: update admin extension docs (#4778)

* docs: update admin extension docs

* eslint fixes

* fix broken links

* fix broken link

* added admin upgrade guide

* fix lint errors
This commit is contained in:
Shahed Nasser authored and GitHub committed 2023-08-17 20:04:13 +03:00
1 parent 75881a2cfe
commit 80b1bff8cb
21 files changed
+234 -146

No files matched your search

+107
View File
@@ -0,0 +1,107 @@
---
description: "In this document, you'll learn about the different ways you can configure the admin dashboard for your custom use cases."
---
# Admin Custom Configuration
In this document, you'll learn about the different ways you can configure the admin dashboard for your custom use cases.
## Admin CLI
### Build Command Options
The `build` command in the admin CLI allows you to manually build the admin dashboard. If you intend to use it, you should typically add it to the `package.json` of the Medusa backend:
```json title=package.json
{
"scripts": {
// other scripts...
"build:admin": "medusa-admin build"
}
}
```
You can add the following options to the `medusa-admin build` command:
- `--deployment`: a boolean value indicating that the build should be ready for deployment. When this option is added, options are not loaded from `medusa-config.js` anymore, and it means the admin will be built to be hosted on an external host. For example, `medusa-admin build --deployment`.
- `--backend` or `-b`: a string specifying the URL of the Medusa backend. This can be useful with the `--deployment` option. The default here is the value of the environment variable `MEDUSA_ADMIN_BACKEND_URL`. For example, `medusa-admin build --deployment --backend example.com`
- `--out-dir` or `-o`: a string specifying a custom path to output the build files to. By default, it will be the `build` directory. For example, `medusa-admin --deployment --out-dir public`.
- `--include` or `-i`: a list of strings of paths to files you want to include in the build output. It can be useful if you want to inject files that are relevant to your external hosting, such as adding a `200.html` file that is needed for redirects on Surge. For example, `medusa-admin --deployment --include 200.html`
- `--include-dist` or `-d`: a string specifying the path to copy the files specified in `--include` to. By default, the files are copied to the root of the build directory. You can use this option to change that. For example, `medusa-admin --deployment --include 200.html --include-dist static`.
### Dev Command Options
The `dev` command in the admin CLI allows you to run the admin dashboard in development separately from the Medusa backend. If you intend to use it, you should typically add it to the `package.json` of the Medusa backend:
```json title=package.json
{
"scripts": {
// other scripts...
"dev:admin": "medusa-admin dev"
}
}
```
You can add the following options to the `medusa-admin dev` command:
- `--backend` or `-b`: a string specifying the URL of the Medusa backend. By default, it's the value of the environment variable `MEDUSA_ADMIN_BACKEND_URL`. For example, `medusa-admin dev --backend example.com`.
- `--port` or `-p`: the port to run the admin on. By default, it's `7001`. For example, `medusa-admin dev --port 8000`.
---
## Custom Environment Variables
If you want to set environment variables that you want to access in your admin dashboard's customizations (such as in [widgets](./widgets.md) or [UI routes](./routes.md)), your environment variables must be prefixed with `MEDUSA_ADMIN_`. Otherwise, it won't be loaded within the admin.
For example:
```bash
MEDUSA_ADMIN_CUSTOM_API_KEY=123...
```
---
## Custom Webpack Configurations
:::note
Plugins cannot include webpack customizations.
:::
The admin dashboard uses [Webpack](https://webpack.js.org/) to define the necessary configurations for both the core admin plugin and your extensions. So, for example, everything works out of the box with Tailwind CSS, the admin dependencies, and more.
However, you may have some advanced case where you need to tweak the webpack configurations. For example, you want to support styling your extensions with CSS Modules.
For such use cases, you can extend the default webpack configurations defined in the admin plugin to add your custom configurations.
To do that, create the file `src/admin/webpack.config.js` that uses the `withCustomWebpackConfig` method imported from `@medusajs/admin` to export the extended configurations. The method accepts a callback function that must return an object of [webpack configuration](https://webpack.js.org/configuration/). The callback function accepts two parameters:
1. `config`: the first parameter is an object that holds the default webpack configuration. You should add your configurations to this object, then return it. Not returning the default configurations will lead to the application breaking.
2. `webpack`: the second parameter is the webpack instance.
:::warning
This is an advanced feature and requires knowledge of configuring webpack. If configured wrongly, it may lead to the admin application breaking.
:::
For example:
```js title=src/admin/webpack.config.js
import { withCustomWebpackConfig } from "@medusajs/admin"
export default withCustomWebpackConfig((config, webpack) => {
config.plugins.push(
new webpack.DefinePlugin({
"process.env": {
NODE_ENV: JSON.stringify("production"),
API_URL:
JSON.stringify("https://api.medusa-commerce.com"),
},
})
)
return config
})
```
+1 -4
View File
@@ -2,9 +2,6 @@
title: 'Example: How to Create Onboarding Widget'
description: 'Learn how to build the onboarding widget available in the admin dashboard the first time you install a Medusa project.'
addHowToData: true
badge:
variant: orange
text: beta
---
In this guide, you’ll learn how to build the onboarding widget available in the admin dashboard the first time you install a Medusa project.
@@ -28,7 +25,7 @@ By following this tutorial, you’ll:
## Prerequisites
Before you follow along this tutorial, you must have a Medusa backend installed with the `beta` version of the `@medusajs/admin` package. If not, you can use the following command to get started:
Before you follow along this tutorial, you must have a Medusa backend installed. If not, you can use the following command to get started:
```bash
npx create-medusa-app@latest
+27 -65
View File
@@ -52,16 +52,6 @@ In the directory of your Medusa backend, run the following command to install ad
npm install @medusajs/admin
```
:::note
Installing the `@beta` version of the admin and Medusa core allows you to perform customizations such as creating [Admin Widgets](./widgets.md) or [Admin Dashboard Routes](./routes.md). You can install the `beta` version with the following command:
```bash npm2yarn
npm install @medusajs/admin@beta @medusajs/medusa@beta
```
:::
### Step 2: Add Admin to Medusa Configurations
In `medusa-config.js`, add the admin plugin into the array of `plugins`:
@@ -81,20 +71,23 @@ const plugins = [
The plugin accepts the following options:
1. `serve`: (default: `true`) a boolean indicating whether to serve the admin dashboard when the Medusa backend starts. If set to `false`, you can serve the admin dashboard using the [dev command](#dev-command-options).
2. `path`: (default: `app`) a string indicating the path the admin server should run on when [running the Medusa backend in production](#note-on-admin-path). It shouldn't be prefixed or suffixed with a slash `/`, and it can't be one of the reserved paths: "admin" and "store".
1. `serve`: (default: `true`) a boolean indicating whether to serve the admin dashboard when the Medusa backend starts. If set to `false`, you can serve the admin dashboard using the [dev command](./configuration.md#dev-command-options).
2. `path`: (default: `app`) a string indicating the path the admin server should run on when running the Medusa backend in production. It must be prefixed with a slash `/`, but it can't end with a `/`, which throws an error. It also can't be one of the reserved paths: "admin" and "store".
3. `outDir`: Optional path for where to output the admin build files.
4. `autoRebuild`: (default: `false`) a boolean indicating whether the admin UI should be rebuilt if there are any changes or if a missing build is detected when the backend starts. If not set, you must [manually build the admin dashboard](#build-command-options).
4. `autoRebuild`: (default: `false`) a boolean indicating whether the admin UI should be rebuilt if there are any changes or if a missing build is detected when the backend starts. If not set, you must [manually build the admin dashboard](./configuration.md#build-command-options).
5. `develop`: An optional object that accepts the following properties:
- `open`: (default: `true`) a boolean value that indicates if the browser should be opened when the medusa project is first started.
- `port`: (default: `7001`) a number indicating the port the admin dashboard runs on.
### Optional: Manually Building Admin Dashboard
If you have `autoRebuild` disabled, you must build your admin dashboard before starting the Medusa backend. Refer to the [build command](#build-command-options) for more details.
If you have `autoRebuild` disabled, you must build your admin dashboard before starting the Medusa backend. Refer to the [build command](./configuration.md#build-command-options) for more details.
### Step 3: Test the Admin Dashboard
:::tip
If you disabled the `serve` option, you need to run the admin dashboard separately using the [dev command](#dev-command-options)
If you disabled the `serve` option, you need to run the admin dashboard separately using the [dev command](./configuration.md#dev-command-options)
:::
@@ -104,13 +97,7 @@ You can test the admin dashboard by running the following command in the directo
npx medusa develop
```
This starts the Medusa Backend and the admin dashboard. By default, the admin will be available on the URL `localhost:9000/app`. If you set the path option, then the admin will be available on `localhost:9000/<PATH>` with `<PATH>` being the value of the path option.
:::note
If you're using the `@beta` version of the admin plugin, the admin dashboard will run on `localhost:7001` when you run the `develop` command.
:::
This starts the Medusa Backend and the admin dashboard in a development environment. By default, the admin will be available on the URL `localhost:7001` and the browser will open automatically to the admin dashboard in your default browser, unless you have the `develop.open` option disabled.
<Feedback
event="survey_admin_quickstart"
@@ -121,6 +108,23 @@ If you're using the `@beta` version of the admin plugin, the admin dashboard wil
---
## Production Path
:::note
This doesn't apply if you have the `serve` option disabled.
:::
When you run the Medusa project in a production environment (such as with the `npx medusa start` command), the admin dashboard will be available at `<MEDUSA_URL>/<ADMIN_PATH>`, where:
1. `<MEDUSA_URL>` is the URL of your Medusa backend. By default, it'll be `localhost:9000` locally.
2. `<ADMIN_PATH>` is the path you define in the [admin's configurations](#step-2-add-admin-to-medusa-configurations).
So, if you simulate a production environment locally, the admin dashboard will run by default on `localhost:9000/app`.
---
## Demo Credentials
If you installed the demo data when you installed the Medusa backend by running:
@@ -151,49 +155,6 @@ This will create a new user that you can use to log into your admin panel.
---
## Build Command Options
The `build` command in the admin CLI allows you to manually build the admin dashboard. If you intend to use it, you should typically add it to the `package.json` of the Medusa backend:
```json title=package.json
{
"scripts": {
// other scripts...
"build:admin": "medusa-admin build"
}
}
```
You can add the following options to the `medusa-admin build` command:
- `--deployment`: a boolean value indicating that the build should be ready for deployment. When this option is added, options are not loaded from `medusa-config.js` anymore, and it means the admin will be built to be hosted on an external host. For example, `medusa-admin build --deployment`.
- `--backend` or `-b`: a string specifying the URL of the Medusa backend. This can be useful with the `--deployment` option. The default here is the value of the environment variable `MEDUSA_BACKEND_URL`. For example, `medusa-admin build --deployment --backend example.com`
- `--out-dir` or `-o`: a string specifying a custom path to output the build files to. By default, it will be the `build` directory. For example, `medusa-admin --deployment --out-dir public`.
- `--include` or `-i`: a list of strings of paths to files you want to include in the build output. It can be useful if you want to inject files that are relevant to your external hosting, such as adding a `200.html` file that is needed for redirects on Surge. For example, `medusa-admin --deployment --include 200.html`
- `--include-dist` or `-d`: a string specifying the path to copy the files specified in `--include` to. By default, the files are copied to the root of the build directory. You can use this option to change that. For example, `medusa-admin --deployment --include 200.html --include-dist static`.
---
## Dev Command Options
The `dev` command in the admin CLI allows you to run the admin dashboard in development separately from the Medusa backend. If you intend to use it, you should typically add it to the `package.json` of the Medusa backend:
```json title=package.json
{
"scripts": {
// other scripts...
"dev:admin": "medusa-admin dev"
}
}
```
You can add the following options to the `medusa-admin dev` command:
- `--backend` or `-b`: a string specifying the URL of the Medusa backend. By default, it's the value of the environment variable `MEDUSA_BACKEND_URL`. For example, `medusa-admin dev --backend example.com`.
- `--port` or `-p`: the port to run the admin on. By default, it's `7001`. For example, `medusa-admin dev --port 8000`.
---
## Admin User Guide
The admin dashboard provides a lot of ecommerce features including managing Return Merchandise Authorization (RMA) flows, store settings, products, orders, and much more.
@@ -221,5 +182,6 @@ You can learn more about the admin dashboard and its features in the [User Guide
## See Also
- [Admin Configuration](./configuration.md)
- [Admin widgets](./widgets.md)
- [Admin UI routes](./routes.md)
+1 -10
View File
@@ -2,9 +2,6 @@
title: 'How to Create an Admin UI Route'
description: 'Learn how to create a new route in the admin dashboard.'
addHowToData: true
badge:
variant: orange
text: beta
---
In this document, you’ll learn how to create a new route in the admin dashboard.
@@ -29,12 +26,6 @@ If you want to create a page under the Settings page, please refer to [this docu
It’s assumed you already have a Medusa backend with the admin plugin installed before you move forward with this guide. If not, you can follow [this documentation page](../create-medusa-app.mdx) to install a Medusa project.
Furthermore, Admin UI Routes are currently available as a beta feature. So, you must install the `beta` version of the `@medusajs/admin` and `@medusajs/medusa` packages:
```bash npm2yarn
npm install @medusajs/admin@beta @medusajs/medusa@beta
```
### (Optional) TypeScript Preparations
Since routes are React components, they should be written in `.tsx` or `.jsx` files. If you’re using Typescript, you need to make some adjustments to avoid Typescript errors in your Admin files.
@@ -190,7 +181,7 @@ For example:
```tsx
import { Post } from "../../../../../models/post"
import PostForm from "../../../../components/post/form"
import { RouteProps } from "@medusajs/admin-ui"
import { RouteProps } from "@medusajs/admin"
const BlogPostCreatePage = ({
notify,
+7 -10
View File
@@ -2,9 +2,6 @@
title: 'How to Create an Admin Setting Page'
description: 'Learn how to create a new setting page in the admin dashboard.'
addHowToData: true
badge:
variant: orange
text: beta
---
In this document, you’ll learn how to create a setting page in the admin.
@@ -25,12 +22,6 @@ This guide explains how to create a new setting page in the admin dashboard with
It’s assumed you already have a Medusa backend with the admin plugin installed before you move forward with this guide. If not, you can follow [this documentation page](../create-medusa-app.mdx) to install a Medusa project.
Furthermore, custom Admin Setting Pages are currently available as a beta feature. So, you must install the `beta` version of the `@medusajs/admin` and `@medusajs/medusa` packages:
```bash npm2yarn
npm install @medusajs/admin@beta @medusajs/medusa@beta
```
### (Optional) TypeScript Preparations
Since setting pages are React components, they should be written in `.tsx` or `.jsx` files. If you’re using Typescript, you need to make some adjustments to avoid Typescript errors in your Admin files.
@@ -128,6 +119,12 @@ All setting page paths are prefixed with `/a/settings`.
For example, if you want the setting page to be available in the admin dashboard under the path `/a/settings/custom` you should create your setting page under the file path `src/admin/settings/custom/page.tsx`.
:::note
Only one-level deep files are accepted. So, for example, you can create the page `src/admin/settings/custom/page.tsx`, but not `src/admin/settings/custom/nested/page.tsx`.
:::
### Step 2: Create React Component in File
For a setting page to be valid, it must default export a React component. There are no restrictions on the content of the React component. It must also export a configuration object that indicates how the tab is shown on the Setting page.
@@ -189,7 +186,7 @@ For example:
```tsx title=src/admin/settings/custom/page.tsx
import type { SettingConfig } from "@medusajs/admin"
import type { SettingProps } from "@medusajs/admin-ui"
import type { SettingProps } from "@medusajs/admin"
const CustomSettingPage = ({
notify,
-9
View File
@@ -2,9 +2,6 @@
title: 'How to Create an Admin Widget'
description: 'Learn about what Admin widgets are and how you can create your own.'
addHowToData: true
badge:
variant: orange
text: beta
---
In this document, you will learn about what Admin widgets are and how you can create your own.
@@ -945,12 +942,6 @@ In this section, you’ll learn how to create an admin widget.
It’s assumed you already have a Medusa backend with the admin plugin installed before you move forward with this guide. If not, you can follow [this documentation page](../create-medusa-app.mdx) to install a Medusa project.
Furthermore, Admin Widgets are currently available as a beta feature. So, you must install the `beta` version of the `@medusajs/admin` and `@medusajs/medusa` packages:
```bash npm2yarn
npm install @medusajs/admin@beta @medusajs/medusa@beta
```
### (Optional) TypeScript Preparations
Since Widgets are React components, they should be written in `.tsx` or `.jsx` files. If you’re using Typescript, you need to make some adjustments to avoid Typescript errors in your Admin files.