Skip to content

Commit 0574539

Browse files
test: fix
1 parent 8ce49f3 commit 0574539

3 files changed

Lines changed: 140 additions & 54 deletions

File tree

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -481,8 +481,11 @@ instance.waitUntilValid(() => {
481481

482482
Creates middleware instance in plugin mode.
483483

484-
In plugin mode, stats output is written through custom code (i.e. in callback for `watch` or where you are calling `stats.toString(options)`)
485-
(`compiler.getInfrastructureLogger`) instead of `console.log`.
484+
In plugin mode, stats output is written through custom code (i.e. in callback for `watch` or where you are calling `stats.toString(options)`) instead of `console.log`.
485+
In this case, the `stats` option is not supported because `webpack-dev-middleware` does not have access to the code where the stats will be output.
486+
You will also need to manually run the `watch` method.
487+
488+
Why do you need this mode? In some cases, you may want to have multiple dev servers or run only one dev server when you have multiple configurations, and this is suitable for you.
486489

487490
```js
488491
const webpack = require("webpack");

test/__snapshots__/logging.test.js.snap.webpack5

Lines changed: 93 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -299,24 +299,56 @@ exports[`logging plugin should logging on successfully build using the "stats" o
299299
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with object value and no colors: stdout 1`] = `
300300
"asset bundle.js x KiB [emitted] (name: main)
301301
asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main)
302-
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)"
302+
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)
303+
runtime modules x KiB x modules
304+
cacheable modules x bytes
305+
./foo.js x bytes [built] [code generated]
306+
./svg.svg x bytes [built] [code generated]
307+
./index.html x bytes [built] [code generated]
308+
webpack x.x.x compiled successfully in x ms"
303309
`;
304310

305311
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with object value: stderr 1`] = `""`;
306312

307313
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with object value: stdout 1`] = `
308314
"asset bundle.js x KiB [emitted] (name: main)
309315
asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main)
310-
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)"
316+
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)
317+
runtime modules x KiB x modules
318+
cacheable modules x bytes
319+
./foo.js x bytes [built] [code generated]
320+
./svg.svg x bytes [built] [code generated]
321+
./index.html x bytes [built] [code generated]
322+
webpack x.x.x compiled successfully in x ms"
311323
`;
312324

313325
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with the "false" value: stderr 1`] = `""`;
314326

315-
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with the "false" value: stdout 1`] = `""`;
327+
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with the "false" value: stdout 1`] = `
328+
"asset bundle.js x KiB [emitted] (name: main)
329+
asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main)
330+
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)
331+
runtime modules x KiB x modules
332+
cacheable modules x bytes
333+
./foo.js x bytes [built] [code generated]
334+
./svg.svg x bytes [built] [code generated]
335+
./index.html x bytes [built] [code generated]
336+
webpack x.x.x compiled successfully in x ms"
337+
`;
316338

317339
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with the "none" value: stderr 1`] = `""`;
318340

319-
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with the "none" value: stdout 1`] = `""`;
341+
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with the "none" value: stdout 1`] = `
342+
"asset bundle.js x KiB [emitted] (name: main)
343+
asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main)
344+
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)
345+
runtime modules x KiB x modules
346+
cacheable modules x bytes
347+
./foo.js x bytes [built] [code generated]
348+
./svg.svg x bytes [built] [code generated]
349+
./index.html x bytes [built] [code generated]
350+
webpack x.x.x compiled successfully in x ms"
351+
`;
320352

321353
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with the "normal" value: stderr 1`] = `""`;
322354

