@rsbuild/plugin-image-compress 1.2.0 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/dist/{shared/codecs.d.ts → codecs.d.ts} +2 -1
- package/dist/index.cjs +12 -3
- package/dist/index.js +25 -16
- package/dist/types.d.ts +2 -0
- package/dist/{shared/utils.d.ts → utils.d.ts} +1 -1
- package/package.json +10 -10
package/README.md
CHANGED
|
@@ -55,6 +55,7 @@ The plugin supports the following compressors:
|
|
|
55
55
|
- `ico`: For ICO images.
|
|
56
56
|
- `svg`: For SVG images.
|
|
57
57
|
- `avif`: For AVIF images.
|
|
58
|
+
- `webp`: For WEBP images.
|
|
58
59
|
|
|
59
60
|
Only SVG are compressed by `svgo`, other compressors are compressed by `@napi-rs/image`.
|
|
60
61
|
|
|
@@ -75,6 +76,8 @@ pluginImageCompress([
|
|
|
75
76
|
{ use: "png", minQuality: 50 },
|
|
76
77
|
// Options for @napi-rs/image `avif` method
|
|
77
78
|
{ use: "avif", quality: 80 },
|
|
79
|
+
// Options for @napi-rs/image `webp` method
|
|
80
|
+
{ use: "webp", quality: 80 },
|
|
78
81
|
// Options for svgo
|
|
79
82
|
{ use: 'svg', floatPrecision: 2 }
|
|
80
83
|
// No options yet
|
|
@@ -132,6 +135,11 @@ export default defineConfig({
|
|
|
132
135
|
test: /\.avif$/,
|
|
133
136
|
quality: 80,
|
|
134
137
|
}),
|
|
138
|
+
new ImageMinimizerPlugin({
|
|
139
|
+
use: "webp",
|
|
140
|
+
test: /\.webp$/,
|
|
141
|
+
quality: 80,
|
|
142
|
+
}),
|
|
135
143
|
new ImageMinimizerPlugin({
|
|
136
144
|
use: "svg",
|
|
137
145
|
test: /\.svg$/,
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import type { Codec, Codecs } from '
|
|
1
|
+
import type { Codec, Codecs } from './types.js';
|
|
2
2
|
export declare const jpegCodec: Codec<'jpeg'>;
|
|
3
3
|
export declare const pngCodec: Codec<'png'>;
|
|
4
4
|
export declare const pngLosslessCodec: Codec<'pngLossless'>;
|
|
5
5
|
export declare const icoCodec: Codec<'ico'>;
|
|
6
6
|
export declare const avifCodec: Codec<'avif'>;
|
|
7
|
+
export declare const webpCodec: Codec<'webp'>;
|
|
7
8
|
export declare const svgCodec: Codec<'svg'>;
|
|
8
9
|
declare const codecs: Record<Codecs, Codec<any>>;
|
|
9
10
|
export default codecs;
|
package/dist/index.cjs
CHANGED
|
@@ -34,8 +34,8 @@ var __webpack_exports__ = {};
|
|
|
34
34
|
__webpack_require__.r(__webpack_exports__);
|
|
35
35
|
__webpack_require__.d(__webpack_exports__, {
|
|
36
36
|
pluginImageCompress: ()=>pluginImageCompress,
|
|
37
|
-
DEFAULT_OPTIONS: ()=>DEFAULT_OPTIONS,
|
|
38
37
|
PLUGIN_IMAGE_COMPRESS_NAME: ()=>PLUGIN_IMAGE_COMPRESS_NAME,
|
|
38
|
+
DEFAULT_OPTIONS: ()=>DEFAULT_OPTIONS,
|
|
39
39
|
ImageMinimizerPlugin: ()=>ImageMinimizerPlugin
|
|
40
40
|
});
|
|
41
41
|
const external_node_assert_namespaceObject = require("node:assert");
|
|
@@ -84,6 +84,14 @@ const avifCodec = {
|
|
|
84
84
|
test: /\.avif$/
|
|
85
85
|
}
|
|
86
86
|
};
|
|
87
|
+
const webpCodec = {
|
|
88
|
+
async handler (buf, options) {
|
|
89
|
+
return new image_namespaceObject.Transformer(buf).webp(options.quality, options.signal);
|
|
90
|
+
},
|
|
91
|
+
defaultOptions: {
|
|
92
|
+
test: /\.webp$/
|
|
93
|
+
}
|
|
94
|
+
};
|
|
87
95
|
const svgCodec = {
|
|
88
96
|
async handler (buf, options) {
|
|
89
97
|
const result = external_svgo_default().optimize(buf.toString(), options);
|
|
@@ -99,7 +107,8 @@ const codecs_codecs = {
|
|
|
99
107
|
pngLossless: pngLosslessCodec,
|
|
100
108
|
ico: icoCodec,
|
|
101
109
|
svg: svgCodec,
|
|
102
|
-
avif: avifCodec
|
|
110
|
+
avif: avifCodec,
|
|
111
|
+
webp: webpCodec
|
|
103
112
|
};
|
|
104
113
|
const codecs = codecs_codecs;
|
|
105
114
|
function _define_property(obj, key, value) {
|
|
@@ -133,7 +142,7 @@ class ImageMinimizerPlugin {
|
|
|
133
142
|
};
|
|
134
143
|
const handleAsset = async (name)=>{
|
|
135
144
|
var _compilation_getAsset;
|
|
136
|
-
const info = null
|
|
145
|
+
const info = null == (_compilation_getAsset = compilation.getAsset(name)) ? void 0 : _compilation_getAsset.info;
|
|
137
146
|
const fileName = name.split('?')[0];
|
|
138
147
|
if ((null == info ? void 0 : info.minimized) || !matchObject(opts, fileName)) return;
|
|
139
148
|
const asset = compilation.getAsset(name);
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import node_assert from "node:assert";
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
import { Transformer, compressJpeg, losslessCompressPng, pngQuantize } from "@napi-rs/image";
|
|
4
|
+
import svgo from "svgo";
|
|
5
5
|
const jpegCodec = {
|
|
6
6
|
handler (buf, options) {
|
|
7
|
-
return
|
|
7
|
+
return compressJpeg(buf, options);
|
|
8
8
|
},
|
|
9
9
|
defaultOptions: {
|
|
10
10
|
test: /\.(?:jpg|jpeg)$/
|
|
@@ -12,7 +12,7 @@ const jpegCodec = {
|
|
|
12
12
|
};
|
|
13
13
|
const pngCodec = {
|
|
14
14
|
handler (buf, options) {
|
|
15
|
-
return
|
|
15
|
+
return pngQuantize(buf, options);
|
|
16
16
|
},
|
|
17
17
|
defaultOptions: {
|
|
18
18
|
test: /\.png$/
|
|
@@ -20,7 +20,7 @@ const pngCodec = {
|
|
|
20
20
|
};
|
|
21
21
|
const pngLosslessCodec = {
|
|
22
22
|
handler (buf, options) {
|
|
23
|
-
return
|
|
23
|
+
return losslessCompressPng(buf, options);
|
|
24
24
|
},
|
|
25
25
|
defaultOptions: {
|
|
26
26
|
test: /\.png$/
|
|
@@ -28,7 +28,7 @@ const pngLosslessCodec = {
|
|
|
28
28
|
};
|
|
29
29
|
const icoCodec = {
|
|
30
30
|
handler (buf) {
|
|
31
|
-
return new
|
|
31
|
+
return new Transformer(buf).ico();
|
|
32
32
|
},
|
|
33
33
|
defaultOptions: {
|
|
34
34
|
test: /\.(?:ico|icon)$/
|
|
@@ -36,16 +36,24 @@ const icoCodec = {
|
|
|
36
36
|
};
|
|
37
37
|
const avifCodec = {
|
|
38
38
|
handler (buf, options) {
|
|
39
|
-
return new
|
|
39
|
+
return new Transformer(buf).avif(options);
|
|
40
40
|
},
|
|
41
41
|
defaultOptions: {
|
|
42
42
|
test: /\.avif$/
|
|
43
43
|
}
|
|
44
44
|
};
|
|
45
|
+
const webpCodec = {
|
|
46
|
+
async handler (buf, options) {
|
|
47
|
+
return new Transformer(buf).webp(options.quality, options.signal);
|
|
48
|
+
},
|
|
49
|
+
defaultOptions: {
|
|
50
|
+
test: /\.webp$/
|
|
51
|
+
}
|
|
52
|
+
};
|
|
45
53
|
const svgCodec = {
|
|
46
54
|
async handler (buf, options) {
|
|
47
|
-
const result =
|
|
48
|
-
return
|
|
55
|
+
const result = svgo.optimize(buf.toString(), options);
|
|
56
|
+
return Buffer.from(result.data);
|
|
49
57
|
},
|
|
50
58
|
defaultOptions: {
|
|
51
59
|
test: /\.svg$/
|
|
@@ -57,7 +65,8 @@ const codecs_codecs = {
|
|
|
57
65
|
pngLossless: pngLosslessCodec,
|
|
58
66
|
ico: icoCodec,
|
|
59
67
|
svg: svgCodec,
|
|
60
|
-
avif: avifCodec
|
|
68
|
+
avif: avifCodec,
|
|
69
|
+
webp: webpCodec
|
|
61
70
|
};
|
|
62
71
|
const codecs = codecs_codecs;
|
|
63
72
|
function _define_property(obj, key, value) {
|
|
@@ -91,7 +100,7 @@ class ImageMinimizerPlugin {
|
|
|
91
100
|
};
|
|
92
101
|
const handleAsset = async (name)=>{
|
|
93
102
|
var _compilation_getAsset;
|
|
94
|
-
const info = null
|
|
103
|
+
const info = null == (_compilation_getAsset = compilation.getAsset(name)) ? void 0 : _compilation_getAsset.info;
|
|
95
104
|
const fileName = name.split('?')[0];
|
|
96
105
|
if ((null == info ? void 0 : info.minimized) || !matchObject(opts, fileName)) return;
|
|
97
106
|
const asset = compilation.getAsset(name);
|
|
@@ -103,7 +112,7 @@ class ImageMinimizerPlugin {
|
|
|
103
112
|
try {
|
|
104
113
|
if (!result) {
|
|
105
114
|
const input = inputSource.source();
|
|
106
|
-
const buf = await codec.handler('string' == typeof input ?
|
|
115
|
+
const buf = await codec.handler('string' == typeof input ? Buffer.from(input) : input, opts);
|
|
107
116
|
result = {
|
|
108
117
|
source: new RawSource(buf)
|
|
109
118
|
};
|
|
@@ -147,7 +156,7 @@ const withDefaultOptions = (opt)=>{
|
|
|
147
156
|
...defaultOptions,
|
|
148
157
|
...options
|
|
149
158
|
};
|
|
150
|
-
(
|
|
159
|
+
node_assert('test' in ret);
|
|
151
160
|
return ret;
|
|
152
161
|
};
|
|
153
162
|
const DEFAULT_OPTIONS = [
|
|
@@ -160,7 +169,7 @@ const castOptions = (args)=>{
|
|
|
160
169
|
if (Array.isArray(head)) return head;
|
|
161
170
|
const ret = [];
|
|
162
171
|
for (const arg of args){
|
|
163
|
-
(
|
|
172
|
+
node_assert(!Array.isArray(arg));
|
|
164
173
|
ret.push(arg);
|
|
165
174
|
}
|
|
166
175
|
return ret;
|
package/dist/types.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import type { Config as SvgoConfig } from 'svgo';
|
|
|
4
4
|
export type OneOrMany<T> = T | T[];
|
|
5
5
|
export interface WebpTransformOptions {
|
|
6
6
|
quality?: number;
|
|
7
|
+
signal?: AbortSignal;
|
|
7
8
|
}
|
|
8
9
|
export interface CodecBaseOptions {
|
|
9
10
|
jpeg: JpegCompressOptions;
|
|
@@ -12,6 +13,7 @@ export interface CodecBaseOptions {
|
|
|
12
13
|
ico: Record<string, unknown>;
|
|
13
14
|
svg: SvgoConfig;
|
|
14
15
|
avif: AvifConfig;
|
|
16
|
+
webp: WebpTransformOptions;
|
|
15
17
|
}
|
|
16
18
|
export interface BaseCompressOptions<T extends Codecs> {
|
|
17
19
|
use: T;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import type { FinalOptions, Options } from '
|
|
1
|
+
import type { FinalOptions, Options } from './types.js';
|
|
2
2
|
export declare const withDefaultOptions: (opt: Options) => FinalOptions;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsbuild/plugin-image-compress",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"repository": "https://github.com/rspack-contrib/rsbuild-plugin-image-compress",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -33,19 +33,19 @@
|
|
|
33
33
|
]
|
|
34
34
|
},
|
|
35
35
|
"dependencies": {
|
|
36
|
-
"@napi-rs/image": "^1.
|
|
36
|
+
"@napi-rs/image": "^1.11.1",
|
|
37
37
|
"svgo": "^3.3.2"
|
|
38
38
|
},
|
|
39
39
|
"devDependencies": {
|
|
40
40
|
"@biomejs/biome": "^1.9.4",
|
|
41
|
-
"@playwright/test": "^1.
|
|
42
|
-
"@rsbuild/core": "1.
|
|
43
|
-
"@rslib/core": "^0.
|
|
44
|
-
"@types/node": "^22.
|
|
41
|
+
"@playwright/test": "^1.54.1",
|
|
42
|
+
"@rsbuild/core": "1.4.12",
|
|
43
|
+
"@rslib/core": "^0.11.0",
|
|
44
|
+
"@types/node": "^22.17.0",
|
|
45
45
|
"nano-staged": "^0.8.0",
|
|
46
|
-
"playwright": "^1.
|
|
47
|
-
"simple-git-hooks": "^2.
|
|
48
|
-
"typescript": "^5.
|
|
46
|
+
"playwright": "^1.54.1",
|
|
47
|
+
"simple-git-hooks": "^2.13.1",
|
|
48
|
+
"typescript": "^5.9.2"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"@rsbuild/core": "1.x"
|
|
@@ -55,7 +55,7 @@
|
|
|
55
55
|
"optional": true
|
|
56
56
|
}
|
|
57
57
|
},
|
|
58
|
-
"packageManager": "pnpm@10.
|
|
58
|
+
"packageManager": "pnpm@10.14.0",
|
|
59
59
|
"publishConfig": {
|
|
60
60
|
"access": "public",
|
|
61
61
|
"registry": "https://registry.npmjs.org/"
|