diff --git a/bin/process-options.js b/bin/process-options.js index c766e3942f3..ba1fd64ad84 100644 --- a/bin/process-options.js +++ b/bin/process-options.js @@ -179,7 +179,9 @@ module.exports = function processOptions(yargs, argv) { ); } else if (stats.hash !== lastHash) { lastHash = stats.hash; - const delimiter = outputOptions.buildDelimiter ? `${outputOptions.buildDelimiter}\n` : ""; + const delimiter = outputOptions.buildDelimiter + ? `${outputOptions.buildDelimiter}\n` + : ""; process.stdout.write("\n" + new Date() + "\n" + "\n"); process.stdout.write(`${stats.toString(outputOptions)}\n${delimiter}`); if (argv.s) lastHash = null; diff --git a/bin/webpack.js b/bin/webpack.js index 6731b1cf4e8..c09cb9ddb94 100755 --- a/bin/webpack.js +++ b/bin/webpack.js @@ -8,11 +8,9 @@ (function() { // wrap in IIFE to be able to use return - const resolveCwd = require("resolve-cwd"); - // Local version replace global one - const localCLI = resolveCwd.silent("webpack-cli/bin/webpack"); - if (localCLI && localCLI !== __filename) { - require(localCLI); + const importLocal = require("import-local"); + // Prefer the local installation of webpack-cli + if (importLocal(__filename)) { return; } @@ -48,7 +46,9 @@ return; } - const yargs = require("yargs").usage(`webpack-cli ${require("../package.json").version} + const yargs = require("yargs").usage(`webpack-cli ${ + require("../package.json").version + } Usage: webpack-cli [options] webpack-cli [options] --entry --output @@ -482,7 +482,9 @@ For more information, see https://webpack.js.org/api/cli/.`); } else if (stats.hash !== lastHash) { lastHash = stats.hash; const statsString = stats.toString(outputOptions); - const delimiter = outputOptions.buildDelimiter ? `${outputOptions.buildDelimiter}\n` : ""; + const delimiter = outputOptions.buildDelimiter + ? `${outputOptions.buildDelimiter}\n` + : ""; if (statsString) stdout.write(`${statsString}\n${delimiter}`); } if (!options.watch && stats.hasErrors()) { diff --git a/lib/generate-loader/index.js b/lib/commands/generate-loader.js similarity index 99% rename from lib/generate-loader/index.js rename to lib/commands/generate-loader.js index 99b4280b968..9e90bc0af64 100644 --- a/lib/generate-loader/index.js +++ b/lib/commands/generate-loader.js @@ -5,6 +5,7 @@ const { LoaderGenerator } = require("../generators/loader-generator"); * Runs a yeoman generator to create a new webpack loader project * @returns {void} */ + function loaderCreator() { const env = yeoman.createEnv(); const generatorName = "webpack-loader-generator"; diff --git a/lib/generate-plugin/index.js b/lib/commands/generate-plugin.js similarity index 81% rename from lib/generate-plugin/index.js rename to lib/commands/generate-plugin.js index a6b05c02206..2b6c1123f2f 100644 --- a/lib/generate-plugin/index.js +++ b/lib/commands/generate-plugin.js @@ -1,10 +1,11 @@ const yeoman = require("yeoman-environment"); -const PluginGenerator = require("../generators/plugin-generator").PluginGenerator; +const { PluginGenerator } = require("../generators/plugin-generator"); /** * Runs a yeoman generator to create a new webpack plugin project * @returns {void} */ + function pluginCreator() { const env = yeoman.createEnv(); const generatorName = "webpack-plugin-generator"; diff --git a/lib/commands/info.js b/lib/commands/info.js index a7c411f743f..3e5e5c54148 100644 --- a/lib/commands/info.js +++ b/lib/commands/info.js @@ -13,7 +13,7 @@ module.exports = function info() { Binaries: ["Node", "Yarn", "npm"], Browsers: ["Chrome", "Firefox", "Safari"], npmPackages: "*webpack*", - npmGlobalPackages: ["webpack", "webpack-cli"], + npmGlobalPackages: ["webpack", "webpack-cli"] }) ); }; diff --git a/lib/commands/init.js b/lib/commands/init.js index ecc72cfa92b..284d38fe822 100644 --- a/lib/commands/init.js +++ b/lib/commands/init.js @@ -9,14 +9,17 @@ const modifyHelper = require("../utils/modify-config-helper"); * First function to be called after running the init flag. This is a check, * if we are running the init command with no arguments or if we got dependencies * - * @param {Object} pkg - packages included when running the init command + * @param {Array} args - array of arguments such as + * packages included when running the init command * @returns {Function} creator/npmPackagesExists - returns an installation of the package, * followed up with a yeoman instance of that if there's packages. If not, it creates a defaultGenerator */ -module.exports = function initializeInquirer(pkg) { - if (pkg.length === 0) { +module.exports = function initializeInquirer(...args) { + const packages = args.slice(3); + + if (packages.length === 0) { return modifyHelper("init", defaultGenerator); } - return npmPackagesExists(pkg); + return npmPackagesExists(packages); }; diff --git a/lib/commands/migrate.js b/lib/commands/migrate.js index 68314cac057..cb55a3c9e07 100644 --- a/lib/commands/migrate.js +++ b/lib/commands/migrate.js @@ -1,42 +1,83 @@ "use strict"; const fs = require("fs"); +const path = require("path"); const chalk = require("chalk"); const diff = require("diff"); const inquirer = require("inquirer"); const PLazy = require("p-lazy"); const Listr = require("listr"); -const validate = require("webpack").validate; -const WebpackOptionsValidationError = require("webpack") - .WebpackOptionsValidationError; +const { validate } = require("webpack"); +const { WebpackOptionsValidationError } = require("webpack"); const runPrettier = require("../utils/run-prettier"); /** -* -* Runs migration on a given configuration using AST's and promises -* to sequentially transform a configuration file. -* -* @param {String} currentConfigPath - Location of the configuration to be migrated -* @param {String} outputConfigPath - Location to where the configuration should be written -* @param {Object} options - Any additional options regarding code style of the written configuration + * + * Runs migration on a given configuration using AST's and promises + * to sequentially transform a configuration file. + * + * @param {Array} args - Migrate arguments such as input and + * output path + * @returns {Function} Runs the migration using the 'runMigrate' + * function. + */ -* @returns {Promise} Runs the migration using a promise that will throw any errors during each transform -* or output if the user decides to abort the migration -*/ +module.exports = function migrate(...args) { + const filePaths = args.slice(3); + if (!filePaths.length) { + const errMsg = "\n ✖ Please specify a path to your webpack config \n "; + console.error(chalk.red(errMsg)); + return; + } + const currentConfigPath = path.resolve(process.cwd(), filePaths[0]); + let outputConfigPath; + if (!filePaths[1]) { + return inquirer + .prompt([ + { + type: "confirm", + name: "confirmPath", + message: + "Migration output path not specified. " + + "Do you want to use your existing webpack " + + "configuration?", + default: "Y" + } + ]) + .then(ans => { + if (!ans["confirmPath"]) { + console.error(chalk.red("✖ ︎Migration aborted due no output path")); + return; + } + outputConfigPath = path.resolve(process.cwd(), filePaths[0]); + return runMigration(currentConfigPath, outputConfigPath); + }) + .catch(err => { + console.error(err); + }); + } + outputConfigPath = path.resolve(process.cwd(), filePaths[1]); + return runMigration(currentConfigPath, outputConfigPath); +}; -module.exports = function migrate( - currentConfigPath, - outputConfigPath, - options -) { - const recastOptions = Object.assign( - { - quote: "single" - }, - options - ); +/** + * + * Runs migration on a given configuration using AST's and promises + * to sequentially transform a configuration file. + * + * @param {String} currentConfigPath - input path for config + * @param {String} outputConfigPath - output path for config + * @returns {Promise} Runs the migration using a promise that + * will throw any errors during each transform or output if the + * user decides to abort the migration + */ + +function runMigration(currentConfigPath, outputConfigPath) { + const recastOptions = { + quote: "single" + }; const tasks = new Listr([ { title: "Reading webpack config", @@ -145,8 +186,9 @@ module.exports = function migrate( }); }) .catch(err => { - console.log(chalk.red("✖ ︎Migration aborted due to some errors")); + const errMsg = "\n ✖ ︎Migration aborted due to some errors: \n"; + console.error(chalk.red(errMsg)); console.error(err); process.exitCode = 1; }); -}; +} diff --git a/lib/generate-loader/templates/examples/simple/src/index.js.tpl b/lib/generate-loader/examples/simple/src/index.js.tpl similarity index 100% rename from lib/generate-loader/templates/examples/simple/src/index.js.tpl rename to lib/generate-loader/examples/simple/src/index.js.tpl diff --git a/lib/generate-loader/templates/examples/simple/src/lazy-module.js.tpl b/lib/generate-loader/examples/simple/src/lazy-module.js.tpl similarity index 100% rename from lib/generate-loader/templates/examples/simple/src/lazy-module.js.tpl rename to lib/generate-loader/examples/simple/src/lazy-module.js.tpl diff --git a/lib/generate-loader/templates/examples/simple/src/static-esm-module.js.tpl b/lib/generate-loader/examples/simple/src/static-esm-module.js.tpl similarity index 100% rename from lib/generate-loader/templates/examples/simple/src/static-esm-module.js.tpl rename to lib/generate-loader/examples/simple/src/static-esm-module.js.tpl diff --git a/lib/generate-loader/templates/examples/simple/webpack.config.js.tpl b/lib/generate-loader/examples/simple/webpack.config.js.tpl similarity index 100% rename from lib/generate-loader/templates/examples/simple/webpack.config.js.tpl rename to lib/generate-loader/examples/simple/webpack.config.js.tpl diff --git a/lib/generate-loader/templates/src/_index.js.tpl b/lib/generate-loader/src/_index.js.tpl similarity index 100% rename from lib/generate-loader/templates/src/_index.js.tpl rename to lib/generate-loader/src/_index.js.tpl diff --git a/lib/generate-loader/templates/src/cjs.js.tpl b/lib/generate-loader/src/cjs.js.tpl similarity index 100% rename from lib/generate-loader/templates/src/cjs.js.tpl rename to lib/generate-loader/src/cjs.js.tpl diff --git a/lib/generate-loader/templates/test/fixtures/simple-file.js.tpl b/lib/generate-loader/test/fixtures/simple-file.js.tpl similarity index 100% rename from lib/generate-loader/templates/test/fixtures/simple-file.js.tpl rename to lib/generate-loader/test/fixtures/simple-file.js.tpl diff --git a/lib/generate-loader/templates/test/functional.test.js.tpl b/lib/generate-loader/test/functional.test.js.tpl similarity index 100% rename from lib/generate-loader/templates/test/functional.test.js.tpl rename to lib/generate-loader/test/functional.test.js.tpl diff --git a/lib/generate-loader/templates/test/test-utils.js.tpl b/lib/generate-loader/test/test-utils.js.tpl similarity index 100% rename from lib/generate-loader/templates/test/test-utils.js.tpl rename to lib/generate-loader/test/test-utils.js.tpl diff --git a/lib/generate-loader/templates/test/unit.test.js.tpl b/lib/generate-loader/test/unit.test.js.tpl similarity index 100% rename from lib/generate-loader/templates/test/unit.test.js.tpl rename to lib/generate-loader/test/unit.test.js.tpl diff --git a/lib/generate-plugin/templates/examples/simple/_webpack.config.js.tpl b/lib/generate-plugin/examples/simple/_webpack.config.js.tpl similarity index 100% rename from lib/generate-plugin/templates/examples/simple/_webpack.config.js.tpl rename to lib/generate-plugin/examples/simple/_webpack.config.js.tpl diff --git a/lib/generate-plugin/templates/examples/simple/src/index.js.tpl b/lib/generate-plugin/examples/simple/src/index.js.tpl similarity index 100% rename from lib/generate-plugin/templates/examples/simple/src/index.js.tpl rename to lib/generate-plugin/examples/simple/src/index.js.tpl diff --git a/lib/generate-plugin/templates/examples/simple/src/lazy-module.js.tpl b/lib/generate-plugin/examples/simple/src/lazy-module.js.tpl similarity index 100% rename from lib/generate-plugin/templates/examples/simple/src/lazy-module.js.tpl rename to lib/generate-plugin/examples/simple/src/lazy-module.js.tpl diff --git a/lib/generate-plugin/templates/examples/simple/src/static-esm-module.js.tpl b/lib/generate-plugin/examples/simple/src/static-esm-module.js.tpl similarity index 100% rename from lib/generate-plugin/templates/examples/simple/src/static-esm-module.js.tpl rename to lib/generate-plugin/examples/simple/src/static-esm-module.js.tpl diff --git a/lib/generate-plugin/templates/src/_index.js.tpl b/lib/generate-plugin/src/_index.js.tpl similarity index 100% rename from lib/generate-plugin/templates/src/_index.js.tpl rename to lib/generate-plugin/src/_index.js.tpl diff --git a/lib/generate-plugin/templates/src/cjs.js.tpl b/lib/generate-plugin/src/cjs.js.tpl similarity index 100% rename from lib/generate-plugin/templates/src/cjs.js.tpl rename to lib/generate-plugin/src/cjs.js.tpl diff --git a/lib/generate-plugin/templates/test/fixtures/simple-file.js.tpl b/lib/generate-plugin/test/fixtures/simple-file.js.tpl similarity index 100% rename from lib/generate-plugin/templates/test/fixtures/simple-file.js.tpl rename to lib/generate-plugin/test/fixtures/simple-file.js.tpl diff --git a/lib/generate-plugin/templates/test/functional.test.js.tpl b/lib/generate-plugin/test/functional.test.js.tpl similarity index 100% rename from lib/generate-plugin/templates/test/functional.test.js.tpl rename to lib/generate-plugin/test/functional.test.js.tpl diff --git a/lib/generate-plugin/templates/test/test-utils.js.tpl b/lib/generate-plugin/test/test-utils.js.tpl similarity index 100% rename from lib/generate-plugin/templates/test/test-utils.js.tpl rename to lib/generate-plugin/test/test-utils.js.tpl diff --git a/lib/generators/init-generator.js b/lib/generators/init-generator.js index ce1c9a4dd16..09a29dc2e78 100644 --- a/lib/generators/init-generator.js +++ b/lib/generators/init-generator.js @@ -31,11 +31,7 @@ module.exports = class InitGenerator extends Generator { constructor(args, opts) { super(args, opts); this.isProd = false; - this.dependencies = [ - "webpack", - "webpack-cli", - "uglifyjs-webpack-plugin" - ]; + this.dependencies = ["webpack", "webpack-cli", "uglifyjs-webpack-plugin"]; this.configuration = { config: { webpackOptions: {}, @@ -119,7 +115,9 @@ module.exports = class InitGenerator extends Generator { Confirm("prodConfirm", "Are you going to use this in production?") ]); }) - .then(prodConfirmAnswer => this.isProd = prodConfirmAnswer["prodConfirm"]) + .then( + prodConfirmAnswer => (this.isProd = prodConfirmAnswer["prodConfirm"]) + ) .then(() => { return this.prompt([ Confirm("babelConfirm", "Will you be using ES2015?") @@ -398,8 +396,9 @@ module.exports = class InitGenerator extends Generator { ) ]) .then(nameTypeAnswer => { - this.configuration.config.configName = nameTypeAnswer["nameType"].length ? - nameTypeAnswer["nameType"] : defaultName; + this.configuration.config.configName = nameTypeAnswer["nameType"].length + ? nameTypeAnswer["nameType"] + : defaultName; }) .then(() => { asyncNamePrompt(); diff --git a/lib/generators/loader-generator.js b/lib/generators/loader-generator.js index 331f1feacd0..ce37dae2090 100644 --- a/lib/generators/loader-generator.js +++ b/lib/generators/loader-generator.js @@ -25,6 +25,7 @@ function makeLoaderName(name) { * @class LoaderGenerator * @extends {Generator} */ + const LoaderGenerator = webpackGenerator( [ { @@ -36,7 +37,7 @@ const LoaderGenerator = webpackGenerator( validate: str => str.length > 0 } ], - path.join(__dirname, "../generate-loader/templates"), + path.resolve(__dirname, "..", "generate-loader"), [ "src/cjs.js.tpl", "test/test-utils.js.tpl", diff --git a/lib/generators/loader-generator.test.js b/lib/generators/loader-generator.test.js index c98660dd7d5..283f6bf551a 100644 --- a/lib/generators/loader-generator.test.js +++ b/lib/generators/loader-generator.test.js @@ -1,6 +1,6 @@ "use strict"; -const makeLoaderName = require("./loader-generator").makeLoaderName; +const { makeLoaderName } = require("./loader-generator"); describe("makeLoaderName", () => { it("should kebab-case loader name and append '-loader'", () => { diff --git a/lib/generators/plugin-generator.js b/lib/generators/plugin-generator.js index 5184ada3379..9a7cc71757e 100644 --- a/lib/generators/plugin-generator.js +++ b/lib/generators/plugin-generator.js @@ -21,7 +21,7 @@ const PluginGenerator = webpackGenerator( validate: str => str.length > 0 } ], - path.join(__dirname, "../generate-plugin/templates"), + path.resolve(__dirname, "..", "generate-plugin"), [ "src/cjs.js.tpl", "test/test-utils.js.tpl", diff --git a/lib/generators/webpack-generator.js b/lib/generators/webpack-generator.js index 8d2ab7f3c8e..79d863a63e3 100644 --- a/lib/generators/webpack-generator.js +++ b/lib/generators/webpack-generator.js @@ -68,7 +68,7 @@ function webpackGenerator( this.npmInstall(["webpack-defaults", "bluebird"], { "save-dev": true }).then(() => { - this.spawnCommand("npm", ["run", "webpack-defaults"]); + this.spawnCommand("npm", ["run", "defaults"]); }); } }; diff --git a/lib/index.js b/lib/index.js index 93ebbe5c502..51f576fed5a 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,7 +1,5 @@ "use strict"; -const path = require("path"); - /** * * First function to be called after running a flag. This is a check, @@ -14,50 +12,10 @@ const path = require("path"); */ module.exports = function initialize(command, args) { - const popArgs = args ? args.slice(2).pop() : null; - switch (command) { - case "init": { - const initPkgs = args.slice(2).length === 1 ? [] : [popArgs]; - //eslint-disable-next-line - return require("./commands/init.js")(initPkgs); - } - case "migrate": { - const filePaths = args.slice(2).length === 1 ? [] : [popArgs]; - if (!filePaths.length) { - throw new Error("Please specify a path to your webpack config"); - } - const inputConfigPath = path.resolve(process.cwd(), filePaths[0]); - //eslint-disable-next-line - return require("./commands/migrate.js")(inputConfigPath, inputConfigPath); - } - case "add": { - //eslint-disable-next-line - return require("./commands/add")(); - } - case "remove": { - //eslint-disable-next-line - return require("./commands/remove")(); - } - case "update": { - return require("./commands/update")(); - } - case "serve": { - return require("./commands/serve").serve(); - } - case "make": { - return require("./commands/make")(); - } - case "generate-loader": { - return require("./generate-loader/index.js")(); - } - case "generate-plugin": { - return require("./generate-plugin/index.js")(); - } - case "info": { - return require("./commands/info.js")(); - } - default: { - throw new Error(`Unknown command ${command} found`); - } + if (!command) { + throw new Error(`Unknown command ${command} found`); + } else if (command === "serve") { + return require(`./commands/${command}`).serve(); } + return require(`./commands/${command}`)(...args); }; diff --git a/lib/init/transformations/top-scope/top-scope.test.js b/lib/init/transformations/top-scope/top-scope.test.js index 559a40e4e28..22f8f5885bc 100644 --- a/lib/init/transformations/top-scope/top-scope.test.js +++ b/lib/init/transformations/top-scope/top-scope.test.js @@ -2,7 +2,13 @@ const defineTest = require("../../../utils/defineTest"); -defineTest(__dirname, "top-scope", "top-scope-0", ["const test = 'me';"], "init"); +defineTest( + __dirname, + "top-scope", + "top-scope-0", + ["const test = 'me';"], + "init" +); defineTest( __dirname, "top-scope", diff --git a/lib/migrate/loaderOptionsPlugin/loaderOptionsPlugin.js b/lib/migrate/loaderOptionsPlugin/loaderOptionsPlugin.js index e7daf5c45b1..9d0f4a42192 100644 --- a/lib/migrate/loaderOptionsPlugin/loaderOptionsPlugin.js +++ b/lib/migrate/loaderOptionsPlugin/loaderOptionsPlugin.js @@ -20,11 +20,9 @@ module.exports = function(j, ast) { // If there is debug: true, set debug: true in the plugin if (ast.find(j.Identifier, { name: "debug" }).size()) { loaderOptions.debug = true; - ast - .find(j.Identifier, { name: "debug" }) - .forEach(p => { - p.parent.prune(); - }); + ast.find(j.Identifier, { name: "debug" }).forEach(p => { + p.parent.prune(); + }); } // If there is UglifyJsPlugin, set minimize: true diff --git a/package-lock.json b/package-lock.json index 11e30ec9b95..960e23b800c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -7364,7 +7364,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/import-local/-/import-local-1.0.0.tgz", "integrity": "sha512-vAaZHieK9qjGo58agRBg+bhHX3hoTZU/Oa3GESWLz7t1U62fk63aHuDJJEteXoDeTCcPmUT+z38gkHPZkkmpmQ==", - "dev": true, "requires": { "pkg-dir": "2.0.0", "resolve-cwd": "2.0.0" @@ -14715,7 +14714,6 @@ "version": "2.0.0", "resolved": "https://registry.npmjs.org/pkg-dir/-/pkg-dir-2.0.0.tgz", "integrity": "sha1-9tXREJ4Z1j7fQo4L1X4Sd3YVM0s=", - "dev": true, "requires": { "find-up": "2.1.0" } diff --git a/package.json b/package.json index bf68adad485..e260009bbe9 100644 --- a/package.json +++ b/package.json @@ -89,6 +89,7 @@ "glob-all": "^3.1.0", "global-modules": "^1.0.0", "got": "^8.2.0", + "import-local": "^1.0.0", "inquirer": "^5.1.0", "interpret": "^1.0.4", "jscodeshift": "^0.5.0", @@ -100,7 +101,6 @@ "p-each-series": "^1.0.0", "p-lazy": "^1.0.0", "prettier": "^1.5.3", - "resolve-cwd": "^2.0.0", "supports-color": "^5.3.0", "v8-compile-cache": "^1.1.2", "webpack-addons": "^1.1.5",