Skip to content

Commit 237887b

Browse files
chore(lib): refactored lib utils and groups (#1140)
chore: fixed the changes requested in the PR review chore: fixes from PR review
1 parent 6663e94 commit 237887b

5 files changed

Lines changed: 48 additions & 35 deletions

File tree

lib/groups/AdvancedGroup.js

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,39 @@ class AdvancedGroup extends GroupHelper {
4343
// }
4444
// }
4545
resolveOptions() {
46-
const { args } = this;
46+
const {
47+
args,
48+
opts: { options },
49+
} = this;
50+
4751
if (args.hot) {
4852
const { HotModuleReplacementPlugin } = require('webpack');
4953
const hotModuleVal = new HotModuleReplacementPlugin();
50-
if (this.opts.options && this.opts.options.plugins) {
51-
this.opts.options.plugins.unshift(hotModuleVal);
54+
if (options && options.plugins) {
55+
options.plugins.unshift(hotModuleVal);
5256
} else {
53-
this.opts.options.plugins = [hotModuleVal];
57+
options.plugins = [hotModuleVal];
5458
}
5559
}
5660
if (args.prefetch) {
5761
const { PrefetchPlugin } = require('webpack');
5862
const prefetchVal = new PrefetchPlugin(null, args.prefetch);
59-
if (this.opts.options && this.opts.options.plugins) {
60-
this.opts.options.plugins.unshift(prefetchVal);
63+
if (options && options.plugins) {
64+
options.plugins.unshift(prefetchVal);
6165
} else {
62-
this.opts.options.plugins = [prefetchVal];
66+
// Currently the Plugin function is not functional -> https://github.com/webpack/webpack-cli/pull/1140#discussion_r376761359
67+
// options.plugins = [prefetchVal];
6368
}
6469
}
6570
if (args.plugin) {
66-
if (this.opts.options && this.opts.options.plugins) {
67-
this.opts.options.plugins.unshift(this.loadPlugin(args.plugin));
71+
if (options && options.plugins) {
72+
options.plugins.unshift(this.loadPlugin(args.plugin));
6873
} else {
69-
this.opts.options.plugins = [this.loadPlugin(args.plugin)];
74+
options.plugins = [this.loadPlugin(args.plugin)];
7075
}
7176
}
7277
if (args.target) {
73-
this.opts.options.target = args.target;
78+
options.target = args.target;
7479
}
7580

7681
if (args.global) {
@@ -119,10 +124,10 @@ class AdvancedGroup extends GroupHelper {
119124

120125
const { ProvidePlugin } = require('webpack');
121126
const globalVal = new ProvidePlugin(providePluginObject);
122-
if (this.opts.options && this.opts.options.plugins) {
123-
this.opts.options.plugins.unshift(globalVal);
127+
if (options && options.plugins) {
128+
options.plugins.unshift(globalVal);
124129
} else {
125-
this.opts.options.plugins = [globalVal];
130+
options.plugins = [globalVal];
126131
}
127132
}
128133
}

lib/groups/BasicGroup.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,26 @@ class BasicGroup extends GroupHelper {
1818
}
1919
resolveFlags() {
2020
const { args } = this;
21+
const { outputOptions, options } = this.opts;
2122
Object.keys(args).forEach(arg => {
2223
if (this.WEBPACK_OPTION_FLAGS.includes(arg)) {
23-
this.opts.outputOptions[arg] = args[arg];
24+
outputOptions[arg] = args[arg];
2425
}
2526
// TODO: to add once webpack bundle analyzer is installed, which is not at the moment
2627
// if (arg == 'analyze') {
2728
// const { BundleAnalyzerPlugin } = require('webpack-bundle-analyzer');
2829
// this.opts.options.plugins = [new BundleAnalyzerPlugin()];
2930
// }
3031
if (arg === 'sourcemap') {
31-
this.opts.options.devtool = args[arg] || 'eval';
32-
this.opts.outputOptions.devtool = args[arg];
32+
options.devtool = args[arg] || 'eval';
33+
outputOptions.devtool = args[arg];
3334
}
3435
if (arg === 'entry') {
35-
this.opts.options[arg] = this.resolveFilePath(args[arg], 'index.js');
36+
options[arg] = this.resolveFilePath(args[arg], 'index.js');
3637
}
3738
});
38-
if (this.opts.outputOptions['dev']) {
39-
this.opts.outputOptions['prod'] = undefined;
39+
if (outputOptions['dev']) {
40+
outputOptions['prod'] = undefined;
4041
}
4142
}
4243

lib/utils/Compiler.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,20 @@ class Compiler {
4444
});
4545

4646
if (outputOptions.infoVerbosity === 'verbose') {
47+
const resolveCompilationName = compilation => {
48+
return compilation.name ? compilation.name : '';
49+
};
4750
if (outputOptions.watch) {
4851
compilation.hooks.watchRun.tap('WebpackInfo', compilation => {
49-
const compilationName = compilation.name ? compilation.name : '';
50-
logger.info('Compilation ' + compilationName + ' starting…');
52+
logger.info(`Compilation ${resolveCompilationName(compilation)} starting…`);
5153
});
5254
} else {
5355
compilation.hooks.beforeRun.tap('WebpackInfo', compilation => {
54-
const compilationName = compilation.name ? compilation.name : '';
55-
logger.info('Compilation ' + compilationName + ' starting…');
56+
logger.info(`Compilation ${resolveCompilationName(compilation)} starting…`);
5657
});
5758
}
5859
compilation.hooks.done.tap('WebpackInfo', compilation => {
59-
const compilationName = compilation.name ? compilation.name : '';
60-
logger.info('Compilation ' + compilationName + ' finished');
60+
logger.info(`Compilation ${resolveCompilationName(compilation)} finished`);
6161
});
6262
}
6363
}

lib/utils/interactive.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,19 @@ module.exports = async function(config, outputOptions, processingMessageBuffer)
124124
if (isExitCtrl(key)) {
125125
console.clear();
126126
process.exit();
127-
} else if (key.name === 'down') {
128-
process.stdout.write(ansiEscapes.cursorNextLine);
129-
} else if (key.name === 'up') {
130-
process.stdout.write(ansiEscapes.cursorPrevLine);
131-
} else if (key.name === 'return') {
132-
// TODO: get line and do stuff
127+
}
128+
switch (key.name) {
129+
case 'down':
130+
process.stdout.write(ansiEscapes.cursorNextLine);
131+
break;
132+
case 'up':
133+
process.stdout.write(ansiEscapes.cursorPrevLine);
134+
break;
135+
case 'return':
136+
// TODO: get line and do stuff
137+
break;
138+
default:
139+
break;
133140
}
134141
});
135142

lib/utils/zero-config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
function getEnvFromOptionsAndMode(mode, optionsObject) {
22
const { dev, prod } = optionsObject;
3-
4-
if ((process.env.NODE_ENV && process.env.NODE_ENV === 'production') || process.env.NODE_ENV === 'development') {
5-
return process.env.NODE_ENV;
3+
const NODE_ENV = process.env.NODE_ENV;
4+
if (NODE_ENV && (NODE_ENV === 'production' || NODE_ENV === 'development')) {
5+
return NODE_ENV;
66
} else if (prod) {
77
return 'production';
88
} else if (dev) {

0 commit comments

Comments
 (0)