@docusaurus/types 2.0.0-beta.16 → 2.0.0-beta.19

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.
Files changed (2) hide show
  1. package/package.json +5 -4
  2. package/src/index.d.ts +571 -285
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/types",
3
- "version": "2.0.0-beta.16",
3
+ "version": "2.0.0-beta.19",
4
4
  "description": "Common types for Docusaurus packages.",
5
5
  "types": "./src/index.d.ts",
6
6
  "publishConfig": {
@@ -17,11 +17,12 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "commander": "^5.1.0",
20
+ "history": "^4.9.0",
20
21
  "joi": "^17.6.0",
21
- "querystring": "0.2.1",
22
+ "react-helmet-async": "^1.3.0",
22
23
  "utility-types": "^3.10.0",
23
- "webpack": "^5.69.1",
24
+ "webpack": "^5.72.0",
24
25
  "webpack-merge": "^5.8.0"
25
26
  },
26
- "gitHead": "eb43c4d4f95a4fb97dc9bb9dc615413e0dc2e1e7"
27
+ "gitHead": "a71e60a49cce93c1006ef10c41ac03187f057102"
27
28
  }
package/src/index.d.ts CHANGED
@@ -5,84 +5,345 @@
5
5
  * LICENSE file in the root directory of this source tree.
6
6
  */
7
7
 
8
- import type {RuleSetRule, Configuration} from 'webpack';
8
+ import type {RuleSetRule, Configuration as WebpackConfiguration} from 'webpack';
9
9
  import type {CustomizeRuleString} from 'webpack-merge/dist/types';
10
10
  import type {CommanderStatic} from 'commander';
11
11
  import type {ParsedUrlQueryInput} from 'querystring';
12
12
  import type Joi from 'joi';
13
- import type {Overwrite, DeepPartial} from 'utility-types';
13
+ import type {HelmetServerState} from 'react-helmet-async';
14
+ import type {
15
+ DeepRequired,
16
+ Required as RequireKeys,
17
+ DeepPartial,
18
+ } from 'utility-types';
14
19
  import type {Location} from 'history';
15
20
 
21
+ // === Configuration ===
22
+
16
23
  export type ReportingSeverity = 'ignore' | 'log' | 'warn' | 'error' | 'throw';
17
24
 
25
+ export type PluginOptions = {id?: string} & {[key: string]: unknown};
26
+
27
+ export type PluginConfig =
28
+ | string
29
+ | [string, PluginOptions]
30
+ | [PluginModule, PluginOptions]
31
+ | PluginModule
32
+ | false
33
+ | null;
34
+
35
+ export type PresetConfig =
36
+ | string
37
+ | [string, {[key: string]: unknown}]
38
+ | false
39
+ | null;
40
+
18
41
  export type ThemeConfig = {
19
42
  [key: string]: unknown;
20
43
  };
21
44
 
