mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-01 18:32:52 +02:00
refactor(v2): fix and improve typing (#1842)
This commit is contained in:
parent
c630e1aab9
commit
46e8e03be0
8 changed files with 24 additions and 42 deletions
15
packages/docusaurus-types/src/index.d.ts
vendored
15
packages/docusaurus-types/src/index.d.ts
vendored
|
@ -34,7 +34,7 @@ export interface DocusaurusConfig {
|
|||
}
|
||||
|
||||
export interface DocusaurusContext {
|
||||
siteConfig?: Partial<DocusaurusConfig>;
|
||||
siteConfig?: DocusaurusConfig;
|
||||
}
|
||||
|
||||
export interface Preset {
|
||||
|
@ -44,15 +44,20 @@ export interface Preset {
|
|||
|
||||
export type PresetConfig = [string, Object] | [string] | string;
|
||||
|
||||
export interface CLIOptions {
|
||||
[option: string]: any;
|
||||
export interface StartCLIOptions {
|
||||
port: string;
|
||||
host: string;
|
||||
hotOnly: boolean;
|
||||
}
|
||||
|
||||
export interface BuildCLIOptions {
|
||||
bundleAnalyzer: boolean;
|
||||
}
|
||||
|
||||
export interface LoadContext {
|
||||
siteDir: string;
|
||||
generatedFilesDir: string;
|
||||
siteConfig: Partial<DocusaurusConfig>;
|
||||
cliOptions: CLIOptions;
|
||||
siteConfig: DocusaurusConfig;
|
||||
outDir: string;
|
||||
baseUrl: string;
|
||||
}
|
||||
|
|
|
@ -45,11 +45,9 @@ program
|
|||
'--bundle-analyzer',
|
||||
'Visualize size of webpack output files with an interactive zoomable treemap (default = false)',
|
||||
)
|
||||
.option('--no-cache-loader', 'Do not use cache-loader')
|
||||
.action((siteDir = '.', {bundleAnalyzer, cacheLoader}) => {
|
||||
.action((siteDir = '.', {bundleAnalyzer}) => {
|
||||
wrapCommand(build)(path.resolve(siteDir), {
|
||||
bundleAnalyzer,
|
||||
cacheLoader,
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -76,13 +74,11 @@ program
|
|||
'--hot-only',
|
||||
'Do not fallback to page refresh if hot reload fails (default: false)',
|
||||
)
|
||||
.option('--no-cache-loader', 'Do not use cache-loader')
|
||||
.action((siteDir = '.', {port, host, hotOnly, cacheLoader}) => {
|
||||
.action((siteDir = '.', {port, host, hotOnly}) => {
|
||||
wrapCommand(start)(path.resolve(siteDir), {
|
||||
port,
|
||||
host,
|
||||
hotOnly,
|
||||
cacheLoader,
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
@ -15,7 +15,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 '@docusaurus/types';
|
||||
import {BuildCLIOptions, Props} from '@docusaurus/types';
|
||||
import {createClientConfig} from '../webpack/client';
|
||||
import {createServerConfig} from '../webpack/server';
|
||||
import {applyConfigureWebpack} from '../webpack/utils';
|
||||
|
@ -46,12 +46,12 @@ function compile(config: Configuration[]): Promise<any> {
|
|||
|
||||
export async function build(
|
||||
siteDir: string,
|
||||
cliOptions: CLIOptions = {},
|
||||
cliOptions: Partial<BuildCLIOptions> = {},
|
||||
): Promise<void> {
|
||||
process.env.NODE_ENV = 'production';
|
||||
console.log(chalk.blue('Creating an optimized production build...'));
|
||||
|
||||
const props: Props = await load(siteDir, cliOptions);
|
||||
const props: Props = await load(siteDir);
|
||||
|
||||
// Apply user webpack config.
|
||||
const {outDir, plugins} = props;
|
||||
|
|
|
@ -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 '@docusaurus/types';
|
||||
import {StartCLIOptions} from '@docusaurus/types';
|
||||
import {CONFIG_FILE_NAME, STATIC_DIR_NAME, DEFAULT_PORT} from '../constants';
|
||||
import {createClientConfig} from '../webpack/client';
|
||||
import {applyConfigureWebpack} from '../webpack/utils';
|
||||
|
@ -37,12 +37,12 @@ async function getPort(reqPort: string | undefined): Promise<number> {
|
|||
|
||||
export async function start(
|
||||
siteDir: string,
|
||||
cliOptions: CLIOptions = {},
|
||||
cliOptions: Partial<StartCLIOptions> = {},
|
||||
): Promise<void> {
|
||||
console.log(chalk.blue('Starting the development server...'));
|
||||
|
||||
// Process all related files as a prop.
|
||||
const props = await load(siteDir, cliOptions);
|
||||
const props = await load(siteDir);
|
||||
|
||||
// Reload files processing.
|
||||
const reload = () => {
|
||||
|
|
|
@ -21,17 +21,13 @@ import {loadPresets} from './presets';
|
|||
import {loadRoutes} from './routes';
|
||||
import {loadThemeAlias} from './themes';
|
||||
import {
|
||||
CLIOptions,
|
||||
DocusaurusConfig,
|
||||
LoadContext,
|
||||
PluginConfig,
|
||||
Props,
|
||||
} from '@docusaurus/types';
|
||||
|
||||
export async function load(
|
||||
siteDir: string,
|
||||
cliOptions: CLIOptions = {},
|
||||
): Promise<Props> {
|
||||
export async function load(siteDir: string): Promise<Props> {
|
||||
const generatedFilesDir: string = path.resolve(
|
||||
siteDir,
|
||||
GENERATED_FILES_DIR_NAME,
|
||||
|
@ -51,7 +47,6 @@ export async function load(
|
|||
siteDir,
|
||||
generatedFilesDir,
|
||||
siteConfig,
|
||||
cliOptions,
|
||||
outDir,
|
||||
baseUrl,
|
||||
};
|
||||
|
@ -148,7 +143,6 @@ ${Object.keys(registry)
|
|||
generatedFilesDir,
|
||||
routesPaths,
|
||||
plugins,
|
||||
cliOptions,
|
||||
};
|
||||
|
||||
return props;
|
||||
|
|
|
@ -15,7 +15,7 @@ import {
|
|||
} from '@docusaurus/types';
|
||||
|
||||
export function loadPresets(
|
||||
context: Partial<LoadContext>,
|
||||
context: LoadContext,
|
||||
): {
|
||||
plugins: PluginConfig[];
|
||||
themes: PluginConfig[];
|
||||
|
|
|
@ -22,14 +22,7 @@ export function createBaseConfig(
|
|||
props: Props,
|
||||
isServer: boolean,
|
||||
): Configuration {
|
||||
const {
|
||||
outDir,
|
||||
siteDir,
|
||||
baseUrl,
|
||||
generatedFilesDir,
|
||||
cliOptions: {cacheLoader},
|
||||
routesPaths,
|
||||
} = props;
|
||||
const {outDir, siteDir, baseUrl, generatedFilesDir, routesPaths} = props;
|
||||
|
||||
const totalPages = routesPaths.length;
|
||||
const isProd = process.env.NODE_ENV === 'production';
|
||||
|
@ -110,10 +103,9 @@ export function createBaseConfig(
|
|||
/node_modules/.test(modulePath) && !/docusaurus/.test(modulePath)
|
||||
);
|
||||
},
|
||||
use: [
|
||||
cacheLoader && getCacheLoader(isServer),
|
||||
getBabelLoader(isServer),
|
||||
].filter(Boolean) as Loader[],
|
||||
use: [getCacheLoader(isServer), getBabelLoader(isServer)].filter(
|
||||
Boolean,
|
||||
) as Loader[],
|
||||
},
|
||||
{
|
||||
test: CSS_REGEX,
|
||||
|
|
|
@ -48,14 +48,9 @@ interface LoadContext {
|
|||
siteDir: string;
|
||||
generatedFilesDir: string;
|
||||
siteConfig: DocusaurusConfig;
|
||||
cliOptions: CLIOptions;
|
||||
outDir: string;
|
||||
baseUrl: string;
|
||||
}
|
||||
|
||||
interface CLIOptions {
|
||||
[option: string]: any;
|
||||
}
|
||||
```
|
||||
|
||||
#### `options`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue