Skip to content

Commit

Permalink
[webpack] Add ProgressPlugin (#4530)
Browse files Browse the repository at this point in the history
* add progress plugin

* restructure tests

* add tests

* update spread args

* remove element type and property type
  • Loading branch information
Brianzchen committed Oct 12, 2023
1 parent 590dbb1 commit 5f92108
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 50 deletions.
112 changes: 86 additions & 26 deletions definitions/npm/webpack_v5.x.x/flow_v0.104.x-/test_webpack_v5.x.x.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { describe, test } from 'flow-typed-test';
import webpack from 'webpack';

import type { WebpackError, Stats, WebpackOptions } from 'webpack';
Expand Down Expand Up @@ -72,35 +73,94 @@ const options: WebpackOptions = {
],
};

webpack(options, function(err: WebpackError, stats: Stats) {
if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}
return;
}
describe('webpack', () => {
test('basic', () => {
webpack(options, function(err: WebpackError, stats: Stats) {
if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}
return;
}

const info = stats.toJson();
const info = stats.toJson();

if (stats.hasErrors()) {
console.error(info.errors);
}
if (stats.hasErrors()) {
console.error(info.errors);
}

if (stats.hasWarnings()) {
console.warn(info.warnings);
}
});
if (stats.hasWarnings()) {
console.warn(info.warnings);
}
});

const compiler1 = webpack(options);
compiler1.run(function(err: WebpackError, stats: Stats) {});

const compiler2 = webpack(options);
const watching2 = compiler2.watch({}, function(err: WebpackError, stats: Stats) {});
watching2.invalidate();
watching2.close();

const compiler3 = webpack([options, options]);
const watching3 = compiler3.watch({}, function(err: WebpackError, stats: Stats) {});
watching3.invalidate();
watching3.close();
});

const compiler1 = webpack(options);
compiler1.run(function(err: WebpackError, stats: Stats) {});
test('ProgressPlugin', () => {
webpack({
plugins: [
new webpack.ProgressPlugin({
activeModules: false,
dependencies: false,
dependenciesCount: 1,
entries: false,
handler: (percentage, msg) => {
percentage.toFixed(2);
msg.toLowerCase();

const compiler2 = webpack(options);
const watching2 = compiler2.watch({}, function(err: WebpackError, stats: Stats) {});
watching2.invalidate();
watching2.close();
// $FlowExpectedError[prop-missing]
msg.toFixed(2);
// $FlowExpectedError[prop-missing]
percentage.toLowerCase();
},
modules: false,
modulesCount: 1,
percentBy: 'entries',
profile: true,
}),
]
});

webpack({
plugins: [
new webpack.ProgressPlugin((percentage, msg) => {
percentage.toFixed(2);
msg.toLowerCase();

const compiler3 = webpack([options, options]);
const watching3 = compiler3.watch({}, function(err: WebpackError, stats: Stats) {});
watching3.invalidate();
watching3.close();
// $FlowExpectedError[prop-missing]
msg.toFixed(2);
// $FlowExpectedError[prop-missing]
percentage.toLowerCase();
}),
]
});

webpack({
plugins: [
// $FlowExpectedError[incompatible-call]
new webpack.ProgressPlugin({
foo: 'bar',
}),
]
});
webpack({
plugins: [
// $FlowExpectedError[incompatible-call]
new webpack.ProgressPlugin('test'),
]
});
});
});
140 changes: 116 additions & 24 deletions definitions/npm/webpack_v5.x.x/flow_v0.104.x-/webpack_v5.x.x.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,29 @@ declare module 'webpack' {

declare type WebpackError = $WebpackError;

declare type WebpackLogger = {|
getChildLogger: (arg0: string | (() => string)) => WebpackLogger,
error(...args: $ReadOnlyArray<any>): void,
warn(...args: $ReadOnlyArray<any>): void,
info(...args: $ReadOnlyArray<any>): void,
log(...args: $ReadOnlyArray<any>): void,
debug(...args: $ReadOnlyArray<any>): void,
assert(assertion: any, ...args: $ReadOnlyArray<any>): void,
trace(): void,
clear(): void,
status(...args: $ReadOnlyArray<any>): void,
group(...args: $ReadOnlyArray<any>): void,
groupCollapsed(...args: $ReadOnlyArray<any>): void,
groupEnd(...args: $ReadOnlyArray<any>): void,
profile(label?: any): void,
profileEnd(label?: any): void,
time(label?: any): void,
timeLog(label?: any): void,
timeEnd(label?: any): void,
timeAggregate(label?: any): void,
timeAggregateEnd(label?: any): void,
|};

declare interface Stats {
hasErrors(): boolean;
hasWarnings(): boolean;
Expand Down Expand Up @@ -612,27 +635,18 @@ declare module 'webpack' {
};

declare class EnvironmentPlugin {
constructor(env: { [string]: mixed, ... } | string[]): $ElementType<
$NonMaybeType<$PropertyType<ResolveOptions, 'plugins'>>,
number
>;
constructor(env: { [string]: mixed, ... } | string[]): $NonMaybeType<ResolveOptions['plugins']>[number];
}

declare class DefinePlugin {
constructor({ [string]: string, ... }): $ElementType<
$NonMaybeType<$PropertyType<ResolveOptions, 'plugins'>>,
number
>;
constructor({ [string]: string, ... }): $NonMaybeType<ResolveOptions['plugins']>[number];
}

declare class IgnorePlugin {
constructor(RegExp | {|
resourceRegExp: RegExp,
contextRegExp?: RegExp,
|}, void | RegExp): $ElementType<
$NonMaybeType<$PropertyType<ResolveOptions, 'plugins'>>,
number
>;
|}, void | RegExp): $NonMaybeType<ResolveOptions['plugins']>[number];
}

