@rsbuild/plugin-image-compress 0.0.17 → 0.0.19
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/dist/index.d.ts +35 -8
- package/dist/index.js +150 -9
- package/dist/index.mjs +192 -0
- package/package.json +6 -5
- package/dist/minimizer.d.ts +0 -13
- package/dist/minimizer.js +0 -111
- package/dist/shared/codecs.d.ts +0 -8
- package/dist/shared/codecs.js +0 -98
- package/dist/shared/utils.d.ts +0 -2
- package/dist/shared/utils.js +0 -46
- package/dist/types/index.d.ts +0 -30
- package/dist/types/index.js +0 -16
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,37 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
import { RsbuildPlugin } from '@rsbuild/webpack';
|
|
2
|
+
import { JpegCompressOptions, PngQuantOptions, PNGLosslessOptions } from '@napi-rs/image';
|
|
3
|
+
import { Config } from 'svgo';
|
|
4
|
+
|
|
5
|
+
type ArrayOrNot<T> = T | T[];
|
|
6
|
+
interface CodecBaseOptions {
|
|
7
|
+
jpeg: JpegCompressOptions;
|
|
8
|
+
png: PngQuantOptions;
|
|
9
|
+
pngLossless: PNGLosslessOptions;
|
|
10
|
+
ico: {};
|
|
11
|
+
svg: Config;
|
|
12
|
+
}
|
|
13
|
+
interface BaseCompressOptions<T extends Codecs> {
|
|
14
|
+
use: T;
|
|
15
|
+
test?: ArrayOrNot<RegExp>;
|
|
16
|
+
include?: ArrayOrNot<RegExp>;
|
|
17
|
+
exclude?: ArrayOrNot<RegExp>;
|
|
18
|
+
}
|
|
19
|
+
type FinalOptionCollection = {
|
|
20
|
+
[K in Codecs]: BaseCompressOptions<K> & CodecBaseOptions[K];
|
|
21
|
+
};
|
|
22
|
+
type Codecs = keyof CodecBaseOptions;
|
|
23
|
+
type OptionCollection = {
|
|
24
|
+
[K in Codecs]: K | FinalOptionCollection[K];
|
|
25
|
+
};
|
|
26
|
+
type Options = OptionCollection[Codecs];
|
|
27
|
+
|
|
28
|
+
type PluginImageCompressOptions = Options[];
|
|
29
|
+
declare const DEFAULT_OPTIONS: Codecs[];
|
|
30
|
+
interface IPluginImageCompress {
|
|
31
|
+
(...options: Options[]): RsbuildPlugin;
|
|
32
|
+
(options: Options[]): RsbuildPlugin;
|
|
8
33
|
}
|
|
9
34
|
/** Options enable by default: {@link DEFAULT_OPTIONS} */
|
|
10
|
-
|
|
35
|
+
declare const pluginImageCompress: IPluginImageCompress;
|
|
36
|
+
|
|
37
|
+
export { DEFAULT_OPTIONS, IPluginImageCompress, PluginImageCompressOptions, pluginImageCompress };
|
package/dist/index.js
CHANGED
|
@@ -26,34 +26,175 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
26
|
mod
|
|
27
27
|
));
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
29
31
|
var src_exports = {};
|
|
30
32
|
__export(src_exports, {
|
|
31
33
|
DEFAULT_OPTIONS: () => DEFAULT_OPTIONS,
|
|
32
34
|
pluginImageCompress: () => pluginImageCompress
|
|
33
35
|
});
|
|
34
36
|
module.exports = __toCommonJS(src_exports);
|
|
37
|
+
var import_assert2 = __toESM(require("assert"));
|
|
38
|
+
|
|
39
|
+
// src/minimizer.ts
|
|
40
|
+
var import_buffer2 = require("buffer");
|
|
41
|
+
|
|
42
|
+
// src/shared/codecs.ts
|
|
43
|
+
var import_buffer = require("buffer");
|
|
44
|
+
var import_image = require("@napi-rs/image");
|
|
45
|
+
var import_svgo = __toESM(require("svgo"));
|
|
46
|
+
var jpegCodec = {
|
|
47
|
+
handler(buf, options) {
|
|
48
|
+
return (0, import_image.compressJpeg)(buf, options);
|
|
49
|
+
},
|
|
50
|
+
defaultOptions: {
|
|
51
|
+
test: /\.(jpg|jpeg)$/
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
var pngCodec = {
|
|
55
|
+
handler(buf, options) {
|
|
56
|
+
return (0, import_image.pngQuantize)(buf, options);
|
|
57
|
+
},
|
|
58
|
+
defaultOptions: {
|
|
59
|
+
test: /\.png$/
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
var pngLosslessCodec = {
|
|
63
|
+
handler(buf, options) {
|
|
64
|
+
return (0, import_image.losslessCompressPng)(buf, options);
|
|
65
|
+
},
|
|
66
|
+
defaultOptions: {
|
|
67
|
+
test: /\.png$/
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
var icoCodec = {
|
|
71
|
+
handler(buf) {
|
|
72
|
+
return new import_image.Transformer(buf).ico();
|
|
73
|
+
},
|
|
74
|
+
defaultOptions: {
|
|
75
|
+
test: /\.(ico|icon)$/
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
var svgCodec = {
|
|
79
|
+
async handler(buf, options) {
|
|
80
|
+
const result = import_svgo.default.optimize(buf.toString(), options);
|
|
81
|
+
return import_buffer.Buffer.from(result.data);
|
|
82
|
+
},
|
|
83
|
+
defaultOptions: {
|
|
84
|
+
test: /\.svg$/
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var codecs = {
|
|
88
|
+
jpeg: jpegCodec,
|
|
89
|
+
png: pngCodec,
|
|
90
|
+
pngLossless: pngLosslessCodec,
|
|
91
|
+
ico: icoCodec,
|
|
92
|
+
svg: svgCodec
|
|
93
|
+
};
|
|
94
|
+
var codecs_default = codecs;
|
|
95
|
+
|
|
96
|
+
// src/minimizer.ts
|
|
97
|
+
var MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME = "@rsbuild/plugin-image-compress/minimizer";
|
|
98
|
+
var ModernJsImageMinimizerPlugin = class {
|
|
99
|
+
constructor(options) {
|
|
100
|
+
this.name = MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME;
|
|
101
|
+
this.options = options;
|
|
102
|
+
}
|
|
103
|
+
async optimize(compiler, compilation, assets) {
|
|
104
|
+
const cache = compilation.getCache(MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME);
|
|
105
|
+
const { RawSource } = compiler.webpack.sources;
|
|
106
|
+
const { matchObject } = compiler.webpack.ModuleFilenameHelpers;
|
|
107
|
+
const buildError = (error, file, context) => {
|
|
108
|
+
const cause = error instanceof Error ? error : new Error();
|
|
109
|
+
const message = file && context ? `"${file}" in "${context}" from Modern.js Image Minimizer:
|
|
110
|
+
${cause.message}` : cause.message;
|
|
111
|
+
const ret = new compiler.webpack.WebpackError(message);
|
|
112
|
+
error instanceof Error && (ret.error = error);
|
|
113
|
+
return ret;
|
|
114
|
+
};
|
|
115
|
+
const codec = codecs_default[this.options.use];
|
|
116
|
+
if (!codec) {
|
|
117
|
+
compilation.errors.push(
|
|
118
|
+
buildError(new Error(`Codec ${this.options.use} is not supported`))
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
const opts = { ...codec.defaultOptions, ...this.options };
|
|
122
|
+
const handleAsset = async (name) => {
|
|
123
|
+
var _a;
|
|
124
|
+
const info = (_a = compilation.getAsset(name)) == null ? void 0 : _a.info;
|
|
125
|
+
if ((info == null ? void 0 : info.minimized) || !matchObject(opts, name)) {
|
|
126
|
+
return;
|
|
127
|
+
}
|
|
128
|
+
const { source: inputSource } = compilation.getAsset(name);
|
|
129
|
+
const eTag = cache.getLazyHashedEtag(inputSource);
|
|
130
|
+
const cacheItem = cache.getItemCache(name, eTag);
|
|
131
|
+
let result = await cacheItem.getPromise();
|
|
132
|
+
try {
|
|
133
|
+
if (!result) {
|
|
134
|
+
const input = inputSource.source();
|
|
135
|
+
const buf = await codec.handler(import_buffer2.Buffer.from(input), opts);
|
|
136
|
+
result = { source: new RawSource(buf) };
|
|
137
|
+
await cacheItem.storePromise(result);
|
|
138
|
+
}
|
|
139
|
+
compilation.updateAsset(name, result.source, { minimized: true });
|
|
140
|
+
} catch (error) {
|
|
141
|
+
compilation.errors.push(buildError(error, name, compiler.context));
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
const promises = Object.keys(assets).map((name) => handleAsset(name));
|
|
145
|
+
await Promise.all(promises);
|
|
146
|
+
}
|
|
147
|
+
apply(compiler) {
|
|
148
|
+
const handleCompilation = (compilation) => {
|
|
149
|
+
compilation.hooks.processAssets.tapPromise(
|
|
150
|
+
{
|
|
151
|
+
name: this.name,
|
|
152
|
+
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
|
|
153
|
+
additionalAssets: true
|
|
154
|
+
},
|
|
155
|
+
(assets) => this.optimize(compiler, compilation, assets)
|
|
156
|
+
);
|
|
157
|
+
compilation.hooks.statsPrinter.tap(this.name, (stats) => {
|
|
158
|
+
stats.hooks.print.for("asset.info.minimized").tap(
|
|
159
|
+
"@rsbuild/plugin-image-compress",
|
|
160
|
+
(minimized, { green, formatFlag }) => minimized && green && formatFlag ? green(formatFlag("minimized")) : ""
|
|
161
|
+
);
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
|
+
compiler.hooks.compilation.tap(this.name, handleCompilation);
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
// src/shared/utils.ts
|
|
35
169
|
var import_assert = __toESM(require("assert"));
|
|
36
|
-
var
|
|
37
|
-
|
|
38
|
-
const
|
|
39
|
-
const
|
|
170
|
+
var withDefaultOptions = (opt) => {
|
|
171
|
+
const options = typeof opt === "string" ? { use: opt } : opt;
|
|
172
|
+
const { defaultOptions } = codecs_default[options.use];
|
|
173
|
+
const ret = { ...defaultOptions, ...options };
|
|
174
|
+
(0, import_assert.default)("test" in ret);
|
|
175
|
+
return ret;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
// src/index.ts
|
|
179
|
+
var DEFAULT_OPTIONS = ["jpeg", "png", "ico"];
|
|
180
|
+
var castOptions = (args) => {
|
|
40
181
|
const head = args[0];
|
|
41
182
|
if (Array.isArray(head)) {
|
|
42
183
|
return head;
|
|
43
184
|
}
|
|
44
185
|
const ret = [];
|
|
45
186
|
for (const arg of args) {
|
|
46
|
-
(0,
|
|
187
|
+
(0, import_assert2.default)(!Array.isArray(arg));
|
|
47
188
|
ret.push(arg);
|
|
48
189
|
}
|
|
49
190
|
return ret;
|
|
50
191
|
};
|
|
51
|
-
|
|
192
|
+
var normalizeOptions = (options) => {
|
|
52
193
|
const opts = options.length ? options : DEFAULT_OPTIONS;
|
|
53
|
-
const normalized = opts.map((opt) =>
|
|
194
|
+
const normalized = opts.map((opt) => withDefaultOptions(opt));
|
|
54
195
|
return normalized;
|
|
55
196
|
};
|
|
56
|
-
|
|
197
|
+
var pluginImageCompress = (...args) => ({
|
|
57
198
|
name: "plugin-image-compress",
|
|
58
199
|
setup(api) {
|
|
59
200
|
const opts = normalizeOptions(castOptions(args));
|
|
@@ -63,7 +204,7 @@ const pluginImageCompress = (...args) => ({
|
|
|
63
204
|
}
|
|
64
205
|
chain.optimization.minimize(true);
|
|
65
206
|
for (const opt of opts) {
|
|
66
|
-
chain.optimization.minimizer(`image-compress-${opt.use}`).use(
|
|
207
|
+
chain.optimization.minimizer(`image-compress-${opt.use}`).use(ModernJsImageMinimizerPlugin, [opt]);
|
|
67
208
|
}
|
|
68
209
|
});
|
|
69
210
|
}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
// ../../node_modules/.pnpm/@modern-js+module-tools@2.40.0_typescript@5.2.2/node_modules/@modern-js/module-tools/shims/esm.js
|
|
2
|
+
import { fileURLToPath } from "url";
|
|
3
|
+
import path from "path";
|
|
4
|
+
|
|
5
|
+
// ../../scripts/require_shims.js
|
|
6
|
+
import { createRequire } from "module";
|
|
7
|
+
global.require = createRequire(import.meta.url);
|
|
8
|
+
|
|
9
|
+
// src/index.ts
|
|
10
|
+
import assert2 from "assert";
|
|
11
|
+
|
|
12
|
+
// src/minimizer.ts
|
|
13
|
+
import { Buffer as Buffer2 } from "buffer";
|
|
14
|
+
|
|
15
|
+
// src/shared/codecs.ts
|
|
16
|
+
import { Buffer } from "buffer";
|
|
17
|
+
import {
|
|
18
|
+
compressJpeg,
|
|
19
|
+
losslessCompressPng,
|
|
20
|
+
pngQuantize,
|
|
21
|
+
Transformer
|
|
22
|
+
} from "@napi-rs/image";
|
|
23
|
+
import svgo from "svgo";
|
|
24
|
+
var jpegCodec = {
|
|
25
|
+
handler(buf, options) {
|
|
26
|
+
return compressJpeg(buf, options);
|
|
27
|
+
},
|
|
28
|
+
defaultOptions: {
|
|
29
|
+
test: /\.(jpg|jpeg)$/
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var pngCodec = {
|
|
33
|
+
handler(buf, options) {
|
|
34
|
+
return pngQuantize(buf, options);
|
|
35
|
+
},
|
|
36
|
+
defaultOptions: {
|
|
37
|
+
test: /\.png$/
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
var pngLosslessCodec = {
|
|
41
|
+
handler(buf, options) {
|
|
42
|
+
return losslessCompressPng(buf, options);
|
|
43
|
+
},
|
|
44
|
+
defaultOptions: {
|
|
45
|
+
test: /\.png$/
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
var icoCodec = {
|
|
49
|
+
handler(buf) {
|
|
50
|
+
return new Transformer(buf).ico();
|
|
51
|
+
},
|
|
52
|
+
defaultOptions: {
|
|
53
|
+
test: /\.(ico|icon)$/
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var svgCodec = {
|
|
57
|
+
async handler(buf, options) {
|
|
58
|
+
const result = svgo.optimize(buf.toString(), options);
|
|
59
|
+
return Buffer.from(result.data);
|
|
60
|
+
},
|
|
61
|
+
defaultOptions: {
|
|
62
|
+
test: /\.svg$/
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
var codecs = {
|
|
66
|
+
jpeg: jpegCodec,
|
|
67
|
+
png: pngCodec,
|
|
68
|
+
pngLossless: pngLosslessCodec,
|
|
69
|
+
ico: icoCodec,
|
|
70
|
+
svg: svgCodec
|
|
71
|
+
};
|
|
72
|
+
var codecs_default = codecs;
|
|
73
|
+
|
|
74
|
+
// src/minimizer.ts
|
|
75
|
+
var MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME = "@rsbuild/plugin-image-compress/minimizer";
|
|
76
|
+
var ModernJsImageMinimizerPlugin = class {
|
|
77
|
+
constructor(options) {
|
|
78
|
+
this.name = MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME;
|
|
79
|
+
this.options = options;
|
|
80
|
+
}
|
|
81
|
+
async optimize(compiler, compilation, assets) {
|
|
82
|
+
const cache = compilation.getCache(MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME);
|
|
83
|
+
const { RawSource } = compiler.webpack.sources;
|
|
84
|
+
const { matchObject } = compiler.webpack.ModuleFilenameHelpers;
|
|
85
|
+
const buildError = (error, file, context) => {
|
|
86
|
+
const cause = error instanceof Error ? error : new Error();
|
|
87
|
+
const message = file && context ? `"${file}" in "${context}" from Modern.js Image Minimizer:
|
|
88
|
+
${cause.message}` : cause.message;
|
|
89
|
+
const ret = new compiler.webpack.WebpackError(message);
|
|
90
|
+
error instanceof Error && (ret.error = error);
|
|
91
|
+
return ret;
|
|
92
|
+
};
|
|
93
|
+
const codec = codecs_default[this.options.use];
|
|
94
|
+
if (!codec) {
|
|
95
|
+
compilation.errors.push(
|
|
96
|
+
buildError(new Error(`Codec ${this.options.use} is not supported`))
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
const opts = { ...codec.defaultOptions, ...this.options };
|
|
100
|
+
const handleAsset = async (name) => {
|
|
101
|
+
const info = compilation.getAsset(name)?.info;
|
|
102
|
+
if (info?.minimized || !matchObject(opts, name)) {
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const { source: inputSource } = compilation.getAsset(name);
|
|
106
|
+
const eTag = cache.getLazyHashedEtag(inputSource);
|
|
107
|
+
const cacheItem = cache.getItemCache(name, eTag);
|
|
108
|
+
let result = await cacheItem.getPromise();
|
|
109
|
+
try {
|
|
110
|
+
if (!result) {
|
|
111
|
+
const input = inputSource.source();
|
|
112
|
+
const buf = await codec.handler(Buffer2.from(input), opts);
|
|
113
|
+
result = { source: new RawSource(buf) };
|
|
114
|
+
await cacheItem.storePromise(result);
|
|
115
|
+
}
|
|
116
|
+
compilation.updateAsset(name, result.source, { minimized: true });
|
|
117
|
+
} catch (error) {
|
|
118
|
+
compilation.errors.push(buildError(error, name, compiler.context));
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
const promises = Object.keys(assets).map((name) => handleAsset(name));
|
|
122
|
+
await Promise.all(promises);
|
|
123
|
+
}
|
|
124
|
+
apply(compiler) {
|
|
125
|
+
const handleCompilation = (compilation) => {
|
|
126
|
+
compilation.hooks.processAssets.tapPromise(
|
|
127
|
+
{
|
|
128
|
+
name: this.name,
|
|
129
|
+
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
|
|
130
|
+
additionalAssets: true
|
|
131
|
+
},
|
|
132
|
+
(assets) => this.optimize(compiler, compilation, assets)
|
|
133
|
+
);
|
|
134
|
+
compilation.hooks.statsPrinter.tap(this.name, (stats) => {
|
|
135
|
+
stats.hooks.print.for("asset.info.minimized").tap(
|
|
136
|
+
"@rsbuild/plugin-image-compress",
|
|
137
|
+
(minimized, { green, formatFlag }) => minimized && green && formatFlag ? green(formatFlag("minimized")) : ""
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
compiler.hooks.compilation.tap(this.name, handleCompilation);
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
// src/shared/utils.ts
|
|
146
|
+
import assert from "assert";
|
|
147
|
+
var withDefaultOptions = (opt) => {
|
|
148
|
+
const options = typeof opt === "string" ? { use: opt } : opt;
|
|
149
|
+
const { defaultOptions } = codecs_default[options.use];
|
|
150
|
+
const ret = { ...defaultOptions, ...options };
|
|
151
|
+
assert("test" in ret);
|
|
152
|
+
return ret;
|
|
153
|
+
};
|
|
154
|
+
|
|
155
|
+
// src/index.ts
|
|
156
|
+
var DEFAULT_OPTIONS = ["jpeg", "png", "ico"];
|
|
157
|
+
var castOptions = (args) => {
|
|
158
|
+
const head = args[0];
|
|
159
|
+
if (Array.isArray(head)) {
|
|
160
|
+
return head;
|
|
161
|
+
}
|
|
162
|
+
const ret = [];
|
|
163
|
+
for (const arg of args) {
|
|
164
|
+
assert2(!Array.isArray(arg));
|
|
165
|
+
ret.push(arg);
|
|
166
|
+
}
|
|
167
|
+
return ret;
|
|
168
|
+
};
|
|
169
|
+
var normalizeOptions = (options) => {
|
|
170
|
+
const opts = options.length ? options : DEFAULT_OPTIONS;
|
|
171
|
+
const normalized = opts.map((opt) => withDefaultOptions(opt));
|
|
172
|
+
return normalized;
|
|
173
|
+
};
|
|
174
|
+
var pluginImageCompress = (...args) => ({
|
|
175
|
+
name: "plugin-image-compress",
|
|
176
|
+
setup(api) {
|
|
177
|
+
const opts = normalizeOptions(castOptions(args));
|
|
178
|
+
api.modifyBundlerChain((chain, { env }) => {
|
|
179
|
+
if (env !== "production") {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
chain.optimization.minimize(true);
|
|
183
|
+
for (const opt of opts) {
|
|
184
|
+
chain.optimization.minimizer(`image-compress-${opt.use}`).use(ModernJsImageMinimizerPlugin, [opt]);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
export {
|
|
190
|
+
DEFAULT_OPTIONS,
|
|
191
|
+
pluginImageCompress
|
|
192
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsbuild/plugin-image-compress",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.19",
|
|
4
4
|
"description": "Image compress plugin for Rsbuild",
|
|
5
5
|
"homepage": "https://rsbuild.dev",
|
|
6
6
|
"repository": {
|
|
@@ -12,6 +12,7 @@
|
|
|
12
12
|
"exports": {
|
|
13
13
|
".": {
|
|
14
14
|
"types": "./dist/index.d.ts",
|
|
15
|
+
"import": "./dist/index.mjs",
|
|
15
16
|
"default": "./dist/index.js"
|
|
16
17
|
}
|
|
17
18
|
},
|
|
@@ -23,15 +24,15 @@
|
|
|
23
24
|
"dependencies": {
|
|
24
25
|
"@napi-rs/image": "^1.7.0",
|
|
25
26
|
"svgo": "^3.0.2",
|
|
26
|
-
"@rsbuild/shared": "0.0.
|
|
27
|
+
"@rsbuild/shared": "0.0.19"
|
|
27
28
|
},
|
|
28
29
|
"devDependencies": {
|
|
29
30
|
"@types/node": "^16",
|
|
30
31
|
"typescript": "^5.2.2",
|
|
31
32
|
"webpack": "^5.89.0",
|
|
32
|
-
"@rsbuild/core": "0.0.
|
|
33
|
-
"@rsbuild/test-helper": "0.0.
|
|
34
|
-
"@rsbuild/webpack": "0.0.
|
|
33
|
+
"@rsbuild/core": "0.0.19",
|
|
34
|
+
"@rsbuild/test-helper": "0.0.19",
|
|
35
|
+
"@rsbuild/webpack": "0.0.19"
|
|
35
36
|
},
|
|
36
37
|
"publishConfig": {
|
|
37
38
|
"access": "public",
|
package/dist/minimizer.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import type { webpack } from '@rsbuild/webpack';
|
|
2
|
-
import type { FinalOptions } from './types';
|
|
3
|
-
export declare const MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME: "@rsbuild/plugin-image-compress/minimizer";
|
|
4
|
-
export interface MinimizedResult {
|
|
5
|
-
source: webpack.sources.RawSource;
|
|
6
|
-
}
|
|
7
|
-
export declare class ModernJsImageMinimizerPlugin {
|
|
8
|
-
name: "@rsbuild/plugin-image-compress/minimizer";
|
|
9
|
-
options: FinalOptions;
|
|
10
|
-
constructor(options: FinalOptions);
|
|
11
|
-
optimize(compiler: webpack.Compiler, compilation: webpack.Compilation, assets: Record<string, webpack.sources.Source>): Promise<void>;
|
|
12
|
-
apply(compiler: webpack.Compiler): void;
|
|
13
|
-
}
|
package/dist/minimizer.js
DELETED
|
@@ -1,111 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var minimizer_exports = {};
|
|
30
|
-
__export(minimizer_exports, {
|
|
31
|
-
MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME: () => MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME,
|
|
32
|
-
ModernJsImageMinimizerPlugin: () => ModernJsImageMinimizerPlugin
|
|
33
|
-
});
|
|
34
|
-
module.exports = __toCommonJS(minimizer_exports);
|
|
35
|
-
var import_buffer = require("buffer");
|
|
36
|
-
var import_codecs = __toESM(require("./shared/codecs"));
|
|
37
|
-
const MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME = "@rsbuild/plugin-image-compress/minimizer";
|
|
38
|
-
class ModernJsImageMinimizerPlugin {
|
|
39
|
-
constructor(options) {
|
|
40
|
-
this.name = MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME;
|
|
41
|
-
this.options = options;
|
|
42
|
-
}
|
|
43
|
-
async optimize(compiler, compilation, assets) {
|
|
44
|
-
const cache = compilation.getCache(MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME);
|
|
45
|
-
const { RawSource } = compiler.webpack.sources;
|
|
46
|
-
const { matchObject } = compiler.webpack.ModuleFilenameHelpers;
|
|
47
|
-
const buildError = (error, file, context) => {
|
|
48
|
-
const cause = error instanceof Error ? error : new Error();
|
|
49
|
-
const message = file && context ? `"${file}" in "${context}" from Modern.js Image Minimizer:
|
|
50
|
-
${cause.message}` : cause.message;
|
|
51
|
-
const ret = new compiler.webpack.WebpackError(message);
|
|
52
|
-
error instanceof Error && (ret.error = error);
|
|
53
|
-
return ret;
|
|
54
|
-
};
|
|
55
|
-
const codec = import_codecs.default[this.options.use];
|
|
56
|
-
if (!codec) {
|
|
57
|
-
compilation.errors.push(
|
|
58
|
-
buildError(new Error(`Codec ${this.options.use} is not supported`))
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
const opts = { ...codec.defaultOptions, ...this.options };
|
|
62
|
-
const handleAsset = async (name) => {
|
|
63
|
-
var _a;
|
|
64
|
-
const info = (_a = compilation.getAsset(name)) == null ? void 0 : _a.info;
|
|
65
|
-
if ((info == null ? void 0 : info.minimized) || !matchObject(opts, name)) {
|
|
66
|
-
return;
|
|
67
|
-
}
|
|
68
|
-
const { source: inputSource } = compilation.getAsset(name);
|
|
69
|
-
const eTag = cache.getLazyHashedEtag(inputSource);
|
|
70
|
-
const cacheItem = cache.getItemCache(name, eTag);
|
|
71
|
-
let result = await cacheItem.getPromise();
|
|
72
|
-
try {
|
|
73
|
-
if (!result) {
|
|
74
|
-
const input = inputSource.source();
|
|
75
|
-
const buf = await codec.handler(import_buffer.Buffer.from(input), opts);
|
|
76
|
-
result = { source: new RawSource(buf) };
|
|
77
|
-
await cacheItem.storePromise(result);
|
|
78
|
-
}
|
|
79
|
-
compilation.updateAsset(name, result.source, { minimized: true });
|
|
80
|
-
} catch (error) {
|
|
81
|
-
compilation.errors.push(buildError(error, name, compiler.context));
|
|
82
|
-
}
|
|
83
|
-
};
|
|
84
|
-
const promises = Object.keys(assets).map((name) => handleAsset(name));
|
|
85
|
-
await Promise.all(promises);
|
|
86
|
-
}
|
|
87
|
-
apply(compiler) {
|
|
88
|
-
const handleCompilation = (compilation) => {
|
|
89
|
-
compilation.hooks.processAssets.tapPromise(
|
|
90
|
-
{
|
|
91
|
-
name: this.name,
|
|
92
|
-
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE,
|
|
93
|
-
additionalAssets: true
|
|
94
|
-
},
|
|
95
|
-
(assets) => this.optimize(compiler, compilation, assets)
|
|
96
|
-
);
|
|
97
|
-
compilation.hooks.statsPrinter.tap(this.name, (stats) => {
|
|
98
|
-
stats.hooks.print.for("asset.info.minimized").tap(
|
|
99
|
-
"@rsbuild/plugin-image-compress",
|
|
100
|
-
(minimized, { green, formatFlag }) => minimized && green && formatFlag ? green(formatFlag("minimized")) : ""
|
|
101
|
-
);
|
|
102
|
-
});
|
|
103
|
-
};
|
|
104
|
-
compiler.hooks.compilation.tap(this.name, handleCompilation);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
108
|
-
0 && (module.exports = {
|
|
109
|
-
MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME,
|
|
110
|
-
ModernJsImageMinimizerPlugin
|
|
111
|
-
});
|
package/dist/shared/codecs.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
import { Codec, Codecs } from '../types';
|
|
2
|
-
export declare const jpegCodec: Codec<'jpeg'>;
|
|
3
|
-
export declare const pngCodec: Codec<'png'>;
|
|
4
|
-
export declare const pngLosslessCodec: Codec<'pngLossless'>;
|
|
5
|
-
export declare const icoCodec: Codec<'ico'>;
|
|
6
|
-
export declare const svgCodec: Codec<'svg'>;
|
|
7
|
-
declare const codecs: Record<Codecs, Codec<any>>;
|
|
8
|
-
export default codecs;
|
package/dist/shared/codecs.js
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var codecs_exports = {};
|
|
30
|
-
__export(codecs_exports, {
|
|
31
|
-
default: () => codecs_default,
|
|
32
|
-
icoCodec: () => icoCodec,
|
|
33
|
-
jpegCodec: () => jpegCodec,
|
|
34
|
-
pngCodec: () => pngCodec,
|
|
35
|
-
pngLosslessCodec: () => pngLosslessCodec,
|
|
36
|
-
svgCodec: () => svgCodec
|
|
37
|
-
});
|
|
38
|
-
module.exports = __toCommonJS(codecs_exports);
|
|
39
|
-
var import_buffer = require("buffer");
|
|
40
|
-
var import_image = require("@napi-rs/image");
|
|
41
|
-
var import_svgo = __toESM(require("svgo"));
|
|
42
|
-
const jpegCodec = {
|
|
43
|
-
handler(buf, options) {
|
|
44
|
-
return (0, import_image.compressJpeg)(buf, options);
|
|
45
|
-
},
|
|
46
|
-
defaultOptions: {
|
|
47
|
-
test: /\.(jpg|jpeg)$/
|
|
48
|
-
}
|
|
49
|
-
};
|
|
50
|
-
const pngCodec = {
|
|
51
|
-
handler(buf, options) {
|
|
52
|
-
return (0, import_image.pngQuantize)(buf, options);
|
|
53
|
-
},
|
|
54
|
-
defaultOptions: {
|
|
55
|
-
test: /\.png$/
|
|
56
|
-
}
|
|
57
|
-
};
|
|
58
|
-
const pngLosslessCodec = {
|
|
59
|
-
handler(buf, options) {
|
|
60
|
-
return (0, import_image.losslessCompressPng)(buf, options);
|
|
61
|
-
},
|
|
62
|
-
defaultOptions: {
|
|
63
|
-
test: /\.png$/
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
const icoCodec = {
|
|
67
|
-
handler(buf) {
|
|
68
|
-
return new import_image.Transformer(buf).ico();
|
|
69
|
-
},
|
|
70
|
-
defaultOptions: {
|
|
71
|
-
test: /\.(ico|icon)$/
|
|
72
|
-
}
|
|
73
|
-
};
|
|
74
|
-
const svgCodec = {
|
|
75
|
-
async handler(buf, options) {
|
|
76
|
-
const result = import_svgo.default.optimize(buf.toString(), options);
|
|
77
|
-
return import_buffer.Buffer.from(result.data);
|
|
78
|
-
},
|
|
79
|
-
defaultOptions: {
|
|
80
|
-
test: /\.svg$/
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
const codecs = {
|
|
84
|
-
jpeg: jpegCodec,
|
|
85
|
-
png: pngCodec,
|
|
86
|
-
pngLossless: pngLosslessCodec,
|
|
87
|
-
ico: icoCodec,
|
|
88
|
-
svg: svgCodec
|
|
89
|
-
};
|
|
90
|
-
var codecs_default = codecs;
|
|
91
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
92
|
-
0 && (module.exports = {
|
|
93
|
-
icoCodec,
|
|
94
|
-
jpegCodec,
|
|
95
|
-
pngCodec,
|
|
96
|
-
pngLosslessCodec,
|
|
97
|
-
svgCodec
|
|
98
|
-
});
|
package/dist/shared/utils.d.ts
DELETED
package/dist/shared/utils.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
|
-
var __defProp = Object.defineProperty;
|
|
4
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var utils_exports = {};
|
|
30
|
-
__export(utils_exports, {
|
|
31
|
-
withDefaultOptions: () => withDefaultOptions
|
|
32
|
-
});
|
|
33
|
-
module.exports = __toCommonJS(utils_exports);
|
|
34
|
-
var import_assert = __toESM(require("assert"));
|
|
35
|
-
var import_codecs = __toESM(require("./codecs"));
|
|
36
|
-
const withDefaultOptions = (opt) => {
|
|
37
|
-
const options = typeof opt === "string" ? { use: opt } : opt;
|
|
38
|
-
const { defaultOptions } = import_codecs.default[options.use];
|
|
39
|
-
const ret = { ...defaultOptions, ...options };
|
|
40
|
-
(0, import_assert.default)("test" in ret);
|
|
41
|
-
return ret;
|
|
42
|
-
};
|
|
43
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
44
|
-
0 && (module.exports = {
|
|
45
|
-
withDefaultOptions
|
|
46
|
-
});
|
package/dist/types/index.d.ts
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
|
-
import type { Buffer } from 'buffer';
|
|
3
|
-
import { JpegCompressOptions, PNGLosslessOptions, PngQuantOptions } from '@napi-rs/image';
|
|
4
|
-
import type { Config as SvgoConfig } from 'svgo';
|
|
5
|
-
export type ArrayOrNot<T> = T | T[];
|
|
6
|
-
export interface WebpTransformOptions {
|
|
7
|
-
quality?: number;
|
|
8
|
-
}
|
|
9
|
-
export interface CodecBaseOptions {
|
|
10
|
-
jpeg: JpegCompressOptions;
|
|
11
|
-
png: PngQuantOptions;
|
|
12
|
-
pngLossless: PNGLosslessOptions;
|
|
13
|
-
ico: {};
|
|
14
|
-
svg: SvgoConfig;
|
|
15
|
-
}
|
|
16
|
-
export interface BaseCompressOptions<T extends Codecs> {
|
|
17
|
-
use: T;
|
|
18
|
-
test?: ArrayOrNot<RegExp>;
|
|
19
|
-
include?: ArrayOrNot<RegExp>;
|
|
20
|
-
exclude?: ArrayOrNot<RegExp>;
|
|
21
|
-
}
|
|
22
|
-
export type FinalOptionCollection = { [K in Codecs]: BaseCompressOptions<K> & CodecBaseOptions[K] };
|
|
23
|
-
export type FinalOptions = FinalOptionCollection[Codecs];
|
|
24
|
-
export interface Codec<T extends Codecs> {
|
|
25
|
-
handler: (buf: Buffer, options: CodecBaseOptions[T]) => Promise<Buffer>;
|
|
26
|
-
defaultOptions: Omit<FinalOptionCollection[T], 'use'>;
|
|
27
|
-
}
|
|
28
|
-
export type Codecs = keyof CodecBaseOptions;
|
|
29
|
-
export type OptionCollection = { [K in Codecs]: K | FinalOptionCollection[K] };
|
|
30
|
-
export type Options = OptionCollection[Codecs];
|
package/dist/types/index.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __copyProps = (to, from, except, desc) => {
|
|
7
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
8
|
-
for (let key of __getOwnPropNames(from))
|
|
9
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
10
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
11
|
-
}
|
|
12
|
-
return to;
|
|
13
|
-
};
|
|
14
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
15
|
-
var types_exports = {};
|
|
16
|
-
module.exports = __toCommonJS(types_exports);
|