@docusaurus/types 2.0.0-beta.20 → 2.0.0-beta.22

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,195 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import type {TranslationFile} from './i18n';
9
+ import type {RuleSetRule, Configuration as WebpackConfiguration} from 'webpack';
10
+ import type {CustomizeRuleString} from 'webpack-merge/dist/types';
11
+ import type {CommanderStatic} from 'commander';
12
+ import type Joi from 'joi';
13
+ import type {HelmetServerState} from 'react-helmet-async';
14
+ import type {ThemeConfig} from './config';
15
+ import type {LoadContext, Props} from './context';
16
+ import type {SwizzleConfig} from './swizzle';
17
+ import type {RouteConfig} from './routing';
18
+
19
+ export type PluginOptions = {id?: string} & {[key: string]: unknown};
20
+
21
+ export type PluginConfig =
22
+ | string
23
+ | [string, PluginOptions]
24
+ | [PluginModule, PluginOptions]
25
+ | PluginModule
26
+ | false
27
+ | null;
28
+
29
+ export type PresetConfig =
30
+ | string
31
+ | [string, {[key: string]: unknown}]
32
+ | false
33
+ | null;
34
+
35
+ /**
36
+ * - `type: 'package'`, plugin is in a different package.
37
+ * - `type: 'project'`, plugin is in the same docusaurus project.
38
+ * - `type: 'local'`, none of the plugin's ancestor directories contains a
39
+ * package.json.
40
+ * - `type: 'synthetic'`, docusaurus generated internal plugin.
41
+ */
42
+ export type PluginVersionInformation =
43
+ | {
44
+ readonly type: 'package';
45
+ readonly name?: string;
46
+ readonly version?: string;
47
+ }
48
+ | {readonly type: 'project'}
49
+ | {readonly type: 'local'}
50
+ | {readonly type: 'synthetic'};
51
+
52
+ export type PluginContentLoadedActions = {
53
+ addRoute: (config: RouteConfig) => void;
54
+ createData: (name: string, data: string) => Promise<string>;
55
+ setGlobalData: (data: unknown) => void;
56
+ };
57
+
58
+ export type ConfigureWebpackUtils = {
59
+ getStyleLoaders: (
60
+ isServer: boolean,
61
+ cssOptions: {[key: string]: unknown},
62
+ ) => RuleSetRule[];
63
+ getJSLoader: (options: {
64
+ isServer: boolean;
65
+ babelOptions?: {[key: string]: unknown};
66
+ }) => RuleSetRule;
67
+ };
68
+
69
+ export type AllContent = {
70
+ [pluginName: string]: {
71
+ [pluginID: string]: unknown;
72
+ };
73
+ };
74
+
75
+ // TODO improve type (not exposed by postcss-loader)
76
+ export type PostCssOptions = {plugins: unknown[]; [key: string]: unknown};
77
+
78
+ export type ValidationSchema<T> = Joi.ObjectSchema<T>;
79
+
80
+ export type Validate<In, Out> = (
81
+ validationSchema: ValidationSchema<Out>,
82
+ options: In,
83
+ ) => Out;
84
+
85
+ export type OptionValidationContext<In, Out> = {
86
+ validate: Validate<In, Out>;
87
+ options: In;
88
+ };
89
+
90
+ export type ThemeConfigValidationContext<In, Out = In> = {
91
+ validate: Validate<In, Out>;
92
+ themeConfig: In;
93
+ };
94
+
95
+ export type HtmlTagObject = {
96
+ /**
97
+ * Attributes of the html tag.
98
+ * E.g. `{ disabled: true, value: "demo", rel: "preconnect" }`
99
+ */
100
+ attributes?: Partial<{[key: string]: string | boolean}>;
101
+ /** The tag name, e.g. `div`, `script`, `link`, `meta` */
102
+ tagName: string;
103
+ /** The inner HTML */
104
+ innerHTML?: string;
105
+ };
106
+
107
+ export type HtmlTags = string | HtmlTagObject | (string | HtmlTagObject)[];
108
+
109
+ export type Plugin<Content = unknown> = {
110
+ name: string;
111
+ loadContent?: () => Promise<Content> | Content;
112
+ contentLoaded?: (args: {
113
+ /** The content loaded by this plugin instance */
114
+ content: Content; //
115
+ /** Content loaded by ALL the plugins */
116
+ allContent: AllContent;
117
+ actions: PluginContentLoadedActions;
118
+ }) => Promise<void> | void;
119
+ postBuild?: (
120
+ props: Props & {
121
+ content: Content;
122
+ head: {[location: string]: HelmetServerState};
123
+ },
124
+ ) => Promise<void> | void;
125
+ // TODO refactor the configureWebpack API surface: use an object instead of
126
+ // multiple params (requires breaking change)
127
+ configureWebpack?: (
128
+ config: WebpackConfiguration,
129
+ isServer: boolean,
130
+ utils: ConfigureWebpackUtils,
131
+ content: Content,
132
+ ) => WebpackConfiguration & {
133
+ mergeStrategy?: {
134
+ [key: string]: CustomizeRuleString;
135
+ };
136
+ };
137
+ configurePostCss?: (options: PostCssOptions) => PostCssOptions;
138
+ getThemePath?: () => string;
139
+ getTypeScriptThemePath?: () => string;
140
+ getPathsToWatch?: () => string[];
141
+ getClientModules?: () => string[];
142
+ extendCli?: (cli: CommanderStatic) => void;
143
+ injectHtmlTags?: (args: {content: Content}) => {
144
+ headTags?: HtmlTags;
145
+ preBodyTags?: HtmlTags;
146
+ postBodyTags?: HtmlTags;
147
+ };
148
+ // TODO before/afterDevServer implementation
149
+
150
+ // translations
151
+ getTranslationFiles?: (args: {
152
+ content: Content;
153
+ }) => Promise<TranslationFile[]> | TranslationFile[];
154
+ getDefaultCodeTranslationMessages?: () =>
155
+ | Promise<{[id: string]: string}>
156
+ | {[id: string]: string};
157
+ translateContent?: (args: {
158
+ /** The content loaded by this plugin instance. */
159
+ content: Content;
160
+ translationFiles: TranslationFile[];
161
+ }) => Content;
162
+ translateThemeConfig?: (args: {
163
+ themeConfig: ThemeConfig;
164
+ translationFiles: TranslationFile[];
165
+ }) => ThemeConfig;
166
+ };
167
+
168
+ export type InitializedPlugin = Plugin & {
169
+ readonly options: Required<PluginOptions>;
170
+ readonly version: PluginVersionInformation;
171
+ /** The absolute path to the folder containing the entry point file. */
172
+ readonly path: string;
173
+ };
174
+
175
+ export type LoadedPlugin = InitializedPlugin & {
176
+ readonly content: unknown;
177
+ };
178
+
179
+ export type PluginModule = {
180
+ (context: LoadContext, options: unknown): Plugin | Promise<Plugin>;
181
+ validateOptions?: <T, U>(data: OptionValidationContext<T, U>) => U;
182
+ validateThemeConfig?: <T>(data: ThemeConfigValidationContext<T>) => T;
183
+
184
+ getSwizzleComponentList?: () => string[] | undefined; // TODO deprecate this one later
185
+ getSwizzleConfig?: () => SwizzleConfig | undefined;
186
+ };
187
+
188
+ export type Preset = {
189
+ plugins?: PluginConfig[];
190
+ themes?: PluginConfig[];
191
+ };
192
+
193
+ export type PresetModule = {
194
+ (context: LoadContext, presetOptions: unknown): Preset;
195
+ };
@@ -0,0 +1,138 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import type {ParsedUrlQueryInput} from 'querystring';
9
+
10
+ /**
11
+ * A "module" represents a unit of serialized data emitted from the plugin. It
12
+ * will be imported on client-side and passed as props, context, etc.
13
+ *
14
+ * If it's a string, it's a file path that Webpack can `require`; if it's
15
+ * an object, it can also contain `query` or other metadata.
16
+ */
17
+ export type Module =
18
+ | {
19
+ /**
20
+ * A marker that tells the route generator this is an import and not a
21
+ * nested object to recurse.
22
+ */
23
+ __import?: boolean;
24
+ path: string;
25
+ query?: ParsedUrlQueryInput;
26
+ }
27
+ | string;
28
+
29
+ /**
30
+ * Represents the data attached to each route. Since the routes.js is a
31
+ * monolithic data file, any data (like props) should be serialized separately
32
+ * and registered here as file paths (a {@link Module}), so that we could
33
+ * code-split.
34
+ */
35
+ export type RouteModules = {
36
+ [propName: string]: Module | RouteModules | RouteModules[];
37
+ };
38
+
39
+ /**
40
+ * Represents a "slice" of the final route structure returned from the plugin
41
+ * `addRoute` action.
42
+ */
43
+ export type RouteConfig = {
44
+ /** With leading slash. Trailing slash will be normalized by config. */
45
+ path: string;
46
+ /** Component used to render this route, a path that Webpack can `require`. */
47
+ component: string;
48
+ /**
49
+ * Props. Each entry should be `[propName]: pathToPropModule` (created with
50
+ * `createData`)
51
+ */
52
+ modules?: RouteModules;
53
+ /**
54
+ * The route context will wrap the `component`. Use `useRouteContext` to
55
+ * retrieve what's declared here. Note that all custom route context declared
56
+ * here will be namespaced under {@link RouteContext.data}.
57
+ */
58
+ context?: RouteModules;
59
+ /** Nested routes config. */
60
+ routes?: RouteConfig[];
61
+ /** React router config option: `exact` routes would not match subroutes. */
62
+ exact?: boolean;
63
+ /** Used to sort routes. Higher-priority routes will be placed first. */
64
+ priority?: number;
65
+ /** Extra props; will be copied to routes.js. */
66
+ [propName: string]: unknown;
67
+ };
68
+
69
+ export type RouteContext = {
70
+ /**
71
+ * Plugin-specific context data.
72
+ */
73
+ data?: object | undefined;
74
+ };
75
+
76
+ /**
77
+ * Top-level plugin routes automatically add some context data to the route.
78
+ * This permits us to know which plugin is handling the current route.
79
+ */
80
+ export type PluginRouteContext = RouteContext & {
81
+ plugin: {
82
+ id: string;
83
+ name: string;
84
+ };
85
+ };
86
+
87
+ /**
88
+ * The shape would be isomorphic to {@link RouteModules}:
89
+ * {@link Module} -> `string`, `RouteModules[]` -> `ChunkNames[]`.
90
+ *
91
+ * Each `string` chunk name will correlate with one key in the {@link Registry}.
92
+ */
93
+ export type ChunkNames = {
94
+ [propName: string]: string | ChunkNames | ChunkNames[];
95
+ };
96
+
97
+ /**
98
+ * A map from route paths (with a hash) to the chunk names of each module, which
99
+ * the bundler will collect.
100
+ *
101
+ * Chunk keys are routes with a hash, because 2 routes can conflict with each
102
+ * other if they have the same path, e.g.: parent=/docs, child=/docs
103
+ *
104
+ * @see https://github.com/facebook/docusaurus/issues/2917
105
+ */
106
+ export type RouteChunkNames = {
107
+ [routePathHashed: string]: ChunkNames;
108
+ };
109
+
110
+ /**
111
+ * Each key is the chunk name, which you can get from `routeChunkNames` (see
112
+ * {@link RouteChunkNames}). The values are the opts data that react-loadable
113
+ * needs. For example:
114
+ *
115
+ * ```js
116
+ * const options = {
117
+ * optsLoader: {
118
+ * component: () => import('./Pages.js'),
119
+ * content.foo: () => import('./doc1.md'),
120
+ * },
121
+ * optsModules: ['./Pages.js', './doc1.md'],
122
+ * optsWebpack: [
123
+ * require.resolveWeak('./Pages.js'),
124
+ * require.resolveWeak('./doc1.md'),
125
+ * ],
126
+ * }
127
+ * ```
128
+ *
129
+ * @see https://github.com/jamiebuilds/react-loadable#declaring-which-modules-are-being-loaded
130
+ */
131
+ export type Registry = {
132
+ readonly [chunkName: string]: [
133
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
134
+ Loader: () => Promise<any>,
135
+ ModuleName: string,
136
+ ResolvedModuleName: string,
137
+ ];
138
+ };
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import type {JSXElementConstructor} from 'react';
9
+
10
+ export type SwizzleAction = 'eject' | 'wrap';
11
+ export type SwizzleActionStatus = 'safe' | 'unsafe' | 'forbidden';
12
+
13
+ export type SwizzleComponentConfig = {
14
+ actions: {[action in SwizzleAction]: SwizzleActionStatus};
15
+ description?: string;
16
+ };
17
+
18
+ export type SwizzleConfig = {
19
+ components: {[componentName: string]: SwizzleComponentConfig};
20
+ // Other settings could be added here, like the ability to declare the config
21
+ // as exhaustive so that we can emit errors
22
+ };
23
+
24
+ /**
25
+ * This type is almost the same as `React.ComponentProps`, but with one minor
26
+ * fix: when the component is a function with no parameters, it produces `{}`
27
+ * instead of `unknown`, allowing us to spread the props derived from another
28
+ * component. This is useful for wrap swizzling.
29
+ *
30
+ * @see https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/60766
31
+ */
32
+ export type WrapperProps<
33
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
34
+ T extends keyof JSX.IntrinsicElements | JSXElementConstructor<any>,
35
+ > = T extends JSXElementConstructor<infer P>
36
+ ? unknown extends P
37
+ ? // eslint-disable-next-line @typescript-eslint/ban-types
38
+ {}
39
+ : P
40
+ : T extends keyof JSX.IntrinsicElements
41
+ ? JSX.IntrinsicElements[T]
42
+ : // eslint-disable-next-line @typescript-eslint/ban-types
43
+ {};
package/src/utils.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ /**
2
+ * Copyright (c) Facebook, Inc. and its affiliates.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ export type UseDataOptions = {
9
+ /**
10
+ * Throw an error, or simply return undefined if the data cannot be found. Use
11
+ * `true` if you are sure the data must exist.
12
+ */
13
+ failfast?: boolean;
14
+ };