@docusaurus/types 2.0.0-beta.18 → 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 -3
  2. package/src/index.d.ts +528 -250
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@docusaurus/types",
3
- "version": "2.0.0-beta.18",
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,10 +17,12 @@
17
17
  },
18
18
  "dependencies": {
19
19
  "commander": "^5.1.0",
20
+ "history": "^4.9.0",
20
21
  "joi": "^17.6.0",
22
+ "react-helmet-async": "^1.3.0",
21
23
  "utility-types": "^3.10.0",
22
- "webpack": "^5.70.0",
24
+ "webpack": "^5.72.0",
23
25
  "webpack-merge": "^5.8.0"
24
26
  },
25
- "gitHead": "1a945d06993d53376e61bed2c942799fe07dc336"
27
+ "gitHead": "a71e60a49cce93c1006ef10c41ac03187f057102"
26
28
  }
package/src/index.d.ts CHANGED
@@ -10,80 +10,340 @@ 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
- import type Loadable from 'react-loadable';
20
+
21
+ // === Configuration ===
16
22
 
17
23
  export type ReportingSeverity = 'ignore' | 'log' | 'warn' | 'error' | 'throw';
18
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
+
19
41
  export type ThemeConfig = {
20
42
  [key: string]: unknown;
21
43
  };
22
44
 
23
- // Docusaurus config, after validation/normalization
24
- export interface DocusaurusConfig {
25
- baseUrl: string;
26
- baseUrlIssueBanner: boolean;
27
- favicon?: string;
28
- 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
+ */
29
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
+ */
30
97
  url: string;
31
- // trailingSlash undefined = legacy retrocompatible behavior
32
- // /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
+ */
33
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
+ */
34
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
+ */
35
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
+ */
36
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
+ */
37
165
  onDuplicateRoutes: ReportingSeverity;
38
- 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
+ */
39
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
+ */
40
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
+ */
41
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
+ */
42
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
+ */
43
207
  githubPort?: string;
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
+ */
215
+ themeConfig: ThemeConfig;
216
+ /**
217
+ * List of plugins.
218
+ *
219
+ * @see https://docusaurus.io/docs/api/docusaurus-config#plugins
220
+ * @default []
221
+ */
44
222
  plugins: PluginConfig[];
223
+ /**
224
+ * List of themes.
225
+ *
226
+ * @see https://docusaurus.io/docs/api/docusaurus-config#themes
227
+ * @default []
228
+ */
45
229
  themes: PluginConfig[];
230
+ /**
231
+ * List of presets.
232
+ *
233
+ * @see https://docusaurus.io/docs/api/docusaurus-config#presets
234
+ * @default []
235
+ */
46
236
  presets: PresetConfig[];
47
- themeConfig: ThemeConfig;
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
+ */
48
244
  customFields?: {
49
245
  [key: string]: unknown;
50
246
  };
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
+ */
51
266
  scripts: (
52
267
  | string
53
268
  | {
54
269
  src: string;
55
- [key: string]: unknown;
270
+ [key: string]: string | boolean | undefined;
56
271
  }
57
272
  )[];
58
- clientModules: string[];
59
- ssrTemplate?: string;
60
- staticDirectories: string[];
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
+ */
61
281
  stylesheets: (
62
282
  | string
63
283
  | {
64
284
  href: string;
65
- [key: string]: unknown;
285
+ [key: string]: string | boolean | undefined;
66
286
  }
67
287
  )[];
68
- 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. */
69
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
+ */
70
331
  jsLoader: 'babel' | ((isServer: boolean) => RuleSetRule);
71
332
  };
72
- }
73
-
74
- // Docusaurus config, as provided by the user (partial/unnormalized)
75
- // This type is used to provide type-safety / IDE auto-complete on the config
76
- // file. See https://docusaurus.io/docs/typescript-support
77
- export type Config = Overwrite<
78
- Partial<DocusaurusConfig>,
79
- {
80
- title: Required<DocusaurusConfig['title']>;
81
- url: Required<DocusaurusConfig['url']>;
82
- baseUrl: Required<DocusaurusConfig['baseUrl']>;
83
- i18n?: DeepPartial<DocusaurusConfig['i18n']>;
84
- }
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'
85
343
  >;
