///
import { Configuration } from 'webpack';
import type { WebpackChain } from '@modern-js/utils';
interface BundlerPluginInstance {
[index: string]: any;
apply: (compiler: {
hooks: {
compilation: {
tap: any;
};
};
}) => void;
}
type SplitChunks = Configuration extends {
optimization?: {
splitChunks?: infer P;
};
} ? P : never;
type WebpackOptimization = NonNullable;
type WebpackResolve = NonNullable;
type WebpackOutput = NonNullable;
type WebpackInfrastructureLogging = NonNullable;
type RspackResolve = {
preferRelative?: boolean;
extensions?: string[];
mainFiles?: string[];
mainFields?: string[];
browserField?: boolean;
conditionNames?: string[];
alias?: Record;
tsConfigPath?: string;
modules?: string[];
fallback?: Record;
};
type RspackOutput = {
libraryTarget?: string;
path?: string;
publicPath?: string;
assetModuleFilename?: string;
filename?: string;
chunkFilename?: string;
globalObject?: string;
uniqueName?: string;
hashFunction?: string;
cssFilename?: string;
cssChunkFilename?: string;
library?: string;
chunkLoadingGlobal?: string;
crossOriginLoading?: false | 'anonymous' | 'use-credentials';
};
type FilterTypes = FilterItemTypes[] | FilterItemTypes;
type FilterItemTypes = RegExp | string | ((value: string) => boolean);
export interface RspackInfrastructureLogging {
appendOnly?: boolean;
colors?: boolean;
console?: Console;
debug?: boolean | FilterTypes;
level?: 'none' | 'error' | 'warn' | 'info' | 'log' | 'verbose';
stream?: NodeJS.WritableStream;
}
type Overlap, E extends Record, Key extends Extract = Extract> = {
[key in Key]: Extract;
};
type Resolve = Overlap;
type Output = Overlap;
type InfrastructureLogging = Overlap;
type Externals = Configuration['externals'];
/** The intersection of webpack and Rspack */
export type BundlerConfig = {
name?: string;
entry?: Record;
context?: Configuration['context'];
plugins?: BundlerPluginInstance[];
module?: Configuration['module'];
target?: Configuration['target'];
mode?: Configuration['mode'];
externals?: Externals;
externalsType?: Configuration['externalsType'];
externalsPresets?: Configuration['externalsPresets'];
output?: Output;
resolve?: Resolve;
devtool?: Configuration['devtool'];
infrastructureLogging?: InfrastructureLogging;
cache?: Configuration['cache'];
optimization?: {
splitChunks: SplitChunks;
runtimeChunk?: WebpackOptimization['runtimeChunk'];
};
};
type ExtendsExcludeAny = T extends any ? T extends E ? true : false : false;
/**
* use `fn: (...args) => BundlerChain` instead of `fn: (...args) => WebpackChain`
*/
type ModifyReturnThis any, R = ReturnType> = (...values: Parameters) => ExtendsExcludeAny extends true ? BundlerChain : ExtendsExcludeAny> extends true ? PickAndModifyThis : R;
/**
* use `{ a: () => BundlerChain; b: { c: () => BundlerChain }}` instead of `{ a: () => WebpackChain; b: { c: () => WebpackChain }}`
*/
type PickAndModifyThis = {
[P in K]: T[P] extends (...args: any) => any ? ModifyReturnThis : ExtendsExcludeAny> extends true ? PickAndModifyThis : T[P];
};
export interface BundlerChain extends PickAndModifyThis {
toConfig: () => BundlerConfig;
optimization: PickAndModifyThis;
externals: (value: Externals) => BundlerChain;
resolve: PickAndModifyThis, keyof WebpackChain['resolve']> | 'merge' | 'get'>;
output: PickAndModifyThis, keyof WebpackChain['output']> | 'get' | 'merge'>;
infrastructureLogging: PickAndModifyThis, keyof WebpackChain['infrastructureLogging']>>;
module: PickAndModifyThis;
}
export type WebpackChainRule = WebpackChain.Rule;
export type BundlerChainRule = PickAndModifyThis;
export {};