refactor(v2): fix and improve typing (#1842)

This commit is contained in:
Endi 2019-10-15 20:30:10 +07:00 committed by GitHub
parent c630e1aab9
commit 46e8e03be0
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 24 additions and 42 deletions

View file

@ -34,7 +34,7 @@ export interface DocusaurusConfig {
} }
export interface DocusaurusContext { export interface DocusaurusContext {
siteConfig?: Partial<DocusaurusConfig>; siteConfig?: DocusaurusConfig;
} }
export interface Preset { export interface Preset {
@ -44,15 +44,20 @@ export interface Preset {
export type PresetConfig = [string, Object] | [string] | string; export type PresetConfig = [string, Object] | [string] | string;
export interface CLIOptions { export interface StartCLIOptions {
[option: string]: any; port: string;
host: string;
hotOnly: boolean;
}
export interface BuildCLIOptions {
bundleAnalyzer: boolean;
} }
export interface LoadContext { export interface LoadContext {
siteDir: string; siteDir: string;
generatedFilesDir: string; generatedFilesDir: string;
siteConfig: Partial<DocusaurusConfig>; siteConfig: DocusaurusConfig;
cliOptions: CLIOptions;
outDir: string; outDir: string;
baseUrl: string; baseUrl: string;
} }

View file

@ -45,11 +45,9 @@ program
'--bundle-analyzer', '--bundle-analyzer',
'Visualize size of webpack output files with an interactive zoomable treemap (default = false)', '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}) => {
.action((siteDir = '.', {bundleAnalyzer, cacheLoader}) => {
wrapCommand(build)(path.resolve(siteDir), { wrapCommand(build)(path.resolve(siteDir), {
bundleAnalyzer, bundleAnalyzer,
cacheLoader,
}); });
}); });
@ -76,13 +74,11 @@ program
'--hot-only', '--hot-only',
'Do not fallback to page refresh if hot reload fails (default: false)', '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}) => {
.action((siteDir = '.', {port, host, hotOnly, cacheLoader}) => {
wrapCommand(start)(path.resolve(siteDir), { wrapCommand(start)(path.resolve(siteDir), {
port, port,
host, host,
hotOnly, hotOnly,
cacheLoader,
}); });
}); });

View file

