Skip to content

Commit b1af0dc

Browse files
snitin315alexander-akait
authored andcommittedNov 15, 2022
refactor!: the minimum supported webpack version is v5.0.0 (#3342)
BREAKING CHANGE: webpack-cli no longer supports webpack v4, the minimum supported version is webpack v5.0.0
1 parent c737383 commit b1af0dc

File tree

7 files changed

+657
-753
lines changed

7 files changed

+657
-753
lines changed
 

‎packages/webpack-cli/src/webpack-cli.ts

+97-234
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,11 @@ class WebpackCLI implements IWebpackCLI {
472472
makeOption(command: WebpackCLICommand, option: WebpackCLIBuiltInOption) {
473473
let mainOption: WebpackCLIMainOption;
474474
let negativeOption;
475+
const flagsWithAlias = ["devtool", "output-path", "target", "watch"];
476+
477+
if (flagsWithAlias.includes(option.name)) {
478+
option.alias = option.name[0];
479+
}
475480

476481
if (option.configs) {
477482
let needNegativeOption = false;
@@ -856,110 +861,6 @@ class WebpackCLI implements IWebpackCLI {
856861
alias: "j",
857862
description: "Prints result as JSON or store it in a file.",
858863
},
859-
860-
// For webpack@4
861-
{
862-
name: "entry",
863-
configs: [
864-
{
865-
type: "string",
866-
},
867-
],
868-
multiple: true,
869-
description: "The entry point(s) of your application e.g. ./src/main.js.",
870-
},
871-
{
872-
name: "output-path",
873-
alias: "o",
874-
configs: [
875-
{
876-
type: "string",
877-
},
878-
],
879-
description: "Output location of the file generated by webpack e.g. ./dist/.",
880-
},
881-
{
882-
name: "target",
883-
alias: "t",
884-
configs: [
885-
{
886-
type: "string",
887-
},
888-
],
889-
multiple: this.webpack.cli !== undefined,
890-
description: "Sets the build target e.g. node.",
891-
},
892-
{
893-
name: "devtool",
894-
configs: [
895-
{
896-
type: "string",
897-
},
898-
{
899-
type: "enum",
900-
values: [false],
901-
},
902-
],
903-
negative: true,
904-
alias: "d",
905-
description: "Determine source maps to use.",
906-
negatedDescription: "Do not generate source maps.",
907-
},
908-
{
909-
name: "mode",
910-
configs: [
911-
{
912-
type: "string",
913-
},
914-
],
915-
description: "Defines the mode to pass to webpack.",
916-
},
917-
{
918-
name: "name",
919-
configs: [
920-
{
921-
type: "string",
922-
},
923-
],
924-
description: "Name of the configuration. Used when loading multiple configurations.",
925-
},
926-
{
927-
name: "stats",
928-
configs: [
929-
{
930-
type: "string",
931-
},
932-
{
933-
type: "boolean",
934-
},
935-
],
936-
negative: true,
937-
description: "It instructs webpack on how to treat the stats e.g. verbose.",
938-
negatedDescription: "Disable stats output.",
939-
},
940-
{
941-
name: "watch",
942-
configs: [
943-
{
944-
type: "boolean",
945-
},
946-
],
947-
negative: true,
948-
alias: "w",
949-
description: "Watch for files changes.",
950-
negatedDescription: "Do not watch for file changes.",
951-
},
952-
{
953-
name: "watch-options-stdin",
954-
configs: [
955-
{
956-
type: "boolean",
957-
},
958-
],
959-
negative: true,
960-
description: "Stop watching when stdin stream has ended.",
961-
negatedDescription: "Do not stop watching when stdin stream has ended.",
962-
},
963864
{
964865
name: "fail-on-warnings",
965866
configs: [
@@ -974,32 +875,31 @@ class WebpackCLI implements IWebpackCLI {
974875

975876
// Extract all the flags being exported from core.
976877
// A list of cli flags generated by core can be found here https://github.com/webpack/webpack/blob/master/test/__snapshots__/Cli.test.js.snap
977-
const coreFlags = this.webpack.cli
978-
? Object.entries(this.webpack.cli.getArguments()).map(([flag, meta]) => {
979-
const inBuiltIn = builtInFlags.find((builtInFlag) => builtInFlag.name === flag);
980-
981-
if (inBuiltIn) {
982-
return {
983-
...meta,
984-
// @ts-expect-error this might be overwritten
985-
name: flag,
986-
group: "core",
987-
...inBuiltIn,
988-
configs: meta.configs || [],
989-
};
990-
}
878+
const coreArguments = Object.entries(this.webpack.cli.getArguments()).map(([flag, meta]) => {
879+
const inBuiltIn = builtInFlags.find((builtInFlag) => builtInFlag.name === flag);
880+
881+
if (inBuiltIn) {
882+
return {
883+
...meta,
884+
// @ts-expect-error this might be overwritten
885+
name: flag,
886+
group: "core",
887+
...inBuiltIn,
888+
configs: meta.configs || [],
889+
};
890+
}
991891

992-
return { ...meta, name: flag, group: "core" };
993-
})
994-
: [];
892+
return { ...meta, name: flag, group: "core" };
893+
});
995894

996895
const options: WebpackCLIBuiltInOption[] = ([] as WebpackCLIBuiltInFlag[])
997896
.concat(
998897
builtInFlags.filter(
999-
(builtInFlag) => !coreFlags.find((coreFlag) => builtInFlag.name === coreFlag.name),
898+
(builtInFlag) =>
899+
!coreArguments.find((coreArgument) => builtInFlag.name === coreArgument.name),
1000900
),
1001901
)
1002-
.concat(coreFlags)
902+
.concat(coreArguments)
1003903
.map((option): WebpackCLIBuiltInOption => {
1004904
(option as WebpackCLIBuiltInOption).helpLevel = minimumHelpFlags.includes(option.name)
1005905
? "minimum"
@@ -1680,6 +1580,8 @@ class WebpackCLI implements IWebpackCLI {
16801580
// Default action
16811581
this.program.usage("[options]");
16821582
this.program.allowUnknownOption(true);
1583+
1584+
// Basic command for lazy loading other commands
16831585
this.program.action(async (options, program: WebpackCLICommand) => {
16841586
if (!isInternalActionCalled) {
16851587
isInternalActionCalled = true;
@@ -2090,139 +1992,100 @@ class WebpackCLI implements IWebpackCLI {
20901992
}
20911993

20921994
// Apply options
2093-
if (this.webpack.cli) {
2094-
const args: Record<string, Argument> = this.getBuiltInOptions()
2095-
.filter((flag) => flag.group === "core")
2096-
.reduce((accumulator: Record<string, Argument>, flag) => {
2097-
accumulator[flag.name] = flag as unknown as Argument;
1995+
const args: Record<string, Argument> = this.getBuiltInOptions()
1996+
.filter((flag) => flag.group === "core")
1997+
.reduce((accumulator: Record<string, Argument>, flag) => {
1998+
accumulator[flag.name] = flag as unknown as Argument;
1999+
return accumulator;
2000+
}, {});
2001+
2002+
const values: ProcessedArguments = Object.keys(options).reduce(
2003+
(accumulator: ProcessedArguments, name) => {
2004+
if (name === "argv") {
20982005
return accumulator;
2099-
}, {});
2100-
2101-
const values: ProcessedArguments = Object.keys(options).reduce(
2102-
(accumulator: ProcessedArguments, name) => {
2103-
if (name === "argv") {
2104-
return accumulator;
2105-
}
2106-
2107-
const kebabName = this.toKebabCase(name);
2108-
2109-
if (args[kebabName]) {
2110-
accumulator[kebabName] = options[name as keyof typeof options as string];
2111-
}
2112-
2113-
return accumulator;
2114-
},
2115-
{},
2116-
);
2117-
2118-
const problems: Problem[] | null = this.webpack.cli.processArguments(args, item, values);
2119-
2120-
if (problems) {
2121-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2122-
const groupBy = (xs: Record<string, any>[], key: string) => {
2123-
return xs.reduce((rv, x) => {
2124-
(rv[x[key]] = rv[x[key]] || []).push(x);
2006+
}
21252007

2126-
return rv;
2127-
}, {});
2128-
};
2129-
const problemsByPath = groupBy(problems, "path");
2008+
const kebabName = this.toKebabCase(name);
21302009

2131-
for (const path in problemsByPath) {
2132-
const problems = problemsByPath[path];
2010+
if (args[kebabName]) {
2011+
accumulator[kebabName] = options[name as keyof typeof options as string];
2012+
}
21332013

2134-
problems.forEach((problem: Problem) => {
2135-
this.logger.error(
2136-
`${this.capitalizeFirstLetter(problem.type.replace(/-/g, " "))}${
2137-
problem.value ? ` '${problem.value}'` : ""
2138-
} for the '--${problem.argument}' option${
2139-
problem.index ? ` by index '${problem.index}'` : ""
2140-
}`,
2141-
);
2014+
return accumulator;
2015+
},
2016+
{},
2017+
);
21422018

2143-
if (problem.expected) {
2144-
this.logger.error(`Expected: '${problem.expected}'`);
2145-
}
2146-
});
2147-
}
2019+
const problems: Problem[] | null = this.webpack.cli.processArguments(args, item, values);
21482020

2149-
process.exit(2);
2150-
}
2021+
if (problems) {
2022+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2023+
const groupBy = (xs: Record<string, any>[], key: string) => {
2024+
return xs.reduce((rv, x) => {
2025+
(rv[x[key]] = rv[x[key]] || []).push(x);
21512026

2152-
const isFileSystemCacheOptions = (
2153-
config: WebpackConfiguration,
2154-
): config is FileSystemCacheOptions => {
2155-
return (
2156-
Boolean(config.cache) && (config as FileSystemCacheOptions).cache.type === "filesystem"
2157-
);
2027+
return rv;
2028+
}, {});
21582029
};
2030+
const problemsByPath = groupBy(problems, "path");
21592031

2160-
// Setup default cache options
2161-
if (isFileSystemCacheOptions(item)) {
2162-
const configPath = config.path.get(item);
2032+
for (const path in problemsByPath) {
2033+
const problems = problemsByPath[path];
21632034

2164-
if (configPath) {
2165-
if (!item.cache.buildDependencies) {
2166-
item.cache.buildDependencies = {};
2167-
}
2035+
problems.forEach((problem: Problem) => {
2036+
this.logger.error(
2037+
`${this.capitalizeFirstLetter(problem.type.replace(/-/g, " "))}${
2038+
problem.value ? ` '${problem.value}'` : ""
2039+
} for the '--${problem.argument}' option${
2040+
problem.index ? ` by index '${problem.index}'` : ""
2041+
}`,
2042+
);
21682043

2169-
if (!item.cache.buildDependencies.defaultConfig) {
2170-
item.cache.buildDependencies.defaultConfig = [];
2044+
if (problem.expected) {
2045+
this.logger.error(`Expected: '${problem.expected}'`);
21712046
}
2172-
2173-
if (Array.isArray(configPath)) {
2174-
configPath.forEach((oneOfConfigPath) => {
2175-
(
2176-
item.cache.buildDependencies as NonNullable<
2177-
FileSystemCacheOptions["cache"]["buildDependencies"]
2178-
>
2179-
).defaultConfig.push(oneOfConfigPath);
2180-
});
2181-
} else {
2182-
item.cache.buildDependencies.defaultConfig.push(configPath);
2183-
}
2184-
}
2047+
});
21852048
}
2186-
}
21872049

2188-
// Setup legacy logic for webpack@4
2189-
// TODO respect `--entry-reset` in th next major release
2190-
// TODO drop in the next major release
2191-
if (options.entry) {
2192-
item.entry = options.entry;
2193-
}
2194-
2195-
if (options.outputPath) {
2196-
item.output = { ...item.output, ...{ path: path.resolve(options.outputPath) } };
2197-
}
2198-
2199-
if (options.target) {
2200-
item.target = options.target;
2050+
process.exit(2);
22012051
}
22022052

2203-
if (typeof options.devtool !== "undefined") {
2204-
item.devtool = options.devtool;
2205-
}
2053+
const isFileSystemCacheOptions = (
2054+
config: WebpackConfiguration,
2055+
): config is FileSystemCacheOptions => {
2056+
return (
2057+
Boolean(config.cache) && (config as FileSystemCacheOptions).cache.type === "filesystem"
2058+
);
2059+
};
22062060

2207-
if (options.name) {
2208-
item.name = options.name;
2209-
}
2061+
// Setup default cache options
2062+
if (isFileSystemCacheOptions(item)) {
2063+
const configPath = config.path.get(item);
22102064

2211-
if (typeof options.stats !== "undefined") {
2212-
item.stats = options.stats;
2213-
}
2065+
if (configPath) {
2066+
if (!item.cache.buildDependencies) {
2067+
item.cache.buildDependencies = {};
2068+
}
22142069

2215-
if (typeof options.watch !== "undefined") {
2216-
item.watch = options.watch;
2217-
}
2070+
if (!item.cache.buildDependencies.defaultConfig) {
2071+
item.cache.buildDependencies.defaultConfig = [];
2072+
}
22182073

2219-
if (typeof options.watchOptionsStdin !== "undefined") {
2220-
item.watchOptions = { ...item.watchOptions, ...{ stdin: options.watchOptionsStdin } };
2074+
if (Array.isArray(configPath)) {
2075+
configPath.forEach((oneOfConfigPath) => {
2076+
(
2077+
item.cache.buildDependencies as NonNullable<
2078+
FileSystemCacheOptions["cache"]["buildDependencies"]
2079+
>
2080+
).defaultConfig.push(oneOfConfigPath);
2081+
});
2082+
} else {
2083+
item.cache.buildDependencies.defaultConfig.push(configPath);
2084+
}
2085+
}
22212086
}
22222087

2223-
if (options.mode) {
2224-
item.mode = options.mode;
2225-
}
2088+
// TODO respect `--entry-reset` in th next major release
22262089

22272090
// Respect `process.env.NODE_ENV`
22282091
if (

0 commit comments

Comments
 (0)
Please sign in to comment.