86
344
 
345
+ // === Data loading ===
346
+
87
347
  /**
88
348
  * - `type: 'package'`, plugin is in a different package.
89
349
  * - `type: 'project'`, plugin is in the same docusaurus project.
@@ -101,137 +361,99 @@ export type PluginVersionInformation =
101
361
  | {readonly type: 'local'}
102
362
  | {readonly type: 'synthetic'};
103
363
 
104
- export interface DocusaurusSiteMetadata {
364
+ export type SiteMetadata = {
105
365
  readonly docusaurusVersion: string;
106
366
  readonly siteVersion?: string;
107
367
  readonly pluginVersions: {[pluginName: string]: PluginVersionInformation};
108
- }
109
-
110
- // Inspired by Chrome JSON, because it's a widely supported i18n format
111
- // https://developer.chrome.com/apps/i18n-messages
112
- // https://support.crowdin.com/file-formats/chrome-json/
113
- // https://www.applanga.com/docs/formats/chrome_i18n_json
114
- // https://docs.transifex.com/formats/chrome-json
115
- // https://help.phrase.com/help/chrome-json-messages
116
- export type TranslationMessage = {message: string; description?: string};
117
- export type TranslationFileContent = {[key: string]: TranslationMessage};
118
- export type TranslationFile = {path: string; content: TranslationFileContent};
119
- export type TranslationFiles = TranslationFile[];
120
-
121
- export type I18nLocaleConfig = {
122
- label: string;
123
- htmlLang: string;
124
- direction: string;
125
368
  };
126
369
 
127
- export type I18nConfig = {
128
- defaultLocale: string;
129
- locales: [string, ...string[]];
130
- localeConfigs: {[locale: 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;
131
393
  };
132
394
 
133
- export type I18n = {
134
- defaultLocale: string;
135
- locales: [string, ...string[]];
136
- currentLocale: string;
137
- localeConfigs: {[locale: string]: I18nLocaleConfig};
138
- };
395
+ export type I18n = DeepRequired<I18nConfig> & {currentLocale: string};
139
396
 
140
397
  export type GlobalData = {[pluginName: string]: {[pluginId: string]: unknown}};
141
398
 
142
- export interface DocusaurusContext {
399
+ export type CodeTranslations = {[msgId: string]: string};
400
+
401
+ export type DocusaurusContext = {
143
402
  siteConfig: DocusaurusConfig;
144
- siteMetadata: DocusaurusSiteMetadata;
403
+ siteMetadata: SiteMetadata;
145
404
  globalData: GlobalData;
146
405
  i18n: I18n;
147
- codeTranslations: {[msgId: string]: string};
406
+ codeTranslations: CodeTranslations;
148
407
 
149
408
  // Don't put mutable values here, to avoid triggering re-renders
150
409
  // We could reconsider that choice if context selectors are implemented
151
410
  // isBrowser: boolean; // Not here on purpose!
152
- }
153
-
154
- export interface Preset {
155
- plugins?: PluginConfig[];
156
- themes?: PluginConfig[];
157
- }
158
-
159
- export type PresetModule = {
160
- <T>(context: LoadContext, presetOptions: T): Preset;
161
- };
162
-
163
- export type ImportedPresetModule = PresetModule & {
164
- default?: PresetModule;
165
- };
166
-
167
- export type PresetConfig = string | [string, {[key: string]: unknown}];
168
-
169
- export type HostPortCLIOptions = {
170
- host?: string;
171
- port?: string;
172
- };
173
-
174
- export type ConfigOptions = {
175
- config: string;
176
- };
177
-
178
- export type StartCLIOptions = HostPortCLIOptions &
179
- ConfigOptions & {
180
- hotOnly: boolean;
181
- open: boolean;
182
- poll: boolean | number;
183
- locale?: string;
184
- };
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
- };
198
-
199
- export type BuildCLIOptions = BuildOptions & {
200
- locale?: string;
201
411
  };
202
412
 
203
- export interface LoadContext {
413
+ export type LoadContext = {
204
414
  siteDir: string;
205
415
  generatedFilesDir: string;
206
416
  siteConfig: DocusaurusConfig;
207
417
  siteConfigPath: string;
208
418
  outDir: string;
209
- baseUrl: string; // TODO to remove: useless, there's already siteConfig.baseUrl!
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;
210
425
  i18n: I18n;
211
- ssrTemplate: string;
212
- codeTranslations: {[msgId: string]: string};
213
- }
426
+ codeTranslations: CodeTranslations;
427
+ };
214
428
 
215
- export interface InjectedHtmlTags {
429
+ export type Props = LoadContext & {
216
430
  headTags: string;
217
431
  preBodyTags: string;
218
432
  postBodyTags: string;
219
- }
220
-
221
- export type HtmlTags = string | HtmlTagObject | (string | HtmlTagObject)[];
433
+ siteMetadata: SiteMetadata;
434
+ routes: RouteConfig[];
435
+ routesPaths: string[];
436
+ plugins: LoadedPlugin[];
437
+ };
222
438
 
223
- export interface Props extends LoadContext, InjectedHtmlTags {
224
- readonly siteMetadata: DocusaurusSiteMetadata;
225
- readonly routes: RouteConfig[];
226
- readonly routesPaths: string[];
227
- readonly plugins: LoadedPlugin[];
228
- }
439
+ // === Plugin ===
229
440
 
230
- export interface PluginContentLoadedActions {
441
+ export type PluginContentLoadedActions = {
231
442
  addRoute: (config: RouteConfig) => void;
232
443
  createData: (name: string, data: string) => Promise<string>;
233
444
  setGlobalData: (data: unknown) => void;
234
- }
445
+ };
446
+
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;
456
+ };
235
457
 
236
458
  export type AllContent = {
237
459
  [pluginName: string]: {
@@ -240,20 +462,55 @@ export type AllContent = {
240
462
  };
241
463
 
242
464
  // TODO improve type (not exposed by postcss-loader)
243
- export type PostCssOptions = {[key: string]: unknown} & {plugins: unknown[]};
465
+ export type PostCssOptions = {plugins: unknown[]; [key: string]: unknown};
244
466
 
245
- export interface Plugin<Content = unknown> {
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
+ };
478
+
479
+ export type HtmlTags = string | HtmlTagObject | (string | HtmlTagObject)[];
480
+
481
+ export type ValidationSchema<T> = Joi.ObjectSchema<T>;
482
+
483
+ export type Validate<T, U> = (
484
+ validationSchema: ValidationSchema<U>,
485
+ options: T,
486
+ ) => U;
487
+
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
+ };
497
+
498
+ export type Plugin<Content = unknown> = {
246
499
  name: string;
247
- loadContent?: () => Promise<Content>;
500
+ loadContent?: () => Promise<Content> | Content;
248
501
  contentLoaded?: (args: {
249
- /** the content loaded by this plugin instance */
502
+ /** The content loaded by this plugin instance */
250
503
  content: Content; //