@ -15,7 +15,7 @@ import {BundleAnalyzerPlugin} from 'webpack-bundle-analyzer';
import merge from 'webpack-merge'; import merge from 'webpack-merge';
import {STATIC_DIR_NAME} from '../constants'; import {STATIC_DIR_NAME} from '../constants';
import {load} from '../server'; import {load} from '../server';
import {CLIOptions, Props} from '@docusaurus/types'; import {BuildCLIOptions, Props} from '@docusaurus/types';
import {createClientConfig} from '../webpack/client'; import {createClientConfig} from '../webpack/client';
import {createServerConfig} from '../webpack/server'; import {createServerConfig} from '../webpack/server';
import {applyConfigureWebpack} from '../webpack/utils'; import {applyConfigureWebpack} from '../webpack/utils';
@ -46,12 +46,12 @@ function compile(config: Configuration[]): Promise<any> {
export async function build( export async function build(
siteDir: string, siteDir: string,
cliOptions: CLIOptions = {}, cliOptions: Partial<BuildCLIOptions> = {},
): Promise<void> { ): Promise<void> {
process.env.NODE_ENV = 'production'; process.env.NODE_ENV = 'production';
console.log(chalk.blue('Creating an optimized production build...')); 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. // Apply user webpack config.
const {outDir, plugins} = props; const {outDir, plugins} = props;

View file

@ -20,7 +20,7 @@ import WebpackDevServer from 'webpack-dev-server';
import merge from 'webpack-merge'; import merge from 'webpack-merge';
import HotModuleReplacementPlugin from 'webpack/lib/HotModuleReplacementPlugin'; import HotModuleReplacementPlugin from 'webpack/lib/HotModuleReplacementPlugin';
import {load} from '../server'; 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 {CONFIG_FILE_NAME, STATIC_DIR_NAME, DEFAULT_PORT} from '../constants';
import {createClientConfig} from '../webpack/client'; import {createClientConfig} from '../webpack/client';
import {applyConfigureWebpack} from '../webpack/utils'; import {applyConfigureWebpack} from '../webpack/utils';
@ -37,12 +37,12 @@ async function getPort(reqPort: string | undefined): Promise<number> {
export async function start( export async function start(
siteDir: string, siteDir: string,
cliOptions: CLIOptions = {}, cliOptions: Partial<StartCLIOptions> = {},
): Promise<void> { ): Promise<void> {
console.log(chalk.blue('Starting the development server...')); console.log(chalk.blue('Starting the development server...'));
// Process all related files as a prop. // Process all related files as a prop.
const props = await load(siteDir, cliOptions); const props = await load(siteDir);
// Reload files processing. // Reload files processing.
const reload = () => { const reload = () => {

View file

@ -21,17 +21,13 @@ import {loadPresets} from './presets';
import {loadRoutes} from './routes'; import {loadRoutes} from './routes';
import {loadThemeAlias} from './themes'; import {loadThemeAlias} from './themes';
import { import {
CLIOptions,
DocusaurusConfig, DocusaurusConfig,
LoadContext, LoadContext,
PluginConfig, PluginConfig,
Props, Props,
} from '@docusaurus/types'; } from '@docusaurus/types';
export async function load( export async function load(siteDir: string): Promise<Props> {
siteDir: string,
cliOptions: CLIOptions = {},
): Promise<Props> {
const generatedFilesDir: string = path.resolve( const generatedFilesDir: string = path.resolve(
siteDir, siteDir,
GENERATED_FILES_DIR_NAME, GENERATED_FILES_DIR_NAME,
@ -51,7 +47,6 @@ export async function load(
siteDir, siteDir,
generatedFilesDir, generatedFilesDir,
siteConfig, siteConfig,
cliOptions,
outDir, outDir,
baseUrl, baseUrl,
}; };
@ -148,7 +143,6 @@ ${Object.keys(registry)
generatedFilesDir, generatedFilesDir,
routesPaths, routesPaths,
plugins, plugins,
cliOptions,
}; };
return props; return props;

View file

@ -15,7 +15,7 @@ import {
} from '@docusaurus/types'; } from '@docusaurus/types';
export function loadPresets( export function loadPresets(
context: Partial<LoadContext>, context: LoadContext,
): { ): {
plugins: PluginConfig[]; plugins: PluginConfig[];
themes: PluginConfig[]; themes: PluginConfig[];

View file

@ -22,14 +22,7 @@ export function createBaseConfig(
props: Props, props: Props,
isServer: boolean, isServer: boolean,
): Configuration { ): Configuration {
const { const {outDir, siteDir, baseUrl, generatedFilesDir, routesPaths} = props;
outDir,
siteDir,
baseUrl,
generatedFilesDir,
cliOptions: {cacheLoader},
routesPaths,
} = props;
const totalPages = routesPaths.length; const totalPages = routesPaths.length;
const isProd = process.env.NODE_ENV === 'production'; const isProd = process.env.NODE_ENV === 'production';
@ -110,10 +103,9 @@ export function createBaseConfig(
/node_modules/.test(modulePath) && !/docusaurus/.test(modulePath) /node_modules/.test(modulePath) && !/docusaurus/.test(modulePath)
); );
}, },
use: [ use: [getCacheLoader(isServer), getBabelLoader(isServer)].filter(
cacheLoader && getCacheLoader(isServer), Boolean,
getBabelLoader(isServer), ) as Loader[],
].filter(Boolean) as Loader[],
}, },
{ {
test: CSS_REGEX, test: CSS_REGEX,

View file

@ -48,14 +48,9 @@ interface LoadContext {
siteDir: string; siteDir: string;
generatedFilesDir: string; generatedFilesDir: string;
siteConfig: DocusaurusConfig; siteConfig: DocusaurusConfig;
cliOptions: CLIOptions;
outDir: string; outDir: string;
baseUrl: string; baseUrl: string;
} }
interface CLIOptions {
[option: string]: any;
}
``` ```
#### `options` #### `options`