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: defer setting default entry to core #1856

Merged
merged 6 commits into from Oct 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/webpack-cli/README.md
Expand Up @@ -46,7 +46,7 @@ yarn add webpack-cli --dev
--env string Environment passed to the configuration when it is a function
--name string Name of the configuration. Used when loading multiple configurations
--help Outputs list of supported flags
-o, --output string Output location of the file generated by webpack
-o, --output-path string Output location of the generated bundle
-t, --target string Sets the build target
-w, --watch Watch for files changes
-h, --hot Enables Hot Module Replacement
Expand Down
34 changes: 8 additions & 26 deletions packages/webpack-cli/lib/webpack-cli.js
@@ -1,18 +1,18 @@
const path = require('path');
const { existsSync } = require('fs');
const { options } = require('colorette');
const webpackMerge = require('webpack-merge');
const { options } = require('colorette');
const GroupHelper = require('./utils/GroupHelper');
const { Compiler } = require('./utils/Compiler');
const { groups, core } = require('./utils/cli-flags');
const argParser = require('./utils/arg-parser');
const { outputStrategy } = require('./utils/merge-strategies');
const { toKebabCase } = require('./utils/helpers');

// CLI arg resolvers
const handleConfigResolution = require('./groups/ConfigGroup');
const resolveMode = require('./groups/resolveMode');
const resolveStats = require('./groups/resolveStats');
const resolveOutput = require('./groups/resolveOutput');
const { Compiler } = require('./utils/Compiler');
const { groups, core } = require('./utils/cli-flags');
const basicResolver = require('./groups/basicResolver');
const { toKebabCase } = require('./utils/helpers');
const argParser = require('./utils/arg-parser');
const { outputStrategy } = require('./utils/merge-strategies');

class WebpackCLI extends GroupHelper {
constructor() {
Expand Down Expand Up @@ -186,23 +186,6 @@ class WebpackCLI extends GroupHelper {
}
}

/**
* Get the defaultEntry for merge with config rightly
* @private
* @returns {void}
*/
_handleDefaultEntry() {
let defaultEntry;
const rootIndexPath = path.resolve('index.js');
if (existsSync(rootIndexPath)) {
defaultEntry = './index.js';
} else {
defaultEntry = './src/index.js';
}
const options = { entry: defaultEntry };
this._mergeOptionsToConfiguration(options);
}

/**
* It runs in a fancy order all the expected groups.
* Zero config and configuration goes first.
Expand All @@ -212,7 +195,6 @@ class WebpackCLI extends GroupHelper {
*/
async runOptionGroups(parsedArgs) {
await Promise.resolve()
.then(() => this._handleDefaultEntry())
.then(() => this._baseResolver(handleConfigResolution, parsedArgs))
.then(() => this._baseResolver(resolveMode, parsedArgs))
.then(() => this._baseResolver(resolveOutput, parsedArgs, outputStrategy))
Expand Down
Expand Up @@ -6,7 +6,7 @@ describe('Zero Config', () => {
it('runs when config is present but not supplied via flag', () => {
const { stdout, stderr, exitCode } = run(__dirname, [], false);
// default entry should be used
expect(stdout).toContain('./index.js');
expect(stdout).toContain('./src/index.js');
// should pick up the output path from config
expect(stdout).toContain('test-output');
if (!isWebpack5) {
Expand Down
Expand Up @@ -6,7 +6,7 @@ describe('Default Config:', () => {
it('Should be able to pick cjs config by default', () => {
const { stdout, stderr, exitCode } = run(__dirname, [], false);
// default entry should be used
expect(stdout).toContain('./index.js');
expect(stdout).toContain('./src/index.js');
// should pick up the output path from config
expect(stdout).toContain('test-output');
if (!isWebpack5) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 10 additions & 0 deletions test/mode/mode-with-config/src/index.js
@@ -0,0 +1,10 @@
require("react")
console.log("Ichigo")
if (process.env.NODE_ENV === "production") {
console.log("production mode")
} else if (process.env.NODE_ENV === "development") {
console.log(console.log("development mode"))
} else {
console.log(console.log("none mode"))
}

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion test/zero-config/entry-absent/zero-config.test.js
Expand Up @@ -4,7 +4,7 @@ describe('Zero Config tests', () => {
it('runs when config and entry are both absent', () => {
const { stdout, stderr } = run(__dirname, [], false);
// Entry file is absent, should log the Error from the compiler
expect(stdout).toContain("Error: Can't resolve './src/index.js'");
expect(stdout).toContain("Error: Can't resolve './src'");
expect(stderr).toBeFalsy();
});
});