Skip to content

Commit

Permalink
fix: respect stdout and stderr of workers and do not create warnings (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
evilebottnawi committed Jan 30, 2020
1 parent 8d9837d commit 5708574
Show file tree
Hide file tree
Showing 12 changed files with 322 additions and 154 deletions.
13 changes: 11 additions & 2 deletions src/TaskRunner.js
Expand Up @@ -49,12 +49,21 @@ export default class TaskRunner {

this.worker = new Worker(workerPath, { numWorkers });

// Show syntax error from jest-worker
// https://github.com/facebook/jest/issues/8872#issuecomment-524822081
const workerStdout = this.worker.getStdout();

if (workerStdout) {
workerStdout.on('data', (chunk) => {
return process.stdout.write(chunk);
});
}

const workerStderr = this.worker.getStderr();

if (workerStderr) {
workerStderr.pipe(process.stderr);
workerStderr.on('data', (chunk) => {
return process.stderr.write(chunk);
});
}
}

Expand Down
131 changes: 131 additions & 0 deletions test/TerserPlugin.test.js
Expand Up @@ -10,6 +10,7 @@ import ChunkTemplate from 'webpack/lib/ChunkTemplate';
import TerserPlugin from '../src/index';

import {
BrokenCodePlugin,
compile,
countPlugins,
getCompiler,
Expand Down Expand Up @@ -438,4 +439,134 @@ describe('TerserPlugin', () => {
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should emit an error on a broken code in parallel mode', async () => {
const compiler = getCompiler({
entry: {
one: `${__dirname}/fixtures/entry.js`,
two: `${__dirname}/fixtures/entry.js`,
},
});

new BrokenCodePlugin().apply(compiler);

new TerserPlugin({ parallel: true }).apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should emit an error on a broken code in not parallel mode', async () => {
const compiler = getCompiler({
entry: {
one: `${__dirname}/fixtures/entry.js`,
two: `${__dirname}/fixtures/entry.js`,
},
});

new BrokenCodePlugin().apply(compiler);

new TerserPlugin({ parallel: false }).apply(compiler);

const stats = await compile(compiler);

expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');
});

it('should write stdout and stderr of workers to stdout and stderr of main process in parallel mode', async () => {
const { write: stdoutWrite } = process.stdout;
const { write: stderrWrite } = process.stderr;

let stdoutOutput = '';
let stderrOutput = '';

process.stdout.write = (str) => {
stdoutOutput += str;
};

process.stderr.write = (str) => {
stderrOutput += str;
};

const compiler = getCompiler({
entry: {
one: `${__dirname}/fixtures/empty.js`,
two: `${__dirname}/fixtures/empty.js`,
},
});

new TerserPlugin({
parallel: true,
minify: () => {
// eslint-disable-next-line no-console
process.stdout.write('stdout\n');
// eslint-disable-next-line no-console
process.stderr.write('stderr\n');

return { code: '' };
},
}).apply(compiler);

const stats = await compile(compiler);

expect(stdoutOutput).toMatchSnapshot('process stdout output');
expect(stderrOutput).toMatchSnapshot('process stderr output');
expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');

process.stdout.write = stdoutWrite;
process.stderr.write = stderrWrite;
});

it('should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode', async () => {
const { write: stdoutWrite } = process.stdout;
const { write: stderrWrite } = process.stderr;

let stdoutOutput = '';
let stderrOutput = '';

process.stdout.write = (str) => {
stdoutOutput += str;
};

process.stderr.write = (str) => {
stderrOutput += str;
};

const compiler = getCompiler({
entry: {
one: `${__dirname}/fixtures/empty.js`,
two: `${__dirname}/fixtures/empty.js`,
},
});

new TerserPlugin({
parallel: false,
minify: () => {
// eslint-disable-next-line no-console
process.stdout.write('stdout\n');
// eslint-disable-next-line no-console
process.stderr.write('stderr\n');

return { code: '' };
},
}).apply(compiler);

const stats = await compile(compiler);

expect(stdoutOutput).toMatchSnapshot('process stdout output');
expect(stderrOutput).toMatchSnapshot('process stderr output');
expect(readsAssets(compiler, stats)).toMatchSnapshot('assets');
expect(getErrors(stats)).toMatchSnapshot('errors');
expect(getWarnings(stats)).toMatchSnapshot('warnings');

process.stdout.write = stdoutWrite;
process.stderr.write = stderrWrite;
});
});
80 changes: 80 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack4
Expand Up @@ -68,6 +68,40 @@ exports[`TerserPlugin buildWarning method 5`] = `"Terser Plugin: Warning [http:/

exports[`TerserPlugin buildWarning method 6`] = `null`;

exports[`TerserPlugin should emit an error on a broken code in not parallel mode: assets 1`] = `
Object {
"broken.js": "\`Broken===",
"one.js": "!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\\"a\\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\\"\\",n(n.s=0)}([function(e,t){e.exports=function(){console.log(7)}}]);",
"two.js": "!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\\"a\\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\\"\\",n(n.s=0)}([function(e,t){e.exports=function(){console.log(7)}}]);",
}
`;

exports[`TerserPlugin should emit an error on a broken code in not parallel mode: errors 1`] = `
Array [
"Error: broken.js from Terser
Unterminated template [broken.js:1,0]",
]
`;

exports[`TerserPlugin should emit an error on a broken code in not parallel mode: warnings 1`] = `Array []`;

exports[`TerserPlugin should emit an error on a broken code in parallel mode: assets 1`] = `
Object {
"broken.js": "\`Broken===",
"one.js": "!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\\"a\\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\\"\\",n(n.s=0)}([function(e,t){e.exports=function(){console.log(7)}}]);",
"two.js": "!function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){\\"undefined\\"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:\\"Module\\"}),Object.defineProperty(e,\\"__esModule\\",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&\\"object\\"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,\\"default\\",{enumerable:!0,value:e}),2&t&&\\"string\\"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,\\"a\\",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p=\\"\\",n(n.s=0)}([function(e,t){e.exports=function(){console.log(7)}}]);",
}
`;

exports[`TerserPlugin should emit an error on a broken code in parallel mode: errors 1`] = `
Array [
"Error: broken.js from Terser
Unterminated template [broken.js:1,0]",
]
`;

exports[`TerserPlugin should emit an error on a broken code in parallel mode: warnings 1`] = `Array []`;

exports[`TerserPlugin should regenerate hash: assets 1`] = `
Object {
"4.4.f1a57a9ca1b30ec6dc5a.js": "(window.webpackJsonp=window.webpackJsonp||[]).push([[4],{4:function(n,p,s){\\"use strict\\";s.r(p),p.default=\\"async-dep\\"}}]);",
Expand Down Expand Up @@ -313,3 +347,49 @@ exports[`TerserPlugin should work in multi compiler mode: warnings 1`] = `Array
exports[`TerserPlugin should work in multi compiler mode: warnings 2`] = `Array []`;

exports[`TerserPlugin should work in multi compiler mode: warnings 3`] = `Array []`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: assets 1`] = `
Object {
"one.js": "",
"two.js": "",
}
`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: errors 1`] = `Array []`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: process stderr output 1`] = `
"stderr
stderr
"
`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: process stdout output 1`] = `
"stdout
stdout
"
`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: warnings 1`] = `Array []`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: assets 1`] = `
Object {
"one.js": "",
"two.js": "",
}
`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: errors 1`] = `Array []`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: process stderr output 1`] = `
"stderr
stderr
"
`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: process stdout output 1`] = `
"stdout
stdout
"
`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: warnings 1`] = `Array []`;
80 changes: 80 additions & 0 deletions test/__snapshots__/TerserPlugin.test.js.snap.webpack5
Expand Up @@ -68,6 +68,40 @@ exports[`TerserPlugin buildWarning method 5`] = `"Terser Plugin: Warning [http:/

exports[`TerserPlugin buildWarning method 6`] = `null`;

exports[`TerserPlugin should emit an error on a broken code in not parallel mode: assets 1`] = `
Object {
"broken.js": "\`Broken===",
"one.js": "(()=>{var r={791:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(791)})();",
"two.js": "(()=>{var r={791:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(791)})();",
}
`;

exports[`TerserPlugin should emit an error on a broken code in not parallel mode: errors 1`] = `
Array [
"Error: broken.js from Terser
Unterminated template [broken.js:1,0]",
]
`;

exports[`TerserPlugin should emit an error on a broken code in not parallel mode: warnings 1`] = `Array []`;

exports[`TerserPlugin should emit an error on a broken code in parallel mode: assets 1`] = `
Object {
"broken.js": "\`Broken===",
"one.js": "(()=>{var r={791:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(791)})();",
"two.js": "(()=>{var r={791:r=>{r.exports=function(){console.log(7)}}},o={};!function t(e){if(o[e])return o[e].exports;var n=o[e]={exports:{}};return r[e](n,n.exports,t),n.exports}(791)})();",
}
`;

exports[`TerserPlugin should emit an error on a broken code in parallel mode: errors 1`] = `
Array [
"Error: broken.js from Terser
Unterminated template [broken.js:1,0]",
]
`;

exports[`TerserPlugin should emit an error on a broken code in parallel mode: warnings 1`] = `Array []`;

exports[`TerserPlugin should regenerate hash: assets 1`] = `
Object {
"598.598.231e24cc7ba6b0dd72f5.js": "(window.webpackJsonp=window.webpackJsonp||[]).push([[598],{598:(s,n,p)=>{\\"use strict\\";p.r(n),p.d(n,{default:()=>w});const w=\\"async-dep\\"}}]);",
Expand Down Expand Up @@ -261,3 +295,49 @@ exports[`TerserPlugin should work in multi compiler mode: warnings 1`] = `Array
exports[`TerserPlugin should work in multi compiler mode: warnings 2`] = `Array []`;

exports[`TerserPlugin should work in multi compiler mode: warnings 3`] = `Array []`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: assets 1`] = `
Object {
"one.js": "",
"two.js": "",
}
`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: errors 1`] = `Array []`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: process stderr output 1`] = `
"stderr
stderr
"
`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: process stdout output 1`] = `
"stdout
stdout
"
`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in not parallel mode: warnings 1`] = `Array []`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: assets 1`] = `
Object {
"one.js": "",
"two.js": "",
}
`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: errors 1`] = `Array []`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: process stderr output 1`] = `
"stderr
stderr
"
`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: process stdout output 1`] = `
"stdout
stdout
"
`;

exports[`TerserPlugin should write stdout and stderr of workers to stdout and stderr of main process in parallel mode: warnings 1`] = `Array []`;
23 changes: 0 additions & 23 deletions test/__snapshots__/parallel-option-failure.test.js.snap.webpack4

This file was deleted.

12 changes: 0 additions & 12 deletions test/__snapshots__/parallel-option-failure.test.js.snap.webpack5

This file was deleted.

Empty file added test/fixtures/empty.js
Empty file.

0 comments on commit 5708574

Please sign in to comment.