compression-webpack-plugin: Support custom algorithm

Had to bump the TypeScript version to detect correct option type based on `algorithm` argument.
This commit is contained in:
Rhys van der Waerden 2018-11-19 11:51:13 +11:00
parent 55f32e2382
commit ceee1391c7
2 changed files with 75 additions and 15 deletions

View File

@ -1,7 +1,9 @@
import { Configuration } from 'webpack';
import CompressionPlugin = require('compression-webpack-plugin');
const c: Configuration = {
new CompressionPlugin();
const config: Configuration = {
plugins: [
new CompressionPlugin({
asset: "[path].gz[query]",
@ -13,3 +15,52 @@ const c: Configuration = {
})
]
};
const configDefaultAlgo = new CompressionPlugin({
compressionOptions: { level: 7 }
});
const zlib: Configuration = {
plugins: [
new CompressionPlugin({
algorithm: "deflate",
compressionOptions: {
flush: 5,
windowBits: 20,
level: 7
}
})
]
};
const badZlib: Configuration = {
plugins: [
// $ExpectError
new CompressionPlugin({
algorithm: "deflate",
compressionOptions: 5
})
]
};
function customAlgorithm(input: string, options: number, callback: (err: Error, result: Buffer) => void) {
}
const custom: Configuration = {
plugins: [
new CompressionPlugin({
algorithm: customAlgorithm,
compressionOptions: 5
})
]
};
const badCustom: Configuration = {
plugins: [
// $ExpectError
new CompressionPlugin({
algorithm: customAlgorithm,
compressionOptions: { flush: 5 }
})
]
};

View File

@ -2,33 +2,42 @@
// Project: https://github.com/webpack-contrib/compression-webpack-plugin
// Definitions by: Anton Kandybo <https://github.com/dublicator>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
// TypeScript Version: 2.4
import { Plugin } from 'webpack';
import { ZlibOptions as ZlibCompressionOptions } from 'zlib';
export = CompressionPlugin;
declare class CompressionPlugin extends Plugin {
constructor(options?: CompressionPlugin.Options);
declare class CompressionPlugin<O = any> extends Plugin {
constructor(options?: CompressionPlugin.Options<O>);
}
declare namespace CompressionPlugin {
interface Options {
type AlgorithmCallback = (error: Error | null, result: Buffer) => void;
type Algorithm<O> = (source: string, options: O, callback: AlgorithmCallback) => void;
// NOTE: These are the compression algorithms exported by zlib.
type ZlibAlgorithm = 'deflate' | 'deflateRaw' | 'gzip';
interface BaseOptions {
asset?: string;
algorithm?: string;
cache?: boolean | string;
test?: RegExp | RegExp[];
regExp?: RegExp | RegExp[];
threshold?: number;
minRatio?: number;
// zlib options
level?: number;
flush?: number;
chunkSize?: number;
windowBits?: number;
memLevel?: number;
strategy?: number;
dictionary?: any;
}
interface ZlibOptions extends BaseOptions {
algorithm?: ZlibAlgorithm;
compressionOptions?: ZlibCompressionOptions;
}
interface CustomOptions<O> extends BaseOptions {
algorithm: Algorithm<O>;
compressionOptions?: O;
}
type Options<O> = ZlibOptions | CustomOptions<O>;
}