Skip to content

Commit 97c21b7

Browse files
feat: added types
1 parent 47250cb commit 97c21b7

File tree

8 files changed

+1177
-948
lines changed

8 files changed

+1177
-948
lines changed

package-lock.json

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

package.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,22 @@
1212
"url": "https://opencollective.com/webpack"
1313
},
1414
"main": "dist/cjs.js",
15+
"types": "types/cjs.d.ts",
1516
"engines": {
1617
"node": ">= 12.13.0"
1718
},
1819
"scripts": {
1920
"start": "npm run build -- -w",
2021
"clean": "del-cli dist",
2122
"prebuild": "npm run clean",
22-
"build": "cross-env NODE_ENV=production babel src -d dist --copy-files",
23+
"build:types": "tsc --declaration --emitDeclarationOnly --outDir types && prettier \"types/**/*.ts\" --write",
24+
"build:code": "cross-env NODE_ENV=production babel src -d dist --copy-files",
25+
"build": "npm-run-all -p \"build:**\"",
2326
"commitlint": "commitlint --from=master",
2427
"security": "npm audit --security",
2528
"lint:prettier": "prettier \"{**/*,*}.{js,json,md,yml,css,ts}\" --list-different",
2629
"lint:js": "eslint --cache .",
30+
"lint:types": "tsc --pretty --noEmit",
2731
"lint": "npm-run-all -l -p \"lint:**\"",
2832
"test:only": "cross-env NODE_ENV=test jest",
2933
"test:watch": "npm run test:only -- --watch",
@@ -34,7 +38,8 @@
3438
"release": "standard-version"
3539
},
3640
"files": [
37-
"dist"
41+
"dist",
42+
"types"
3843
],
3944
"peerDependencies": {
4045
"webpack": "^5.1.0"
@@ -64,6 +69,7 @@
6469
"npm-run-all": "^4.1.5",
6570
"prettier": "^2.3.2",
6671
"standard-version": "^9.3.1",
72+
"typescript": "^4.5.2",
6773
"webpack": "^5.50.0"
6874
},
6975
"keywords": [

src/index.js

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,51 @@ import { validate } from "schema-utils";
33
import schema from "./options.json";
44
import { minify as minifyFn } from "./minify";
55

6+
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
7+
/** @typedef {import("webpack").Compiler} Compiler */
8+
/** @typedef {import("webpack").Compilation} Compilation */
9+
/** @typedef {import("webpack").Asset} Asset */
10+
/** @typedef {import("webpack").WebpackError} WebpackError */
11+
12+
/** @typedef {RegExp | string} Rule */
13+
14+
/** @typedef {Rule[] | Rule} Rules */
15+
16+
/**
17+
* @typedef {Object} JSONOptions
18+
* @property {(this: any, key: string, value: any) => any | (number | string)[] | null} [replacer]
19+
* @property {string | number} [space]
20+
*/
21+
22+
/**
23+
* @typedef {Object} BasePluginOptions
24+
* @property {Rules} [test]
25+
* @property {Rules} [include]
26+
* @property {Rules} [exclude]
27+
* @property {JSONOptions} [minimizerOptions]
28+
*/
29+
30+
/**
31+
* @typedef {Object} MinimizedResult
32+
* @property {string} code
33+
*/
34+
35+
/**
36+
* @typedef {Object} InternalOptions
37+
* @property {string} input
38+
* @property {JSONOptions} [minimizerOptions]
39+
*/
40+
41+
/**
42+
* @typedef {BasePluginOptions} InternalPluginOptions
43+
*/
44+
645
class JsonMinimizerPlugin {
46+
/**
47+
* @param {BasePluginOptions} [options]
48+
*/
749
constructor(options = {}) {
8-
validate(schema, options, {
50+
validate(/** @type {Schema} */ (schema), options, {
951
name: "Json Minimizer Plugin",
1052
baseDataPath: "options",
1153
});
@@ -17,6 +59,10 @@ class JsonMinimizerPlugin {
1759
exclude,
1860
} = options;
1961

62+
/**
63+
* @private
64+
* @type {InternalPluginOptions}
65+
*/
2066
this.options = {
2167
test,
2268
include,
@@ -25,18 +71,31 @@ class JsonMinimizerPlugin {
2571
};
2672
}
2773

74+
/**
75+
* @param {any} error
76+
* @param {string} file
77+
* @param {string} context
78+
* @returns {Error}
79+
*/
2880
static buildError(error, file, context) {
2981
return new Error(
3082
`"${file}" in "${context}" from Json Minimizer:\n${error}`
3183
);
3284
}
3385

86+
/**
87+
* @private
88+
* @param {Compiler} compiler
89+
* @param {Compilation} compilation
90+
* @param {Record<string, import("webpack").sources.Source>} assets
91+
* @returns {Promise<void>}
92+
*/
3493
async optimize(compiler, compilation, assets) {
3594
const cache = compilation.getCache("JsonMinimizerWebpackPlugin");
3695
const assetsForMinify = await Promise.all(
3796
Object.keys(assets)
3897
.filter((name) => {
39-
const { info } = compilation.getAsset(name);
98+
const { info } = /** @type {Asset} */ (compilation.getAsset(name));
4099

41100
// Skip double minimize assets from child compilation
42101
if (info.minimized) {
@@ -56,7 +115,9 @@ class JsonMinimizerPlugin {
56115
return true;
57116
})
58117
.map(async (name) => {
59-
const { info, source } = compilation.getAsset(name);
118+
const { info, source } = /** @type {Asset} */ (
119+
compilation.getAsset(name)
120+
);
60121

61122
const eTag = cache.getLazyHashedEtag(source);
62123
const cacheItem = cache.getItemCache(name, eTag);
@@ -86,17 +147,21 @@ class JsonMinimizerPlugin {
86147
input = input.toString();
87148
}
88149

150+
/**
151+
* @type {InternalOptions}
152+
*/
89153
const options = {
90154
input,
91-
minimizerOptions: { ...this.options.minimizerOptions },
92-
minify: this.options.minify,
155+
minimizerOptions: this.options.minimizerOptions,
93156
};
94157

95158
try {
96159
output = await minifyFn(options);
97160
} catch (error) {
98161
compilation.errors.push(
99-
JsonMinimizerPlugin.buildError(error, name, compiler.context)
162+
/** @type {WebpackError} */ (
163+
JsonMinimizerPlugin.buildError(error, name, compiler.context)
164+
)
100165
);
101166

102167
return;
@@ -120,6 +185,10 @@ class JsonMinimizerPlugin {
120185
Promise.all(scheduledTasks);
121186
}
122187

188+
/**
189+
* @param {Compiler} compiler
190+
* @returns {void}
191+
*/
123192
apply(compiler) {
124193
const pluginName = this.constructor.name;
125194

@@ -140,8 +209,11 @@ class JsonMinimizerPlugin {
140209
.tap(
141210
"json-minimizer-webpack-plugin",
142211
(minimized, { green, formatFlag }) =>
143-
// eslint-disable-next-line no-undefined
144-
minimized ? green(formatFlag("minimized")) : undefined
212+
minimized
213+
? /** @type {Function} */ (green)(
214+
/** @type {Function} */ (formatFlag)("minimized")
215+
)
216+
: ""
145217
);
146218
});
147219
});

src/minify.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
1+
/** @typedef {import("./index.js").InternalOptions} InternalOptions */
2+
/** @typedef {import("./index.js").JSONOptions} JSONOptions */
3+
/** @typedef {import("./index.js").MinimizedResult} MinimizedResult */
4+
15
const defaultMinimizerOptions = {
2-
replacer: null,
3-
space: null,
6+
// eslint-disable-next-line no-undefined
7+
replacer: undefined,
8+
// eslint-disable-next-line no-undefined
9+
space: undefined,
410
};
511

12+
/**
13+
* @param {InternalOptions} options
14+
* @returns {Promise<MinimizedResult>}
15+
*/
616
const minify = async (options) => {
717
const { input, minimizerOptions } = options;
818
const { replacer, space } = {

tsconfig.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
// "module": "nodenext",
5+
"moduleResolution": "node",
6+
"allowJs": true,
7+
"checkJs": true,
8+
"strict": true,
9+
"types": ["node"],
10+
"resolveJsonModule": true,
11+
"newLine": "LF",
12+
"allowSyntheticDefaultImports": true
13+
},
14+
"include": ["./src/**/*"]
15+
}

types/cjs.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
declare const _exports: typeof import("./index").default;
2+
export = _exports;

types/index.d.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
export default JsonMinimizerPlugin;
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 Asset = import("webpack").Asset;
6+
export type WebpackError = import("webpack").WebpackError;
7+
export type Rule = RegExp | string;
8+
export type Rules = Rule[] | Rule;
9+
export type JSONOptions = {
10+
replacer?:
11+
| ((this: any, key: string, value: any) => any | (number | string)[] | null)
12+
| undefined;
13+
space?: string | number | undefined;
14+
};
15+
export type BasePluginOptions = {
16+
test?: Rules | undefined;
17+
include?: Rules | undefined;
18+
exclude?: Rules | undefined;
19+
minimizerOptions?: JSONOptions | undefined;
20+
};
21+
export type MinimizedResult = {
22+
code: string;
23+
};
24+
export type InternalOptions = {
25+
input: string;
26+
minimizerOptions?: JSONOptions | undefined;
27+
};
28+
export type InternalPluginOptions = BasePluginOptions;
29+
/** @typedef {import("schema-utils/declarations/validate").Schema} Schema */
30+
/** @typedef {import("webpack").Compiler} Compiler */
31+
/** @typedef {import("webpack").Compilation} Compilation */
32+
/** @typedef {import("webpack").Asset} Asset */
33+
/** @typedef {import("webpack").WebpackError} WebpackError */
34+
/** @typedef {RegExp | string} Rule */
35+
/** @typedef {Rule[] | Rule} Rules */
36+
/**
37+
* @typedef {Object} JSONOptions
38+
* @property {(this: any, key: string, value: any) => any | (number | string)[] | null} [replacer]
39+
* @property {string | number} [space]
40+
*/
41+
/**
42+
* @typedef {Object} BasePluginOptions
43+
* @property {Rules} [test]
44+
* @property {Rules} [include]
45+
* @property {Rules} [exclude]
46+
* @property {JSONOptions} [minimizerOptions]
47+
*/
48+
/**
49+
* @typedef {Object} MinimizedResult
50+
* @property {string} code
51+
*/
52+
/**
53+
* @typedef {Object} InternalOptions
54+
* @property {string} input
55+
* @property {JSONOptions} [minimizerOptions]
56+
*/
57+
/**
58+
* @typedef {BasePluginOptions} InternalPluginOptions
59+
*/
60+
declare class JsonMinimizerPlugin {
61+
/**
62+
* @private
63+
* @param {any} error
64+
* @param {string} file
65+
* @param {string} context
66+
* @returns {Error}
67+
*/
68+
private static buildError;
69+
/**
70+
* @param {BasePluginOptions} [options]
71+
*/
72+
constructor(options?: BasePluginOptions | undefined);
73+
/**
74+
* @private
75+
* @type {InternalPluginOptions}
76+
*/
77+
private options;
78+
/**
79+
* @private
80+
* @param {Compiler} compiler
81+
* @param {Compilation} compilation
82+
* @param {Record<string, import("webpack").sources.Source>} assets
83+
* @returns {Promise<void>}
84+
*/
85+
private optimize;
86+
/**
87+
* @param {Compiler} compiler
88+
* @returns {void}
89+
*/
90+
apply(compiler: Compiler): void;
91+
}

types/minify.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export type InternalOptions = import("./index.js").InternalOptions;
2+
export type JSONOptions = import("./index.js").JSONOptions;
3+
export type MinimizedResult = import("./index.js").MinimizedResult;
4+
/**
5+
* @param {InternalOptions} options
6+
* @returns {Promise<MinimizedResult>}
7+
*/
8+
export function minify(options: InternalOptions): Promise<MinimizedResult>;

0 commit comments

Comments
 (0)