From d8f160f07ce4a9e81e72bd76d3054aa15a80ed69 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Sat, 1 Jan 2022 14:04:49 +0100 Subject: [PATCH 1/4] Allow returning null from compressors --- .../brotli/src/BrotliCompressor.js | 8 ++++-- .../compressors/gzip/src/GzipCompressor.js | 8 ++++-- .../core/src/requests/WriteBundleRequest.js | 26 ++++++++++--------- packages/core/plugin/src/PluginAPI.d.ts | 7 ++++- packages/core/plugin/src/PluginAPI.js | 2 +- packages/core/types/index.js | 2 +- 6 files changed, 34 insertions(+), 19 deletions(-) diff --git a/packages/compressors/brotli/src/BrotliCompressor.js b/packages/compressors/brotli/src/BrotliCompressor.js index b74771dcec5..320422ed5c3 100644 --- a/packages/compressors/brotli/src/BrotliCompressor.js +++ b/packages/compressors/brotli/src/BrotliCompressor.js @@ -1,9 +1,13 @@ -// @flow +// @flow strict-local import {Compressor} from '@parcel/plugin'; import zlib from 'zlib'; export default (new Compressor({ - compress({stream}) { + compress({options, stream}) { + if (options.mode !== 'production') { + return null; + } + return { stream: stream.pipe(zlib.createBrotliCompress()), type: 'br', diff --git a/packages/compressors/gzip/src/GzipCompressor.js b/packages/compressors/gzip/src/GzipCompressor.js index 4e99328f39d..9bd81889d76 100644 --- a/packages/compressors/gzip/src/GzipCompressor.js +++ b/packages/compressors/gzip/src/GzipCompressor.js @@ -1,9 +1,13 @@ -// @flow +// @flow strict-local import {Compressor} from '@parcel/plugin'; import zlib from 'zlib'; export default (new Compressor({ - compress({stream}) { + compress({options, stream}) { + if (options.mode !== 'production') { + return null; + } + return { stream: stream.pipe(zlib.createGzip()), type: 'gz', diff --git a/packages/core/core/src/requests/WriteBundleRequest.js b/packages/core/core/src/requests/WriteBundleRequest.js index b87e7a8afd3..07fdc488a15 100644 --- a/packages/core/core/src/requests/WriteBundleRequest.js +++ b/packages/core/core/src/requests/WriteBundleRequest.js @@ -248,19 +248,21 @@ async function runCompressor( logger: new PluginLogger({origin: compressor.name}), }); - await new Promise((resolve, reject) => - pipeline( - res.stream, - outputFS.createWriteStream( - filePath + (res.type != null ? '.' + res.type : ''), - writeOptions, + if (res != null) { + await new Promise((resolve, reject) => + pipeline( + res.stream, + outputFS.createWriteStream( + filePath + (res.type != null ? '.' + res.type : ''), + writeOptions, + ), + err => { + if (err) reject(err); + else resolve(); + }, ), - err => { - if (err) reject(err); - else resolve(); - }, - ), - ); + ); + } } catch (err) { throw new ThrowableDiagnostic({ diagnostic: errorToDiagnostic(err, { diff --git a/packages/core/plugin/src/PluginAPI.d.ts b/packages/core/plugin/src/PluginAPI.d.ts index 9bbb186aae7..05e8db27795 100644 --- a/packages/core/plugin/src/PluginAPI.d.ts +++ b/packages/core/plugin/src/PluginAPI.d.ts @@ -6,10 +6,11 @@ import type { Runtime as RuntimeOpts, Packager as PackagerOpts, Optimizer as OptimizerOpts, + Compressor as CompressorOpts, Reporter as ReporterOpts, Validator as ValidatorOpts, } from '@parcel/types'; - + export declare class Transformer { constructor(opts: TransformerOpts); } @@ -42,6 +43,10 @@ export declare class Optimizer { constructor(opts: OptimizerOpts); } +export declare class Compressor { + constructor(opts: CompressorOpts); +} + export declare class Reporter { constructor(opts: ReporterOpts); } diff --git a/packages/core/plugin/src/PluginAPI.js b/packages/core/plugin/src/PluginAPI.js index 077f32b6fef..07d7ea1a0e5 100644 --- a/packages/core/plugin/src/PluginAPI.js +++ b/packages/core/plugin/src/PluginAPI.js @@ -1,4 +1,4 @@ -// @flow +// @flow strict-local import type { Transformer as TransformerOpts, diff --git a/packages/core/types/index.js b/packages/core/types/index.js index 36e6af9fd42..f21c1d2d992 100644 --- a/packages/core/types/index.js +++ b/packages/core/types/index.js @@ -1614,7 +1614,7 @@ export type Compressor = {| stream: Readable, options: PluginOptions, logger: PluginLogger, - |}): Async<{| + |}): Async, From f77d99e0e52094b332b0155ee124193fa6a727f6 Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Sat, 1 Jan 2022 14:06:42 +0100 Subject: [PATCH 2/4] Escape more error messages --- packages/core/core/src/requests/EntryRequest.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/core/core/src/requests/EntryRequest.js b/packages/core/core/src/requests/EntryRequest.js index 0c3b75d64cb..2f4031e3315 100644 --- a/packages/core/core/src/requests/EntryRequest.js +++ b/packages/core/core/src/requests/EntryRequest.js @@ -105,7 +105,7 @@ async function assertFile( throw new ThrowableDiagnostic({ diagnostic: { origin: '@parcel/core', - message: `${path.relative(process.cwd(), source)} does not exist.`, + message: md`${path.relative(process.cwd(), source)} does not exist.`, codeFrames: [ { filePath: pkgFilePath, @@ -118,7 +118,7 @@ async function assertFile( }, ], hints: alternatives.map(r => { - return `Did you mean '__${r}__'?`; + return md`Did you mean '__${r}__'?`; }), }, }); @@ -129,7 +129,7 @@ async function assertFile( throw new ThrowableDiagnostic({ diagnostic: { origin: '@parcel/core', - message: `${path.relative(process.cwd(), source)} is not a file.`, + message: md`${path.relative(process.cwd(), source)} is not a file.`, codeFrames: [ { filePath: pkgFilePath, @@ -309,7 +309,7 @@ export class EntryResolver { throw new ThrowableDiagnostic({ diagnostic: { - message: `Unknown entry: ${entry}`, + message: md`Unknown entry: ${entry}`, }, }); } From 12280e56a7eabc2d8b991da30f7e6fba905f4e6b Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Sat, 1 Jan 2022 16:49:41 +0100 Subject: [PATCH 3/4] Fix tests and add new one --- packages/core/integration-tests/test/cache.js | 1 + .../core/integration-tests/test/compressors.js | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/core/integration-tests/test/cache.js b/packages/core/integration-tests/test/cache.js index de60bceb491..4ddfb121864 100644 --- a/packages/core/integration-tests/test/cache.js +++ b/packages/core/integration-tests/test/cache.js @@ -5454,6 +5454,7 @@ describe('cache', function () { }), ); }, + mode: 'production', }); let files = await outputFS.readdir(distDir); diff --git a/packages/core/integration-tests/test/compressors.js b/packages/core/integration-tests/test/compressors.js index e3de14e3a74..867cbe85b84 100644 --- a/packages/core/integration-tests/test/compressors.js +++ b/packages/core/integration-tests/test/compressors.js @@ -4,9 +4,18 @@ import zlib from 'zlib'; import {bundle, outputFS, distDir} from '@parcel/test-utils'; describe('compressors', function () { - it('should compress output with gzip and brotli', async function () { + it('should not compress output with gzip and brotli in development', async function () { await bundle(path.join(__dirname, 'integration/compressors/index.js')); + let output = await outputFS.readdir(distDir); + assert.deepEqual(output.sort(), ['index.js', 'index.js.map']); + }); + + it('should compress output with gzip and brotli', async function () { + await bundle(path.join(__dirname, 'integration/compressors/index.js'), { + mode: 'production', + }); + let output = await outputFS.readdir(distDir); assert.deepEqual(output.sort(), [ 'index.js', @@ -28,6 +37,9 @@ describe('compressors', function () { it('should be able to disable raw output', async function () { await bundle( path.join(__dirname, 'integration/compressors-disable-default/index.js'), + { + mode: 'production', + }, ); let output = await outputFS.readdir(distDir); From d1237d7fad932f023f8f083f371d21a1e0c2a4fb Mon Sep 17 00:00:00 2001 From: Niklas Mischkulnig <4586894+mischnic@users.noreply.github.com> Date: Sat, 1 Jan 2022 17:50:41 +0100 Subject: [PATCH 4/4] Fix failing unit tests on Windows --- packages/core/core/test/EntryRequest.test.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/core/core/test/EntryRequest.test.js b/packages/core/core/test/EntryRequest.test.js index 6b5ca40a6f7..d8f43932dcc 100644 --- a/packages/core/core/test/EntryRequest.test.js +++ b/packages/core/core/test/EntryRequest.test.js @@ -1,6 +1,7 @@ // @flow strict-local import assert from 'assert'; import path from 'path'; +import {md} from '@parcel/diagnostic'; import {inputFS as fs} from '@parcel/test-utils'; import {EntryResolver} from '../src/requests/EntryRequest'; import {DEFAULT_OPTIONS as _DEFAULT_OPTIONS} from './test-utils'; @@ -45,7 +46,7 @@ describe('EntryResolver', function () { diagnostics: [ { origin: '@parcel/core', - message: `${path.join( + message: md`${path.join( path.relative(fs.cwd(), INVALID_SOURCE_MISSING_FIXTURE_PATH), 'missing.js', )} does not exist.`, @@ -85,7 +86,7 @@ describe('EntryResolver', function () { diagnostics: [ { origin: '@parcel/core', - message: `${path.join( + message: md`${path.join( path.relative(fs.cwd(), INVALID_SOURCE_NOT_FILE_FIXTURE_PATH), 'src', )} is not a file.`, @@ -125,7 +126,7 @@ describe('EntryResolver', function () { diagnostics: [ { origin: '@parcel/core', - message: `${path.join( + message: md`${path.join( path.relative( fs.cwd(), INVALID_TARGET_SOURCE_MISSING_FIXTURE_PATH, @@ -169,7 +170,7 @@ describe('EntryResolver', function () { diagnostics: [ { origin: '@parcel/core', - message: `${path.join( + message: md`${path.join( path.relative( fs.cwd(), INVALID_TARGET_SOURCE_NOT_FILE_FIXTURE_PATH,