Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(webpack-cli): to void defaultEntry override the webpack config entry #1289

Merged
merged 7 commits into from
Mar 10, 2020
1 change: 1 addition & 0 deletions packages/webpack-cli/lib/groups/BasicGroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
1 change: 0 additions & 1 deletion packages/webpack-cli/lib/utils/cli-flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ module.exports = {
name: 'entry',
usage: '--entry <path to entry file> e.g. ./src/main.js',
type: String,
defaultValue: null,
defaultOption: true,
group: BASIC_GROUP,
description: 'The entry point of your application.',
Expand Down
16 changes: 16 additions & 0 deletions packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -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\
Expand All @@ -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))
Expand Down
11 changes: 11 additions & 0 deletions test/entry/config-entry/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { resolve } = require('path');

module.exports = {
entry: {
index: '../a.js',
},
output: {
path: resolve(process.cwd(), 'binary'),
filename: '[name].bundle.js',
},
};
Empty file added test/entry/config-entry/a.js
Empty file.
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
Empty file.
Original file line number Diff line number Diff line change
@@ -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();
});
});
});
Empty file.