Skip to content

Commit

Permalink
refactor: migrate basic group (#1837)
Browse files Browse the repository at this point in the history
* refactor: migrate basic group

* chore: rm stale utils, pass entry directly

* tests: add test for cjs entry

* tests: fix tests
  • Loading branch information
anshumanv committed Oct 3, 2020
1 parent 55d0bd8 commit 6f49e96
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 113 deletions.
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
@@ -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
@@ -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
@@ -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';
}
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
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
@@ -0,0 +1 @@
console.log('Kazuya Miyuki');
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'");
expect(stdout).toContain("Error: Can't resolve './src/index.js'");
expect(stderr).toBeFalsy();
});
});

0 comments on commit 6f49e96

Please sign in to comment.