@@ -349,52 +381,29 @@ webpack x.x.x compiled successfully in x ms"
349381
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with the "verbose" value: stderr 1`] = `""`;
350382

351383
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with the "verbose" value: stdout 1`] = `
352-
"PublicPath: auto
353-
asset bundle.js x KiB {main} [emitted] (name: main)
354-
asset svg.svg x KiB ({main}) [emitted] [from: svg.svg] (auxiliary name: main)
355-
asset index.html x bytes ({main}) [emitted] [from: index.html] (auxiliary name: main)
356-
Entrypoint main x KiB (x KiB) = bundle.js 2 auxiliary assets
357-
chunk {main} (runtime: main) bundle.js (xxxx) x bytes (xxxx) x KiB (xxxx) [entry] [rendered]
358-
> ./foo.js main
359-
runtime modules x KiB
360-
webpack/runtime/define property getters x bytes {main} [code generated]
361-
[no exports]
362-
[used exports unknown]
363-
webpack/runtime/global x bytes {main} [code generated]
364-
[no exports]
365-
[used exports unknown]
366-
webpack/runtime/hasOwnProperty shorthand x bytes {main} [code generated]
367-
[no exports]
368-
[used exports unknown]
369-
webpack/runtime/make namespace object x bytes {main} [code generated]
370-
[no exports]
371-
[used exports unknown]
372-
webpack/runtime/publicPath x KiB {main} [code generated]
373-
[no exports]
374-
[used exports unknown]
384+
"asset bundle.js x KiB [emitted] (name: main)
385+
asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main)
386+
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)
387+
runtime modules x KiB x modules
375388
cacheable modules x bytes
376-
./foo.js x bytes {main} [depth 0] [built] [code generated]
377-
[used exports unknown]
378-
entry ./foo.js main
379-
./index.html x bytes {main} [depth 1] [dependent] [built] [code generated]
380-
[exports: default]
381-
[used exports unknown]
382-
cjs require ./index.html [./foo.js] 4:0-23
383-
./svg.svg x bytes {main} [depth 1] [dependent] [built] [code generated]
384-
[exports: default]
385-
[used exports unknown]
386-
cjs require ./svg.svg [./foo.js] 3:0-20
387-
388-
389-
LOG from xxx"
389+
./foo.js x bytes [built] [code generated]
390+
./svg.svg x bytes [built] [code generated]
391+
./index.html x bytes [built] [code generated]
392+
webpack x.x.x compiled successfully in x ms"
390393
`;
391394

