build-scripts
Version:
scripts core
180 lines (179 loc) • 5.89 kB
TypeScript
import { AggregatedResult } from '@jest/test-result';
import { GlobalConfig } from '@jest/types/build/Config';
import { Logger } from 'npmlog';
import { IHash, Json, JsonValue, MaybeArray, MaybePromise, JsonArray } from '../types';
import webpack = require('webpack');
import WebpackChain = require('webpack-chain');
import WebpackDevServer = require('webpack-dev-server');
declare const PLUGIN_CONTEXT_KEY: ("command" | "commandArgs" | "rootDir" | "userConfig" | "pkg" | "webpack")[];
declare const VALIDATION_MAP: {
string: "isString";
number: "isNumber";
array: "isArray";
object: "isObject";
boolean: "isBoolean";
};
export declare type PluginContext = Pick<Context, typeof PLUGIN_CONTEXT_KEY[number]>;
export declare type UserConfigContext = PluginContext & {
taskName: string;
};
export declare type ValidationKey = keyof typeof VALIDATION_MAP;
export interface IJestResult {
results: AggregatedResult;
globalConfig: GlobalConfig;
}
export interface IOnHookCallbackArg {
err?: Error;
args?: CommandArgs;
stats?: webpack.compilation.MultiStats;
url?: string;
devServer?: WebpackDevServer;
config?: any;
result?: IJestResult;
}
export interface IOnHookCallback {
(arg?: IOnHookCallbackArg): MaybePromise<void>;
}
export interface IOnHook {
(eventName: string, callback: IOnHookCallback): void;
}
export interface IPluginConfigWebpack {
(config: WebpackChain): void;
}
export interface IUserConfigWebpack {
(config: WebpackChain, value: JsonValue, context: UserConfigContext): Promise<void>;
}
export interface IUserConfigArgs {
name: string;
configWebpack?: IUserConfigWebpack;
defaultValue?: any;
validation?: ValidationKey;
}
export interface ICliOptionArgs {
name: string;
configWebpack?: IUserConfigWebpack;
commands?: string[];
}
export interface IOnGetWebpackConfig {
(name: string, fn: IPluginConfigWebpack): void;
(fn: IPluginConfigWebpack): void;
}
export interface IOnGetJestConfig {
(fn: IJestConfigFunction): void;
}
export interface IRegisterTask {
(name: string, chainConfig: WebpackChain): void;
}
export interface IMethodFunction {
(args?: any): void;
}
export interface IRegsiterMethod {
(name: string, fn: IMethodFunction): void;
}
export interface IApplyMethod {
(name: string, ...args: any[]): any;
}
export interface IModifyConfig {
(userConfig: IUserConfig): IHash<any>;
}
export interface IModifyUserConfig {
(configKey: string | IModifyConfig, value?: any): void;
}
export interface IGetAllPlugin {
(dataKeys?: string[]): Partial<IPluginInfo>[];
}
export interface IPluginAPI {
log: Logger;
context: PluginContext;
registerTask: IRegisterTask;
getAllTask: () => string[];
getAllPlugin: IGetAllPlugin;
onGetWebpackConfig: IOnGetWebpackConfig;
onGetJestConfig: IOnGetJestConfig;
onHook: IOnHook;
setValue: (name: string, value: any) => void;
getValue: (name: string) => any;
registerUserConfig: (args: MaybeArray<IUserConfigArgs>) => void;
registerCliOption: (args: MaybeArray<ICliOptionArgs>) => void;
registerMethod: IRegsiterMethod;
applyMethod: IApplyMethod;
modifyUserConfig: IModifyUserConfig;
}
export interface IPluginInfo {
fn: IPlugin;
name?: string;
pluginPath?: string;
options: IPluginOptions;
}
export declare type IPluginOptions = Json | JsonArray;
export interface IPlugin {
(api: IPluginAPI, options?: IPluginOptions): MaybePromise<void>;
}
export declare type CommandName = 'start' | 'build' | 'test';
export declare type CommandArgs = IHash<any>;
export declare type IPluginList = (string | [string, Json])[];
export declare type IGetBuiltInPlugins = (userConfig: IUserConfig) => IPluginList;
export interface IContextOptions {
command: CommandName;
rootDir: string;
args: CommandArgs;
plugins?: IPluginList;
getBuiltInPlugins?: IGetBuiltInPlugins;
}
export interface ITaskConfig {
name: string;
chainConfig: WebpackChain;
modifyFunctions: IPluginConfigWebpack[];
}
export interface IUserConfig extends Json {
plugins: IPluginList;
}
export interface IJestConfigFunction {
(JestConfig: Json): Json;
}
export declare type IOnGetWebpackConfigArgs = [string, IPluginConfigWebpack] | [IPluginConfigWebpack];
declare class Context {
command: CommandName;
commandArgs: CommandArgs;
rootDir: string;
webpack: (options: webpack.Configuration[]) => webpack.MultiCompiler;
private configArr;
private modifyConfigFns;
private modifyJestConfig;
private eventHooks;
private internalValue;
private userConfigRegistration;
private cliOptionRegistration;
private methodRegistration;
pkg: Json;
userConfig: IUserConfig;
plugins: IPluginInfo[];
constructor({ command, rootDir, args, plugins, getBuiltInPlugins, }: IContextOptions);
private registerConfig;
private runConfigWebpack;
private getProjectFile;
private getUserConfig;
private resolvePlugins;
getAllPlugin: IGetAllPlugin;
registerTask: IRegisterTask;
registerMethod: IRegsiterMethod;
applyMethod: IApplyMethod;
modifyUserConfig: IModifyUserConfig;
getAllTask: () => string[];
onGetWebpackConfig: IOnGetWebpackConfig;
onGetJestConfig: IOnGetJestConfig;
runJestConfig: (jestConfig: Json) => Json;
onHook: IOnHook;
applyHook: (key: string, opts?: {}) => Promise<void>;
setValue: (key: string | number, value: any) => void;
getValue: (key: string | number) => any;
registerUserConfig: (args: MaybeArray<IUserConfigArgs>) => void;
registerCliOption: (args: MaybeArray<ICliOptionArgs>) => void;
private runPlugins;
private checkPluginValue;
private runUserConfig;
private runCliOption;
private runWebpackFunctions;
setUp: () => Promise<ITaskConfig[]>;
}
export default Context;