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

migrate basic group #1837

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions packages/webpack-cli/lib/groups/BasicGroup.js

This file was deleted.

40 changes: 40 additions & 0 deletions packages/webpack-cli/lib/groups/basicResolver.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
const { core, groups } = require('../utils/cli-flags');

const WEBPACK_OPTION_FLAGS = core
.filter((coreFlag) => {
return coreFlag.group === groups.BASIC_GROUP;
})
.reduce((result, flagObject) => {
result.push(flagObject.name);
if (flagObject.alias) {
result.push(flagObject.alias);
}
return result;
}, []);

function resolveArgs(args) {
const finalOptions = {
options: {},
outputOptions: {},
};
Object.keys(args).forEach((arg) => {
if (WEBPACK_OPTION_FLAGS.includes(arg)) {
finalOptions.outputOptions[arg] = args[arg];
}
if (arg === 'devtool') {
finalOptions.options.devtool = args[arg];
}
if (arg === 'name') {
finalOptions.options.name = args[arg];
}
if (arg === 'watch') {
finalOptions.options.watch = true;
}
if (arg === 'entry') {
finalOptions.options[arg] = args[arg];
}
});
return finalOptions;
}

module.exports = resolveArgs;
32 changes: 0 additions & 32 deletions packages/webpack-cli/lib/utils/GroupHelper.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const { resolve, join } = require('path');
const { existsSync } = require('fs');
const { arrayToObject } = require('../utils/arg-utils');