251
- /** content loaded by ALL the plugins */
504
+ /** Content loaded by ALL the plugins */
252
505
  allContent: AllContent;
253
506
  actions: PluginContentLoadedActions;
254
- }) => Promise<void>;
255
- routesLoaded?: (routes: RouteConfig[]) => void; // TODO remove soon, deprecated (alpha-60)
256
- 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;
257
514
  // TODO refactor the configureWebpack API surface: use an object instead of
258
515
  // multiple params (requires breaking change)
259
516
  configureWebpack?: (
@@ -262,7 +519,9 @@ export interface Plugin<Content = unknown> {
262
519
  utils: ConfigureWebpackUtils,
263
520
  content: Content,
264
521
  ) => WebpackConfiguration & {
265
- mergeStrategy?: ConfigureWebpackFnMergeStrategy;
522
+ mergeStrategy?: {
523
+ [key: string]: CustomizeRuleString;
524
+ };
266
525
  };
267
526
  configurePostCss?: (options: PostCssOptions) => PostCssOptions;
268
527
  getThemePath?: () => string;
@@ -278,29 +537,32 @@ export interface Plugin<Content = unknown> {
278
537
  // TODO before/afterDevServer implementation
279
538
 
280
539
  // translations
281
- getTranslationFiles?: (args: {content: Content}) => Promise<TranslationFiles>;
282
- getDefaultCodeTranslationMessages?: () => Promise<{[id: string]: string}>;
540
+ getTranslationFiles?: (args: {
541
+ content: Content;
542
+ }) => Promise<TranslationFile[]> | TranslationFile[];
543
+ getDefaultCodeTranslationMessages?: () =>
544
+ | Promise<{[id: string]: string}>
545
+ | {[id: string]: string};
283
546
  translateContent?: (args: {
284
- content: Content; // the content loaded by this plugin instance
285
- translationFiles: TranslationFiles;
547
+ /** The content loaded by this plugin instance. */
548
+ content: Content;
549
+ translationFiles: TranslationFile[];
286
550
  }) => Content;
287
551
  translateThemeConfig?: (args: {
288
552
  themeConfig: ThemeConfig;
289
- translationFiles: TranslationFiles;
553
+ translationFiles: TranslationFile[];
290
554
  }) => ThemeConfig;
291
- }
555
+ };
292
556
 
