From 99ff04779cad1a90d8ac47345db5f8540c6ddc23 Mon Sep 17 00:00:00 2001 From: harley <15850682376@163.com> Date: Tue, 10 Mar 2020 23:00:42 +0800 Subject: [PATCH] fix(webpack-cli): to void defaultEntry override the webpack config entry (#1289) * fix(webpack-cli): to void defaultEntry override the webpack config entry if not pass the entry option through command line, webpack-cli would look for defaultEntry The action would override the entry in webpack config files ISSUES CLOSED: #1288 * chore(webpack-cli): remove the unused eslint comment and console statement * tests(jest): rename the tests for entry-with command and entry-with-config ISSUES CLOSED: #1288 * fix(bugs): to void defaultEntry override config entry To get the correct entry, the entry should use defaultEntry, config entry, command entry in order ISSUES CLOSED: #1288 * tests(entry): fix the word spelling of test case * fix(bugs): execute the basicGroup only once --- packages/webpack-cli/lib/groups/BasicGroup.js | 1 + packages/webpack-cli/lib/utils/cli-flags.js | 1 - packages/webpack-cli/lib/webpack-cli.js | 16 +++++++++++++++ test/entry/config-entry/1.js | 11 ++++++++++ test/entry/config-entry/a.js | 0 .../entry-with-command.test.js | 20 +++++++++++++++++++ .../config-entry/entry-with-command/index.js | 0 .../entry-with-config.test.js | 20 +++++++++++++++++++ .../config-entry/entry-with-config/index.js | 0 9 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 test/entry/config-entry/1.js create mode 100644 test/entry/config-entry/a.js create mode 100644 test/entry/config-entry/entry-with-command/entry-with-command.test.js create mode 100644 test/entry/config-entry/entry-with-command/index.js create mode 100644 test/entry/config-entry/entry-with-config/entry-with-config.test.js create mode 100644 test/entry/config-entry/entry-with-config/index.js diff --git a/packages/webpack-cli/lib/groups/BasicGroup.js b/packages/webpack-cli/lib/groups/BasicGroup.js index e72be04bf4e..471b367c8a8 100644 --- a/packages/webpack-cli/lib/groups/BasicGroup.js +++ b/packages/webpack-cli/lib/groups/BasicGroup.js @@ -18,6 +18,7 @@ class BasicGroup extends GroupHelper { } resolveFlags() { const { args } = this; + if (!args) return const { outputOptions, options } = this.opts; Object.keys(args).forEach(arg => { if (this.WEBPACK_OPTION_FLAGS.includes(arg)) { diff --git a/packages/webpack-cli/lib/utils/cli-flags.js b/packages/webpack-cli/lib/utils/cli-flags.js index 76f01ef637b..ff6da9acffa 100644 --- a/packages/webpack-cli/lib/utils/cli-flags.js +++ b/packages/webpack-cli/lib/utils/cli-flags.js @@ -127,7 +127,6 @@ module.exports = { name: 'entry', usage: '--entry e.g. ./src/main.js', type: String, - defaultValue: null, defaultOption: true, group: BASIC_GROUP, description: 'The entry point of your application.', diff --git a/packages/webpack-cli/lib/webpack-cli.js b/packages/webpack-cli/lib/webpack-cli.js index 8e382df1df6..9c6fa362b14 100644 --- a/packages/webpack-cli/lib/webpack-cli.js +++ b/packages/webpack-cli/lib/webpack-cli.js @@ -226,6 +226,21 @@ class WebpackCLI extends GroupHelper { } } + /** + * Get the defaultEntry for merge with config rightly + * @private + * @returns {void} + */ + _handleDefaultEntry() { + if (!this.basicGroup) { + const BasicGroup = require('./groups/BasicGroup'); + this.basicGroup = new BasicGroup(); + } + const defaultEntry = this.basicGroup.resolveFilePath(null, 'index.js'); + const options = { entry: defaultEntry }; + this._mergeOptionsToConfiguration(options); + } + /** * Responsible for applying defaults, if necessary * @private\ @@ -248,6 +263,7 @@ class WebpackCLI extends GroupHelper { async runOptionGroups() { await Promise.resolve() .then(() => this._handleGroupHelper(this.zeroConfigGroup)) + .then(() => this._handleDefaultEntry()) .then(() => this._handleGroupHelper(this.configGroup)) .then(() => this._handleGroupHelper(this.outputGroup)) .then(() => this._handleGroupHelper(this.basicGroup)) diff --git a/test/entry/config-entry/1.js b/test/entry/config-entry/1.js new file mode 100644 index 00000000000..f14be438871 --- /dev/null +++ b/test/entry/config-entry/1.js @@ -0,0 +1,11 @@ +const { resolve } = require('path'); + +module.exports = { + entry: { + index: '../a.js', + }, + output: { + path: resolve(process.cwd(), 'binary'), + filename: '[name].bundle.js', + }, +}; diff --git a/test/entry/config-entry/a.js b/test/entry/config-entry/a.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/entry/config-entry/entry-with-command/entry-with-command.test.js b/test/entry/config-entry/entry-with-command/entry-with-command.test.js new file mode 100644 index 00000000000..91be955ddd9 --- /dev/null +++ b/test/entry/config-entry/entry-with-command/entry-with-command.test.js @@ -0,0 +1,20 @@ +'use strict'; +const { stat } = require('fs'); +const { resolve } = require('path'); +const { run, extractSummary } = require('../../../utils/test-utils'); + +describe('config entry and command entry all exist', () => { + it('should use command entry if config command existed', done => { + const { stdout } = run(__dirname, ['-c', '../1.js', './index.js'], false); + const summary = extractSummary(stdout); + const outputDir = 'entry-with-command/binary'; + expect(summary['Output Directory']).toContain(outputDir); + + expect(stdout).toContain('./index.js'); + stat(resolve(__dirname, './binary/main.bundle.js'), (err, stats) => { + expect(err).toBeFalsy(); + expect(stats.isFile()).toBe(true); + done(); + }); + }); +}); diff --git a/test/entry/config-entry/entry-with-command/index.js b/test/entry/config-entry/entry-with-command/index.js new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/entry/config-entry/entry-with-config/entry-with-config.test.js b/test/entry/config-entry/entry-with-config/entry-with-config.test.js new file mode 100644 index 00000000000..a09cbc3332d --- /dev/null +++ b/test/entry/config-entry/entry-with-config/entry-with-config.test.js @@ -0,0 +1,20 @@ +'use strict'; +const { stat } = require('fs'); +const { resolve } = require('path'); +const { run, extractSummary } = require('../../../utils/test-utils'); + +describe('default entry and config entry all exist', () => { + it('should use config entry if config entry existed', done => { + const { stdout } = run(__dirname, ['-c', '../1.js'], false); + const summary = extractSummary(stdout); + const outputDir = 'entry-with-config/binary'; + expect(summary['Output Directory']).toContain(outputDir); + + expect(stdout).toContain('./a.js'); + stat(resolve(__dirname, './binary/index.bundle.js'), (err, stats) => { + expect(err).toBeFalsy(); + expect(stats.isFile()).toBe(true); + done(); + }); + }); +}); diff --git a/test/entry/config-entry/entry-with-config/index.js b/test/entry/config-entry/entry-with-config/index.js new file mode 100644 index 00000000000..e69de29bb2d