Skip to content

Commit 9a2f306

Browse files
authored
refactor: use optional chaining & delete unnecessary assertion (#4465)
1 parent cd435ab commit 9a2f306

2 files changed

Lines changed: 9 additions & 14 deletions

File tree

packages/create-webpack-app/src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ program
7676
.option("-t --template <template>", "Template to be used for scaffolding", "default")
7777
.action(async function (projectPath, opts: InitOptions) {
7878
const { force } = opts;
79-
let templateOption = opts.template as string;
79+
let templateOption = opts.template;
8080
let generator = initGenerators[templateOption];
8181

8282
if (generator === undefined) {
@@ -134,7 +134,7 @@ program
134134
.argument("[projectPath]", "Path to create the project")
135135
.option("-t --template <template>", "Template to be used for scaffolding", "default")
136136
.action(async function (projectPath, opts: LoaderOptions) {
137-
let templateOption = opts.template as string;
137+
let templateOption = opts.template;
138138
let generator = loaderGenerators[templateOption];
139139

140140
if (generator === undefined) {
@@ -175,7 +175,7 @@ program
175175
.argument("[projectPath]", "Path to create the project")
176176
.option("-t --template <template>", "Template to be used for scaffolding", "default")
177177
.action(async function (projectPath, opts: PluginOptions) {
178-
let templateOption = opts.template as string;
178+
let templateOption = opts.template;
179179
let generator = pluginGenerators[templateOption];
180180

181181
if (generator === undefined) {

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

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,7 +1637,7 @@ class WebpackCLI implements IWebpackCLI {
16371637

16381638
const flag = this.getBuiltInOptions().find((flag) => option.long === `--${flag.name}`);
16391639

1640-
if (flag && flag.configs) {
1640+
if (flag?.configs) {
16411641
const possibleValues = flag.configs.reduce(
16421642
(accumulator, currentValue) => {
16431643
if (currentValue.values) {
@@ -2243,9 +2243,8 @@ class WebpackCLI implements IWebpackCLI {
22432243

22442244
if (
22452245
options.isWatchingLikeCommand &&
2246-
options.argv &&
2247-
options.argv.env &&
2248-
(typeof originalWatchValue !== "undefined" || typeof options.argv.watch !== "undefined")
2246+
options.argv?.env &&
2247+
(typeof originalWatchValue !== "undefined" || typeof options.argv?.watch !== "undefined")
22492248
) {
22502249
this.logger.warn(
22512250
`No need to use the '${
@@ -2292,8 +2291,7 @@ class WebpackCLI implements IWebpackCLI {
22922291
// Respect `process.env.NODE_ENV`
22932292
if (
22942293
!item.mode &&
2295-
process.env &&
2296-
process.env.NODE_ENV &&
2294+
process.env?.NODE_ENV &&
22972295
(process.env.NODE_ENV === "development" ||
22982296
process.env.NODE_ENV === "production" ||
22992297
process.env.NODE_ENV === "none")
@@ -2408,14 +2406,11 @@ class WebpackCLI implements IWebpackCLI {
24082406
needWatchStdin(compiler: Compiler | MultiCompiler): boolean {
24092407
if (this.isMultipleCompiler(compiler)) {
24102408
return Boolean(
2411-
compiler.compilers.some(
2412-
(compiler: Compiler) =>
2413-
compiler.options.watchOptions && compiler.options.watchOptions.stdin,
2414-
),
2409+
compiler.compilers.some((compiler: Compiler) => compiler.options.watchOptions?.stdin),
24152410
);
24162411
}
24172412

2418-
return Boolean(compiler.options.watchOptions && compiler.options.watchOptions.stdin);
2413+
return Boolean(compiler.options.watchOptions?.stdin);
24192414
}
24202415

24212416
async runWebpack(options: WebpackRunOptions, isWatchCommand: boolean): Promise<void> {

0 commit comments

Comments
 (0)