refactor(preset-classic): migrate preset-classic to TypeScript (#5579)

This commit is contained in:
Joshua Chen 2021-09-22 17:09:52 +08:00 committed by GitHub
parent 578470a24c
commit 2ef70cb806
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 93 additions and 57 deletions

View File

@ -9,12 +9,12 @@ declare module '@docusaurus/mdx-loader' {
type RemarkOrRehypePlugin =
// eslint-disable-next-line @typescript-eslint/ban-types
[Function, Record<string, unknown>] | Function;
export interface RemarkAndRehypePluginOptions {
export type RemarkAndRehypePluginOptions = {
remarkPlugins: RemarkOrRehypePlugin[];
rehypePlugins: string[];
beforeDefaultRemarkPlugins: RemarkOrRehypePlugin[];
beforeDefaultRehypePlugins: RemarkOrRehypePlugin[];
}
};
}
// TODO Types provided by MDX 2.0 https://github.com/mdx-js/mdx/blob/main/packages/mdx/types/index.d.ts

View File

@ -31,7 +31,7 @@ export type EditUrlFunction = (editUrlParams: {
locale: string;
}) => string | undefined;
export interface PluginOptions extends RemarkAndRehypePluginOptions {
export type PluginOptions = RemarkAndRehypePluginOptions & {
id?: string;
path: string;
routeBasePath: string;
@ -61,7 +61,7 @@ export interface PluginOptions extends RemarkAndRehypePluginOptions {
editLocalizedFiles?: boolean;
admonitions: Record<string, unknown>;
authorsMapPath: string;
}
};
export interface BlogTags {
[key: string]: BlogTag;

View File

@ -7,7 +7,7 @@
import type {RemarkAndRehypePluginOptions} from '@docusaurus/mdx-loader';
export interface PluginOptions extends RemarkAndRehypePluginOptions {
export type PluginOptions = RemarkAndRehypePluginOptions & {
id?: string;
path: string;
routeBasePath: string;
@ -15,7 +15,7 @@ export interface PluginOptions extends RemarkAndRehypePluginOptions {
exclude: string[];
mdxPageComponent: string;
admonitions: Record<string, unknown>;
}
};
export type JSXPageMetadata = {
type: 'jsx';

View File

@ -7,8 +7,8 @@
import {EnumChangefreq} from 'sitemap';
export interface PluginOptions {
export type PluginOptions = {
changefreq?: EnumChangefreq;
priority?: number;
trailingSlash?: boolean;
}
};

View File

@ -2,8 +2,12 @@
"name": "@docusaurus/preset-classic",
"version": "2.0.0-beta.6",
"description": "Classic preset for Docusaurus.",
"main": "src/index.js",
"main": "lib/index.js",
"types": "src/preset-classic.d.ts",
"scripts": {
"build": "tsc",
"watch": "tsc --watch"
},
"publishConfig": {
"access": "public"
},

View File

@ -1,48 +0,0 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
module.exports = function preset(context, opts = {}) {
const {siteConfig} = context;
const {themeConfig} = siteConfig;
const {algolia, googleAnalytics, gtag} = themeConfig;
const isProd = process.env.NODE_ENV === 'production';
const debug =
typeof opts.debug !== 'undefined' ? Boolean(opts.debug) : !isProd;
return {
themes: [
[require.resolve('@docusaurus/theme-classic'), opts.theme],
// Don't add this if algolia config is not defined.
algolia && require.resolve('@docusaurus/theme-search-algolia'),
],
plugins: [
opts.docs !== false && [
require.resolve('@docusaurus/plugin-content-docs'),
opts.docs,
],
opts.blog !== false && [
require.resolve('@docusaurus/plugin-content-blog'),
opts.blog,
],
opts.pages !== false && [
require.resolve('@docusaurus/plugin-content-pages'),
opts.pages,
],
isProd &&
googleAnalytics &&
require.resolve('@docusaurus/plugin-google-analytics'),
debug && require.resolve('@docusaurus/plugin-debug'),
isProd && gtag && require.resolve('@docusaurus/plugin-google-gtag'),
isProd &&
opts.sitemap !== false && [
require.resolve('@docusaurus/plugin-sitemap'),
opts.sitemap,
],
],
};
};

View File

@ -0,0 +1,71 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
import type {
Preset,
LoadContext,
PluginConfig,
PluginOptions,
} from '@docusaurus/types';
import type {Options, ThemeConfig} from '@docusaurus/preset-classic';
function makePluginConfig(
source: string,
options?: PluginOptions,
): string | [string, PluginOptions] {
if (options) {
return [require.resolve(source), options];
}
return require.resolve(source);
}
export default function preset(
context: LoadContext,
opts: Options = {},
): Preset {
const {siteConfig} = context;
const {themeConfig} = siteConfig;
const {algolia, googleAnalytics, gtag} = themeConfig as ThemeConfig;
const isProd = process.env.NODE_ENV === 'production';
const themes: PluginConfig[] = [];
themes.push(makePluginConfig('@docusaurus/theme-classic', opts.theme));
if (algolia) {
themes.push(require.resolve('@docusaurus/theme-search-algolia'));
}
const plugins: PluginConfig[] = [];
if (opts.docs !== false) {
plugins.push(
makePluginConfig('@docusaurus/plugin-content-docs', opts.docs),
);
}
if (opts.blog !== false) {
plugins.push(
makePluginConfig('@docusaurus/plugin-content-blog', opts.blog),
);
}
if (opts.pages !== false) {
plugins.push(
makePluginConfig('@docusaurus/plugin-content-pages', opts.pages),
);
}
if (isProd && googleAnalytics) {
plugins.push(require.resolve('@docusaurus/plugin-google-analytics'));
}
if (opts.debug || (typeof opts.debug === 'undefined' && !isProd)) {
plugins.push(require.resolve('@docusaurus/plugin-debug'));
}
if (isProd && gtag) {
plugins.push(require.resolve('@docusaurus/plugin-google-gtag'));
}
if (isProd && opts.sitemap !== false) {
plugins.push(makePluginConfig('@docusaurus/plugin-sitemap', opts.sitemap));
}
return {themes, plugins};
}

View File

@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"incremental": true,
"tsBuildInfoFile": "./lib/.tsbuildinfo",
"rootDir": "src",
"outDir": "lib"
}
}