392395
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with the object value and colors: stderr 1`] = `""`;
393396

394397
exports[`logging plugin should logging on successfully build using the "stats" option for middleware with the object value and colors: stdout 1`] = `
395398
"asset bundle.js x KiB [emitted] (name: main)
396399
asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main)
397-
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)"
400+
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)
401+
runtime modules x KiB x modules
402+
cacheable modules x bytes
403+
./foo.js x bytes [built] [code generated]
404+
./svg.svg x bytes [built] [code generated]
405+
./index.html x bytes [built] [code generated]
406+
webpack x.x.x compiled successfully in x ms"
398407
`;
399408

400409
exports[`logging plugin should logging on successfully build when the 'stats' doesn't exist: stderr 1`] = `""`;
@@ -431,8 +440,16 @@ exports[`logging plugin should logging on successfully multi-compiler build usin
431440
"asset bundle.js x KiB [emitted] (name: main)
432441
asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main)
433442
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)
443+
runtime modules x bytes x modules
444+
cacheable modules x bytes
445+
./foo.js x bytes [built] [code generated]
446+
./svg.svg x bytes [built] [code generated]
447+
./index.html x bytes [built] [code generated]
448+
webpack x.x.x compiled successfully in x ms
434449

435-
asset bundle.js x KiB [emitted] (name: main)"
450+
asset bundle.js x KiB [emitted] (name: main)
451+
./bar.js x bytes [built] [code generated]
452+
webpack x.x.x compiled successfully in x ms"
436453
`;
437454

438455
exports[`logging plugin should logging on successfully multi-compiler build using the "stats" option for middleware with object value and no colors: stderr 1`] = `""`;
@@ -441,13 +458,35 @@ exports[`logging plugin should logging on successfully multi-compiler build usin
441458
"asset bundle.js x KiB [emitted] (name: main)
442459
asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main)
443460
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)
461+
runtime modules x bytes x modules
462+
cacheable modules x bytes
463+
./foo.js x bytes [built] [code generated]
464+
./svg.svg x bytes [built] [code generated]
465+
./index.html x bytes [built] [code generated]
466+
webpack x.x.x compiled successfully in x ms
444467

445-
asset bundle.js x KiB [emitted] (name: main)"
468+
asset bundle.js x KiB [emitted] (name: main)
469+
./bar.js x bytes [built] [code generated]
470+
webpack x.x.x compiled successfully in x ms"
446471
`;
447472

448473
exports[`logging plugin should logging on successfully multi-compiler build using the "stats" option for middleware with the "false" value: stderr 1`] = `""`;
449474

450-
exports[`logging plugin should logging on successfully multi-compiler build using the "stats" option for middleware with the "false" value: stdout 1`] = `""`;
475+
exports[`logging plugin should logging on successfully multi-compiler build using the "stats" option for middleware with the "false" value: stdout 1`] = `
476+
"asset bundle.js x KiB [emitted] (name: main)
477+
asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main)
478+
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)
479+
runtime modules x bytes x modules
480+
cacheable modules x bytes
481+
./foo.js x bytes [built] [code generated]
482+
./svg.svg x bytes [built] [code generated]
483+
./index.html x bytes [built] [code generated]
484+
webpack x.x.x compiled successfully in x ms
485+
486+
asset bundle.js x KiB [emitted] (name: main)
487+
./bar.js x bytes [built] [code generated]
488+
webpack x.x.x compiled successfully in x ms"
489+
`;
451490

452491
exports[`logging plugin should logging on successfully multi-compiler build using the "stats" option for middleware with the "normal" value: stderr 1`] = `""`;
453492

@@ -491,8 +530,16 @@ exports[`logging plugin should logging on successfully multi-compiler build usin
491530
"asset bundle.js x KiB [emitted] (name: main)
492531
asset svg.svg x KiB [emitted] [from: svg.svg] (auxiliary name: main)
493532
asset index.html x bytes [emitted] [from: index.html] (auxiliary name: main)
533+
runtime modules x bytes x modules
534+
cacheable modules x bytes
535+
./foo.js x bytes [built] [code generated]
536+
./svg.svg x bytes [built] [code generated]
537+
./index.html x bytes [built] [code generated]
538+
webpack x.x.x compiled successfully in x ms
494539

495-
asset bundle.js x KiB [emitted] (name: main)"
540+
asset bundle.js x KiB [emitted] (name: main)
541+
./bar.js x bytes [built] [code generated]
542+
webpack x.x.x compiled successfully in x ms"
496543
`;
497544

498545
exports[`logging plugin should logging on unsuccessful build in multi-compiler: stderr 1`] = `""`;

test/helpers/runner.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,11 @@ const handleStdin = (chunk) => {
133133
};
134134

135135
/**
136+
* @param {import("webpack").Compiler} compiler compiler
136137
* @param {import("webpack").StatsOptions} statsOptions stats options
137138
* @returns {{ preset: string }} normalized stats
138139
*/
139-
function normalizeStatsOptions(statsOptions) {
140+
function normalizeStatsOptions(compiler, statsOptions) {
140141
if (typeof statsOptions === "undefined") {
141142
statsOptions = { preset: "normal" };
142143
} else if (typeof statsOptions === "boolean") {
@@ -145,10 +146,28 @@ function normalizeStatsOptions(statsOptions) {
145146
statsOptions = { preset: statsOptions };
146147
}
147148

149+
if (typeof statsOptions.colors === "undefined") {
150+
statsOptions.colors = compiler.webpack.cli.isColorSupported();
151+
}
152+
153+
// Just for test, in the real world webpack-dev-middleware doesn't support stats options as a plugin
154+
switch (process.env.WEBPACK_DEV_MIDDLEWARE_STATS) {
155+
case "object_colors_true":
156+
statsOptions.colors = true;
157+
break;
158+
case "object_colors_false":
159+
statsOptions.colors = false;
160+
break;
161+
}
162+
148163
return statsOptions;
149164
}
150165

151-
if (isPlugin) {
166+
/**
167+
* @param {import("webpack").Configuration} config configuration
168+
* @returns {import("webpack").Configuration} configuration with the test plugin
169+
*/
170+
function addPlugin(config) {
152171
if (!config.plugins) config.plugins = [];
153172

154173
config.plugins.push({
@@ -171,19 +190,36 @@ if (isPlugin) {
171190
},
172191
});
173192

174-
const compiler = webpack(config);
193+
return config;
194+
}
195+
196+
if (isPlugin) {
197+
const isMultiCompiler = Array.isArray(config);
198+
199+
const compiler = webpack(
200+
isMultiCompiler ? config.map((item) => addPlugin(item)) : addPlugin(config),
201+
);
175202

176203
compiler.watch({}, (err, stats) => {
177204
if (err) {
178205
throw err;
179206
}
180207

181-
const statsOptions = normalizeStatsOptions(config.stats);
208+
if (process.env.WEBPACK_BREAK_WATCH) {
209+
const error = new Error("Watch error");
210+
error.code = "watch error";
182211

183-
if (typeof statsOptions.colors === "undefined") {
184-
statsOptions.colors = compiler.webpack.cli.isColorSupported();
212+
throw error;
185213
}
186214

215+
const statsOptions = isMultiCompiler
216+
? {
217+
children: config.map((config, idx) =>
218+
normalizeStatsOptions(compiler.compilers[idx], config.stats),
219+
),
220+
}
221+
: normalizeStatsOptions(compiler, config.stats);
222+
187223
process.stdout.write("compiled-for-tests");
188224
process.stdout.write(stats.toString(statsOptions));
189225
});

0 commit comments

Comments
 (0)