293
- export type InitializedPlugin<Content = unknown> = Plugin<Content> & {
557
+ export type InitializedPlugin = Plugin & {
294
558
  readonly options: Required<PluginOptions>;
295
559
  readonly version: PluginVersionInformation;
296
- /**
297
- * The absolute path to the folder containing the entry point file.
298
- */
560
+ /** The absolute path to the folder containing the entry point file. */
299
561
  readonly path: string;
300
562
  };
301
563
 
302
- export type LoadedPlugin<Content = unknown> = InitializedPlugin<Content> & {
303
- readonly content: Content;
564
+ export type LoadedPlugin = InitializedPlugin & {
565
+ readonly content: unknown;
304
566
  };
305
567
 
306
568
  export type SwizzleAction = 'eject' | 'wrap';
@@ -313,15 +575,12 @@ export type SwizzleComponentConfig = {
313
575
 
314
576
  export type SwizzleConfig = {
315
577
  components: {[componentName: string]: SwizzleComponentConfig};
316
- // Other settings could be added here,
317
- // For example: the ability to declare the config as exhaustive
318
- // so that we can emit errors
578
+ // Other settings could be added here, like the ability to declare the config
579
+ // as exhaustive so that we can emit errors
319
580
  };
320
581
 
321
582
  export type PluginModule = {
322
- <Options, Content>(context: LoadContext, options: Options):
323
- | Plugin<Content>
324
- | Promise<Plugin<Content>>;
583
+ (context: LoadContext, options: unknown): Plugin | Promise<Plugin>;
325
584
  validateOptions?: <T, U>(data: OptionValidationContext<T, U>) => U;
326
585
  validateThemeConfig?: <T>(data: ThemeConfigValidationContext<T>) => T;
327
586
 
@@ -329,143 +588,162 @@ export type PluginModule = {
329
588
  getSwizzleConfig?: () => SwizzleConfig | undefined;
330
589
  };
331
590
 
332
- export type ImportedPluginModule = PluginModule & {
333
- default?: PluginModule;
591
+ export type Preset = {
592
+ plugins?: PluginConfig[];
593
+ themes?: PluginConfig[];
334
594
  };
335
595
 
336
- export type ConfigureWebpackFn = Plugin<unknown>['configureWebpack'];
337
- export type ConfigureWebpackFnMergeStrategy = {
338
- [key: string]: CustomizeRuleString;
596
+ export type PresetModule = {
597
+ (context: LoadContext, presetOptions: unknown): Preset;
339
598
  };
340
- export type ConfigurePostCssFn = Plugin<unknown>['configurePostCss'];
341
599
 
342
- export type PluginOptions = {id?: string} & {[key: string]: unknown};
343
-
344
- export type PluginConfig =
345
- | string
346
- | [string, PluginOptions]
347
- | [PluginModule, PluginOptions]
348
- | PluginModule;
349
-
350
- export interface ChunkRegistry {
351
- loader: string;
352
- modulePath: string;
353
- }
600
+ // === Route registry ===
354
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
+ */
355
609
  export type Module =
356
610
  | {
357
- path: string;
611
+ /**
612
+ * A marker that tells the route generator this is an import and not a
613
+ * nested object to recurse.
614
+ */
358
615
  __import?: boolean;
616
+ path: string;
359
617
  query?: ParsedUrlQueryInput;
360
618
  }
361
619
  | string;
362
620
 
363
- export interface RouteModule {
364
- [module: string]: Module | RouteModule | RouteModule[];
365
- }
366
-
367
- export interface ChunkNames {
368
- [name: string]: string | null | ChunkNames | ChunkNames[];
369
- }
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
+ };
370
630
 
371
- 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. */
372
637
  path: string;
638
+ /** Component used to render this route, a path that Webpack can `require`. */
373
639
  component: string;
374
- 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. */
375
652
  routes?: RouteConfig[];
653
+ /** React router config option: `exact` routes would not match subroutes. */
376
654
  exact?: boolean;
655
+ /** Used to sort routes. Higher-priority routes will be placed first. */
377
656
  priority?: number;
657
+ /** Extra props; will be copied to routes.js. */
378
658
  [propName: string]: unknown;
379
- }
659
+ };
380
660
 
381
- export interface RouteContext {
661
+ export type RouteContext = {
382
662
  /**
383
663
  * Plugin-specific context data.
384
664
  */
385
665
  data?: object | undefined;
386
- }
666
+ };
387
667
 
388
668
  /**
389
669
  * Top-level plugin routes automatically add some context data to the route.
390
670
  * This permits us to know which plugin is handling the current route.
391
671
  */
392
- export interface PluginRouteContext extends RouteContext {
672
+ export type PluginRouteContext = RouteContext & {
393
673
  plugin: {
394
674
  id: string;
395
675
  name: string;
396
676
  };
397
- }
398
-
399
- export type Route = {
400
- readonly path: string;
401
- readonly component: ReturnType<typeof Loadable>;
402
- readonly exact?: boolean;
403
- readonly routes?: Route[];
404
677
  };
405
678
 
406
- // Aliases used for Webpack resolution (when using docusaurus swizzle)
407
- export interface ThemeAliases {
408
- [alias: string]: string;
409
- }
410
-
411
- export interface ConfigureWebpackUtils {
412
- getStyleLoaders: (
413
- isServer: boolean,
414
- cssOptions: {
415
- [key: string]: unknown;
416
- },
417
- ) => RuleSetRule[];
418
- getJSLoader: (options: {
419
- isServer: boolean;
420
- babelOptions?: {[key: string]: unknown};
421
- }) => RuleSetRule;
422
- }
423
-
424
- interface HtmlTagObject {
425
- /**
426
- * Attributes of the html tag
427
- * E.g. `{'disabled': true, 'value': 'demo', 'rel': 'preconnect'}`
428
- */
429
- attributes?: Partial<{[key: string]: string | boolean}>;
430
- /**
431
- * The tag name e.g. `div`, `script`, `link`, `meta`
432
- */
433
- tagName: string;
434
- /**
435
- * The inner HTML
436
- */
437
- innerHTML?: string;
438
- }
439
-
440
- export type ValidationSchema<T> = Joi.ObjectSchema<T>;
441
-
442
- export type Validate<T, U> = (
443
- validationSchema: ValidationSchema<U>,
444
- options: T,
445
- ) => U;
446
-
447
- export type OptionValidationContext<T, U> = {
448
- validate: Validate<T, U>;
449
- options: T;
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[];
450
687
  };
451
688
 
452
- export interface ThemeConfigValidationContext<T> {
453
- validate: Validate<T, T>;
454
- themeConfig: Partial<T>;
455
- }
456
-
457
- export type TOCItem = {
458
- readonly value: string;
459
- readonly id: string;
460
- 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;
461
700
  };
462
701
 
463
- 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
+ };
464
731
 
465
732
  export type ClientModule = {
733
+ onRouteDidUpdate?: (args: {
734
+ previousLocation: Location | null;
735
+ location: Location;
736
+ }) => (() => void) | void;
466
737
  onRouteUpdate?: (args: {
467
738
  previousLocation: Location | null;
468
739
  location: Location;
469
- }) => void;
470
- 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;
471
749
  };