mirror of
https://github.com/facebook/docusaurus.git
synced 2025-05-02 11:47:23 +02:00
refactor(v2): add common typing package @docusaurus/types
(#1797)
* add common typing package `@docusaurus/typing-common` . * change name to `@docusaurus/types` * refactor `packages/docusaurus/src/server/types` * use directly `index.d.ts` . * add missing dependency `@docusaurus/types` on `@docusaurus/core` * add missing dependency `@types/webpack` * fix mistaken type * typing package move to dev dependency * Update package.json see https://stackoverflow.com/questions/45176661/how-do-i-decide-whether-types-goes-into-dependencies-or-devdependencies
This commit is contained in:
parent
0397f7b719
commit
961eb44ab1
28 changed files with 81 additions and 113 deletions
|
@ -10,6 +10,9 @@
|
|||
"access": "public"
|
||||
},
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@docusaurus/types": "^2.0.0-alpha.24"
|
||||
},
|
||||
"dependencies": {
|
||||
"@docusaurus/mdx-loader": "^2.0.0-alpha.24",
|
||||
"@docusaurus/utils": "^2.0.0-alpha.24",
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import pluginContentBlog from '../index';
|
||||
import {DocusaurusConfig} from '../typesDocusaurus';
|
||||
import {DocusaurusConfig} from '@docusaurus/types';
|
||||
|
||||
describe('loadBlog', () => {
|
||||
test('simple website', async () => {
|
||||
|
|
|
@ -19,13 +19,13 @@ import {
|
|||
BlogContent,
|
||||
BlogItemsToModules,
|
||||
TagsModule,
|
||||
ConfigureWebpackUtils,
|
||||
} from './types';
|
||||
import {
|
||||
LoadContext,
|
||||
PluginContentLoadedActions,
|
||||
RouteModule,
|
||||
} from './typesDocusaurus';
|
||||
ConfigureWebpackUtils,
|
||||
} from '@docusaurus/types';
|
||||
import {Configuration} from 'webpack';
|
||||
|
||||
// YYYY-MM-DD-{name}.mdx?
|
||||
|
@ -57,7 +57,7 @@ export default function pluginContentBlog(
|
|||
context: LoadContext,
|
||||
opts: Partial<PluginOptions>,
|
||||
) {
|
||||
const options = {...DEFAULT_OPTIONS, ...opts};
|
||||
const options: PluginOptions = {...DEFAULT_OPTIONS, ...opts};
|
||||
const contentPath = path.resolve(context.siteDir, options.path);
|
||||
|
||||
return {
|
||||
|
@ -79,7 +79,7 @@ export default function pluginContentBlog(
|
|||
return null;
|
||||
}
|
||||
|
||||
const {baseUrl} = siteConfig;
|
||||
const {baseUrl = ''} = siteConfig;
|
||||
const blogFiles = await globby(include, {
|
||||
cwd: blogDir,
|
||||
});
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
import {Loader} from 'webpack';
|
||||
|
||||
export interface BlogContent {
|
||||
blogPosts: BlogPost[];
|
||||
blogListPaginated: BlogPaginated[];
|
||||
|
@ -80,14 +78,3 @@ export interface TagModule {
|
|||
count: number;
|
||||
permalink: string;
|
||||
}
|
||||
|
||||
export interface ConfigureWebpackUtils {
|
||||
getStyleLoaders: (
|
||||
isServer: boolean,
|
||||
cssOptions: {
|
||||
[key: string]: any;
|
||||
},
|
||||
) => Loader[];
|
||||
getCacheLoader: (isServer: boolean, cacheOptions?: {}) => Loader | null;
|
||||
getBabelLoader: (isServer: boolean, babelOptions?: {}) => Loader;
|
||||
}
|
||||
|
|
|
@ -1,63 +0,0 @@
|
|||
import {ParsedUrlQueryInput} from 'querystring';
|
||||
|
||||
export interface DocusaurusConfig {
|
||||
baseUrl: string;
|
||||
favicon?: string;
|
||||
tagline?: string;
|
||||
title: string;
|
||||
url: string;
|
||||
organizationName?: string;
|
||||
projectName?: string;
|
||||
githubHost?: string;
|
||||
plugins?: PluginConfig[];
|
||||
themes?: PluginConfig[];
|
||||
presets?: PresetConfig[];
|
||||
themeConfig?: {
|
||||
[key: string]: any;
|
||||
};
|
||||
customFields?: {
|
||||
[key: string]: any;
|
||||
};
|
||||
}
|
||||
|
||||
export type PluginConfig = [string, Object | undefined] | string;
|
||||
|
||||
export type PresetConfig = [string, Object | undefined] | string;
|
||||
|
||||
export interface CLIOptions {
|
||||
[option: string]: any;
|
||||
}
|
||||
|
||||
export interface LoadContext {
|
||||
siteDir: string;
|
||||
generatedFilesDir?: string;
|
||||
siteConfig: DocusaurusConfig;
|
||||
cliOptions?: CLIOptions;
|
||||
outDir?: string;
|
||||
baseUrl?: string;
|
||||
}
|
||||
|
||||
export interface PluginContentLoadedActions {
|
||||
addRoute(config: RouteConfig): void;
|
||||
createData(name: string, data: Object): Promise<string>;
|
||||
}
|
||||
|
||||
export type Module =
|
||||
| {
|
||||
path: string;
|
||||
__import?: boolean;
|
||||
query?: ParsedUrlQueryInput;
|
||||
}
|
||||
| string;
|
||||
|
||||
export interface RouteModule {
|
||||
[module: string]: Module | RouteModule | RouteModule[];
|
||||
}
|
||||
|
||||
export interface RouteConfig {
|
||||
path: string;
|
||||
component: string;
|
||||
modules?: RouteModule;
|
||||
routes?: RouteConfig[];
|
||||
exact?: boolean;
|
||||
}
|
15
packages/docusaurus-types/package.json
Normal file
15
packages/docusaurus-types/package.json
Normal file
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"name": "@docusaurus/types",
|
||||
"version": "2.0.0-alpha.24",
|
||||
"description": "Common used typings for Docusaurus packages",
|
||||
"main": "./src/index.js",
|
||||
"types": "./src/index.d.ts",
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@types/webpack": "^4.32.0",
|
||||
"querystring": "0.2.0"
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
import {Loader, Configuration} from 'webpack';
|
||||
import {ParsedUrlQueryInput} from 'querystring';
|
||||
import {Configuration} from 'webpack';
|
||||
|
||||
export interface DocusaurusConfig {
|
||||
baseUrl: string;
|
||||
|
@ -21,12 +21,16 @@ export interface DocusaurusConfig {
|
|||
};
|
||||
}
|
||||
|
||||
export interface DocusaurusContext {
|
||||
siteConfig?: Partial<DocusaurusConfig>;
|
||||
}
|
||||
|
||||
export interface Preset {
|
||||
plugins?: PluginConfig[];
|
||||
themes?: PluginConfig[];
|
||||
}
|
||||
|
||||
export type PresetConfig = [string, Object | undefined] | string;
|
||||
export type PresetConfig = [string, Object] | [string] | string;
|
||||
|
||||
export interface CLIOptions {
|
||||
[option: string]: any;
|
||||
|
@ -35,7 +39,7 @@ export interface CLIOptions {
|
|||
export interface LoadContext {
|
||||
siteDir: string;
|
||||
generatedFilesDir: string;
|
||||
siteConfig: DocusaurusConfig;
|
||||
siteConfig: Partial<DocusaurusConfig>;
|
||||
cliOptions: CLIOptions;
|
||||
outDir: string;
|
||||
baseUrl: string;
|
||||
|
@ -68,7 +72,8 @@ export interface Plugin<T> {
|
|||
getPathsToWatch?(): string[];
|
||||
getClientModules?(): string[];
|
||||
}
|
||||
export type PluginConfig = [string, Object | undefined] | string;
|
||||
|
||||
export type PluginConfig = [string, Object] | [string] | string;
|
||||
|
||||
export interface ChunkRegistry {
|
||||
importStatement: string;
|
||||
|
@ -98,3 +103,14 @@ export interface RouteConfig {
|
|||
export interface ThemeAlias {
|
||||
[alias: string]: string;
|
||||
}
|
||||
|
||||
export interface ConfigureWebpackUtils {
|
||||
getStyleLoaders: (
|
||||
isServer: boolean,
|
||||
cssOptions: {
|
||||
[key: string]: any;
|
||||
},
|
||||
) => Loader[];
|
||||
getCacheLoader: (isServer: boolean, cacheOptions?: {}) => Loader | null;
|
||||
getBabelLoader: (isServer: boolean, babelOptions?: {}) => Loader;
|
||||
}
|
3
packages/docusaurus-types/src/index.js
Normal file
3
packages/docusaurus-types/src/index.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
/* eslint-disable */
|
||||
'use strict';
|
||||
Object.defineProperty(exports, '__esModule', {value: true});
|
|
@ -28,6 +28,9 @@
|
|||
"bugs": {
|
||||
"url": "https://github.com/facebook/Docusaurus/issues"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@docusaurus/types": "^2.0.0-alpha.24"
|
||||
},
|
||||
"dependencies": {
|
||||
"@babel/core": "^7.6.2",
|
||||
"@babel/plugin-syntax-dynamic-import": "^7.2.0",
|
||||
|
|
|
@ -6,6 +6,6 @@
|
|||
*/
|
||||
|
||||
import * as React from 'react';
|
||||
import {DocusaurusContext} from '../types';
|
||||
import {DocusaurusContext} from '@docusaurus/types';
|
||||
|
||||
export default React.createContext<DocusaurusContext>({});
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
import {useContext} from 'react';
|
||||
import context from './context';
|
||||
import {DocusaurusContext} from '../types';
|
||||
import {DocusaurusContext} from '@docusaurus/types';
|
||||
|
||||
function useDocusaurusContext(): DocusaurusContext {
|
||||
return useContext(context);
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
import {DocusaurusConfig} from '../server/types';
|
||||
|
||||
export interface DocusaurusContext {
|
||||
siteConfig?: DocusaurusConfig;
|
||||
}
|
|
@ -16,7 +16,7 @@ import {BundleAnalyzerPlugin} from 'webpack-bundle-analyzer';
|
|||
import merge from 'webpack-merge';
|
||||
import {STATIC_DIR_NAME} from '../constants';
|
||||
import {load} from '../server';
|
||||
import {CLIOptions, Props} from '../server/types';
|
||||
import {CLIOptions, Props} from '@docusaurus/types';
|
||||
import {createClientConfig} from '../webpack/client';
|
||||
import {createServerConfig} from '../webpack/server';
|
||||
import {applyConfigureWebpack} from '../webpack/utils';
|
||||
|
|
|
@ -20,7 +20,7 @@ import WebpackDevServer from 'webpack-dev-server';
|
|||
import merge from 'webpack-merge';
|
||||
import HotModuleReplacementPlugin from 'webpack/lib/HotModuleReplacementPlugin';
|
||||
import {load} from '../server';
|
||||
import {CLIOptions} from '../server/types';
|
||||
import {CLIOptions} from '@docusaurus/types';
|
||||
import {CONFIG_FILE_NAME, STATIC_DIR_NAME, DEFAULT_PORT} from '../constants';
|
||||
import {createClientConfig} from '../webpack/client';
|
||||
import {applyConfigureWebpack} from '../webpack/utils';
|
||||
|
|
|
@ -8,7 +8,6 @@
|
|||
import path from 'path';
|
||||
|
||||
import {loadClientModules} from '../index';
|
||||
import {LoadContext} from '../../index';
|
||||
|
||||
const pluginEmpty = require('./__fixtures__/plugin-empty');
|
||||
const pluginFooBar = require('./__fixtures__/plugin-foo-bar');
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
*/
|
||||
|
||||
import _ from 'lodash';
|
||||
import {Plugin} from '../types';
|
||||
import {Plugin} from '@docusaurus/types';
|
||||
|
||||
export function loadClientModules(plugins: Plugin<any>[]): string[] {
|
||||
return _.compact(
|
||||
|
|
|
@ -10,7 +10,7 @@ import importFresh from 'import-fresh';
|
|||
import _ from 'lodash';
|
||||
import path from 'path';
|
||||
import {CONFIG_FILE_NAME} from '../constants';
|
||||
import {DocusaurusConfig, PluginConfig} from './types';
|
||||
import {DocusaurusConfig, PluginConfig} from '@docusaurus/types';
|
||||
|
||||
const REQUIRED_FIELDS = ['baseUrl', 'favicon', 'tagline', 'title', 'url'];
|
||||
|
||||
|
|
|
@ -26,7 +26,7 @@ import {
|
|||
LoadContext,
|
||||
PluginConfig,
|
||||
Props,
|
||||
} from './types';
|
||||
} from '@docusaurus/types';
|
||||
|
||||
export async function load(
|
||||
siteDir: string,
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
import path from 'path';
|
||||
import {load} from './index';
|
||||
import {Props} from './types';
|
||||
import {Props} from '@docusaurus/types';
|
||||
|
||||
// Helper methods to setup dummy/fake projects
|
||||
export const loadSetup = async (name: string): Promise<Props> => {
|
||||
|
|
|
@ -16,7 +16,7 @@ import {
|
|||
PluginConfig,
|
||||
PluginContentLoadedActions,
|
||||
RouteConfig,
|
||||
} from '../types';
|
||||
} from '@docusaurus/types';
|
||||
|
||||
export async function loadPlugins({
|
||||
pluginConfigs,
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
import path from 'path';
|
||||
|
||||
import {loadPresets} from '../index';
|
||||
import {LoadContext} from '../../index';
|
||||
import {DocusaurusConfig, LoadContext} from '@docusaurus/types';
|
||||
|
||||
describe('loadPresets', () => {
|
||||
test('no presets', () => {
|
||||
|
@ -86,7 +86,7 @@ describe('loadPresets', () => {
|
|||
siteConfig: {
|
||||
presets: [[path.join(__dirname, '__fixtures__/preset-bar.js')]],
|
||||
},
|
||||
} as LoadContext;
|
||||
} as Partial<LoadContext>;
|
||||
const presets = loadPresets(context);
|
||||
expect(presets).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
|
@ -115,7 +115,7 @@ describe('loadPresets', () => {
|
|||
],
|
||||
],
|
||||
},
|
||||
} as LoadContext;
|
||||
} as Partial<LoadContext>;
|
||||
const presets = loadPresets(context);
|
||||
expect(presets).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
|
@ -150,7 +150,7 @@ describe('loadPresets', () => {
|
|||
],
|
||||
],
|
||||
},
|
||||
} as LoadContext;
|
||||
} as Partial<LoadContext>;
|
||||
const presets = loadPresets(context);
|
||||
expect(presets).toMatchInlineSnapshot(`
|
||||
Object {
|
||||
|
|
|
@ -7,15 +7,20 @@
|
|||
|
||||
import importFresh from 'import-fresh';
|
||||
import _ from 'lodash';
|
||||
import {LoadContext, PluginConfig, Preset, PresetConfig} from '../types';
|
||||
import {
|
||||
LoadContext,
|
||||
PluginConfig,
|
||||
Preset,
|
||||
PresetConfig,
|
||||
} from '@docusaurus/types';
|
||||
|
||||
export function loadPresets(
|
||||
context: LoadContext,
|
||||
context: Partial<LoadContext>,
|
||||
): {
|
||||
plugins: PluginConfig[];
|
||||
themes: PluginConfig[];
|
||||
} {
|
||||
const presets: PresetConfig[] = context.siteConfig.presets || [];
|
||||
const presets: PresetConfig[] = (context.siteConfig || {}).presets || [];
|
||||
const unflatPlugins: (PluginConfig[])[] = [];
|
||||
const unflatThemes: (PluginConfig[])[] = [];
|
||||
|
||||
|
|
|
@ -8,7 +8,12 @@
|
|||
import {genChunkName} from '@docusaurus/utils';
|
||||
import _ from 'lodash';
|
||||
import {stringify} from 'querystring';
|
||||
import {ChunkRegistry, Module, RouteConfig, RouteModule} from './types';
|
||||
import {
|
||||
ChunkRegistry,
|
||||
Module,
|
||||
RouteConfig,
|
||||
RouteModule,
|
||||
} from '@docusaurus/types';
|
||||
|
||||
function getModulePath(target: Module): string {
|
||||
if (typeof target === 'string') {
|
||||
|
|
|
@ -9,7 +9,7 @@ import globby from 'globby';
|
|||
import fs from 'fs-extra';
|
||||
import path from 'path';
|
||||
import {fileToPath, posixPath, normalizeUrl} from '@docusaurus/utils';
|
||||
import {ThemeAlias} from '../types';
|
||||
import {ThemeAlias} from '@docusaurus/types';
|
||||
|
||||
export function themeAlias(themePath: string): ThemeAlias {
|
||||
if (!fs.pathExistsSync(themePath)) {
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
import {ThemeAlias} from '../types';
|
||||
import {ThemeAlias} from '@docusaurus/types';
|
||||
import {themeAlias} from './alias';
|
||||
|
||||
export function loadThemeAlias(themePaths: string[]): ThemeAlias {
|
||||
|
|
|
@ -12,7 +12,7 @@ import path from 'path';
|
|||
import TerserPlugin from 'terser-webpack-plugin';
|
||||
import {Configuration, Loader} from 'webpack';
|
||||
|
||||
import {Props} from '../server/types';
|
||||
import {Props} from '@docusaurus/types';
|
||||
import {getBabelLoader, getCacheLoader, getStyleLoaders} from './utils';
|
||||
|
||||
const CSS_REGEX = /\.css$/;
|
||||
|
|
|
@ -9,7 +9,7 @@ import {Configuration} from 'webpack';
|
|||
import merge from 'webpack-merge';
|
||||
import WebpackNiceLog from 'webpack-nicelog';
|
||||
|
||||
import {Props} from '../server/types';
|
||||
import {Props} from '@docusaurus/types';
|
||||
import {createBaseConfig} from './base';
|
||||
import ChunkManifestPlugin from './plugins/ChunkManifestPlugin';
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ import {Configuration} from 'webpack';
|
|||
import merge from 'webpack-merge';
|
||||
import WebpackNiceLog from 'webpack-nicelog';
|
||||
|
||||
import {Props} from '../server/types';
|
||||
import {Props} from '@docusaurus/types';
|
||||
import {createBaseConfig} from './base';
|
||||
import WaitPlugin from './plugins/WaitPlugin';
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue