[6a3a178] | 1 | "use strict";
|
---|
| 2 |
|
---|
| 3 | Object.defineProperty(exports, "__esModule", {
|
---|
| 4 | value: true
|
---|
| 5 | });
|
---|
| 6 | exports.default = setupHooks;
|
---|
| 7 |
|
---|
| 8 | var _webpack = _interopRequireDefault(require("webpack"));
|
---|
| 9 |
|
---|
| 10 | var _colorette = _interopRequireDefault(require("colorette"));
|
---|
| 11 |
|
---|
| 12 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
---|
| 13 |
|
---|
| 14 | function setupHooks(context) {
|
---|
| 15 | function invalid() {
|
---|
| 16 | if (context.state) {
|
---|
| 17 | context.logger.log("Compilation starting...");
|
---|
| 18 | } // We are now in invalid state
|
---|
| 19 | // eslint-disable-next-line no-param-reassign
|
---|
| 20 |
|
---|
| 21 |
|
---|
| 22 | context.state = false; // eslint-disable-next-line no-param-reassign, no-undefined
|
---|
| 23 |
|
---|
| 24 | context.stats = undefined;
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | const statsForWebpack4 = _webpack.default.Stats && _webpack.default.Stats.presetToOptions;
|
---|
| 28 |
|
---|
| 29 | function normalizeStatsOptions(statsOptions) {
|
---|
| 30 | if (statsForWebpack4) {
|
---|
| 31 | if (typeof statsOptions === "undefined") {
|
---|
| 32 | // eslint-disable-next-line no-param-reassign
|
---|
| 33 | statsOptions = {};
|
---|
| 34 | } else if (typeof statsOptions === "boolean" || typeof statsOptions === "string") {
|
---|
| 35 | // eslint-disable-next-line no-param-reassign
|
---|
| 36 | statsOptions = _webpack.default.Stats.presetToOptions(statsOptions);
|
---|
| 37 | }
|
---|
| 38 |
|
---|
| 39 | return statsOptions;
|
---|
| 40 | }
|
---|
| 41 |
|
---|
| 42 | if (typeof statsOptions === "undefined") {
|
---|
| 43 | // eslint-disable-next-line no-param-reassign
|
---|
| 44 | statsOptions = {
|
---|
| 45 | preset: "normal"
|
---|
| 46 | };
|
---|
| 47 | } else if (typeof statsOptions === "boolean") {
|
---|
| 48 | // eslint-disable-next-line no-param-reassign
|
---|
| 49 | statsOptions = statsOptions ? {
|
---|
| 50 | preset: "normal"
|
---|
| 51 | } : {
|
---|
| 52 | preset: "none"
|
---|
| 53 | };
|
---|
| 54 | } else if (typeof statsOptions === "string") {
|
---|
| 55 | // eslint-disable-next-line no-param-reassign
|
---|
| 56 | statsOptions = {
|
---|
| 57 | preset: statsOptions
|
---|
| 58 | };
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | return statsOptions;
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | function done(stats) {
|
---|
| 65 | // We are now on valid state
|
---|
| 66 | // eslint-disable-next-line no-param-reassign
|
---|
| 67 | context.state = true; // eslint-disable-next-line no-param-reassign
|
---|
| 68 |
|
---|
| 69 | context.stats = stats; // Do the stuff in nextTick, because bundle may be invalidated if a change happened while compiling
|
---|
| 70 |
|
---|
| 71 | process.nextTick(() => {
|
---|
| 72 | const {
|
---|
| 73 | compiler,
|
---|
| 74 | logger,
|
---|
| 75 | options,
|
---|
| 76 | state,
|
---|
| 77 | callbacks
|
---|
| 78 | } = context; // Check if still in valid state
|
---|
| 79 |
|
---|
| 80 | if (!state) {
|
---|
| 81 | return;
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | logger.log("Compilation finished");
|
---|
| 85 | const isMultiCompilerMode = Boolean(compiler.compilers);
|
---|
| 86 | let statsOptions;
|
---|
| 87 |
|
---|
| 88 | if (typeof options.stats !== "undefined") {
|
---|
| 89 | statsOptions = isMultiCompilerMode ? {
|
---|
| 90 | children: compiler.compilers.map(() => options.stats)
|
---|
| 91 | } : options.stats;
|
---|
| 92 | } else {
|
---|
| 93 | statsOptions = isMultiCompilerMode ? {
|
---|
| 94 | children: compiler.compilers.map(child => child.options.stats)
|
---|
| 95 | } : compiler.options.stats;
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 | if (isMultiCompilerMode) {
|
---|
| 99 | statsOptions.children = statsOptions.children.map(childStatsOptions => {
|
---|
| 100 | // eslint-disable-next-line no-param-reassign
|
---|
| 101 | childStatsOptions = normalizeStatsOptions(childStatsOptions);
|
---|
| 102 |
|
---|
| 103 | if (typeof childStatsOptions.colors === "undefined") {
|
---|
| 104 | // eslint-disable-next-line no-param-reassign
|
---|
| 105 | childStatsOptions.colors = Boolean(_colorette.default.options.enabled);
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | return childStatsOptions;
|
---|
| 109 | });
|
---|
| 110 | } else {
|
---|
| 111 | statsOptions = normalizeStatsOptions(statsOptions);
|
---|
| 112 |
|
---|
| 113 | if (typeof statsOptions.colors === "undefined") {
|
---|
| 114 | statsOptions.colors = Boolean(_colorette.default.options.enabled);
|
---|
| 115 | }
|
---|
| 116 | } // TODO webpack@4 doesn't support `{ children: [{ colors: true }, { colors: true }] }` for stats
|
---|
| 117 |
|
---|
| 118 |
|
---|
| 119 | if (compiler.compilers && statsForWebpack4) {
|
---|
| 120 | statsOptions.colors = statsOptions.children.some(child => child.colors);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | const printedStats = stats.toString(statsOptions); // Avoid extra empty line when `stats: 'none'`
|
---|
| 124 |
|
---|
| 125 | if (printedStats) {
|
---|
| 126 | // eslint-disable-next-line no-console
|
---|
| 127 | console.log(printedStats);
|
---|
| 128 | } // eslint-disable-next-line no-param-reassign
|
---|
| 129 |
|
---|
| 130 |
|
---|
| 131 | context.callbacks = []; // Execute callback that are delayed
|
---|
| 132 |
|
---|
| 133 | callbacks.forEach(callback => {
|
---|
| 134 | callback(stats);
|
---|
| 135 | });
|
---|
| 136 | });
|
---|
| 137 | }
|
---|
| 138 |
|
---|
| 139 | context.compiler.hooks.watchRun.tap("webpack-dev-middleware", invalid);
|
---|
| 140 | context.compiler.hooks.invalid.tap("webpack-dev-middleware", invalid);
|
---|
| 141 | (context.compiler.webpack ? context.compiler.hooks.afterDone : context.compiler.hooks.done).tap("webpack-dev-middleware", done);
|
---|
| 142 | } |
---|