Skip to content

Commit

Permalink
feat: remove cjs wrapper and generate types in commonjs format (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Dec 16, 2021
1 parent d863988 commit 8664d36
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 97 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
"type": "opencollective",
"url": "https://opencollective.com/webpack"
},
"main": "dist/cjs.js",
"types": "types/cjs.d.ts",
"main": "dist/index.js",
"types": "types/index.d.ts",
"engines": {
"node": ">= 12.13.0"
},
"scripts": {
"start": "npm run build -- -w",
"clean": "del-cli dist",
"clean": "del-cli dist types",
"prebuild": "npm run clean",
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write && prettier types --write",
"build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files",
Expand Down
1 change: 0 additions & 1 deletion src/cjs.js

This file was deleted.

12 changes: 6 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
Author Tobias Koppers @sokra
*/

import path from "path";
import crypto from "crypto";
const path = require("path");
const crypto = require("crypto");

import { validate } from "schema-utils";
import serialize from "serialize-javascript";
const { validate } = require("schema-utils");
const serialize = require("serialize-javascript");

import schema from "./options.json";
const schema = require("./options.json");

/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
/** @typedef {import("webpack").Compiler} Compiler */
Expand Down Expand Up @@ -432,4 +432,4 @@ class CompressionPlugin {
}
}

export default CompressionPlugin;
module.exports = CompressionPlugin;
8 changes: 0 additions & 8 deletions test/cjs.test.js

This file was deleted.

2 changes: 0 additions & 2 deletions types/cjs.d.ts

This file was deleted.

179 changes: 102 additions & 77 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,81 +1,5 @@
/// <reference types="node" />
export default CompressionPlugin;
export type Schema = import("schema-utils/declarations/validate").Schema;
export type Compiler = import("webpack").Compiler;
export type WebpackPluginInstance = import("webpack").WebpackPluginInstance;
export type Compilation = import("webpack").Compilation;
export type Source = import("webpack").sources.Source;
export type Asset = import("webpack").Asset;
export type WebpackError = import("webpack").WebpackError;
export type WithImplicitCoercion<T> =
| T
| {
valueOf(): T;
};
export type Rule = RegExp | string;
export type Rules = Rule[] | Rule;
export type CustomOptions = {
[key: string]: any;
};
export type InferDefaultType<T> = T extends infer U ? U : CustomOptions;
export type CompressionOptions<T> = InferDefaultType<T>;
export type AlgorithmFunction<T> = (
input: Buffer,
options: CompressionOptions<T>,
callback: (
error: Error | null | undefined,
result:
| string
| ArrayBuffer
| SharedArrayBuffer
| Uint8Array
| readonly number[]
| {
valueOf(): ArrayBuffer | SharedArrayBuffer;
}
| {
valueOf(): string | Uint8Array | readonly number[];
}
| {
valueOf(): string;
}
| {
[Symbol.toPrimitive](hint: "string"): string;
}
) => void
) => any;
export type PathData = {
[key: string]: any;
};
export type Filename = string | ((fileData: PathData) => string);
export type DeleteOriginalAssets = boolean | "keep-source-map";
export type BasePluginOptions<T> = {
test?: Rules | undefined;
include?: Rules | undefined;
exclude?: Rules | undefined;
threshold?: number | undefined;
minRatio?: number | undefined;
deleteOriginalAssets?: DeleteOriginalAssets | undefined;
filename?: Filename | undefined;
};
export type ZlibOptions = import("zlib").ZlibOptions;
export type DefinedDefaultAlgorithmAndOptions<T> = T extends ZlibOptions
? {
algorithm?: string | AlgorithmFunction<T> | undefined;
compressionOptions?: CompressionOptions<T> | undefined;
}
: {
algorithm: string | AlgorithmFunction<T>;
compressionOptions?: CompressionOptions<T> | undefined;
};
export type InternalPluginOptions<T> = BasePluginOptions<T> & {
algorithm: string | AlgorithmFunction<T>;
compressionOptions: CompressionOptions<T>;
threshold: number;
minRatio: number;
deleteOriginalAssets: DeleteOriginalAssets;
filename: Filename;
};
export = CompressionPlugin;
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
/** @typedef {import("webpack").Compiler} Compiler */
/** @typedef {import("webpack").WebpackPluginInstance} WebpackPluginInstance */
Expand Down Expand Up @@ -183,3 +107,104 @@ declare class CompressionPlugin<T = import("zlib").ZlibOptions>
*/
apply(compiler: Compiler): void;
}
declare namespace CompressionPlugin {
export {
Schema,
Compiler,
WebpackPluginInstance,
Compilation,
Source,
Asset,
WebpackError,
WithImplicitCoercion,
Rule,
Rules,
CustomOptions,
InferDefaultType,
CompressionOptions,
AlgorithmFunction,
PathData,
Filename,
DeleteOriginalAssets,
BasePluginOptions,
ZlibOptions,
DefinedDefaultAlgorithmAndOptions,
InternalPluginOptions,
};
}
type WebpackPluginInstance = import("webpack").WebpackPluginInstance;
type Compiler = import("webpack").Compiler;
type BasePluginOptions<T> = {
test?: Rules | undefined;
include?: Rules | undefined;
exclude?: Rules | undefined;
threshold?: number | undefined;
minRatio?: number | undefined;
deleteOriginalAssets?: DeleteOriginalAssets | undefined;
filename?: Filename | undefined;
};
type DefinedDefaultAlgorithmAndOptions<T> = T extends ZlibOptions
? {
algorithm?: string | AlgorithmFunction<T> | undefined;
compressionOptions?: CompressionOptions<T> | undefined;
}
: {
algorithm: string | AlgorithmFunction<T>;
compressionOptions?: CompressionOptions<T> | undefined;
};
type Schema = import("schema-utils/declarations/validate").Schema;
type Compilation = import("webpack").Compilation;
type Source = import("webpack").sources.Source;
type Asset = import("webpack").Asset;
type WebpackError = import("webpack").WebpackError;
type WithImplicitCoercion<T> =
| T
| {
valueOf(): T;
};
type Rule = RegExp | string;
type Rules = Rule[] | Rule;
type CustomOptions = {
[key: string]: any;
};
type InferDefaultType<T> = T extends infer U ? U : CustomOptions;
type CompressionOptions<T> = InferDefaultType<T>;
type AlgorithmFunction<T> = (
input: Buffer,
options: CompressionOptions<T>,
callback: (
error: Error | null | undefined,
result:
| string
| ArrayBuffer
| SharedArrayBuffer
| Uint8Array
| readonly number[]
| {
valueOf(): ArrayBuffer | SharedArrayBuffer;
}
| {
valueOf(): string | Uint8Array | readonly number[];
}
| {
valueOf(): string;
}
| {
[Symbol.toPrimitive](hint: "string"): string;
}
) => void
) => any;
type PathData = {
[key: string]: any;
};
type Filename = string | ((fileData: PathData) => string);
type DeleteOriginalAssets = boolean | "keep-source-map";
type ZlibOptions = import("zlib").ZlibOptions;
type InternalPluginOptions<T> = BasePluginOptions<T> & {
algorithm: string | AlgorithmFunction<T>;
compressionOptions: CompressionOptions<T>;
threshold: number;
minRatio: number;
deleteOriginalAssets: DeleteOriginalAssets;
filename: Filename;
};

0 comments on commit 8664d36

Please sign in to comment.