| 1 | "use strict";
|
|---|
| 2 |
|
|---|
| 3 | const webpack = require("webpack");
|
|---|
| 4 |
|
|---|
| 5 | const {
|
|---|
| 6 | isColorSupported
|
|---|
| 7 | } = require("colorette");
|
|---|
| 8 | /** @typedef {import("webpack").Configuration} Configuration */
|
|---|
| 9 |
|
|---|
| 10 | /** @typedef {import("webpack").Compiler} Compiler */
|
|---|
| 11 |
|
|---|
| 12 | /** @typedef {import("webpack").MultiCompiler} MultiCompiler */
|
|---|
| 13 |
|
|---|
| 14 | /** @typedef {import("webpack").Stats} Stats */
|
|---|
| 15 |
|
|---|
| 16 | /** @typedef {import("webpack").MultiStats} MultiStats */
|
|---|
| 17 |
|
|---|
| 18 | /** @typedef {import("../index.js").IncomingMessage} IncomingMessage */
|
|---|
| 19 |
|
|---|
| 20 | /** @typedef {import("../index.js").ServerResponse} ServerResponse */
|
|---|
| 21 |
|
|---|
| 22 | /** @typedef {Configuration["stats"]} StatsOptions */
|
|---|
| 23 |
|
|---|
| 24 | /** @typedef {{ children: Configuration["stats"][] }} MultiStatsOptions */
|
|---|
| 25 |
|
|---|
| 26 | /** @typedef {Exclude<Configuration["stats"], boolean | string | undefined>} NormalizedStatsOptions */
|
|---|
| 27 | // TODO remove `color` after dropping webpack v4
|
|---|
| 28 |
|
|---|
| 29 | /** @typedef {{ children: StatsOptions[], colors?: any }} MultiNormalizedStatsOptions */
|
|---|
| 30 |
|
|---|
| 31 | /**
|
|---|
| 32 | * @template {IncomingMessage} Request
|
|---|
| 33 | * @template {ServerResponse} Response
|
|---|
| 34 | * @param {import("../index.js").Context<Request, Response>} context
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 | function setupHooks(context) {
|
|---|
| 39 | function invalid() {
|
|---|
| 40 | if (context.state) {
|
|---|
| 41 | context.logger.log("Compilation starting...");
|
|---|
| 42 | } // We are now in invalid state
|
|---|
| 43 | // eslint-disable-next-line no-param-reassign
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | context.state = false; // eslint-disable-next-line no-param-reassign, no-undefined
|
|---|
| 47 |
|
|---|
| 48 | context.stats = undefined;
|
|---|
| 49 | } // @ts-ignore
|
|---|
| 50 |
|
|---|
| 51 |
|
|---|
| 52 | const statsForWebpack4 = webpack.Stats && webpack.Stats.presetToOptions;
|
|---|
| 53 | /**
|
|---|
| 54 | * @param {Configuration["stats"]} statsOptions
|
|---|
| 55 | * @returns {NormalizedStatsOptions}
|
|---|
| 56 | */
|
|---|
| 57 |
|
|---|
| 58 | function normalizeStatsOptions(statsOptions) {
|
|---|
| 59 | if (statsForWebpack4) {
|
|---|
| 60 | if (typeof statsOptions === "undefined") {
|
|---|
| 61 | // eslint-disable-next-line no-param-reassign
|
|---|
| 62 | statsOptions = {};
|
|---|
| 63 | } else if (typeof statsOptions === "boolean" || typeof statsOptions === "string") {
|
|---|
| 64 | // @ts-ignore
|
|---|
| 65 | // eslint-disable-next-line no-param-reassign
|
|---|
| 66 | statsOptions = webpack.Stats.presetToOptions(statsOptions);
|
|---|
| 67 | } // @ts-ignore
|
|---|
| 68 |
|
|---|
| 69 |
|
|---|
| 70 | return statsOptions;
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | if (typeof statsOptions === "undefined") {
|
|---|
| 74 | // eslint-disable-next-line no-param-reassign
|
|---|
| 75 | statsOptions = {
|
|---|
| 76 | preset: "normal"
|
|---|
| 77 | };
|
|---|
| 78 | } else if (typeof statsOptions === "boolean") {
|
|---|
| 79 | // eslint-disable-next-line no-param-reassign
|
|---|
| 80 | statsOptions = statsOptions ? {
|
|---|
| 81 | preset: "normal"
|
|---|
| 82 | } : {
|
|---|
| 83 | preset: "none"
|
|---|
| 84 | };
|
|---|
| 85 | } else if (typeof statsOptions === "string") {
|
|---|
| 86 | // eslint-disable-next-line no-param-reassign
|
|---|
| 87 | statsOptions = {
|
|---|
| 88 | preset: statsOptions
|
|---|
| 89 | };
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | return statsOptions;
|
|---|
| 93 | }
|
|---|
| 94 | /**
|
|---|
| 95 | * @param {Stats | MultiStats} stats
|
|---|
| 96 | */
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | function done(stats) {
|
|---|
| 100 | // We are now on valid state
|
|---|
| 101 | // eslint-disable-next-line no-param-reassign
|
|---|
| 102 | context.state = true; // eslint-disable-next-line no-param-reassign
|
|---|
| 103 |
|
|---|
| 104 | context.stats = stats; // Do the stuff in nextTick, because bundle may be invalidated if a change happened while compiling
|
|---|
| 105 |
|
|---|
| 106 | process.nextTick(() => {
|
|---|
| 107 | const {
|
|---|
| 108 | compiler,
|
|---|
| 109 | logger,
|
|---|
| 110 | options,
|
|---|
| 111 | state,
|
|---|
| 112 | callbacks
|
|---|
| 113 | } = context; // Check if still in valid state
|
|---|
| 114 |
|
|---|
| 115 | if (!state) {
|
|---|
| 116 | return;
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | logger.log("Compilation finished");
|
|---|
| 120 | const isMultiCompilerMode = Boolean(
|
|---|
| 121 | /** @type {MultiCompiler} */
|
|---|
| 122 | compiler.compilers);
|
|---|
| 123 | /**
|
|---|
| 124 | * @type {StatsOptions | MultiStatsOptions | NormalizedStatsOptions | MultiNormalizedStatsOptions}
|
|---|
| 125 | */
|
|---|
| 126 |
|
|---|
| 127 | let statsOptions;
|
|---|
| 128 |
|
|---|
| 129 | if (typeof options.stats !== "undefined") {
|
|---|
| 130 | statsOptions = isMultiCompilerMode ? {
|
|---|
| 131 | children:
|
|---|
| 132 | /** @type {MultiCompiler} */
|
|---|
| 133 | compiler.compilers.map(() => options.stats)
|
|---|
| 134 | } : options.stats;
|
|---|
| 135 | } else {
|
|---|
| 136 | statsOptions = isMultiCompilerMode ? {
|
|---|
| 137 | children:
|
|---|
| 138 | /** @type {MultiCompiler} */
|
|---|
| 139 | compiler.compilers.map(child => child.options.stats)
|
|---|
| 140 | } :
|
|---|
| 141 | /** @type {Compiler} */
|
|---|
| 142 | compiler.options.stats;
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | if (isMultiCompilerMode) {
|
|---|
| 146 | /** @type {MultiNormalizedStatsOptions} */
|
|---|
| 147 | statsOptions.children =
|
|---|
| 148 | /** @type {MultiStatsOptions} */
|
|---|
| 149 | statsOptions.children.map(
|
|---|
| 150 | /**
|
|---|
| 151 | * @param {StatsOptions} childStatsOptions
|
|---|
| 152 | * @return {NormalizedStatsOptions}
|
|---|
| 153 | */
|
|---|
| 154 | childStatsOptions => {
|
|---|
| 155 | // eslint-disable-next-line no-param-reassign
|
|---|
| 156 | childStatsOptions = normalizeStatsOptions(childStatsOptions);
|
|---|
| 157 |
|
|---|
| 158 | if (typeof childStatsOptions.colors === "undefined") {
|
|---|
| 159 | // eslint-disable-next-line no-param-reassign
|
|---|
| 160 | childStatsOptions.colors = isColorSupported;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | return childStatsOptions;
|
|---|
| 164 | });
|
|---|
| 165 | } else {
|
|---|
| 166 | /** @type {NormalizedStatsOptions} */
|
|---|
| 167 | statsOptions = normalizeStatsOptions(
|
|---|
| 168 | /** @type {StatsOptions} */
|
|---|
| 169 | statsOptions);
|
|---|
| 170 |
|
|---|
| 171 | if (typeof statsOptions.colors === "undefined") {
|
|---|
| 172 | statsOptions.colors = isColorSupported;
|
|---|
| 173 | }
|
|---|
| 174 | } // TODO webpack@4 doesn't support `{ children: [{ colors: true }, { colors: true }] }` for stats
|
|---|
| 175 |
|
|---|
| 176 |
|
|---|
| 177 | if (
|
|---|
| 178 | /** @type {MultiCompiler} */
|
|---|
| 179 | compiler.compilers && statsForWebpack4) {
|
|---|
| 180 | /** @type {MultiNormalizedStatsOptions} */
|
|---|
| 181 | statsOptions.colors =
|
|---|
| 182 | /** @type {MultiNormalizedStatsOptions} */
|
|---|
| 183 | statsOptions.children.some(
|
|---|
| 184 | /**
|
|---|
| 185 | * @param {StatsOptions} child
|
|---|
| 186 | */
|
|---|
| 187 | // @ts-ignore
|
|---|
| 188 | child => child.colors);
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | const printedStats = stats.toString(statsOptions); // Avoid extra empty line when `stats: 'none'`
|
|---|
| 192 |
|
|---|
| 193 | if (printedStats) {
|
|---|
| 194 | // eslint-disable-next-line no-console
|
|---|
| 195 | console.log(printedStats);
|
|---|
| 196 | } // eslint-disable-next-line no-param-reassign
|
|---|
| 197 |
|
|---|
| 198 |
|
|---|
| 199 | context.callbacks = []; // Execute callback that are delayed
|
|---|
| 200 |
|
|---|
| 201 | callbacks.forEach(
|
|---|
| 202 | /**
|
|---|
| 203 | * @param {(...args: any[]) => Stats | MultiStats} callback
|
|---|
| 204 | */
|
|---|
| 205 | callback => {
|
|---|
| 206 | callback(stats);
|
|---|
| 207 | });
|
|---|
| 208 | });
|
|---|
| 209 | }
|
|---|
| 210 |
|
|---|
| 211 | context.compiler.hooks.watchRun.tap("webpack-dev-middleware", invalid);
|
|---|
| 212 | context.compiler.hooks.invalid.tap("webpack-dev-middleware", invalid);
|
|---|
| 213 | context.compiler.hooks.done.tap("webpack-dev-middleware", done);
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | module.exports = setupHooks; |
|---|