mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-13 09:07:29 +02:00
refactor: improve types (#6866)
This commit is contained in:
parent
8e934450d8
commit
a4e37c4054
4 changed files with 26 additions and 50 deletions
|
@ -189,8 +189,7 @@ export default function pluginPWA(
|
||||||
'**/*.{js,json,css,html}',
|
'**/*.{js,json,css,html}',
|
||||||
'**/*.{png,jpg,jpeg,gif,svg,ico}',
|
'**/*.{png,jpg,jpeg,gif,svg,ico}',
|
||||||
'**/*.{woff,woff2,eot,ttf,otf}',
|
'**/*.{woff,woff2,eot,ttf,otf}',
|
||||||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
// @ts-expect-error: internal API?
|
||||||
// @ts-ignore
|
|
||||||
...(injectManifest.globPatterns ?? []),
|
...(injectManifest.globPatterns ?? []),
|
||||||
],
|
],
|
||||||
// those attributes are not overrideable
|
// those attributes are not overrideable
|
||||||
|
|
51
packages/docusaurus-types/src/index.d.ts
vendored
51
packages/docusaurus-types/src/index.d.ts
vendored
|
@ -226,18 +226,15 @@ export interface Props extends LoadContext, InjectedHtmlTags {
|
||||||
|
|
||||||
export interface PluginContentLoadedActions {
|
export interface PluginContentLoadedActions {
|
||||||
addRoute: (config: RouteConfig) => void;
|
addRoute: (config: RouteConfig) => void;
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
createData: (name: string, data: string) => Promise<string>;
|
||||||
createData: (name: string, data: any) => Promise<string>;
|
|
||||||
setGlobalData: <T = unknown>(data: T) => void;
|
setGlobalData: <T = unknown>(data: T) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type AllContent = Record<
|
export type AllContent = {
|
||||||
string, // plugin name
|
[pluginName: string]: {
|
||||||
Record<
|
[pluginID: string]: unknown;
|
||||||
string, // plugin id
|
};
|
||||||
unknown // plugin data
|
};
|
||||||
>
|
|
||||||
>;
|
|
||||||
|
|
||||||
// TODO improve type (not exposed by postcss-loader)
|
// TODO improve type (not exposed by postcss-loader)
|
||||||
export type PostCssOptions = Record<string, unknown> & {plugins: unknown[]};
|
export type PostCssOptions = Record<string, unknown> & {plugins: unknown[]};
|
||||||
|
@ -245,12 +242,11 @@ export type PostCssOptions = Record<string, unknown> & {plugins: unknown[]};
|
||||||
export interface Plugin<Content = unknown> {
|
export interface Plugin<Content = unknown> {
|
||||||
name: string;
|
name: string;
|
||||||
loadContent?: () => Promise<Content>;
|
loadContent?: () => Promise<Content>;
|
||||||
contentLoaded?: ({
|
contentLoaded?: (args: {
|
||||||
content,
|
/** the content loaded by this plugin instance */
|
||||||
actions,
|
content: Content; //
|
||||||
}: {
|
/** content loaded by ALL the plugins */
|
||||||
content: Content; // the content loaded by this plugin instance
|
allContent: AllContent;
|
||||||
allContent: AllContent; // content loaded by ALL the plugins
|
|
||||||
actions: PluginContentLoadedActions;
|
actions: PluginContentLoadedActions;
|
||||||
}) => Promise<void>;
|
}) => Promise<void>;
|
||||||
routesLoaded?: (routes: RouteConfig[]) => void; // TODO remove soon, deprecated (alpha-60)
|
routesLoaded?: (routes: RouteConfig[]) => void; // TODO remove soon, deprecated (alpha-60)
|
||||||
|
@ -269,7 +265,7 @@ export interface Plugin<Content = unknown> {
|
||||||
getPathsToWatch?: () => string[];
|
getPathsToWatch?: () => string[];
|
||||||
getClientModules?: () => string[];
|
getClientModules?: () => string[];
|
||||||
extendCli?: (cli: CommanderStatic) => void;
|
extendCli?: (cli: CommanderStatic) => void;
|
||||||
injectHtmlTags?: ({content}: {content: Content}) => {
|
injectHtmlTags?: (args: {content: Content}) => {
|
||||||
headTags?: HtmlTags;
|
headTags?: HtmlTags;
|
||||||
preBodyTags?: HtmlTags;
|
preBodyTags?: HtmlTags;
|
||||||
postBodyTags?: HtmlTags;
|
postBodyTags?: HtmlTags;
|
||||||
|
@ -277,28 +273,13 @@ export interface Plugin<Content = unknown> {
|
||||||
// TODO before/afterDevServer implementation
|
// TODO before/afterDevServer implementation
|
||||||
|
|
||||||
// translations
|
// translations
|
||||||
getTranslationFiles?: ({
|
getTranslationFiles?: (args: {content: Content}) => Promise<TranslationFiles>;
|
||||||
content,
|
getDefaultCodeTranslationMessages?: () => Promise<{[id: string]: string}>;
|
||||||
}: {
|
translateContent?: (args: {
|
||||||
content: Content;
|
|
||||||
}) => Promise<TranslationFiles>;
|
|
||||||
getDefaultCodeTranslationMessages?: () => Promise<
|
|
||||||
Record<
|
|
||||||
string, // id
|
|
||||||
string // message
|
|
||||||
>
|
|
||||||
>;
|
|
||||||
translateContent?: ({
|
|
||||||
content,
|
|
||||||
translationFiles,
|
|
||||||
}: {
|
|
||||||
content: Content; // the content loaded by this plugin instance
|
content: Content; // the content loaded by this plugin instance
|
||||||
translationFiles: TranslationFiles;
|
translationFiles: TranslationFiles;
|
||||||
}) => Content;
|
}) => Content;
|
||||||
translateThemeConfig?: ({
|
translateThemeConfig?: (args: {
|
||||||
themeConfig,
|
|
||||||
translationFiles,
|
|
||||||
}: {
|
|
||||||
themeConfig: ThemeConfig;
|
themeConfig: ThemeConfig;
|
||||||
translationFiles: TranslationFiles;
|
translationFiles: TranslationFiles;
|
||||||
}) => ThemeConfig;
|
}) => ThemeConfig;
|
||||||
|
|
|
@ -23,16 +23,7 @@ export default function preload(
|
||||||
const matches = matchRoutes(routes, pathname);
|
const matches = matchRoutes(routes, pathname);
|
||||||
|
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
matches.map((match) => {
|
// @ts-expect-error: ComponentCreator injected this method.
|
||||||
const {component} = match.route;
|
matches.map((match) => match.route.component?.preload?.()),
|
||||||
|
|
||||||
// @ts-expect-error: ComponentCreator injected this method.
|
|
||||||
if (component && component.preload) {
|
|
||||||
// @ts-expect-error: checked above.
|
|
||||||
return component.preload();
|
|
||||||
}
|
|
||||||
|
|
||||||
return undefined;
|
|
||||||
}),
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -236,15 +236,20 @@ export function applyConfigurePostCss(
|
||||||
return config;
|
return config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface Error {
|
||||||
|
/** @see https://webpack.js.org/api/node/#error-handling */
|
||||||
|
details: unknown;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function compile(config: Configuration[]): Promise<void> {
|
export function compile(config: Configuration[]): Promise<void> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
const compiler = webpack(config);
|
const compiler = webpack(config);
|
||||||
compiler.run((err, stats) => {
|
compiler.run((err, stats) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
logger.error(err.stack || err);
|
logger.error(err.stack || err);
|
||||||
// @ts-expect-error: see https://webpack.js.org/api/node/#error-handling
|
|
||||||
if (err.details) {
|
if (err.details) {
|
||||||
// @ts-expect-error: see https://webpack.js.org/api/node/#error-handling
|
|
||||||
logger.error(err.details);
|
logger.error(err.details);
|
||||||
}
|
}
|
||||||
reject(err);
|
reject(err);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue