UNPKG

5.99 kBTypeScriptView Raw
1import { AggregatedResult } from '@jest/test-result';
2import { GlobalConfig } from '@jest/types/build/Config';
3import { Logger } from 'npmlog';
4import { IHash, Json, JsonValue, MaybeArray, MaybePromise, JsonArray } from '../types';
5import webpack = require('webpack');
6import WebpackChain = require('webpack-chain');
7import WebpackDevServer = require('webpack-dev-server');
8declare const PLUGIN_CONTEXT_KEY: ("command" | "commandArgs" | "rootDir" | "userConfig" | "pkg" | "webpack")[];
9declare const VALIDATION_MAP: {
10 string: "isString";
11 number: "isNumber";
12 array: "isArray";
13 object: "isObject";
14 boolean: "isBoolean";
15};
16export declare type PluginContext = Pick<Context, typeof PLUGIN_CONTEXT_KEY[number]>;
17export declare type UserConfigContext = PluginContext & {
18 taskName: string;
19};
20export declare type ValidationKey = keyof typeof VALIDATION_MAP;
21export interface IJestResult {
22 results: AggregatedResult;
23 globalConfig: GlobalConfig;
24}
25export interface IOnHookCallbackArg {
26 err?: Error;
27 args?: CommandArgs;
28 stats?: webpack.compilation.MultiStats;
29 url?: string;
30 devServer?: WebpackDevServer;
31 config?: any;
32 result?: IJestResult;
33}
34export interface IOnHookCallback {
35 (arg?: IOnHookCallbackArg): MaybePromise<void>;
36}
37export interface IOnHook {
38 (eventName: string, callback: IOnHookCallback): void;
39}
40export interface IPluginConfigWebpack {
41 (config: WebpackChain): void;
42}
43export interface IUserConfigWebpack {
44 (config: WebpackChain, value: JsonValue, context: UserConfigContext): Promise<void>;
45}
46export interface IUserConfigArgs {
47 name: string;
48 configWebpack?: IUserConfigWebpack;
49 defaultValue?: any;
50 validation?: ValidationKey;
51}
52export interface ICliOptionArgs {
53 name: string;
54 configWebpack?: IUserConfigWebpack;
55 commands?: string[];
56}
57export interface IOnGetWebpackConfig {
58 (name: string, fn: IPluginConfigWebpack): void;
59 (fn: IPluginConfigWebpack): void;
60}
61export interface IOnGetJestConfig {
62 (fn: IJestConfigFunction): void;
63}
64export interface IRegisterTask {
65 (name: string, chainConfig: WebpackChain): void;
66}
67export interface IMethodFunction {
68 (args?: any): void;
69}
70export interface IRegsiterMethod {
71 (name: string, fn: IMethodFunction): void;
72}
73export interface IApplyMethod {
74 (name: string, ...args: any[]): any;
75}
76export interface IModifyConfig {
77 (userConfig: IUserConfig): IHash<any>;
78}
79export interface IModifyUserConfig {
80 (configKey: string | IModifyConfig, value?: any): void;
81}
82export interface IGetAllPlugin {
83 (dataKeys?: string[]): Partial<IPluginInfo>[];
84}
85export interface IPluginAPI {
86 log: Logger;
87 context: PluginContext;
88 registerTask: IRegisterTask;
89 getAllTask: () => string[];
90 getAllPlugin: IGetAllPlugin;
91 onGetWebpackConfig: IOnGetWebpackConfig;
92 onGetJestConfig: IOnGetJestConfig;
93 onHook: IOnHook;
94 setValue: (name: string, value: any) => void;
95 getValue: (name: string) => any;
96 registerUserConfig: (args: MaybeArray<IUserConfigArgs>) => void;
97 registerCliOption: (args: MaybeArray<ICliOptionArgs>) => void;
98 registerMethod: IRegsiterMethod;
99 applyMethod: IApplyMethod;
100 modifyUserConfig: IModifyUserConfig;
101}
102export interface IPluginInfo {
103 fn: IPlugin;
104 name?: string;
105 pluginPath?: string;
106 options: IPluginOptions;
107}
108export declare type IPluginOptions = Json | JsonArray;
109export interface IPlugin {
110 (api: IPluginAPI, options?: IPluginOptions): MaybePromise<void>;
111}
112export declare type CommandName = 'start' | 'build' | 'test';
113export declare type CommandArgs = IHash<any>;
114export declare type IPluginList = (string | [string, Json])[];
115export declare type IGetBuiltInPlugins = (userConfig: IUserConfig) => IPluginList;
116export interface IContextOptions {
117 command: CommandName;
118 rootDir: string;
119 args: CommandArgs;
120 plugins?: IPluginList;
121 getBuiltInPlugins?: IGetBuiltInPlugins;
122}
123export interface ITaskConfig {
124 name: string;
125 chainConfig: WebpackChain;
126 modifyFunctions: IPluginConfigWebpack[];
127}
128export interface IUserConfig extends Json {
129 plugins: IPluginList;
130}
131export interface IModeConfig {
132 [name: string]: IUserConfig;
133}
134export interface IJestConfigFunction {
135 (JestConfig: Json): Json;
136}
137export declare type IOnGetWebpackConfigArgs = [string, IPluginConfigWebpack] | [IPluginConfigWebpack];
138declare class Context {
139 command: CommandName;
140 commandArgs: CommandArgs;
141 rootDir: string;
142 webpack: (options: webpack.Configuration[]) => webpack.MultiCompiler;
143 private configArr;
144 private modifyConfigFns;
145 private modifyJestConfig;
146 private eventHooks;
147 private internalValue;
148 private userConfigRegistration;
149 private cliOptionRegistration;
150 private methodRegistration;
151 pkg: Json;
152 userConfig: IUserConfig;
153 plugins: IPluginInfo[];
154 constructor({ command, rootDir, args, plugins, getBuiltInPlugins, }: IContextOptions);
155 private registerConfig;
156 private runConfigWebpack;
157 private getProjectFile;
158 private getUserConfig;
159 private mergeModeConfig;
160 private resolvePlugins;
161 getAllPlugin: IGetAllPlugin;
162 registerTask: IRegisterTask;
163 registerMethod: IRegsiterMethod;
164 applyMethod: IApplyMethod;
165 modifyUserConfig: IModifyUserConfig;
166 getAllTask: () => string[];
167 onGetWebpackConfig: IOnGetWebpackConfig;
168 onGetJestConfig: IOnGetJestConfig;
169 runJestConfig: (jestConfig: Json) => Json;
170 onHook: IOnHook;
171 applyHook: (key: string, opts?: {}) => Promise<void>;
172 setValue: (key: string | number, value: any) => void;
173 getValue: (key: string | number) => any;
174 registerUserConfig: (args: MaybeArray<IUserConfigArgs>) => void;
175 registerCliOption: (args: MaybeArray<ICliOptionArgs>) => void;
176 private runPlugins;
177 private checkPluginValue;
178 private runUserConfig;
179 private runCliOption;
180 private runWebpackFunctions;
181 setUp: () => Promise<ITaskConfig[]>;
182}
183export default Context;