Skip to content

Commit 8664d36

Browse files
feat: remove cjs wrapper and generate types in commonjs format (#277)
1 parent d863988 commit 8664d36

File tree

6 files changed

+111
-97
lines changed

6 files changed

+111
-97
lines changed

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
"type": "opencollective",
1212
"url": "https://opencollective.com/webpack"
1313
},
14-
"main": "dist/cjs.js",
15-
"types": "types/cjs.d.ts",
14+
"main": "dist/index.js",
15+
"types": "types/index.d.ts",
1616
"engines": {
1717
"node": ">= 12.13.0"
1818
},
1919
"scripts": {
2020
"start": "npm run build -- -w",
21-
"clean": "del-cli dist",
21+
"clean": "del-cli dist types",
2222
"prebuild": "npm run clean",
2323
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write && prettier types --write",
2424
"build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files",

src/cjs.js

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/index.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
Author Tobias Koppers @sokra
44
*/
55

6-
import path from "path";
7-
import crypto from "crypto";
6+
const path = require("path");
7+
const crypto = require("crypto");
88

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

12-
import schema from "./options.json";
12+
const schema = require("./options.json");
1313

1414
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
1515
/** @typedef {import("webpack").Compiler} Compiler */
@@ -432,4 +432,4 @@ class CompressionPlugin {
432432
}
433433
}
434434

435-
export default CompressionPlugin;
435+
module.exports = CompressionPlugin;

test/cjs.test.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

types/cjs.d.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

types/index.d.ts

