Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline extension API #3180

Open
4 tasks done
elringus opened this issue Nov 4, 2023 · 4 comments · May be fixed by #3478
Open
4 tasks done

Streamline extension API #3180

elringus opened this issue Nov 4, 2023 · 4 comments · May be fixed by #3478
Labels

Comments

@elringus
Copy link

elringus commented Nov 4, 2023

Is your feature request related to a problem? Please describe.

What is the current stance on plugins for vitepress? I'm aware that it was decided to not maintain a plugin system similar to vuepress, but how'd you generally advice authoring an extension for vitepress, which has to access both config and app context?

I'm working on a plugin that needs to inject an md parser (which is done via vitepress config) and attach a route changed listener (which is done via enhanceApp).

It's all fine as long as you're performing this one-time in your own project, but sharing such extension as a package/plugin is bothersome, as the user has to both inject it in the config and create a boilerplate theme/index.js to inject the route listener.

Unless I'm mussing something (like, maybe it's somehow possible to attach the listener via a header script specified in the config?), could you please consider exposing an enhanceApp hook in the config? This way we would be able to install vitepress plugins by overriding the config, eg:

import { defineConfig } from "vitepress";
import plugin from "plugin";

export default defineConfig(plugin({
    ...
}));

Event better would be adding a prop to the config with a (config) => config array, so it'd feel more familiar to how we inject plugins in general:

import { defineConfig } from "vitepress";
import plugin from "plugin";

export default defineConfig({
    plugins: [plugin]
});

Describe the solution you'd like

Describe alternatives you've considered

No response

Additional context

No response

Validations

@jcbhmr
Copy link
Contributor

jcbhmr commented Nov 17, 2023

fwiw you can already kind of do this with extends: existingConfig

this works iirc: #3143

import { defineConfig, mergeConfig } from "vitepress"

function plugin1() {
  return {
    themeConfig: {
      sidebar: [
        { ... }
      ]
    }
  }
}

// plugin2 ...

export default defineConfig({
  extends: [
    plugin1({ doThing: false }),
    plugin2({ isDev: true }),
  ].reduce(mergeConfig), // as long as plugin1() and plugin2() return plain objects lol not promises or arrays

  // ... normal config; can override plugn config from extends
})

related #3136

@elringus
Copy link
Author

I'm aware of this option; this request is about exposing enhanceApp to the config.

@jcbhmr
Copy link
Contributor

jcbhmr commented Nov 21, 2023

ive been playing around and got this to work vuejs:85bd7e8...jcbhmr:dd326de
forked tarball to npm install https:///...tgz is in https://github.com/jcbhmr/vitepress/releases/tag/v1.0.0-rc.29

https://stackblitz.com/edit/vite-fmx81c?file=docs%2F.vitepress%2Fconfig.ts,docs%2F.vitepress%2Fmy-plugin.ts,docs%2F.vitepress%2Fmy-plugin-theme.ts

// config.ts
  extends: [myPlugin()],
// then
export default async function myPlugin(): UserConfig {
  return {
    additionalThemeFiles: [
      fileURLToPath(import.meta.resolve('./my-plugin-theme.ts')),
    ],
// then in the theme
export default {
  enhanceApp({ app }) {
// ...
  },
};

@github-actions github-actions bot added the stale label Dec 26, 2023
@elringus
Copy link
Author

#3314 would resolve this issue as well, as it'd be possible to inject scripts from vite plugin.

@github-actions github-actions bot removed the stale label Jan 18, 2024
@jcbhmr jcbhmr linked a pull request Jan 19, 2024 that will close this issue
@github-actions github-actions bot added the stale label Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants