@rsbuild/plugin-image-compress 1.1.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 +56 -0
- package/dist/{shared/codecs.d.ts → codecs.d.ts} +2 -1
- package/dist/index.cjs +38 -41
- package/dist/index.d.ts +2 -0
- package/dist/index.js +31 -28
- 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
|
@@ -31,6 +31,8 @@ export default {
|
|
|
31
31
|
};
|
|
32
32
|
```
|
|
33
33
|
|
|
34
|
+
This plugin is compatible with both Rsbuild and Rspack. If you are using Rspack instead of Rsbuild, you can import the `ImageMinimizerPlugin` from the package, see [Rspack Usage](#rspack-usage).
|
|
35
|
+
|
|
34
36
|
## Default Compressors
|
|
35
37
|
|
|
36
38
|
By default, the plugin will enable `jpeg`, `png`, `ico` image compressors, which are equivalent to the following two examples:
|
|
@@ -53,6 +55,7 @@ The plugin supports the following compressors:
|
|
|
53
55
|
- `ico`: For ICO images.
|
|
54
56
|
- `svg`: For SVG images.
|
|
55
57
|
- `avif`: For AVIF images.
|
|
58
|
+
- `webp`: For WEBP images.
|
|
56
59
|
|
|
57
60
|
Only SVG are compressed by `svgo`, other compressors are compressed by `@napi-rs/image`.
|
|
58
61
|
|
|
@@ -73,6 +76,8 @@ pluginImageCompress([
|
|
|
73
76
|
{ use: "png", minQuality: 50 },
|
|
74
77
|
// Options for @napi-rs/image `avif` method
|
|
75
78
|
{ use: "avif", quality: 80 },
|
|
79
|
+
// Options for @napi-rs/image `webp` method
|
|
80
|
+
{ use: "webp", quality: 80 },
|
|
76
81
|
// Options for svgo
|
|
77
82
|
{ use: 'svg', floatPrecision: 2 }
|
|
78
83
|
// No options yet
|
|
@@ -98,6 +103,57 @@ For example, the `png` compressor will take precedence over the `pngLossless` co
|
|
|
98
103
|
pluginImageCompress(["jpeg", "pngLossless", "ico", "png"]);
|
|
99
104
|
```
|
|
100
105
|
|
|
106
|
+
## Rspack Usage
|
|
107
|
+
|
|
108
|
+
The plugin is also compatible with Rspack.
|
|
109
|
+
|
|
110
|
+
If you are using Rspack instead of Rsbuild, you can import the `ImageMinimizerPlugin` from the package, use it in the [optimization.minimizer](https://rspack.dev/config/optimization#optimizationminimizer) array.
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
// rspack.config.mjs
|
|
114
|
+
import { ImageMinimizerPlugin } from "@rsbuild/plugin-image-compress";
|
|
115
|
+
import { defineConfig } from "@rspack/cli";
|
|
116
|
+
|
|
117
|
+
export default defineConfig({
|
|
118
|
+
mode: process.env.NODE_ENV === "production" ? "production" : "development",
|
|
119
|
+
optimization: {
|
|
120
|
+
minimizer: [
|
|
121
|
+
// Use `...` to preserve the default JS and CSS minimizers of Rspack
|
|
122
|
+
'...',
|
|
123
|
+
// Add the image minimizer plugins
|
|
124
|
+
new ImageMinimizerPlugin({
|
|
125
|
+
use: "jpeg",
|
|
126
|
+
test: /\.(?:jpg|jpeg)$/,
|
|
127
|
+
}),
|
|
128
|
+
new ImageMinimizerPlugin({
|
|
129
|
+
use: "png",
|
|
130
|
+
test: /\.png$/,
|
|
131
|
+
maxQuality: 50,
|
|
132
|
+
}),
|
|
133
|
+
new ImageMinimizerPlugin({
|
|
134
|
+
use: "avif",
|
|
135
|
+
test: /\.avif$/,
|
|
136
|
+
quality: 80,
|
|
137
|
+
}),
|
|
138
|
+
new ImageMinimizerPlugin({
|
|
139
|
+
use: "webp",
|
|
140
|
+
test: /\.webp$/,
|
|
141
|
+
quality: 80,
|
|
142
|
+
}),
|
|
143
|
+
new ImageMinimizerPlugin({
|
|
144
|
+
use: "svg",
|
|
145
|
+
test: /\.svg$/,
|
|
146
|
+
floatPrecision: 2,
|
|
147
|
+
}),
|
|
148
|
+
new ImageMinimizerPlugin({
|
|
149
|
+
use: "ico",
|
|
150
|
+
test: /\.(?:ico|icon)$/,
|
|
151
|
+
}),
|
|
152
|
+
],
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
```
|
|
156
|
+
|
|
101
157
|
## License
|
|
102
158
|
|
|
103
159
|
[MIT](./LICENSE).
|
|
@@ -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
|
@@ -1,40 +1,27 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
// The require scope
|
|
3
2
|
var __webpack_require__ = {};
|
|
4
|
-
/************************************************************************/ // webpack/runtime/compat_get_default_export
|
|
5
3
|
(()=>{
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
var getter = module && module.__esModule ? function() {
|
|
9
|
-
return module['default'];
|
|
10
|
-
} : function() {
|
|
11
|
-
return module;
|
|
12
|
-
};
|
|
4
|
+
__webpack_require__.n = (module)=>{
|
|
5
|
+
var getter = module && module.__esModule ? ()=>module['default'] : ()=>module;
|
|
13
6
|
__webpack_require__.d(getter, {
|
|
14
7
|
a: getter
|
|
15
8
|
});
|
|
16
9
|
return getter;
|
|
17
10
|
};
|
|
18
11
|
})();
|
|
19
|
-
// webpack/runtime/define_property_getters
|
|
20
12
|
(()=>{
|
|
21
|
-
__webpack_require__.d =
|
|
13
|
+
__webpack_require__.d = (exports1, definition)=>{
|
|
22
14
|
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
|
23
15
|
enumerable: true,
|
|
24
16
|
get: definition[key]
|
|
25
17
|
});
|
|
26
18
|
};
|
|
27
19
|
})();
|
|
28
|
-
// webpack/runtime/has_own_property
|
|
29
20
|
(()=>{
|
|
30
|
-
__webpack_require__.o =
|
|
31
|
-
return Object.prototype.hasOwnProperty.call(obj, prop);
|
|
32
|
-
};
|
|
21
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
|
33
22
|
})();
|
|
34
|
-
// webpack/runtime/make_namespace_object
|
|
35
23
|
(()=>{
|
|
36
|
-
|
|
37
|
-
__webpack_require__.r = function(exports1) {
|
|
24
|
+
__webpack_require__.r = (exports1)=>{
|
|
38
25
|
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
|
39
26
|
value: 'Module'
|
|
40
27
|
});
|
|
@@ -43,14 +30,13 @@ var __webpack_require__ = {};
|
|
|
43
30
|
});
|
|
44
31
|
};
|
|
45
32
|
})();
|
|
46
|
-
|
|
47
|
-
// ESM COMPAT FLAG
|
|
33
|
+
var __webpack_exports__ = {};
|
|
48
34
|
__webpack_require__.r(__webpack_exports__);
|
|
49
|
-
// EXPORTS
|
|
50
35
|
__webpack_require__.d(__webpack_exports__, {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
36
|
+
pluginImageCompress: ()=>pluginImageCompress,
|
|
37
|
+
PLUGIN_IMAGE_COMPRESS_NAME: ()=>PLUGIN_IMAGE_COMPRESS_NAME,
|
|
38
|
+
DEFAULT_OPTIONS: ()=>DEFAULT_OPTIONS,
|
|
39
|
+
ImageMinimizerPlugin: ()=>ImageMinimizerPlugin
|
|
54
40
|
});
|
|
55
41
|
const external_node_assert_namespaceObject = require("node:assert");
|
|
56
42
|
var external_node_assert_default = /*#__PURE__*/ __webpack_require__.n(external_node_assert_namespaceObject);
|
|
@@ -98,6 +84,14 @@ const avifCodec = {
|
|
|
98
84
|
test: /\.avif$/
|
|
99
85
|
}
|
|
100
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
|
+
};
|
|
101
95
|
const svgCodec = {
|
|
102
96
|
async handler (buf, options) {
|
|
103
97
|
const result = external_svgo_default().optimize(buf.toString(), options);
|
|
@@ -107,16 +101,16 @@ const svgCodec = {
|
|
|
107
101
|
test: /\.svg$/
|
|
108
102
|
}
|
|
109
103
|
};
|
|
110
|
-
|
|
111
|
-
const codecs = {
|
|
104
|
+
const codecs_codecs = {
|
|
112
105
|
jpeg: jpegCodec,
|
|
113
106
|
png: pngCodec,
|
|
114
107
|
pngLossless: pngLosslessCodec,
|
|
115
108
|
ico: icoCodec,
|
|
116
109
|
svg: svgCodec,
|
|
117
|
-
avif: avifCodec
|
|
110
|
+
avif: avifCodec,
|
|
111
|
+
webp: webpCodec
|
|
118
112
|
};
|
|
119
|
-
|
|
113
|
+
const codecs = codecs_codecs;
|
|
120
114
|
function _define_property(obj, key, value) {
|
|
121
115
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
122
116
|
value: value,
|
|
@@ -140,7 +134,7 @@ class ImageMinimizerPlugin {
|
|
|
140
134
|
if (error instanceof Error) ret.error = error;
|
|
141
135
|
return ret;
|
|
142
136
|
};
|
|
143
|
-
const codec =
|
|
137
|
+
const codec = codecs[this.options.use];
|
|
144
138
|
if (!codec) compilation.errors.push(buildError(new Error(`Codec ${this.options.use} is not supported`)));
|
|
145
139
|
const opts = {
|
|
146
140
|
...codec.defaultOptions,
|
|
@@ -148,10 +142,8 @@ class ImageMinimizerPlugin {
|
|
|
148
142
|
};
|
|
149
143
|
const handleAsset = async (name)=>{
|
|
150
144
|
var _compilation_getAsset;
|
|
151
|
-
const info = null
|
|
145
|
+
const info = null == (_compilation_getAsset = compilation.getAsset(name)) ? void 0 : _compilation_getAsset.info;
|
|
152
146
|
const fileName = name.split('?')[0];
|
|
153
|
-
// 1. Skip double minimize assets from child compilation
|
|
154
|
-
// 2. Test file by options (e.g. test, include, exclude)
|
|
155
147
|
if ((null == info ? void 0 : info.minimized) || !matchObject(opts, fileName)) return;
|
|
156
148
|
const asset = compilation.getAsset(name);
|
|
157
149
|
if (!asset) return;
|
|
@@ -162,7 +154,7 @@ class ImageMinimizerPlugin {
|
|
|
162
154
|
try {
|
|
163
155
|
if (!result) {
|
|
164
156
|
const input = inputSource.source();
|
|
165
|
-
const buf = await codec.handler(external_node_buffer_namespaceObject.Buffer.from(input), opts);
|
|
157
|
+
const buf = await codec.handler('string' == typeof input ? external_node_buffer_namespaceObject.Buffer.from(input) : input, opts);
|
|
166
158
|
result = {
|
|
167
159
|
source: new RawSource(buf)
|
|
168
160
|
};
|
|
@@ -183,7 +175,6 @@ class ImageMinimizerPlugin {
|
|
|
183
175
|
compilation.hooks.processAssets.tapPromise({
|
|
184
176
|
name: this.name,
|
|
185
177
|
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
|
|
186
|
-
// @ts-expect-error unsupported by Rspack
|
|
187
178
|
additionalAssets: true
|
|
188
179
|
}, (assets)=>this.optimize(compiler, compilation, assets));
|
|
189
180
|
compilation.hooks.statsPrinter.tap(this.name, (stats)=>{
|
|
@@ -202,7 +193,7 @@ const withDefaultOptions = (opt)=>{
|
|
|
202
193
|
const options = 'string' == typeof opt ? {
|
|
203
194
|
use: opt
|
|
204
195
|
} : opt;
|
|
205
|
-
const { defaultOptions } =
|
|
196
|
+
const { defaultOptions } = codecs[options.use];
|
|
206
197
|
const ret = {
|
|
207
198
|
...defaultOptions,
|
|
208
199
|
...options
|
|
@@ -217,9 +208,7 @@ const DEFAULT_OPTIONS = [
|
|
|
217
208
|
];
|
|
218
209
|
const castOptions = (args)=>{
|
|
219
210
|
const head = args[0];
|
|
220
|
-
// expect [['png', { use: 'jpeg' }]]
|
|
221
211
|
if (Array.isArray(head)) return head;
|
|
222
|
-
// expect ['png', { use: 'jpeg' }]
|
|
223
212
|
const ret = [];
|
|
224
213
|
for (const arg of args){
|
|
225
214
|
external_node_assert_default()(!Array.isArray(arg));
|
|
@@ -233,7 +222,7 @@ const normalizeOptions = (options)=>{
|
|
|
233
222
|
return normalized;
|
|
234
223
|
};
|
|
235
224
|
const PLUGIN_IMAGE_COMPRESS_NAME = 'rsbuild:image-compress';
|
|
236
|
-
|
|
225
|
+
const pluginImageCompress = (...args)=>({
|
|
237
226
|
name: PLUGIN_IMAGE_COMPRESS_NAME,
|
|
238
227
|
setup (api) {
|
|
239
228
|
const opts = normalizeOptions(castOptions(args));
|
|
@@ -246,8 +235,16 @@ const PLUGIN_IMAGE_COMPRESS_NAME = 'rsbuild:image-compress';
|
|
|
246
235
|
});
|
|
247
236
|
}
|
|
248
237
|
});
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
238
|
+
exports.DEFAULT_OPTIONS = __webpack_exports__.DEFAULT_OPTIONS;
|
|
239
|
+
exports.ImageMinimizerPlugin = __webpack_exports__.ImageMinimizerPlugin;
|
|
240
|
+
exports.PLUGIN_IMAGE_COMPRESS_NAME = __webpack_exports__.PLUGIN_IMAGE_COMPRESS_NAME;
|
|
241
|
+
exports.pluginImageCompress = __webpack_exports__.pluginImageCompress;
|
|
242
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
|
243
|
+
"DEFAULT_OPTIONS",
|
|
244
|
+
"ImageMinimizerPlugin",
|
|
245
|
+
"PLUGIN_IMAGE_COMPRESS_NAME",
|
|
246
|
+
"pluginImageCompress"
|
|
247
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
|
248
|
+
Object.defineProperty(exports, '__esModule', {
|
|
252
249
|
value: true
|
|
253
250
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { RsbuildPlugin } from '@rsbuild/core';
|
|
2
|
+
import { ImageMinimizerPlugin } from './minimizer.js';
|
|
2
3
|
import type { Codecs, Options } from './types.js';
|
|
3
4
|
export type PluginImageCompressOptions = Options[];
|
|
4
5
|
export declare const DEFAULT_OPTIONS: Codecs[];
|
|
@@ -7,5 +8,6 @@ export interface IPluginImageCompress {
|
|
|
7
8
|
(options: Options[]): RsbuildPlugin;
|
|
8
9
|
}
|
|
9
10
|
export declare const PLUGIN_IMAGE_COMPRESS_NAME = "rsbuild:image-compress";
|
|
11
|
+
export { ImageMinimizerPlugin };
|
|
10
12
|
/** Options enable by default: {@link DEFAULT_OPTIONS} */
|
|
11
13
|
export declare const pluginImageCompress: IPluginImageCompress;
|
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,31 +36,39 @@ 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$/
|
|
52
60
|
}
|
|
53
61
|
};
|
|
54
|
-
|
|
55
|
-
const codecs = {
|
|
62
|
+
const codecs_codecs = {
|
|
56
63
|
jpeg: jpegCodec,
|
|
57
64
|
png: pngCodec,
|
|
58
65
|
pngLossless: pngLosslessCodec,
|
|
59
66
|
ico: icoCodec,
|
|
60
67
|
svg: svgCodec,
|
|
61
|
-
avif: avifCodec
|
|
68
|
+
avif: avifCodec,
|
|
69
|
+
webp: webpCodec
|
|
62
70
|
};
|
|
63
|
-
|
|
71
|
+
const codecs = codecs_codecs;
|
|
64
72
|
function _define_property(obj, key, value) {
|
|
65
73
|
if (key in obj) Object.defineProperty(obj, key, {
|
|
66
74
|
value: value,
|
|
@@ -84,7 +92,7 @@ class ImageMinimizerPlugin {
|
|
|
84
92
|
if (error instanceof Error) ret.error = error;
|
|
85
93
|
return ret;
|
|
86
94
|
};
|
|
87
|
-
const codec =
|
|
95
|
+
const codec = codecs[this.options.use];
|
|
88
96
|
if (!codec) compilation.errors.push(buildError(new Error(`Codec ${this.options.use} is not supported`)));
|
|
89
97
|
const opts = {
|
|
90
98
|
...codec.defaultOptions,
|
|
@@ -92,10 +100,8 @@ class ImageMinimizerPlugin {
|
|
|
92
100
|
};
|
|
93
101
|
const handleAsset = async (name)=>{
|
|
94
102
|
var _compilation_getAsset;
|
|
95
|
-
const info = null
|
|
103
|
+
const info = null == (_compilation_getAsset = compilation.getAsset(name)) ? void 0 : _compilation_getAsset.info;
|
|
96
104
|
const fileName = name.split('?')[0];
|
|
97
|
-
// 1. Skip double minimize assets from child compilation
|
|
98
|
-
// 2. Test file by options (e.g. test, include, exclude)
|
|
99
105
|
if ((null == info ? void 0 : info.minimized) || !matchObject(opts, fileName)) return;
|
|
100
106
|
const asset = compilation.getAsset(name);
|
|
101
107
|
if (!asset) return;
|
|
@@ -106,7 +112,7 @@ class ImageMinimizerPlugin {
|
|
|
106
112
|
try {
|
|
107
113
|
if (!result) {
|
|
108
114
|
const input = inputSource.source();
|
|
109
|
-
const buf = await codec.handler(
|
|
115
|
+
const buf = await codec.handler('string' == typeof input ? Buffer.from(input) : input, opts);
|
|
110
116
|
result = {
|
|
111
117
|
source: new RawSource(buf)
|
|
112
118
|
};
|
|
@@ -127,7 +133,6 @@ class ImageMinimizerPlugin {
|
|
|
127
133
|
compilation.hooks.processAssets.tapPromise({
|
|
128
134
|
name: this.name,
|
|
129
135
|
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
|
|
130
|
-
// @ts-expect-error unsupported by Rspack
|
|
131
136
|
additionalAssets: true
|
|
132
137
|
}, (assets)=>this.optimize(compiler, compilation, assets));
|
|
133
138
|
compilation.hooks.statsPrinter.tap(this.name, (stats)=>{
|
|
@@ -146,12 +151,12 @@ const withDefaultOptions = (opt)=>{
|
|
|
146
151
|
const options = 'string' == typeof opt ? {
|
|
147
152
|
use: opt
|
|
148
153
|
} : opt;
|
|
149
|
-
const { defaultOptions } =
|
|
154
|
+
const { defaultOptions } = codecs[options.use];
|
|
150
155
|
const ret = {
|
|
151
156
|
...defaultOptions,
|
|
152
157
|
...options
|
|
153
158
|
};
|
|
154
|
-
(
|
|
159
|
+
node_assert('test' in ret);
|
|
155
160
|
return ret;
|
|
156
161
|
};
|
|
157
162
|
const DEFAULT_OPTIONS = [
|
|
@@ -161,12 +166,10 @@ const DEFAULT_OPTIONS = [
|
|
|
161
166
|
];
|
|
162
167
|
const castOptions = (args)=>{
|
|
163
168
|
const head = args[0];
|
|
164
|
-
// expect [['png', { use: 'jpeg' }]]
|
|
165
169
|
if (Array.isArray(head)) return head;
|
|
166
|
-
// expect ['png', { use: 'jpeg' }]
|
|
167
170
|
const ret = [];
|
|
168
171
|
for (const arg of args){
|
|
169
|
-
(
|
|
172
|
+
node_assert(!Array.isArray(arg));
|
|
170
173
|
ret.push(arg);
|
|
171
174
|
}
|
|
172
175
|
return ret;
|
|
@@ -177,7 +180,7 @@ const normalizeOptions = (options)=>{
|
|
|
177
180
|
return normalized;
|
|
178
181
|
};
|
|
179
182
|
const PLUGIN_IMAGE_COMPRESS_NAME = 'rsbuild:image-compress';
|
|
180
|
-
|
|
183
|
+
const pluginImageCompress = (...args)=>({
|
|
181
184
|
name: PLUGIN_IMAGE_COMPRESS_NAME,
|
|
182
185
|
setup (api) {
|
|
183
186
|
const opts = normalizeOptions(castOptions(args));
|
|
@@ -190,4 +193,4 @@ const PLUGIN_IMAGE_COMPRESS_NAME = 'rsbuild:image-compress';
|
|
|
190
193
|
});
|
|
191
194
|
}
|
|
192
195
|
});
|
|
193
|
-
export { DEFAULT_OPTIONS, PLUGIN_IMAGE_COMPRESS_NAME, pluginImageCompress };
|
|
196
|
+
export { DEFAULT_OPTIONS, ImageMinimizerPlugin, PLUGIN_IMAGE_COMPRESS_NAME, pluginImageCompress };
|
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@
|
|
58
|
+
"packageManager": "pnpm@10.14.0",
|
|
59
59
|
"publishConfig": {
|
|
60
60
|
"access": "public",
|
|
61
61
|
"registry": "https://registry.npmjs.org/"
|