Skip to content

Commit

Permalink
fix(webpack-cli): to void defaultEntry override the webpack config en…
Browse files Browse the repository at this point in the history
…try (#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
  • Loading branch information
Mistyyyy committed Mar 10, 2020
1 parent 8e90009 commit 99ff047
Show file tree
Hide file tree
Showing 9 changed files with 68 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/webpack-cli/lib/groups/BasicGroup.js
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
Expand Up @@ -127,7 +127,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
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
@@ -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.
@@ -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.
@@ -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.

0 comments on commit 99ff047

Please sign in to comment.