class GroupHelper {
Expand All @@ -12,36 +10,6 @@ class GroupHelper {
this.strategy = undefined;
}

resolveFilePath(filename = '', defaultValue) {
if (filename && Array.isArray(filename)) {
return filename.map((fp) => this.resolveFilePath(fp, defaultValue)).filter((e) => e);
}
if (filename && !filename.includes('.js')) {
filename = filename + '.js';
}
if (defaultValue && !defaultValue.includes('.js')) {
defaultValue = defaultValue + '.js';
}
let configPath;
const predefinedConfigPath = filename ? resolve(process.cwd(), filename) : undefined;
const configPathExists = predefinedConfigPath ? existsSync(predefinedConfigPath) : undefined;

if (!configPathExists) {
const LOOKUP_PATHS = [`${filename}`, `src/${filename}`, ...(defaultValue ? [defaultValue, `src/${defaultValue}`] : [])];

if (filename) {
LOOKUP_PATHS.unshift(filename);
}
LOOKUP_PATHS.forEach((p) => {
const lookUpPath = join(process.cwd(), ...p.split('/'));
if (existsSync(lookUpPath) && !configPath) {
configPath = lookUpPath;
}
});
}
return configPathExists ? predefinedConfigPath : configPath;
}

run() {
throw new Error('You must implement the "run" function');
}
Expand Down
32 changes: 11 additions & 21 deletions packages/webpack-cli/lib/webpack-cli.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
const path = require('path');
const { existsSync } = require('fs');
const { options } = require('colorette');
const webpackMerge = require('webpack-merge');
const GroupHelper = require('./utils/GroupHelper');
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 webpackMerge = require('webpack-merge');
const basicResolver = require('./groups/basicResolver');
const { toKebabCase } = require('./utils/helpers');
const argParser = require('./utils/arg-parser');
const { outputStrategy } = require('./utils/merge-strategies');
Expand All @@ -17,16 +20,6 @@ class WebpackCLI extends GroupHelper {
this.groupMap = new Map();
this.args = {};
this.compilation = new Compiler();
this.defaultEntry = 'index';
this.possibleFileNames = [
`./${this.defaultEntry}`,
`./${this.defaultEntry}.js`,
`${this.defaultEntry}.js`,
this.defaultEntry,
`./src/${this.defaultEntry}`,
`./src/${this.defaultEntry}.js`,
`src/${this.defaultEntry}.js`,
];
this.compilerConfiguration = {};
this.outputConfiguration = {};
}
Expand Down Expand Up @@ -106,11 +99,6 @@ class WebpackCLI extends GroupHelper {
resolveGroups() {
for (const [key, value] of this.groupMap.entries()) {
switch (key) {
case groups.BASIC_GROUP: {
const BasicGroup = require('./groups/BasicGroup');
this.basicGroup = new BasicGroup(value);
break;
}
case groups.ADVANCED_GROUP: {
const AdvancedGroup = require('./groups/AdvancedGroup');
this.advancedGroup = new AdvancedGroup(value);
Expand Down Expand Up @@ -217,11 +205,13 @@ class WebpackCLI extends GroupHelper {
* @returns {void}
*/
_handleDefaultEntry() {
if (!this.basicGroup) {
const BasicGroup = require('./groups/BasicGroup');
this.basicGroup = new BasicGroup();
let defaultEntry;
const rootIndexPath = path.resolve('index.js');
if (existsSync(rootIndexPath)) {
defaultEntry = './index.js';
} else {
defaultEntry = './src/index.js';
Comment on lines +210 to +213
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have tests for both of the default entries i.e, ./index.js and ./src/index.js ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be solved on webpack side, here invalid solution, because index can have cjs/ts/mjs and etc extensions

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to change a bunch of tests again because of current behaviour, let's fix this separately? Here's the issue for any discussion - #1852

}
const defaultEntry = this.basicGroup.resolveFilePath(null, 'index.js');
const options = { entry: defaultEntry };
this._mergeOptionsToConfiguration(options);
}
Expand All @@ -240,7 +230,7 @@ class WebpackCLI extends GroupHelper {
.then(() => this._baseResolver(resolveMode, parsedArgs, this.compilerConfiguration))
.then(() => this._baseResolver(resolveOutput, parsedArgs, {}, outputStrategy))
.then(() => this._handleCoreFlags())
.then(() => this._handleGroupHelper(this.basicGroup))
.then(() => this._baseResolver(basicResolver, parsedArgs))
.then(() => this._handleGroupHelper(this.advancedGroup))
.then(() => this._baseResolver(resolveStats, parsedArgs))
.then(() => this._handleGroupHelper(this.helpGroup));
Expand Down
23 changes: 21 additions & 2 deletions test/entry/flag-entry/entry-with-flag.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,23 @@ const { stat, readFile } = require('fs');
const { resolve } = require('path');

describe('entry flag', () => {
it('should resolve the path to src/index.cjs', (done) => {
const { stderr, stdout } = run(__dirname, ['--entry', './src/index.cjs', '-o', './dist/'], false);
expect(stderr).toBeFalsy();
expect(stdout).toBeTruthy();

stat(resolve(__dirname, './dist/main.js'), (err, stats) => {
expect(err).toBe(null);
expect(stats.isFile()).toBe(true);
done();
});
readFile(resolve(__dirname, './dist/main.js'), 'utf-8', (err, data) => {
expect(err).toBe(null);
expect(data).toContain('Kazuya Miyuki');
done();
});
});

it('should load ./src/a.js as entry', (done) => {
const { stderr, stdout } = run(__dirname, ['--entry', './src/a.js']);
expect(stderr).toBeFalsy();
Expand Down Expand Up @@ -40,7 +57,9 @@ describe('entry flag', () => {
});

it('should throw error for invalid entry file', () => {
const { stderr } = run(__dirname, ['--entry', './src/test.js']);
expect(stderr).toContain('Error: you provided an invalid entry point.');
const { stdout, stderr, exitCode } = run(__dirname, ['--entry', './src/test.js']);
expect(stdout).toContain("Module not found: Error: Can't resolve");
expect(exitCode).toEqual(1);
expect(stderr).toBeFalsy();
});
});
1 change: 1 addition & 0 deletions test/entry/flag-entry/src/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
console.log('Kazuya Miyuki');
2 changes: 1 addition & 1 deletion test/zero-config/entry-absent/zero-config.test.js
Original file line number Diff line number Diff line change
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'");
expect(stdout).toContain("Error: Can't resolve './src/index.js'");
expect(stderr).toBeFalsy();
});
});