123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- "use strict";
- const webpack = require("webpack");
- const {
- isColorSupported
- } = require("colorette");
- function setupHooks(context) {
- function invalid() {
- if (context.state) {
- context.logger.log("Compilation starting...");
- }
-
- context.state = false;
- context.stats = undefined;
- }
- const statsForWebpack4 = webpack.Stats && webpack.Stats.presetToOptions;
-
- function normalizeStatsOptions(statsOptions) {
- if (statsForWebpack4) {
- if (typeof statsOptions === "undefined") {
-
- statsOptions = {};
- } else if (typeof statsOptions === "boolean" || typeof statsOptions === "string") {
-
-
- statsOptions = webpack.Stats.presetToOptions(statsOptions);
- }
- return statsOptions;
- }
- if (typeof statsOptions === "undefined") {
-
- statsOptions = {
- preset: "normal"
- };
- } else if (typeof statsOptions === "boolean") {
-
- statsOptions = statsOptions ? {
- preset: "normal"
- } : {
- preset: "none"
- };
- } else if (typeof statsOptions === "string") {
-
- statsOptions = {
- preset: statsOptions
- };
- }
- return statsOptions;
- }
-
- function done(stats) {
-
-
- context.state = true;
- context.stats = stats;
- process.nextTick(() => {
- const {
- compiler,
- logger,
- options,
- state,
- callbacks
- } = context;
- if (!state) {
- return;
- }
- logger.log("Compilation finished");
- const isMultiCompilerMode = Boolean(
-
- compiler.compilers);
-
- let statsOptions;
- if (typeof options.stats !== "undefined") {
- statsOptions = isMultiCompilerMode ? {
- children:
-
- compiler.compilers.map(() => options.stats)
- } : options.stats;
- } else {
- statsOptions = isMultiCompilerMode ? {
- children:
-
- compiler.compilers.map(child => child.options.stats)
- } :
-
- compiler.options.stats;
- }
- if (isMultiCompilerMode) {
-
- statsOptions.children =
-
- statsOptions.children.map(
-
- childStatsOptions => {
-
- childStatsOptions = normalizeStatsOptions(childStatsOptions);
- if (typeof childStatsOptions.colors === "undefined") {
-
- childStatsOptions.colors = isColorSupported;
- }
- return childStatsOptions;
- });
- } else {
-
- statsOptions = normalizeStatsOptions(
-
- statsOptions);
- if (typeof statsOptions.colors === "undefined") {
- statsOptions.colors = isColorSupported;
- }
- }
- if (
-
- compiler.compilers && statsForWebpack4) {
-
- statsOptions.colors =
-
- statsOptions.children.some(
-
-
- child => child.colors);
- }
- const printedStats = stats.toString(statsOptions);
- if (printedStats) {
-
- console.log(printedStats);
- }
- context.callbacks = [];
- callbacks.forEach(
-
- callback => {
- callback(stats);
- });
- });
- }
- context.compiler.hooks.watchRun.tap("webpack-dev-middleware", invalid);
- context.compiler.hooks.invalid.tap("webpack-dev-middleware", invalid);
- context.compiler.hooks.done.tap("webpack-dev-middleware", done);
- }
- module.exports = setupHooks;
|