Skip to content

Commit 34fadde

Browse files
feat: remove cjs wrapper and improve types format (#463)
1 parent d5146c9 commit 34fadde

File tree

9 files changed

+520
-145
lines changed

9 files changed

+520
-145
lines changed

package-lock.json

Lines changed: 350 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
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": ">= 10.13.0"
1818
},
1919
"scripts": {
2020
"clean": "del-cli dist types",
2121
"prebuild": "npm run clean",
22-
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write && prettier types --write",
22+
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write",
2323
"build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files",
2424
"build": "npm-run-all -p \"build:**\"",
2525
"commitlint": "commitlint --from=master",
@@ -91,6 +91,7 @@
9191
"typescript": "^4.3.5",
9292
"uglify-js": "^3.14.1",
9393
"webpack": "^5.48.0",
94+
"webpack-cli": "^4.9.1",
9495
"worker-loader": "^3.0.8"
9596
},
9697
"keywords": [

src/cjs.js

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

src/index.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
import * as path from "path";
2-
import * as os from "os";
1+
const path = require("path");
2+
const os = require("os");
33

4-
import { SourceMapConsumer } from "source-map";
5-
import { validate } from "schema-utils";
6-
import serialize from "serialize-javascript";
7-
import { Worker } from "jest-worker";
4+
const { SourceMapConsumer } = require("source-map");
5+
const { validate } = require("schema-utils");
6+
const serialize = require("serialize-javascript");
7+
const { Worker } = require("jest-worker");
88

9-
import {
9+
const {
1010
throttleAll,
1111
terserMinify,
1212
uglifyJsMinify,
1313
swcMinify,
1414
esbuildMinify,
15-
} from "./utils";
15+
} = require("./utils");
1616

17-
import * as schema from "./options.json";
18-
import { minify as minimize } from "./minify";
17+
const schema = require("./options.json");
18+
const { minify } = require("./minify");
1919

2020
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
2121
/** @typedef {import("webpack").Compiler} Compiler */
@@ -495,7 +495,7 @@ class TerserPlugin {
495495
try {
496496
output = await (getWorker
497497
? getWorker().transform(serialize(options))
498-
: minimize(options));
498+
: minify(options));
499499
} catch (error) {
500500
const hasSourceMap =
501501
inputSourceMap && TerserPlugin.isSourceMap(inputSourceMap);
@@ -866,4 +866,4 @@ TerserPlugin.uglifyJsMinify = uglifyJsMinify;
866866
TerserPlugin.swcMinify = swcMinify;
867867
TerserPlugin.esbuildMinify = esbuildMinify;
868868

869-
export default TerserPlugin;
869+
module.exports = TerserPlugin;

src/minify.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,4 @@ async function transform(options) {
4646
return minify(evaluatedOptions);
4747
}
4848

49-
module.exports.minify = minify;
50-
module.exports.transform = transform;
49+
module.exports = { minify, transform };

src/utils.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -720,4 +720,10 @@ esbuildMinify.getMinimizerVersion = () => {
720720
return packageJson && packageJson.version;
721721
};
722722

723-
export { throttleAll, terserMinify, uglifyJsMinify, swcMinify, esbuildMinify };
723+
module.exports = {
724+
throttleAll,
725+
terserMinify,
726+
uglifyJsMinify,
727+
swcMinify,
728+
esbuildMinify,
729+
};

test/cjs.test.js

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

types/cjs.d.ts

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

types/index.d.ts

Lines changed: 146 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,4 @@
1-
export default TerserPlugin;
2-
export type Schema = import("schema-utils/declarations/validate").Schema;
3-
export type Compiler = import("webpack").Compiler;
4-
export type Compilation = import("webpack").Compilation;
5-
export type WebpackError = import("webpack").WebpackError;
6-
export type Asset = import("webpack").Asset;
7-
export type TerserECMA = import("./utils.js").TerserECMA;
8-
export type TerserOptions = import("./utils.js").TerserOptions;
9-
export type JestWorker = import("jest-worker").Worker;
10-
export type RawSourceMap = import("source-map").RawSourceMap;
11-
export type Rule = RegExp | string;
12-
export type Rules = Rule[] | Rule;
13-
export type ExtractCommentsFunction = (
14-
astNode: any,
15-
comment: {
16-
value: string;
17-
type: "comment1" | "comment2" | "comment3" | "comment4";
18-
pos: number;
19-
line: number;
20-
col: number;
21-
}
22-
) => boolean;
23-
export type ExtractCommentsCondition =
24-
| boolean
25-
| "all"
26-
| "some"
27-
| RegExp
28-
| ExtractCommentsFunction;
29-
export type ExtractCommentsFilename = string | ((fileData: any) => string);
30-
export type ExtractCommentsBanner =
31-
| string
32-
| boolean
33-
| ((commentsFile: string) => string);
34-
export type ExtractCommentsObject = {
35-
condition?: ExtractCommentsCondition | undefined;
36-
filename?: ExtractCommentsFilename | undefined;
37-
banner?: ExtractCommentsBanner | undefined;
38-
};
39-
export type ExtractCommentsOptions =
40-
| ExtractCommentsCondition
41-
| ExtractCommentsObject;
42-
export type MinimizedResult = {
43-
code: string;
44-
map?: import("source-map").RawSourceMap | undefined;
45-
errors?: (string | Error)[] | undefined;
46-
warnings?: (string | Error)[] | undefined;
47-
extractedComments?: string[] | undefined;
48-
};
49-
export type Input = {
50-
[file: string]: string;
51-
};
52-
export type CustomOptions = {
53-
[key: string]: any;
54-
};
55-
export type InferDefaultType<T> = T extends infer U ? U : CustomOptions;
56-
export type PredefinedOptions = {
57-
module?: boolean | undefined;
58-
ecma?: any;
59-
};
60-
export type MinimizerOptions<T> = PredefinedOptions & InferDefaultType<T>;
61-
export type BasicMinimizerImplementation<T> = (
62-
input: Input,
63-
sourceMap: RawSourceMap | undefined,
64-
minifyOptions: MinimizerOptions<T>,
65-
extractComments: ExtractCommentsOptions | undefined
66-
) => Promise<MinimizedResult>;
67-
export type MinimizeFunctionHelpers = {
68-
getMinimizerVersion?: (() => string | undefined) | undefined;
69-
};
70-
export type MinimizerImplementation<T> = BasicMinimizerImplementation<T> &
71-
MinimizeFunctionHelpers;
72-
export type InternalOptions<T> = {
73-
name: string;
74-
input: string;
75-
inputSourceMap: RawSourceMap | undefined;
76-
extractComments: ExtractCommentsOptions | undefined;
77-
minimizer: {
78-
implementation: MinimizerImplementation<T>;
79-
options: MinimizerOptions<T>;
80-
};
81-
};
82-
export type MinimizerWorker<T> = Worker & {
83-
transform: (options: string) => MinimizedResult;
84-
minify: (options: InternalOptions<T>) => MinimizedResult;
85-
};
86-
export type Parallel = undefined | boolean | number;
87-
export type BasePluginOptions = {
88-
test?: Rules | undefined;
89-
include?: Rules | undefined;
90-
exclude?: Rules | undefined;
91-
extractComments?: ExtractCommentsOptions | undefined;
92-
parallel?: Parallel;
93-
};
94-
export type DefinedDefaultMinimizerAndOptions<T> = T extends TerserOptions
95-
? {
96-
minify?: MinimizerImplementation<T> | undefined;
97-
terserOptions?: MinimizerOptions<T> | undefined;
98-
}
99-
: {
100-
minify: MinimizerImplementation<T>;
101-
terserOptions?: MinimizerOptions<T> | undefined;
102-
};
103-
export type InternalPluginOptions<T> = BasePluginOptions & {
104-
minimizer: {
105-
implementation: MinimizerImplementation<T>;
106-
options: MinimizerOptions<T>;
107-
};
108-
};
1+
export = TerserPlugin;
1092
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
1103
/** @typedef {import("webpack").Compiler} Compiler */
1114
/** @typedef {import("webpack").Compilation} Compilation */
@@ -284,13 +177,153 @@ declare class TerserPlugin<T = import("terser").MinifyOptions> {
284177
apply(compiler: Compiler): void;
285178
}
286179
declare namespace TerserPlugin {
287-
export { terserMinify };
288-
export { uglifyJsMinify };
289-
export { swcMinify };
290-
export { esbuildMinify };
180+
export {
181+
terserMinify,
182+
uglifyJsMinify,
183+
swcMinify,
184+
esbuildMinify,
185+
Schema,
186+
Compiler,
187+
Compilation,
188+
WebpackError,
189+
Asset,
190+
TerserECMA,
191+
TerserOptions,
192+
JestWorker,
193+
RawSourceMap,
194+
Rule,
195+
Rules,
196+
ExtractCommentsFunction,
197+
ExtractCommentsCondition,
198+
ExtractCommentsFilename,
199+
ExtractCommentsBanner,
200+
ExtractCommentsObject,
201+
ExtractCommentsOptions,
202+
MinimizedResult,
203+
Input,
204+
CustomOptions,
205+
InferDefaultType,
206+
PredefinedOptions,
207+
MinimizerOptions,
208+
BasicMinimizerImplementation,
209+
MinimizeFunctionHelpers,
210+
MinimizerImplementation,
211+
InternalOptions,
212+
MinimizerWorker,
213+
Parallel,
214+
BasePluginOptions,
215+
DefinedDefaultMinimizerAndOptions,
216+
InternalPluginOptions,
217+
};
291218
}
292-
import { Worker } from "jest-worker";
219+
type Compiler = import("webpack").Compiler;
220+
type BasePluginOptions = {
221+
test?: Rules | undefined;
222+
include?: Rules | undefined;
223+
exclude?: Rules | undefined;
224+
extractComments?: ExtractCommentsOptions | undefined;
225+
parallel?: Parallel;
226+
};
227+
type DefinedDefaultMinimizerAndOptions<T> = T extends TerserOptions
228+
? {
229+
minify?: MinimizerImplementation<T> | undefined;
230+
terserOptions?: MinimizerOptions<T> | undefined;
231+
}
232+
: {
233+
minify: MinimizerImplementation<T>;
234+
terserOptions?: MinimizerOptions<T> | undefined;
235+
};
293236
import { terserMinify } from "./utils";
294237
import { uglifyJsMinify } from "./utils";
295238
import { swcMinify } from "./utils";
296239
import { esbuildMinify } from "./utils";
240+
type Schema = import("schema-utils/declarations/validate").Schema;
241+
type Compilation = import("webpack").Compilation;
242+
type WebpackError = import("webpack").WebpackError;
243+
type Asset = import("webpack").Asset;
244+
type TerserECMA = import("./utils.js").TerserECMA;
245+
type TerserOptions = import("./utils.js").TerserOptions;
246+
type JestWorker = import("jest-worker").Worker;
247+
type RawSourceMap = import("source-map").RawSourceMap;
248+
type Rule = RegExp | string;
249+
type Rules = Rule[] | Rule;
250+
type ExtractCommentsFunction = (
251+
astNode: any,
252+
comment: {
253+
value: string;
254+
type: "comment1" | "comment2" | "comment3" | "comment4";
255+
pos: number;
256+
line: number;
257+
col: number;
258+
}
259+
) => boolean;
260+
type ExtractCommentsCondition =
261+
| boolean
262+
| "all"
263+
| "some"
264+
| RegExp
265+
| ExtractCommentsFunction;
266+
type ExtractCommentsFilename = string | ((fileData: any) => string);
267+
type ExtractCommentsBanner =
268+
| string
269+
| boolean
270+
| ((commentsFile: string) => string);
271+
type ExtractCommentsObject = {
272+
condition?: ExtractCommentsCondition | undefined;
273+
filename?: ExtractCommentsFilename | undefined;
274+
banner?: ExtractCommentsBanner | undefined;
275+
};
276+
type ExtractCommentsOptions = ExtractCommentsCondition | ExtractCommentsObject;
277+
type MinimizedResult = {
278+
code: string;
279+
map?: import("source-map").RawSourceMap | undefined;
280+
errors?: (string | Error)[] | undefined;
281+
warnings?: (string | Error)[] | undefined;
282+
extractedComments?: string[] | undefined;
283+
};
284+
type Input = {
285+
[file: string]: string;
286+
};
287+
type CustomOptions = {
288+
[key: string]: any;
289+
};
290+
type InferDefaultType<T> = T extends infer U ? U : CustomOptions;
291+
type PredefinedOptions = {
292+
module?: boolean | undefined;
293+
ecma?: any;
294+
};
295+
type MinimizerOptions<T> = PredefinedOptions & InferDefaultType<T>;
296+
type BasicMinimizerImplementation<T> = (
297+
input: Input,
298+
sourceMap: RawSourceMap | undefined,
299+
minifyOptions: MinimizerOptions<T>,
300+
extractComments: ExtractCommentsOptions | undefined
301+
) => Promise<MinimizedResult>;
302+
type MinimizeFunctionHelpers = {
303+
getMinimizerVersion?: (() => string | undefined) | undefined;
304+
};
305+
type MinimizerImplementation<T> = BasicMinimizerImplementation<T> &
306+
MinimizeFunctionHelpers;
307+
type InternalOptions<T> = {
308+
name: string;
309+
input: string;
310+
inputSourceMap: RawSourceMap | undefined;
311+
extractComments: ExtractCommentsOptions | undefined;
312+
minimizer: {
313+
implementation: MinimizerImplementation<T>;
314+
options: MinimizerOptions<T>;
315+
};
316+
};
317+
type MinimizerWorker<T> = Worker & {
318+
transform: (options: string) => MinimizedResult;
319+
minify: (options: InternalOptions<T>) => MinimizedResult;
320+
};
321+
type Parallel = undefined | boolean | number;
322+
type InternalPluginOptions<T> = BasePluginOptions & {
323+
minimizer: {
324+
implementation: MinimizerImplementation<T>;
325+
options: MinimizerOptions<T>;
326+
};
327+
};
328+
import { minify } from "./minify";
329+
import { Worker } from "jest-worker";

0 commit comments

Comments
 (0)