diff --git a/bin/utils/prompt-command.js b/bin/utils/prompt-command.js index 20a99a1655f..9b3767e116f 100644 --- a/bin/utils/prompt-command.js +++ b/bin/utils/prompt-command.js @@ -48,9 +48,9 @@ const runWhenInstalled = (packages, pathForCmd, ...args) => { module.exports = function promptForInstallation(packages, ...args) { const nameOfPackage = "@webpack-cli/" + packages; - let packageIsInstalled = false; - let pathForCmd; - try { + let packageIsInstalled = true; + let pathForCmd = "../../packages/init"; + /* try { const path = require("path"); const fs = require("fs"); pathForCmd = path.resolve(process.cwd(), "node_modules", "@webpack-cli", packages); @@ -64,7 +64,7 @@ module.exports = function promptForInstallation(packages, ...args) { packageIsInstalled = true; } catch (err) { packageIsInstalled = false; - } + } */ if (!packageIsInstalled) { const path = require("path"); const fs = require("fs"); diff --git a/packages/generators/init-generator.ts b/packages/generators/init-generator.ts index 64fd32351ee..96fcf501e76 100644 --- a/packages/generators/init-generator.ts +++ b/packages/generators/init-generator.ts @@ -45,7 +45,7 @@ export default class InitGenerator extends Generator { public constructor(args, opts) { super(args, opts); - this.usingDefaults = false; + this.usingDefaults = true; this.autoGenerateConfig = opts.autoSetDefaults ? true : false; this.dependencies = ["webpack", "webpack-cli", "babel-plugin-syntax-dynamic-import"]; @@ -122,7 +122,7 @@ export default class InitGenerator extends Generator { self, "outputDir", "In which folder do you want to store your generated bundles?", - "dist", + "'dist'", this.autoGenerateConfig ); @@ -131,13 +131,7 @@ export default class InitGenerator extends Generator { if (!this.usingDefaults) { this.configuration.config.webpackOptions.output = { chunkFilename: "'[name].[chunkhash].js'", - filename: "'[name].[chunkhash].js'", - path: `path.resolve(__dirname, '${outputDir}')` - }; - } else { - this.configuration.config.webpackOptions.output = { - filename: "'bundle.js'", - path: `path.resolve(__dirname, '${outputDir}')` + filename: "'[name].[chunkhash].js'" }; } @@ -164,13 +158,13 @@ export default class InitGenerator extends Generator { ({ ExtractUseProps, regExpForStyles } = styleQuestionHandler(self, stylingType)); - if (this.isProd) { + if (this.usingDefaults) { // Ask if the user wants to use extractPlugin const { useExtractPlugin } = await Input( self, "useExtractPlugin", "If you want to bundle your CSS files, what will you name the bundle? (press enter to skip)", - "null", + "'main.css'", this.autoGenerateConfig ); @@ -205,7 +199,7 @@ export default class InitGenerator extends Generator { }); } } - if (!this.isProd) { + if (this.usingDefaults) { this.dependencies.push("html-webpack-plugin"); const htmlWebpackDependency = "html-webpack-plugin"; const htmlwebpackPlugin = generatePluginName(htmlWebpackDependency); @@ -214,24 +208,24 @@ export default class InitGenerator extends Generator { "\n", tooltip.html() ); - (this.configuration.config.webpackOptions.plugins as string[]).push(`new ${htmlwebpackPlugin}()`); - } - - if (!this.usingDefaults) { + (this.configuration.config.webpackOptions.plugins as string[]).push(`new ${htmlwebpackPlugin}({ + template: 'index.html' + })`); this.dependencies.push("webpack-dev-server"); this.configuration.config.webpackOptions.devServer = { open: true }; - } else { - this.dependencies.push("terser-webpack-plugin"); - this.configuration.config.topScope.push( - tooltip.terser(), - "const TerserPlugin = require('terser-webpack-plugin');", - "\n" - ); } + + this.dependencies.push("terser-webpack-plugin"); + this.configuration.config.topScope.push( + tooltip.terser(), + "const TerserPlugin = require('terser-webpack-plugin');", + "\n" + ); + this.configuration.config.webpackOptions.optimization = getDefaultOptimization(this.usingDefaults); - this.configuration.config.webpackOptions.mode = this.usingDefaults ? "'development'" : "'production'"; + this.configuration.config.webpackOptions.mode = this.usingDefaults ? "'production'" : "'development'"; done(); } @@ -250,7 +244,7 @@ export default class InitGenerator extends Generator { this.config.set("configuration", this.configuration); const packageJsonTemplatePath = "./templates/package.json.js"; - this.fs.extendJSON(this.destinationPath("package.json"), require(packageJsonTemplatePath)(this.isProd)); + this.fs.extendJSON(this.destinationPath("package.json"), require(packageJsonTemplatePath)(this.usingDefaults)); const generateEntryFile = (entryPath: string, name: string): void => { entryPath = entryPath.replace(/'/g, ""); @@ -258,7 +252,7 @@ export default class InitGenerator extends Generator { }; // Generate entry file/files - const entry = this.configuration.config.webpackOptions.entry; + const entry = this.configuration.config.webpackOptions.entry || "./src/index.js"; if (typeof entry === "string") { generateEntryFile(entry, "your main file!"); } else if (typeof entry === "object") { @@ -268,6 +262,15 @@ export default class InitGenerator extends Generator { // Generate README this.fs.copyTpl(path.resolve(__dirname, "./templates/README.md"), this.destinationPath("README.md"), {}); + // Generate HTML template file + if (this.usingDefaults) { + this.fs.copyTpl( + path.resolve(__dirname, "./templates/template.html"), + this.destinationPath("index.html"), + {} + ); + } + // Genrate tsconfig if (this.langType === LangType.Typescript) { const tsConfigTemplatePath = "./templates/tsconfig.json.js"; diff --git a/packages/generators/templates/package.json.js b/packages/generators/templates/package.json.js index caa852fbc6c..9f5f4281677 100644 --- a/packages/generators/templates/package.json.js +++ b/packages/generators/templates/package.json.js @@ -1,8 +1,8 @@ -module.exports = isProd => { +module.exports = usingDefaults => { let scripts = { build: "webpack" }; - if (!isProd) { + if (usingDefaults) { scripts.start = "webpack-dev-server"; } diff --git a/packages/generators/templates/template.html b/packages/generators/templates/template.html new file mode 100644 index 00000000000..070f7dff6f9 --- /dev/null +++ b/packages/generators/templates/template.html @@ -0,0 +1,11 @@ + + + + + Webpack App + + +

Hello world!

+

Tip: Check your console

+ + \ No newline at end of file diff --git a/packages/generators/utils/webpackConfig.ts b/packages/generators/utils/webpackConfig.ts index 32d823feec9..622c1916c1c 100644 --- a/packages/generators/utils/webpackConfig.ts +++ b/packages/generators/utils/webpackConfig.ts @@ -2,8 +2,9 @@ import { WebpackOptions } from "../types"; export function getDefaultOptimization(usingDefaults: boolean): WebpackOptions["optimization"] { let optimizationOptions; - if (usingDefaults) { + if (!usingDefaults) { optimizationOptions = { + minimizer: ["new TerserPlugin()"], splitChunks: { cacheGroups: { vendors: { @@ -14,7 +15,7 @@ export function getDefaultOptimization(usingDefaults: boolean): WebpackOptions[" chunks: "'async'", minChunks: 1, minSize: 30000, - name: !this.isProd + name: !usingDefaults } }; } else { diff --git a/packages/utils/scaffold.ts b/packages/utils/scaffold.ts index 3bccb9a14ba..e45357fa8d2 100644 --- a/packages/utils/scaffold.ts +++ b/packages/utils/scaffold.ts @@ -91,11 +91,11 @@ export default function runTransform(transformConfig: TransformConfig, action: s } ); - const runCommand = getPackageManager() === "yarn" ? "yarn start" : "npm run start"; + const runCommand = getPackageManager() === "yarn" ? "yarn build" : "npm run build"; let successMessage: string = chalk.green(`Congratulations! Your new webpack configuration file has been created!\n\n`) + - `You can now run ${chalk.green(runCommand)} to run your project!\n\n`; + `You can now run ${chalk.green(runCommand)} to bundle your application!\n\n`; if (initActionNotDefined && transformConfig.config.item) { successMessage = chalk.green(`Congratulations! ${transformConfig.config.item} has been ${action}ed!\n`);