declare class SourceMapDevToolPlugin {
Expand All @@ -655,27 +669,104 @@ declare module 'webpack' {
noSources?: ?boolean,
publicPath?: ?string,
fileContext?: ?string,
|}): $ElementType<
$NonMaybeType<$PropertyType<ResolveOptions, 'plugins'>>,
number
>;
|}): $NonMaybeType<ResolveOptions['plugins']>[number];
}

declare class HotModuleReplacementPlugin {
constructor(): $ElementType<
$NonMaybeType<$PropertyType<ResolveOptions, 'plugins'>>,
number
>;
constructor(): $NonMaybeType<ResolveOptions['plugins']>[number];
}

declare class ContextReplacementPlugin {
constructor(
resourceRegExp: RegExp,
newContentRegExp?: RegExp
): $ElementType<
$NonMaybeType<$PropertyType<ResolveOptions, 'plugins'>>,
number
>;
): $NonMaybeType<ResolveOptions['plugins']>[number];
}

declare type ProgressHandler = (percentage: number, msg: string, ...args: $ReadOnlyArray<string>) => void;

/**
* Options object for the ProgressPlugin.
*/
declare type ProgressPluginOptions = {|
/**
* Show active modules count and one active module in progress message.
*/
activeModules?: boolean,

/**
* Show dependencies count in progress message.
*/
dependencies?: boolean,

/**
* Minimum dependencies count to start with. For better progress calculation. Default: 10000.
*/
dependenciesCount?: number,

/**
* Show entries count in progress message.
*/
entries?: boolean,

/**
* Function that executes for every progress step.
*/
handler?: ProgressHandler,

/**
* Show modules count in progress message.
*/
modules?: boolean,

/**
* Minimum modules count to start with. For better progress calculation. Default: 5000.
*/
modulesCount?: number,

/**
* Collect percent algorithm. By default it calculates by a median from modules, entries and dependencies percent.
*/
percentBy?: null | "entries" | "modules" | "dependencies",

/**
* Collect profile data for progress steps. Default: false.
*/
profile?: null | boolean,
|};

declare type ProgressPluginArgument =
| ProgressPluginOptions
| ProgressHandler;

declare class ProgressPlugin {
constructor(options?: ProgressPluginArgument): $NonMaybeType<ResolveOptions['plugins']>[number];
profile?: null | boolean;
handler?: (percentage: number, msg: string, ...args: $ReadOnlyArray<string>) => void;
modulesCount?: number;
dependenciesCount?: number;
showEntries?: boolean;
showModules?: boolean;
showDependencies?: boolean;
showActiveModules?: boolean;
percentBy?: null | "entries" | "modules" | "dependencies";
apply(compiler: WebpackCompiler | WebpackMultiCompiler): void;
static getReporter(
compiler: WebpackCompiler
): void | ((p: number, ...args: $ReadOnlyArray<string>) => void);
static defaultOptions: {|
profile: boolean;
modulesCount: number;
dependenciesCount: number;
modules: boolean;
dependencies: boolean;
activeModules: boolean;
entries: boolean;
|};
static createDefaultHandler: (
profile: void | null | boolean,
logger: WebpackLogger
) => (percentage: number, msg: string, ...args: $ReadOnlyArray<string>) => void;
}

declare function builder(
Expand All @@ -694,6 +785,7 @@ declare module 'webpack' {
SourceMapDevToolPlugin: typeof SourceMapDevToolPlugin,
HotModuleReplacementPlugin: typeof HotModuleReplacementPlugin,
ContextReplacementPlugin: typeof ContextReplacementPlugin,
ProgressPlugin: typeof ProgressPlugin,
...
};
}

0 comments on commit 5f92108

Please sign in to comment.