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

feat: typescript support for config file #2973

Merged
merged 2 commits into from
Dec 22, 2021
Merged

feat: typescript support for config file #2973

merged 2 commits into from
Dec 22, 2021

Conversation

ulivz
Copy link
Member

@ulivz ulivz commented Dec 22, 2021

Overview

1 9-overview

Features

Support .vuepress/config.ts

Previously, VuePress only supported these configurations

  • .vuepress/config.js
  • .vuepress/config.yml
  • .vuepress/config.toml

From now on, .vuepress/config.ts get officially supported.

defineConfig helper for config intellisense

A helper function exposed at vuepress/config, which helps you to have type prompt:

import { defineConfig } from "vuepress/config";

export default defineConfig({
  title: "VuePress",
  description: "Vue-powered Static Site Generator"
  // ...
});

Type Inferences for Theme

By default, defineConfig helper leverages the theme config type from default theme:

import { defineConfig } from "vuepress/config";

export default defineConfig({
  themeConfig: {
    repo: "vuejs/vuepress",
    editLinks: true,
    docsDir: "packages/docs/docs"
    // Type is `DefaultThemeConfig`
  }
});

If you use a custom theme, you can use the defineConfig4CustomTheme helper with ability to pass generic type for your theme:

import { defineConfig4CustomTheme } from "vuepress/config";

interface MyThemeConfig {
  hello: string;
}

export default defineConfig4CustomTheme<MyThemeConfig>(
  {
    themeConfig: {
      // Type is `MyThemeConfig`
      hello: "vuepress"
    }
  },

Type Inferences for Official Plugins

From now, you’ll be able to enjoy the type prompt of the official plugins:

1 9-official-plugin-tuple-usage

Options of the official plugins certainly have type prompts, Both Tuple Style and Object Style, and Plugin Shorthand support type inference:

  • Tuple Style:
import { defineConfig } from 'vuepress/config'

export default defineConfig({
  plugins: [
    [
      '@vuepress/pwa',
      {
        serviceWorker: true
      }
    ]
  ]
})

1 9-official-plugin-options

  • Object Style:
import { defineConfig } from 'vuepress/config'

export default defineConfig({
  plugins: {
    '@vuepress/pwa': {
      serviceWorker: true
    }
  }
})

The illustration snapshot is omitted here, you can try it yourself.

ISO Language Code

Type inference supports ISO Language Code

1 9-lang

Context API

VuePress's configuration can also be a function, while its first parameter is the current app context:

import { defineConfig } from "vuepress/config";

export default defineConfig(ctx => ({
  // do not execute babel compilation under development
  evergreen: ctx.isProd
}));

Limitations

It is worth noting that third-party plugins do not support Plugin Shorthand if you're using Tuple Style to write your config, this is because from the perspective of the type system, the unknown shortcut is equivalent to string, which results in the failure of type inference.

By default, only officially maintained and plugins under VuePress Community support shortcut, feel free to submit pull request to add your plugin at this file.

Credits

@ulivz ulivz force-pushed the feat/typescript branch 7 times, most recently from 95decaa to 3e2da78 Compare December 22, 2021 19:30
@ulivz ulivz changed the title WIP: feat: typescript support for config file feat: typescript support for config file Dec 22, 2021
@ulivz ulivz force-pushed the feat/typescript branch 4 times, most recently from f65a872 to 0bb6d4f Compare December 22, 2021 19:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant