From 376dcbd8ca8c3d6121f5d0e4fe5b0b65f5dcfe86 Mon Sep 17 00:00:00 2001 From: Devid Farinelli Date: Wed, 29 May 2019 16:01:58 +0200 Subject: [PATCH] misc(generators): refactor --- packages/generators/init-generator.ts | 37 +++---------------- .../utils/{language.ts => languageSupport.ts} | 0 .../utils/{style.ts => styleSupport.ts} | 0 packages/generators/utils/webpackConfig.ts | 32 ++++++++++++++++ packages/webpack-scaffold/README.md | 2 +- 5 files changed, 38 insertions(+), 33 deletions(-) rename packages/generators/utils/{language.ts => languageSupport.ts} (100%) rename packages/generators/utils/{style.ts => styleSupport.ts} (100%) create mode 100644 packages/generators/utils/webpackConfig.ts diff --git a/packages/generators/init-generator.ts b/packages/generators/init-generator.ts index 7a4df24f43c..a2733828d8b 100644 --- a/packages/generators/init-generator.ts +++ b/packages/generators/init-generator.ts @@ -6,10 +6,11 @@ import * as path from "path"; import { getPackageManager } from "@webpack-cli/utils/package-manager"; import { Confirm, Input, List } from "@webpack-cli/webpack-scaffold"; +import { getDefaultOptimization } from "./utils/webpackConfig"; import { WebpackOptions } from "./types"; import entryQuestions from "./utils/entry"; -import langQuestionHandler, { LangType } from "./utils/language"; -import styleQuestionHandler, { Loader, StylingType } from "./utils/style"; +import langQuestionHandler, { LangType } from "./utils/languageSupport"; +import styleQuestionHandler, { Loader, StylingType } from "./utils/styleSupport"; import tooltip from "./utils/tooltip"; /** @@ -87,36 +88,8 @@ export default class InitGenerator extends Generator { "new webpack.ProgressPlugin()", ); - let optimizationObj; - - if (!this.isProd) { - optimizationObj = { - splitChunks: { - chunks: "'all'", - }, - }; - } else { - optimizationObj = { - minimizer: [ - "new TerserPlugin()", - ], - splitChunks: { - cacheGroups: { - vendors: { - priority: -10, - test: "/[\\\\/]node_modules[\\\\/]/", - }, - }, - chunks: "'async'", - minChunks: 1, - minSize: 30000, - // for production name is recommended to be off - name: !this.isProd, - }, - }; - } - - this.configuration.config.webpackOptions.optimization = optimizationObj; + let optimizationConfig = getDefaultOptimization(this.isProd); + this.configuration.config.webpackOptions.optimization = optimizationConfig; if (!this.isProd) { this.configuration.config.webpackOptions.devServer = { diff --git a/packages/generators/utils/language.ts b/packages/generators/utils/languageSupport.ts similarity index 100% rename from packages/generators/utils/language.ts rename to packages/generators/utils/languageSupport.ts diff --git a/packages/generators/utils/style.ts b/packages/generators/utils/styleSupport.ts similarity index 100% rename from packages/generators/utils/style.ts rename to packages/generators/utils/styleSupport.ts diff --git a/packages/generators/utils/webpackConfig.ts b/packages/generators/utils/webpackConfig.ts new file mode 100644 index 00000000000..1fddf5ee84d --- /dev/null +++ b/packages/generators/utils/webpackConfig.ts @@ -0,0 +1,32 @@ +import { WebpackOptions } from '../types'; + +export function getDefaultOptimization(isProd: boolean): WebpackOptions["optimization"] { + let optimizationOptions; + if (isProd) { + optimizationOptions = { + splitChunks: { + chunks: "'all'", + }, + }; + } else { + optimizationOptions = { + minimizer: [ + "new TerserPlugin()", + ], + splitChunks: { + cacheGroups: { + vendors: { + priority: -10, + test: "/[\\\\/]node_modules[\\\\/]/", + }, + }, + chunks: "'async'", + minChunks: 1, + minSize: 30000, + // for production name is recommended to be off + name: !this.isProd, + }, + }; + } + return optimizationOptions; +} \ No newline at end of file diff --git a/packages/webpack-scaffold/README.md b/packages/webpack-scaffold/README.md index 2d727cc00bf..5c430fe9495 100755 --- a/packages/webpack-scaffold/README.md +++ b/packages/webpack-scaffold/README.md @@ -192,7 +192,7 @@ const validation = value => { if (value.length > 4) { return true; } else { - return "The entry point must be longer than 4, try again"; + return "Your answer must be longer than 4, try again"; } }; InputValidate("entry", "what is your entry point?", validation, "src/index");