Skip to content

Commit

Permalink
fix: compatibility with webpack@4 (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait committed Jan 13, 2021
1 parent 0d42f71 commit acdfd4d
Show file tree
Hide file tree
Showing 7 changed files with 683 additions and 241 deletions.
54 changes: 28 additions & 26 deletions src/utils/setupHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,25 @@ export default function setupHooks(context) {
context.stats = undefined;
}

const statsForWebpack4 = webpack.Stats && webpack.Stats.presetToOptions;

function normalizeStatsOptions(statsOptions) {
if (statsForWebpack4) {
if (typeof statsOptions === 'undefined') {
// eslint-disable-next-line no-param-reassign
statsOptions = {};
} else if (
typeof statsOptions === 'boolean' ||
typeof statsOptions === 'string'
) {
// eslint-disable-next-line no-param-reassign
statsOptions = webpack.Stats.presetToOptions(statsOptions);
}
}

return statsOptions;
}

function done(stats) {
// We are now on valid state
// eslint-disable-next-line no-param-reassign
Expand All @@ -33,28 +52,14 @@ export default function setupHooks(context) {
logger.log('Compilation finished');

let statsOptions = compiler.compilers
? {
children: compiler.compilers.map((child) =>
// eslint-disable-next-line no-undefined
child.options ? child.options.stats : undefined
),
}
: compiler.options
? compiler.options.stats
: // eslint-disable-next-line no-undefined
undefined;

const statsForWebpack4 = webpack.Stats && webpack.Stats.presetToOptions;
? { children: compiler.compilers.map((child) => child.options.stats) }
: compiler.options.stats;

if (compiler.compilers) {
statsOptions.children = statsOptions.children.map(
(childStatsOptions) => {
if (statsForWebpack4) {
// eslint-disable-next-line no-param-reassign
childStatsOptions = webpack.Stats.presetToOptions(
childStatsOptions
);
}
// eslint-disable-next-line no-param-reassign
childStatsOptions = normalizeStatsOptions(childStatsOptions);

if (typeof childStatsOptions.colors === 'undefined') {
// eslint-disable-next-line no-param-reassign
Expand All @@ -64,15 +69,12 @@ export default function setupHooks(context) {
return childStatsOptions;
}
);
} else if (
typeof statsOptions.colors === 'undefined' ||
typeof statsOptions === 'string'
) {
if (statsForWebpack4) {
statsOptions = webpack.Stats.presetToOptions(statsOptions);
}
} else {
statsOptions = normalizeStatsOptions(statsOptions);

statsOptions.colors = Boolean(colorette.options.enabled);
if (typeof statsOptions.colors === 'undefined') {
statsOptions.colors = Boolean(colorette.options.enabled);
}
}

// TODO webpack@4 doesn't support `{ children: [{ colors: true }, { colors: true }] }` for stats
Expand Down
Loading

0 comments on commit acdfd4d

Please sign in to comment.