diff --git a/packages/generators/__tests__/add-generator.test.ts b/packages/generators/__tests__/add-generator.test.ts new file mode 100644 index 00000000000..7906123cb4e --- /dev/null +++ b/packages/generators/__tests__/add-generator.test.ts @@ -0,0 +1,14 @@ +import { generatePluginName } from "../utils/plugins" + +describe("generatePluginName", () => { + it("should return webpack Standard Plugin Name for Name : extract-text-webpack-plugin", () => { + const pluginName = generatePluginName("extract-text-webpack-plugin"); + expect(pluginName).toEqual("ExtractTextWebpackPlugin"); + }); + + it("should return webpack Standard Plugin Name for Name : webpack.DefinePlugin", () => { + const pluginName = generatePluginName("webpack.DefinePlugin"); + expect(pluginName).toEqual("webpack.DefinePlugin"); + }); +}); + diff --git a/packages/generators/add-generator.ts b/packages/generators/add-generator.ts index bef3c32afd3..705afb0da0c 100644 --- a/packages/generators/add-generator.ts +++ b/packages/generators/add-generator.ts @@ -11,26 +11,11 @@ import { AutoComplete, Confirm, Input, List } from "@webpack-cli/webpack-scaffol import { SchemaProperties, WebpackOptions } from "./types"; import entryQuestions from "./utils/entry"; - -import webpackDevServerSchema from "webpack-dev-server/lib/options.json"; -import webpackSchema from "./utils/optionsSchema.json"; +import { generatePluginName } from "./utils/plugins"; +import * as webpackDevServerSchema from "webpack-dev-server/lib/options.json"; +import * as webpackSchema from "./utils/optionsSchema.json"; const PROPS: string[] = Array.from(PROP_TYPES.keys()); -/** - * - * Replaces the string with a substring at the given index - * https://gist.github.com/efenacigiray/9367920 - * - * @param {String} str - string to be modified - * @param {Number} index - index to replace from - * @param {String} replace - string to replace starting from index - * - * @returns {String} string - The newly mutated string - * - */ -function replaceAt(str: string, index: number, replace: string): string { - return str.substring(0, index) + replace + str.substring(index + 1); -} /** * @@ -395,15 +380,7 @@ export default class AddGenerator extends Generator { (p: boolean): void => { if (p) { this.dependencies.push(answerToAction.actionAnswer); - const normalizePluginName = answerToAction.actionAnswer.replace( - "-webpack-plugin", - "Plugin" - ); - const pluginName = replaceAt( - normalizePluginName, - 0, - normalizePluginName.charAt(0).toUpperCase() - ); + const pluginName = generatePluginName(answerToAction.actionAnswer) this.configuration.config.topScope.push( `const ${pluginName} = require("${answerToAction.actionAnswer}")` ); diff --git a/packages/generators/utils/plugins.ts b/packages/generators/utils/plugins.ts index 6dedb1fab9e..4b60bd3f307 100644 --- a/packages/generators/utils/plugins.ts +++ b/packages/generators/utils/plugins.ts @@ -1,3 +1,4 @@ + /** * * Callable function with the initial plugins @@ -6,6 +7,45 @@ * that consists of terser-webpack-plugin */ -export default function(): string[] { +export default function (): string[] { return ["new TerserPlugin()"]; } + +/** + * + * Replaces the string with a substring at the given index + * https://gist.github.com/efenacigiray/9367920 + * + * @param {String} str - string to be modified + * @param {Number} index - index to replace from + * @param {String} replace - string to replace starting from index + * + * @returns {String} string - The newly mutated string + * + */ + +export const replaceAt = (str: string, index: number, replace: string) : string => { + return str.substring(0, index) + replace + str.substring(index + 1); +} + + +/** + * + * Generate a webpack standard webpack plugin name from the plugin name from the Answer + * + * @param {String} rawPluginName - plugin name from answer + * + * @returns {String} string - the webpack standard plugin name + * + */ + + +export const generatePluginName = (rawPluginName: string): string => { + let myPluginNameArray : string[]; + myPluginNameArray = rawPluginName.split("-"); + const pluginArrLength : number = myPluginNameArray.length; + for (let i = 0; i < pluginArrLength && pluginArrLength > 1 ; i++) { + myPluginNameArray[i] = replaceAt(myPluginNameArray[i], 0, myPluginNameArray[i].charAt(0).toUpperCase()); + } + return myPluginNameArray.join("") +} diff --git a/tsconfig.base.json b/tsconfig.base.json index d92fae8bb6a..dc260d82933 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -4,7 +4,8 @@ "module": "commonjs", "moduleResolution": "node", "allowSyntheticDefaultImports": true, - "skipLibCheck": true + "skipLibCheck": true, + "resolveJsonModule": true, }, "include": ["packages/**/*.ts"], "exclude": [