Lines changed: 102 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,5 @@
11
/// <reference types="node" />
2-
export default CompressionPlugin;
3-
export type Schema = import("schema-utils/declarations/validate").Schema;
4-
export type Compiler = import("webpack").Compiler;
5-
export type WebpackPluginInstance = import("webpack").WebpackPluginInstance;
6-
export type Compilation = import("webpack").Compilation;
7-
export type Source = import("webpack").sources.Source;
8-
export type Asset = import("webpack").Asset;
9-
export type WebpackError = import("webpack").WebpackError;
10-
export type WithImplicitCoercion<T> =
11-
| T
12-
| {
13-
valueOf(): T;
14-
};
15-
export type Rule = RegExp | string;
16-
export type Rules = Rule[] | Rule;
17-
export type CustomOptions = {
18-
[key: string]: any;
19-
};
20-
export type InferDefaultType<T> = T extends infer U ? U : CustomOptions;
21-
export type CompressionOptions<T> = InferDefaultType<T>;
22-
export type AlgorithmFunction<T> = (
23-
input: Buffer,
24-
options: CompressionOptions<T>,
25-
callback: (
26-
error: Error | null | undefined,
27-
result:
28-
| string
29-
| ArrayBuffer
30-
| SharedArrayBuffer
31-
| Uint8Array
32-
| readonly number[]
33-
| {
34-
valueOf(): ArrayBuffer | SharedArrayBuffer;
35-
}
36-
| {
37-
valueOf(): string | Uint8Array | readonly number[];
38-
}
39-
| {
40-
valueOf(): string;
41-
}
42-
| {
43-
[Symbol.toPrimitive](hint: "string"): string;
44-
}
45-
) => void
46-
) => any;
47-
export type PathData = {
48-
[key: string]: any;
49-
};
50-
export type Filename = string | ((fileData: PathData) => string);
51-
export type DeleteOriginalAssets = boolean | "keep-source-map";
52-
export type BasePluginOptions<T> = {
53-
test?: Rules | undefined;
54-
include?: Rules | undefined;
55-
exclude?: Rules | undefined;
56-
threshold?: number | undefined;
57-
minRatio?: number | undefined;
58-
deleteOriginalAssets?: DeleteOriginalAssets | undefined;
59-
filename?: Filename | undefined;
60-
};
61-
export type ZlibOptions = import("zlib").ZlibOptions;
62-
export type DefinedDefaultAlgorithmAndOptions<T> = T extends ZlibOptions
63-
? {
64-
algorithm?: string | AlgorithmFunction<T> | undefined;
65-
compressionOptions?: CompressionOptions<T> | undefined;
66-
}
67-
: {
68-
algorithm: string | AlgorithmFunction<T>;
69-
compressionOptions?: CompressionOptions<T> | undefined;
70-
};
71-
export type InternalPluginOptions<T> = BasePluginOptions<T> & {
72-
algorithm: string | AlgorithmFunction<T>;
73-
compressionOptions: CompressionOptions<T>;
74-
threshold: number;
75-
minRatio: number;
76-
deleteOriginalAssets: DeleteOriginalAssets;
77-
filename: Filename;
78-
};
2+
export = CompressionPlugin;
793
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
804
/** @typedef {import("webpack").Compiler} Compiler */
815
/** @typedef {import("webpack").WebpackPluginInstance} WebpackPluginInstance */
@@ -183,3 +107,104 @@ declare class CompressionPlugin<T = import("zlib").ZlibOptions>
183107
*/
184108
apply(compiler: Compiler): void;
185109
}
110+
declare namespace CompressionPlugin {
111+
export {
112+
Schema,
113+
Compiler,
114+
WebpackPluginInstance,
115+
Compilation,
116+
Source,
117+
Asset,
118+
WebpackError,
119+
WithImplicitCoercion,
120+
Rule,
121+
Rules,
122+
CustomOptions,
123+
InferDefaultType,
124+
CompressionOptions,
125+
AlgorithmFunction,
126+
PathData,
127+
Filename,
128+
DeleteOriginalAssets,
129+
BasePluginOptions,
130+
ZlibOptions,
131+
DefinedDefaultAlgorithmAndOptions,
132+
InternalPluginOptions,
133+
};
134+
}
135+
type WebpackPluginInstance = import("webpack").WebpackPluginInstance;
136+
type Compiler = import("webpack").Compiler;
137+
type BasePluginOptions<T> = {
138+
test?: Rules | undefined;
139+
include?: Rules | undefined;
140+
exclude?: Rules | undefined;
141+
threshold?: number | undefined;
142+
minRatio?: number | undefined;
143+
deleteOriginalAssets?: DeleteOriginalAssets | undefined;
144+
filename?: Filename | undefined;
145+
};
146+
type DefinedDefaultAlgorithmAndOptions<T> = T extends ZlibOptions
147+
? {
148+
algorithm?: string | AlgorithmFunction<T> | undefined;
149+
compressionOptions?: CompressionOptions<T> | undefined;
150+
}
151+
: {
152+
algorithm: string | AlgorithmFunction<T>;
153+
compressionOptions?: CompressionOptions<T> | undefined;
154+
};
155+
type Schema = import("schema-utils/declarations/validate").Schema;
156+
type Compilation = import("webpack").Compilation;
157+
type Source = import("webpack").sources.Source;
158+
type Asset = import("webpack").Asset;
159+
type WebpackError = import("webpack").WebpackError;
160+
type WithImplicitCoercion<T> =
161+
| T
162+
| {
163+
valueOf(): T;
164+
};
165+
type Rule = RegExp | string;
166+
type Rules = Rule[] | Rule;
167+
type CustomOptions = {
168+
[key: string]: any;
169+
};
170+
type InferDefaultType<T> = T extends infer U ? U : CustomOptions;
171+
type CompressionOptions<T> = InferDefaultType<T>;
172+
type AlgorithmFunction<T> = (
173+
input: Buffer,
174+
options: CompressionOptions<T>,
175+
callback: (
176+
error: Error | null | undefined,
177+
result:
178+
| string
179+
| ArrayBuffer
180+
| SharedArrayBuffer
181+
| Uint8Array
182+
| readonly number[]
183+
| {
184+
valueOf(): ArrayBuffer | SharedArrayBuffer;
185+
}
186+
| {
187+
valueOf(): string | Uint8Array | readonly number[];
188+
}
189+
| {
190+
valueOf(): string;
191+
}
192+
| {
193+
[Symbol.toPrimitive](hint: "string"): string;
194+
}
195+
) => void
196+
) => any;
197+
type PathData = {
198+
[key: string]: any;
199+
};
200+
type Filename = string | ((fileData: PathData) => string);
201+
type DeleteOriginalAssets = boolean | "keep-source-map";
202+
type ZlibOptions = import("zlib").ZlibOptions;
203+
type InternalPluginOptions<T> = BasePluginOptions<T> & {
204+
algorithm: string | AlgorithmFunction<T>;
205+
compressionOptions: CompressionOptions<T>;
206+
threshold: number;
207+
minRatio: number;
208+
deleteOriginalAssets: DeleteOriginalAssets;
209+
filename: Filename;
210+
};

0 commit comments

Comments
 (0)