Skip to content

fix: compatibility with webpack@4 #816

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

Merged
merged 4 commits into from
Jan 13, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
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