22
- // Docusaurus config, after validation/normalization
23
- export interface DocusaurusConfig {
24
- baseUrl: string;
25
- baseUrlIssueBanner: boolean;
26
- favicon?: string;
27
- tagline: string;
45
+ export type I18nLocaleConfig = {
46
+ /** The label displayed for this locale in the locales dropdown. */
47
+ label: string;
48
+ /**
49
+ * BCP 47 language tag to use in `<html lang="...">` and in
50
+ * `<link ... hreflang="...">`
51
+ */
52
+ htmlLang: string;
53
+ /** Used to select the locale's CSS and html meta attribute. */
54
+ direction: 'ltr' | 'rtl';
55
+ /**
56
+ * The [calendar](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Locale/calendar)
57
+ * used to calculate the date era. Note that it doesn't control the actual
58
+ * string displayed: `MM/DD/YYYY` and `DD/MM/YYYY` are both gregory. To choose
59
+ * the format (`DD/MM/YYYY` or `MM/DD/YYYY`), set your locale name to `en-GB`
60
+ * or `en-US` (`en` means `en-US`).
61
+ */
62
+ calendar: string;
63
+ };
64
+
65
+ export type I18nConfig = {
66
+ /**
67
+ * The locale that:
68
+ * 1. Does not have its name in the base URL
69
+ * 2. Gets started with `docusaurus start` without `--locale` option
70
+ * 3. Will be used for the `<link hrefLang="x-default">` tag
71
+ */
72
+ defaultLocale: string;
73
+ /** List of locales deployed on your site. Must contain `defaultLocale`. */
74
+ locales: [string, ...string[]];
75
+ /** Individual options for each locale. */
76
+ localeConfigs: {[locale: string]: Partial<I18nLocaleConfig>};
77
+ };
78
+
79
+ /**
80
+ * Docusaurus config, after validation/normalization.
81
+ */
82
+ export type DocusaurusConfig = {
83
+ /**
84
+ * Title for your website. Will be used in metadata and as browser tab title.
85
+ *
86
+ * @see https://docusaurus.io/docs/api/docusaurus-config#title
87
+ */
28
88
  title: string;
89
+ /**
90
+ * URL for your website. This can also be considered the top-level hostname.
91
+ * For example, `https://facebook.github.io` is the URL of
92
+ * https://facebook.github.io/metro/, and `https://docusaurus.io` is the URL
93
+ * for https://docusaurus.io.
94
+ *
95
+ * @see https://docusaurus.io/docs/api/docusaurus-config#url
96
+ */
29
97
  url: string;
30
- // trailingSlash undefined = legacy retrocompatible behavior
31
- // /file => /file/index.html
98
+ /**
99
+ * Can be considered as the path after the host. For example, `/metro/` is the
100
+ * base URL of https://facebook.github.io/metro/. For URLs that have no path,
101
+ * it should be set to `/`. Always has both leading and trailing slash.
102
+ *
103
+ * @see https://docusaurus.io/docs/api/docusaurus-config#baseUrl
104
+ */
105
+ baseUrl: string;
106
+ /**
107
+ * Path to your site favicon; must be a URL that can be used in link's href.
108
+ *
109
+ * @see https://docusaurus.io/docs/api/docusaurus-config#favicon
110
+ */
111
+ favicon?: string;
112
+ /**
113
+ * Allow to customize the presence/absence of a trailing slash at the end of
114
+ * URLs/links, and how static HTML files are generated:
115
+ *
116
+ * - `undefined` (default): keeps URLs untouched, and emit
117
+ * `/docs/myDoc/index.html` for `/docs/myDoc.md`
118
+ * - `true`: add trailing slashes to URLs/links, and emit
119
+ * `/docs/myDoc/index.html` for `/docs/myDoc.md`
120
+ * - `false`: remove trailing slashes from URLs/links, and emit
121
+ * `/docs/myDoc.html` for `/docs/myDoc.md`
122
+ *
123
+ * @see https://github.com/slorber/trailing-slash-guide
124
+ * @see https://docusaurus.io/docs/api/docusaurus-config#trailingSlash
125
+ * @default undefined
126
+ */
32
127
  trailingSlash: boolean | undefined;
128
+ /**
129
+ * The i18n configuration object to [localize your
130
+ * site](https://docusaurus.io/docs/i18n/introduction).
131
+ *
132
+ * @see https://docusaurus.io/docs/api/docusaurus-config#i18n
133
+ */
33
134
  i18n: I18nConfig;
135
+ /**
136
+ * This option adds `<meta name="robots" content="noindex, nofollow">` to
137
+ * every page to tell search engines to avoid indexing your site.
138
+ *
139
+ * @see https://moz.com/learn/seo/robots-meta-directives
140
+ * @see https://docusaurus.io/docs/api/docusaurus-config#noIndex
141
+ * @default false
142
+ */
143
+ noIndex: boolean;
144
+ /**
145
+ * The behavior of Docusaurus when it detects any broken link.
146
+ *
147
+ * @see https://docusaurus.io/docs/api/docusaurus-config#onBrokenLinks
148
+ * @default "throw"
149
+ */
34
150
  onBrokenLinks: ReportingSeverity;
151
+ /**
152
+ * The behavior of Docusaurus when it detects any broken markdown link.
153
+ *
154
+ * @see https://docusaurus.io/docs/api/docusaurus-config#onBrokenMarkdownLinks
155
+ * @default "warn"
156
+ */
35
157
  onBrokenMarkdownLinks: ReportingSeverity;
158
+ /**
159
+ * The behavior of Docusaurus when it detects any [duplicate
160
+ * routes](https://docusaurus.io/docs/creating-pages#duplicate-routes).
161
+ *
162
+ * @see https://docusaurus.io/docs/api/docusaurus-config#onDuplicateRoutes
163
+ * @default "warn"
164
+ */
36
165
  onDuplicateRoutes: ReportingSeverity;
37
- noIndex: boolean;
166
+ /**
167
+ * The tagline for your website.
168
+ *
169
+ * @see https://docusaurus.io/docs/api/docusaurus-config#tagline
170
+ * @default ""
171
+ */
172
+ tagline: string;
173
+ /**
174
+ * The GitHub user or organization that owns the repository. You don't need
175
+ * this if you are not using the `docusaurus deploy` command.
176
+ *
177
+ * @see https://docusaurus.io/docs/api/docusaurus-config#organizationName
178
+ */
38
179
  organizationName?: string;
180
+ /**
181
+ * The name of the GitHub repository. You don't need this if you are not using
182
+ * the `docusaurus deploy` command.
183
+ *
184
+ * @see https://docusaurus.io/docs/api/docusaurus-config#projectName
185
+ */
39
186
  projectName?: string;
187
+ /**
188
+ * The name of the branch to deploy the static files to. You don't need this
189
+ * if you are not using the `docusaurus deploy` command.
190
+ *
191
+ * @see https://docusaurus.io/docs/api/docusaurus-config#deploymentBranch
192
+ */
40
193
  deploymentBranch?: string;
194
+ /**
195
+ * The hostname of your server. Useful if you are using GitHub Enterprise. You
196
+ * don't need this if you are not using the `docusaurus deploy` command.
197
+ *
198
+ * @see https://docusaurus.io/docs/api/docusaurus-config#githubHost
199
+ */
41
200
  githubHost?: string;
201
+ /**
202
+ * The port of your server. Useful if you are using GitHub Enterprise. You
203
+ * don't need this if you are not using the `docusaurus deploy` command.
204
+ *
205
+ * @see https://docusaurus.io/docs/api/docusaurus-config#githubPort
206
+ */
42
207
  githubPort?: string;
43
- plugins?: PluginConfig[];
44
- themes?: PluginConfig[];
45
- presets?: PresetConfig[];
208
+ /**
209
+ * The [theme configuration](https://docusaurus.io/docs/api/themes/configuration)
210
+ * object to customize your site UI like navbar and footer.
211
+ *
212
+ * @see https://docusaurus.io/docs/api/docusaurus-config#themeConfig
213
+ * @default {}
214
+ */
46
215
  themeConfig: ThemeConfig;
216
+ /**
217
+ * List of plugins.
218
+ *
219
+ * @see https://docusaurus.io/docs/api/docusaurus-config#plugins
220
+ * @default []
221
+ */
222
+ plugins: PluginConfig[];
223
+ /**
224
+ * List of themes.
225
+ *
226
+ * @see https://docusaurus.io/docs/api/docusaurus-config#themes
227
+ * @default []
228
+ */
229
+ themes: PluginConfig[];
230
+ /**
231
+ * List of presets.
232
+ *
233
+ * @see https://docusaurus.io/docs/api/docusaurus-config#presets
234
+ * @default []
235
+ */
236
+ presets: PresetConfig[];
237
+ /**
238
+ * Docusaurus guards `docusaurus.config.js` from unknown fields. To add a
239
+ * custom field, define it on `customFields`.
240
+ *
241
+ * @see https://docusaurus.io/docs/api/docusaurus-config#customFields
242
+ * @default {}
243
+ */
47
244
  customFields?: {
48
245
  [key: string]: unknown;
49
246
  };
50
- scripts?: (
247
+ /**
248
+ * An array of paths, relative to the site's directory or absolute. Files
249
+ * under these paths will be copied to the build output as-is.
250
+ *
251
+ * @see https://docusaurus.io/docs/api/docusaurus-config#staticDirectories
252
+ * @default ["static"]
253
+ */
254
+ staticDirectories: string[];
255
+ /**
256
+ * An array of scripts to load. The values can be either strings or plain
257
+ * objects of attribute-value maps. The `<script>` tags will be inserted in
258
+ * the HTML `<head>`.
259
+ *
260
+ * Note that `<script>` added here are render-blocking, so you might want to
261
+ * add `async: true`/`defer: true` to the objects.
262
+ *
263
+ * @see https://docusaurus.io/docs/api/docusaurus-config#scripts
264
+ * @default []
265
+ */
266
+ scripts: (
51
267
  | string
52
268
  | {
53
269
  src: string;
54
- [key: string]: unknown;
270
+ [key: string]: string | boolean | undefined;
55
271
  }
56
272
  )[];
57
- clientModules?: string[];
58
- ssrTemplate?: string;
59
- staticDirectories: string[];
60
- stylesheets?: (
273
+ /**
274
+ * An array of CSS sources to load. The values can be either strings or plain
275
+ * objects of attribute-value maps. The `<link>` tags will be inserted in the
276
+ * HTML `<head>`.
277
+ *
278
+ * @see https://docusaurus.io/docs/api/docusaurus-config#stylesheets
279
+ * @default []
280
+ */
281
+ stylesheets: (
61
282
  | string
62
283
  | {
63
284
  href: string;
64
- [key: string]: unknown;
285
+ [key: string]: string | boolean | undefined;
65
286
  }
66
287
  )[];
67
- titleDelimiter?: string;
288
+ /**
289
+ * An array of [client modules](https://docusaurus.io/docs/advanced/client#client-modules)
290
+ * to load globally on your site.
291
+ *
292
+ * @see https://docusaurus.io/docs/api/docusaurus-config#clientModules
293
+ * @default []
294
+ */
295
+ clientModules: string[];
296
+ /**
297
+ * An HTML template written in [Eta's syntax](https://eta.js.org/docs/syntax#syntax-overview)
298
+ * that will be used to render your application. This can be used to set
299
+ * custom attributes on the `body` tags, additional `meta` tags, customize the
300
+ * `viewport`, etc. Please note that Docusaurus will rely on the template to
301
+ * be correctly structured in order to function properly, once you do
302
+ * customize it, you will have to make sure that your template is compliant
303
+ * with the requirements from upstream.
304
+ *
305
+ * @see https://docusaurus.io/docs/api/docusaurus-config#ssrTemplate
306
+ */
307
+ ssrTemplate?: string;
308
+ /**
309
+ * Will be used as title delimiter in the generated `<title>` tag.
310
+ *
311
+ * @see https://docusaurus.io/docs/api/docusaurus-config#titleDelimiter
312
+ * @default "|"
313
+ */
314
+ titleDelimiter: string;
315
+ /**
316
+ * When enabled, will show a banner in case your site can't load its CSS or
317
+ * JavaScript files, which is a very common issue, often related to a wrong
318
+ * `baseUrl` in site config.
319
+ *
320
+ * @see https://docusaurus.io/docs/api/docusaurus-config#baseUrlIssueBanner
321
+ * @default true
322
+ */
323
+ baseUrlIssueBanner: boolean;
324
+ /** Webpack-related options. */
68
325
  webpack?: {
326
+ /**
327
+ * Configuration for alternative JS loaders. "babel" will use the built-in
328
+ * Babel loader and preset; otherwise, you can provide your custom Webpack
329
+ * rule set.
330
+ */
69
331
  jsLoader: 'babel' | ((isServer: boolean) => RuleSetRule);
70
332
  };
71
- }
72
-
73
- // Docusaurus config, as provided by the user (partial/unnormalized)
74
- // This type is used to provide type-safety / IDE auto-complete on the config
75
- // file. See https://docusaurus.io/docs/typescript-support
76
- export type Config = Overwrite<
77
- Partial<DocusaurusConfig>,
78
- {
79
- title: Required<DocusaurusConfig['title']>;
80
- url: Required<DocusaurusConfig['url']>;
81
- baseUrl: Required<DocusaurusConfig['baseUrl']>;
82
- i18n?: DeepPartial<DocusaurusConfig['i18n']>;
83
- }
333
+ };
334
+
335
+ /**
336
+ * Docusaurus config, as provided by the user (partial/unnormalized). This type
337
+ * is used to provide type-safety / IDE auto-complete on the config file.
338
+ * @see https://docusaurus.io/docs/typescript-support
339
+ */
340
+ export type Config = RequireKeys<
341
+ DeepPartial<DocusaurusConfig>,
342
+ 'title' | 'url' | 'baseUrl'
84
343
  >;
85
344
 
345
+ // === Data loading ===
346
+
86
347
  /**
87
348
  * - `type: 'package'`, plugin is in a different package.
88
349
  * - `type: 'project'`, plugin is in the same docusaurus project.
@@ -90,7 +351,7 @@ export type Config = Overwrite<
90
351
  * package.json.
91
352
  * - `type: 'synthetic'`, docusaurus generated internal plugin.
92
353
  */
93
- export type DocusaurusPluginVersionInformation =
354
+ export type PluginVersionInformation =
94
355
  | {
95
356
  readonly type: 'package';
96
357
  readonly name?: string;
@@ -100,179 +361,175 @@ export type DocusaurusPluginVersionInformation =
100
361
  | {readonly type: 'local'}
101
362
  | {readonly type: 'synthetic'};
102
363
 
103
- export interface DocusaurusSiteMetadata {
364
+ export type SiteMetadata = {
104
365
  readonly docusaurusVersion: string;
105
366
  readonly siteVersion?: string;
106
- readonly pluginVersions: Record<string, DocusaurusPluginVersionInformation>;
107
- }
108
-
109
- // Inspired by Chrome JSON, because it's a widely supported i18n format
110
- // https://developer.chrome.com/apps/i18n-messages
111
- // https://support.crowdin.com/file-formats/chrome-json/
112
- // https://www.applanga.com/docs/formats/chrome_i18n_json
113
- // https://docs.transifex.com/formats/chrome-json
114
- // https://help.phrase.com/help/chrome-json-messages
115
- export type TranslationMessage = {message: string; description?: string};
116
- export type TranslationFileContent = Record<string, TranslationMessage>;
117
- export type TranslationFile = {path: string; content: TranslationFileContent};
118
- export type TranslationFiles = TranslationFile[];
119
-
120
- export type I18nLocaleConfig = {
121
- label: string;
122
- htmlLang: string;
123
- direction: string;
367
+ readonly pluginVersions: {[pluginName: string]: PluginVersionInformation};
124
368
  };
125
369
 
126
- export type I18nConfig = {
127
- defaultLocale: string;
128
- locales: [string, ...string[]];
129
- localeConfigs: Record<string, Partial<I18nLocaleConfig>>;
370
+ /**
371
+ * Inspired by Chrome JSON, because it's a widely supported i18n format
372
+ * @see https://developer.chrome.com/apps/i18n-messages
373
+ * @see https://support.crowdin.com/file-formats/chrome-json/
374
+ * @see https://www.applanga.com/docs/formats/chrome_i18n_json
375
+ * @see https://docs.transifex.com/formats/chrome-json
376
+ * @see https://help.phrase.com/help/chrome-json-messages
377
+ */
378
+ export type TranslationMessage = {message: string; description?: string};
379
+ export type TranslationFileContent = {[msgId: string]: TranslationMessage};
380
+ /**
381
+ * An abstract representation of how a translation file exists on disk. The core
382
+ * would handle the file reading/writing; plugins just need to deal with
383
+ * translations in-memory.
384
+ */
385
+ export type TranslationFile = {
386
+ /**
387
+ * Relative to the directory where it's expected to be found. For plugin
388
+ * files, it's relative to `i18n/<locale>/<pluginName>/<pluginId>`. Should NOT
389
+ * have any extension.
390
+ */
391
+ path: string;
392
+ content: TranslationFileContent;
130
393
  };
131
394
 
132
- export type I18n = {
133
- defaultLocale: string;
134
- locales: [string, ...string[]];
135
- currentLocale: string;
136
- localeConfigs: Record<string, I18nLocaleConfig>;
137
- };
395
+ export type I18n = DeepRequired<I18nConfig> & {currentLocale: string};
138
396
 
139
- export interface DocusaurusContext {
397
+ export type GlobalData = {[pluginName: string]: {[pluginId: string]: unknown}};
398
+
399
+ export type CodeTranslations = {[msgId: string]: string};
400
+
401
+ export type DocusaurusContext = {
140
402
  siteConfig: DocusaurusConfig;
141
- siteMetadata: DocusaurusSiteMetadata;
142
- globalData: Record<string, unknown>;
403
+ siteMetadata: SiteMetadata;
404
+ globalData: GlobalData;
143
405
  i18n: I18n;
144
- codeTranslations: Record<string, string>;
406
+ codeTranslations: CodeTranslations;
145
407
 
146
408
  // Don't put mutable values here, to avoid triggering re-renders
147
409
  // We could reconsider that choice if context selectors are implemented
148
410
  // isBrowser: boolean; // Not here on purpose!
149
- }
150
-
151
- export interface Preset {
152
- plugins?: PluginConfig[];
153
- themes?: PluginConfig[];
154
- }
411
+ };
155
412
 
156
- export type PresetModule = {
157
- <T>(context: LoadContext, presetOptions: T): Preset;
413
+ export type LoadContext = {
414
+ siteDir: string;
415
+ generatedFilesDir: string;
416
+ siteConfig: DocusaurusConfig;
417
+ siteConfigPath: string;
418
+ outDir: string;
419
+ /**
420
+ * Duplicated from `siteConfig.baseUrl`, but probably worth keeping. We mutate
421
+ * `siteConfig` to make `baseUrl` there localized as well, but that's mostly
422
+ * for client-side. `context.baseUrl` is still more convenient for plugins.
423
+ */
424
+ baseUrl: string;
425
+ i18n: I18n;
426
+ codeTranslations: CodeTranslations;
158
427
  };
159
428
 
160
- export type ImportedPresetModule = PresetModule & {
161
- default?: PresetModule;
429
+ export type Props = LoadContext & {
430
+ headTags: string;
431
+ preBodyTags: string;
432
+ postBodyTags: string;
433
+ siteMetadata: SiteMetadata;
434
+ routes: RouteConfig[];
435
+ routesPaths: string[];
436
+ plugins: LoadedPlugin[];
162
437
  };
163
438
 
164
- export type PresetConfig =
165
- | [string, Record<string, unknown>]
166
- | [string]
167
- | string;
439
+ // === Plugin ===
168
440
 
169
- export type HostPortCLIOptions = {
170
- host?: string;
171
- port?: string;
441
+ export type PluginContentLoadedActions = {
442
+ addRoute: (config: RouteConfig) => void;
443
+ createData: (name: string, data: string) => Promise<string>;
444
+ setGlobalData: (data: unknown) => void;
172
445
  };
173
446
 
174
- export type ConfigOptions = {
175
- config: string;
447
+ export type ConfigureWebpackUtils = {
448
+ getStyleLoaders: (
449
+ isServer: boolean,
450
+ cssOptions: {[key: string]: unknown},
451
+ ) => RuleSetRule[];
452
+ getJSLoader: (options: {
453
+ isServer: boolean;
454
+ babelOptions?: {[key: string]: unknown};
455
+ }) => RuleSetRule;
176
456
  };
177
457
 
178
- export type StartCLIOptions = HostPortCLIOptions &
179
- ConfigOptions & {
180
- hotOnly: boolean;
181
- open: boolean;
182
- poll: boolean | number;
183
- locale?: string;
458
+ export type AllContent = {
459
+ [pluginName: string]: {
460
+ [pluginID: string]: unknown;
184
461
  };
185
-
186
- export type ServeCLIOptions = HostPortCLIOptions &
187
- ConfigOptions & {
188
- dir: string;
189
- build: boolean;
190
- };
191
-
192
- export type BuildOptions = ConfigOptions & {
193
- bundleAnalyzer: boolean;
194
- outDir: string;
195
- minify: boolean;
196
- skipBuild: boolean;
197
462
  };
198
463
 
199
- export type BuildCLIOptions = BuildOptions & {
200
- locale?: string;
201
- };
202
-
203
- export interface LoadContext {
204
- siteDir: string;
205
- generatedFilesDir: string;
206
- siteConfig: DocusaurusConfig;
207
- siteConfigPath: string;
208
- outDir: string;
209
- baseUrl: string; // TODO to remove: useless, there's already siteConfig.baseUrl!
210
- i18n: I18n;
211
- ssrTemplate: string;
212
- codeTranslations: Record<string, string>;
213
- }
464
+ // TODO improve type (not exposed by postcss-loader)
465
+ export type PostCssOptions = {plugins: unknown[]; [key: string]: unknown};
214
466
 
215
- export interface InjectedHtmlTags {
216
- headTags: string;
217
- preBodyTags: string;
218
- postBodyTags: string;
219
- }
467
+ type HtmlTagObject = {
468
+ /**
469
+ * Attributes of the html tag.
470
+ * E.g. `{ disabled: true, value: "demo", rel: "preconnect" }`
471
+ */
472
+ attributes?: Partial<{[key: string]: string | boolean}>;
473
+ /** The tag name, e.g. `div`, `script`, `link`, `meta` */
474
+ tagName: string;
475
+ /** The inner HTML */
476
+ innerHTML?: string;
477
+ };
220
478
 
221
479
  export type HtmlTags = string | HtmlTagObject | (string | HtmlTagObject)[];
222
480
 
223
- export interface Props extends LoadContext, InjectedHtmlTags {
224
- readonly siteMetadata: DocusaurusSiteMetadata;
225
- readonly routes: RouteConfig[];
226
- readonly routesPaths: string[];
227
- readonly plugins: LoadedPlugin[];
228
- }
481
+ export type ValidationSchema<T> = Joi.ObjectSchema<T>;
229
482
 
230
- export interface PluginContentLoadedActions {
231
- addRoute: (config: RouteConfig) => void;
232
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
233
- createData: (name: string, data: any) => Promise<string>;
234
- setGlobalData: <T = unknown>(data: T) => void;
235
- }
236
-
237
- export type AllContent = Record<
238
- string, // plugin name
239
- Record<
240
- string, // plugin id
241
- unknown // plugin data
242
- >
243
- >;
483
+ export type Validate<T, U> = (
484
+ validationSchema: ValidationSchema<U>,
485
+ options: T,
486
+ ) => U;
244
487
 
245
- // TODO improve type (not exposed by postcss-loader)
246
- export type PostCssOptions = Record<string, unknown> & {plugins: unknown[]};
488
+ export type OptionValidationContext<T, U> = {
489
+ validate: Validate<T, U>;
490
+ options: T;
491
+ };
492
+
493
+ export type ThemeConfigValidationContext<T> = {
494
+ validate: Validate<T, T>;
495
+ themeConfig: Partial<T>;
496
+ };
247
497
 
248
- export interface Plugin<Content = unknown> {
498
+ export type Plugin<Content = unknown> = {
249
499
  name: string;
250
- loadContent?: () => Promise<Content>;
251
- contentLoaded?: ({
252
- content,
253
- actions,
254
- }: {
255
- content: Content; // the content loaded by this plugin instance
256
- allContent: AllContent; // content loaded by ALL the plugins
500
+ loadContent?: () => Promise<Content> | Content;
501
+ contentLoaded?: (args: {
502
+ /** The content loaded by this plugin instance */
503
+ content: Content; //
504
+ /** Content loaded by ALL the plugins */
505
+ allContent: AllContent;
257
506
  actions: PluginContentLoadedActions;
258
- }) => Promise<void>;
259
- routesLoaded?: (routes: RouteConfig[]) => void; // TODO remove soon, deprecated (alpha-60)
260
- postBuild?: (props: Props & {content: Content}) => Promise<void>;
507
+ }) => Promise<void> | void;
508
+ postBuild?: (
509
+ props: Props & {
510
+ content: Content;
511
+ head: {[location: string]: HelmetServerState};
512
+ },
513
+ ) => Promise<void> | void;
261
514
  // TODO refactor the configureWebpack API surface: use an object instead of
262
515
  // multiple params (requires breaking change)
263
516
  configureWebpack?: (
264
- config: Configuration,
517
+ config: WebpackConfiguration,
265
518
  isServer: boolean,
266
519
  utils: ConfigureWebpackUtils,
267
520
  content: Content,
268
- ) => Configuration & {mergeStrategy?: ConfigureWebpackFnMergeStrategy};
521
+ ) => WebpackConfiguration & {
522
+ mergeStrategy?: {
523
+ [key: string]: CustomizeRuleString;
524
+ };
525
+ };
269
526
  configurePostCss?: (options: PostCssOptions) => PostCssOptions;
270
527
  getThemePath?: () => string;
271
528
  getTypeScriptThemePath?: () => string;
272
529
  getPathsToWatch?: () => string[];
273
530
  getClientModules?: () => string[];
274
531
  extendCli?: (cli: CommanderStatic) => void;
275
- injectHtmlTags?: ({content}: {content: Content}) => {
532
+ injectHtmlTags?: (args: {content: Content}) => {
276
533
  headTags?: HtmlTags;
277
534
  preBodyTags?: HtmlTags;
278
535
  postBodyTags?: HtmlTags;
@@ -280,184 +537,213 @@ export interface Plugin<Content = unknown> {
280
537
  // TODO before/afterDevServer implementation
281
538
 
282
539
  // translations
283
- getTranslationFiles?: ({
284
- content,
285
- }: {
540
+ getTranslationFiles?: (args: {
541
+ content: Content;
542
+ }) => Promise<TranslationFile[]> | TranslationFile[];
543
+ getDefaultCodeTranslationMessages?: () =>
544
+ | Promise<{[id: string]: string}>
545
+ | {[id: string]: string};
546
+ translateContent?: (args: {
547
+ /** The content loaded by this plugin instance. */
286
548
  content: Content;
287
- }) => Promise<TranslationFiles>;
288
- getDefaultCodeTranslationMessages?: () => Promise<
289
- Record<
290
- string, // id
291
- string // message
292
- >
293
- >;
294
- translateContent?: ({
295
- content,
296
- translationFiles,
297
- }: {
298
- content: Content; // the content loaded by this plugin instance
299
- translationFiles: TranslationFiles;
549
+ translationFiles: TranslationFile[];
300
550
  }) => Content;
301
- translateThemeConfig?: ({
302
- themeConfig,
303
- translationFiles,
304
- }: {
551
+ translateThemeConfig?: (args: {
305
552
  themeConfig: ThemeConfig;
306
- translationFiles: TranslationFiles;
553
+ translationFiles: TranslationFile[];
307
554
  }) => ThemeConfig;
308
- }
555
+ };
309
556
 
310
- export type InitializedPlugin<Content = unknown> = Plugin<Content> & {
311
- readonly options: PluginOptions;
312
- readonly version: DocusaurusPluginVersionInformation;
557
+ export type InitializedPlugin = Plugin & {
558
+ readonly options: Required<PluginOptions>;
559
+ readonly version: PluginVersionInformation;
560
+ /** The absolute path to the folder containing the entry point file. */
561
+ readonly path: string;
313
562
  };
314
563
 
315
- export type LoadedPlugin<Content = unknown> = InitializedPlugin<Content> & {
316
- readonly content: Content;
564
+ export type LoadedPlugin = InitializedPlugin & {
565
+ readonly content: unknown;
317
566
  };
318
567
 
319
568
  export type SwizzleAction = 'eject' | 'wrap';
320
569
  export type SwizzleActionStatus = 'safe' | 'unsafe' | 'forbidden';
321
570
 
322
571
  export type SwizzleComponentConfig = {
323
- actions: Record<SwizzleAction, SwizzleActionStatus>;
572
+ actions: {[action in SwizzleAction]: SwizzleActionStatus};
324
573
  description?: string;
325
574
  };
326
575
 
327
576
  export type SwizzleConfig = {
328
- components: Record<string, SwizzleComponentConfig>;
329
- // Other settings could be added here,
330
- // For example: the ability to declare the config as exhaustive
331
- // so that we can emit errors
577
+ components: {[componentName: string]: SwizzleComponentConfig};
578
+ // Other settings could be added here, like the ability to declare the config
579
+ // as exhaustive so that we can emit errors
332
580
  };
333
581
 
334
582
  export type PluginModule = {
335
- <Options, Content>(context: LoadContext, options: Options):
336
- | Plugin<Content>
337
- | Promise<Plugin<Content>>;
338
- validateOptions?: <T>(data: OptionValidationContext<T>) => T;
583
+ (context: LoadContext, options: unknown): Plugin | Promise<Plugin>;
584
+ validateOptions?: <T, U>(data: OptionValidationContext<T, U>) => U;
339
585
  validateThemeConfig?: <T>(data: ThemeConfigValidationContext<T>) => T;
340
586
 
341
587
  getSwizzleComponentList?: () => string[] | undefined; // TODO deprecate this one later
342
588
  getSwizzleConfig?: () => SwizzleConfig | undefined;
343
589
  };
344
590
 
345
- export type ImportedPluginModule = PluginModule & {
346
- default?: PluginModule;
591
+ export type Preset = {
592
+ plugins?: PluginConfig[];
593
+ themes?: PluginConfig[];
347
594
  };
348
595
 
349
- export type ConfigureWebpackFn = Plugin<unknown>['configureWebpack'];
350
- export type ConfigureWebpackFnMergeStrategy = Record<
351
- string,
352
- CustomizeRuleString
353
- >;
354
- export type ConfigurePostCssFn = Plugin<unknown>['configurePostCss'];
355
-
356
- export type PluginOptions = {id?: string} & Record<string, unknown>;
357
-
358
- export type PluginConfig =
359
- | [string, PluginOptions]
360
- | [string]
361
- | string
362
- | [PluginModule, PluginOptions]
363
- | PluginModule;
596
+ export type PresetModule = {
597
+ (context: LoadContext, presetOptions: unknown): Preset;
598
+ };
364
599
 
365
- export interface ChunkRegistry {
366
- loader: string;
367
- modulePath: string;
368
- }
600
+ // === Route registry ===
369
601
 
602
+ /**
603
+ * A "module" represents a unit of serialized data emitted from the plugin. It
604
+ * will be imported on client-side and passed as props, context, etc.
605
+ *
606
+ * If it's a string, it's a file path that Webpack can `require`; if it's
607
+ * an object, it can also contain `query` or other metadata.
608
+ */
370
609
  export type Module =
371
610
  | {
372
- path: string;
611
+ /**
612
+ * A marker that tells the route generator this is an import and not a
613
+ * nested object to recurse.
614
+ */
373
615
  __import?: boolean;
616
+ path: string;
374
617
  query?: ParsedUrlQueryInput;
375
618
  }
376
619
  | string;
377
620
 
378
- export interface RouteModule {
379
- [module: string]: Module | RouteModule | RouteModule[];
380
- }
381
-
382
- export interface ChunkNames {
383
- [name: string]: string | null | ChunkNames | ChunkNames[];
384
- }
621
+ /**
622
+ * Represents the data attached to each route. Since the routes.js is a
623
+ * monolithic data file, any data (like props) should be serialized separately
624
+ * and registered here as file paths (a {@link Module}), so that we could
625
+ * code-split.
626
+ */
627
+ export type RouteModules = {
628
+ [propName: string]: Module | RouteModules | RouteModules[];
629
+ };
385
630
 
386
- export interface RouteConfig {
631
+ /**
632
+ * Represents a "slice" of the final route structure returned from the plugin
633
+ * `addRoute` action.
634
+ */
635
+ export type RouteConfig = {
636
+ /** With leading slash. Trailing slash will be normalized by config. */
387
637
  path: string;
638
+ /** Component used to render this route, a path that Webpack can `require`. */
388
639
  component: string;
389
- modules?: RouteModule;
640
+ /**
641
+ * Props. Each entry should be `[propName]: pathToPropModule` (created with
642
+ * `createData`)
643
+ */
644
+ modules?: RouteModules;
645
+ /**
646
+ * The route context will wrap the `component`. Use `useRouteContext` to
647
+ * retrieve what's declared here. Note that all custom route context declared
648
+ * here will be namespaced under {@link RouteContext.data}.
649
+ */
650
+ context?: RouteModules;
651
+ /** Nested routes config. */
390
652
  routes?: RouteConfig[];
653
+ /** React router config option: `exact` routes would not match subroutes. */
391
654
  exact?: boolean;
655
+ /** Used to sort routes. Higher-priority routes will be placed first. */
392
656
  priority?: number;
657
+ /** Extra props; will be copied to routes.js. */
393
658
  [propName: string]: unknown;
394
- }
395
-
396
- // Aliases used for Webpack resolution (when using docusaurus swizzle)
397
- export interface ThemeAliases {
398
- [alias: string]: string;
399
- }
400
-
401
- export interface ConfigureWebpackUtils {
402
- getStyleLoaders: (
403
- isServer: boolean,
404
- cssOptions: {
405
- [key: string]: unknown;
406
- },
407
- ) => RuleSetRule[];
408
- getJSLoader: (options: {
409
- isServer: boolean;
410
- babelOptions?: Record<string, unknown>;
411
- }) => RuleSetRule;
412
- }
659
+ };
413
660
 
414
- interface HtmlTagObject {
415
- /**
416
- * Attributes of the html tag
417
- * E.g. `{'disabled': true, 'value': 'demo', 'rel': 'preconnect'}`
418
- */
419
- attributes?: Partial<Record<string, string | boolean>>;
420
- /**
421
- * The tag name e.g. `div`, `script`, `link`, `meta`
422
- */
423
- tagName: string;
661
+ export type RouteContext = {
424
662
  /**
425
- * The inner HTML
663
+ * Plugin-specific context data.
426
664
  */
427
- innerHTML?: string;
428
- }
429
-
430
- export type ValidationResult<T> = T;
431
-
432
- export type ValidationSchema<T> = Joi.ObjectSchema<T>;
433
-
434
- export type Validate<T> = (
435
- validationSchema: ValidationSchema<T>,
436
- options: Partial<T>,
437
- ) => ValidationResult<T>;
665
+ data?: object | undefined;
666
+ };
438
667
 
439
- export interface OptionValidationContext<T> {
440
- validate: Validate<T>;
441
- options: Partial<T>;
442
- }
668
+ /**
669
+ * Top-level plugin routes automatically add some context data to the route.
670
+ * This permits us to know which plugin is handling the current route.
671
+ */
672
+ export type PluginRouteContext = RouteContext & {
673
+ plugin: {
674
+ id: string;
675
+ name: string;
676
+ };
677
+ };
443
678
 
444
- export interface ThemeConfigValidationContext<T> {
445
- validate: Validate<T>;
446
- themeConfig: Partial<T>;
447
- }
679
+ /**
680
+ * The shape would be isomorphic to {@link RouteModules}:
681
+ * {@link Module} -> `string`, `RouteModules[]` -> `ChunkNames[]`.
682
+ *
683
+ * Each `string` chunk name will correlate with one key in the {@link Registry}.
684
+ */
685
+ export type ChunkNames = {
686
+ [propName: string]: string | ChunkNames | ChunkNames[];
687
+ };
448
688
 
449
- export type TOCItem = {
450
- readonly value: string;
451
- readonly id: string;
452
- readonly level: number;
689
+ /**
690
+ * A map from route paths (with a hash) to the chunk names of each module, which
691
+ * the bundler will collect.
692
+ *
693
+ * Chunk keys are routes with a hash, because 2 routes can conflict with each
694
+ * other if they have the same path, e.g.: parent=/docs, child=/docs
695
+ *
696
+ * @see https://github.com/facebook/docusaurus/issues/2917
697
+ */
698
+ export type RouteChunkNames = {
699
+ [routePathHashed: string]: ChunkNames;
453
700
  };
454
701
 
455
- export type RouteChunksTree = {[x: string | number]: string | RouteChunksTree};
702
+ /**
703
+ * Each key is the chunk name, which you can get from `routeChunkNames` (see
704
+ * {@link RouteChunkNames}). The values are the opts data that react-loadable
705
+ * needs. For example:
706
+ *
707
+ * ```js
708
+ * const options = {
709
+ * optsLoader: {
710
+ * component: () => import('./Pages.js'),
711
+ * content.foo: () => import('./doc1.md'),
712
+ * },
713
+ * optsModules: ['./Pages.js', './doc1.md'],
714
+ * optsWebpack: [
715
+ * require.resolveWeak('./Pages.js'),
716
+ * require.resolveWeak('./doc1.md'),
717
+ * ],
718
+ * }
719
+ * ```
720
+ *
721
+ * @see https://github.com/jamiebuilds/react-loadable#declaring-which-modules-are-being-loaded
722
+ */
723
+ export type Registry = {
724
+ readonly [chunkName: string]: [
725
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
726
+ Loader: () => Promise<any>,
727
+ ModuleName: string,
728
+ ResolvedModuleName: string,
729
+ ];
730
+ };
456
731
 
457
732
  export type ClientModule = {
733
+ onRouteDidUpdate?: (args: {
734
+ previousLocation: Location | null;
735
+ location: Location;
736
+ }) => (() => void) | void;
458
737
  onRouteUpdate?: (args: {
459
738
  previousLocation: Location | null;
460
739
  location: Location;
461
- }) => void;
462
- onRouteUpdateDelayed?: (args: {location: Location}) => void;
740
+ }) => (() => void) | void;
741
+ };
742
+
743
+ export type UseDataOptions = {
744
+ /**
745
+ * Throw an error, or simply return undefined if the data cannot be found. Use
746
+ * `true` if you are sure the data must exist.
747
+ */
748
+ failfast?: boolean;
463
749
  };