Skip to content

Commit

Permalink
refactor: move appendProgressPlugin as method, fine-tune logging
Browse files Browse the repository at this point in the history
  • Loading branch information
piecyk committed Nov 4, 2020
1 parent b6d8861 commit f3bfd47
Showing 1 changed file with 31 additions and 31 deletions.
62 changes: 31 additions & 31 deletions packages/webpack-cli/lib/plugins/WebpackCLIPlugin.js
Expand Up @@ -3,34 +3,6 @@ const webpack = packageExists('webpack') ? require('webpack') : undefined;
const { getStatsOptions } = require('../utils/stats-options');
const { PluginName } = require('../utils/name');

const appendProgressPlugin = (compiler, progress) => {
const { ProgressPlugin } = compiler.webpack || webpack;

const logger = compiler.getInfrastructureLogger(PluginName);

const compilers = compiler.compilers || [compiler];

for (const compiler of compilers) {
let progressPluginExists;

if (compiler.options.plugins) {
progressPluginExists = Boolean(compiler.options.plugins.find((plugin) => plugin instanceof ProgressPlugin));
}

if (!progressPluginExists) {
if (typeof progress === 'string' && progress !== 'profile') {
logger.error(`'${progress}' is an invalid value for the --progress option. Only 'profile' is allowed.`);

process.exit(2);
}

const isProfile = progress === 'profile';

new ProgressPlugin({ profile: isProfile }).apply(compiler);
}
}
};

class WebpackCLIPlugin {
constructor(options) {
this.options = options;
Expand All @@ -42,12 +14,12 @@ class WebpackCLIPlugin {
const { progress } = this.options;

if (progress) {
appendProgressPlugin(compiler, progress);
this.appendProgressPlugin(compiler, progress);
}

const logger = compiler.getInfrastructureLogger(PluginName);

const resolveName = (obj) => (obj.name ? `${obj.name}: ` : '');
const resolveName = (obj) => (obj.name ? `${obj.name} ` : '');

const done = (stats) => {
const printStats = (childCompiler, childStats) => {
Expand All @@ -60,7 +32,7 @@ class WebpackCLIPlugin {
...statusOptions(false),
};

const statsString = childStats.toString(statsOptions);
const statsString = resolveName(childCompiler) + 'output:\n' + childStats.toString(statsOptions);

if (statsString.length) {
if (childStats.hasErrors()) {
Expand Down Expand Up @@ -109,6 +81,34 @@ class WebpackCLIPlugin {
});
compiler.hooks.done.tap(PluginName, done);
}

appendProgressPlugin(compiler, progress) {
const { ProgressPlugin } = compiler.webpack || webpack;

const logger = compiler.getInfrastructureLogger(PluginName);

const compilers = compiler.compilers || [compiler];

for (const compiler of compilers) {
let progressPluginExists;

if (compiler.options.plugins) {
progressPluginExists = Boolean(compiler.options.plugins.find((plugin) => plugin instanceof ProgressPlugin));
}

if (!progressPluginExists) {
if (typeof progress === 'string' && progress !== 'profile') {
logger.error(`'${progress}' is an invalid value for the --progress option. Only 'profile' is allowed.`);

process.exit(2);
}

const isProfile = progress === 'profile';

new ProgressPlugin({ profile: isProfile }).apply(compiler);
}
}
}
}

module.exports = WebpackCLIPlugin;

0 comments on commit f3bfd47

Please sign in to comment.