Skip to content

Commit

Permalink
Don't run Gzip and Brotli compressors in development (#7510)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Jan 1, 2022
1 parent 8c43da4 commit 5d7af89
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 28 deletions.
8 changes: 6 additions & 2 deletions 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',
Expand Down
8 changes: 6 additions & 2 deletions 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',
Expand Down
8 changes: 4 additions & 4 deletions packages/core/core/src/requests/EntryRequest.js
Expand Up @@ -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,
Expand All @@ -118,7 +118,7 @@ async function assertFile(
},
],
hints: alternatives.map(r => {
return `Did you mean '__${r}__'?`;
return md`Did you mean '__${r}__'?`;
}),
},
});
Expand All @@ -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,
Expand Down Expand Up @@ -309,7 +309,7 @@ export class EntryResolver {

throw new ThrowableDiagnostic({
diagnostic: {
message: `Unknown entry: ${entry}`,
message: md`Unknown entry: ${entry}`,
},
});
}
Expand Down
26 changes: 14 additions & 12 deletions packages/core/core/src/requests/WriteBundleRequest.js
Expand Up @@ -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, {
Expand Down
9 changes: 5 additions & 4 deletions 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';
Expand Down Expand Up @@ -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.`,
Expand Down Expand Up @@ -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.`,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/core/integration-tests/test/cache.js
Expand Up @@ -5454,6 +5454,7 @@ describe('cache', function () {
}),
);
},
mode: 'production',
});

let files = await outputFS.readdir(distDir);
Expand Down
14 changes: 13 additions & 1 deletion packages/core/integration-tests/test/compressors.js
Expand Up @@ -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',
Expand All @@ -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);
Expand Down
7 changes: 6 additions & 1 deletion packages/core/plugin/src/PluginAPI.d.ts
Expand Up @@ -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<T> {
constructor(opts: TransformerOpts<T>);
}
Expand Down Expand Up @@ -42,6 +43,10 @@ export declare class Optimizer<T> {
constructor(opts: OptimizerOpts<T>);
}

export declare class Compressor {
constructor(opts: CompressorOpts);
}

export declare class Reporter {
constructor(opts: ReporterOpts);
}
2 changes: 1 addition & 1 deletion packages/core/plugin/src/PluginAPI.js
@@ -1,4 +1,4 @@
// @flow
// @flow strict-local

import type {
Transformer as TransformerOpts,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/types/index.js
Expand Up @@ -1614,7 +1614,7 @@ export type Compressor = {|
stream: Readable,
options: PluginOptions,
logger: PluginLogger,
|}): Async<{|
|}): Async<?{|
stream: Readable,
type?: string,
|}>,
Expand Down

0 comments on commit 5d7af89

Please sign in to comment.