@rsbuild/plugin-image-compress 0.0.0-nightly-20231017160746
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/LICENSE +21 -0
- package/README.md +24 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +75 -0
- package/dist/minimizer.d.ts +13 -0
- package/dist/minimizer.js +111 -0
- package/dist/shared/codecs.d.ts +8 -0
- package/dist/shared/codecs.js +98 -0
- package/dist/shared/utils.d.ts +2 -0
- package/dist/shared/utils.js +47 -0
- package/dist/types/index.d.ts +30 -0
- package/dist/types/index.js +16 -0
- package/package.json +44 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2023-present Bytedance, Inc. and its affiliates.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<a href="https://rsbuild.dev" target="blank"><img src="https://github.com/web-infra-dev/rsbuild/assets/7237365/84abc13e-b620-468f-a90b-dbf28e7e9427" alt="Rsbuild Logo" /></a>
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# Rsbuild
|
|
6
|
+
|
|
7
|
+
Unleash the power of Rspack with the out-of-the-box build tool.
|
|
8
|
+
|
|
9
|
+
## Getting Started
|
|
10
|
+
|
|
11
|
+
Please follow [Quick Start](https://rsbuild.dev/guides/get-started/quick-start) to get started with Rsbuild.
|
|
12
|
+
|
|
13
|
+
## Documentation
|
|
14
|
+
|
|
15
|
+
- [English Documentation](https://rsbuild.dev/)
|
|
16
|
+
- [中文文档](https://rsbuild.dev/zh)
|
|
17
|
+
|
|
18
|
+
## Contributing
|
|
19
|
+
|
|
20
|
+
Please read the [Contributing Guide](https://github.com/web-infra-dev/rsbuild/blob/main/CONTRIBUTING.md).
|
|
21
|
+
|
|
22
|
+
## License
|
|
23
|
+
|
|
24
|
+
Rsbuild is [MIT licensed](https://github.com/web-infra-dev/rsbuild/blob/main/LICENSE).
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { BuilderPlugin } from '@modern-js/builder-webpack-provider/types';
|
|
2
|
+
import { Codecs, Options } from './types';
|
|
3
|
+
export type PluginImageCompressOptions = Options[];
|
|
4
|
+
export declare const DEFAULT_OPTIONS: Codecs[];
|
|
5
|
+
export interface IPluginImageCompress {
|
|
6
|
+
(...options: Options[]): BuilderPlugin;
|
|
7
|
+
(options: Options[]): BuilderPlugin;
|
|
8
|
+
}
|
|
9
|
+
/** Options enable by default: {@link DEFAULT_OPTIONS} */
|
|
10
|
+
export declare const builderPluginImageCompress: IPluginImageCompress;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
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 src_exports = {};
|
|
30
|
+
__export(src_exports, {
|
|
31
|
+
DEFAULT_OPTIONS: () => DEFAULT_OPTIONS,
|
|
32
|
+
builderPluginImageCompress: () => builderPluginImageCompress
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(src_exports);
|
|
35
|
+
var import_assert = __toESM(require("assert"));
|
|
36
|
+
var import_minimizer = require("./minimizer");
|
|
37
|
+
var import_utils = require("./shared/utils");
|
|
38
|
+
const DEFAULT_OPTIONS = ["jpeg", "png", "ico"];
|
|
39
|
+
const castOptions = (args) => {
|
|
40
|
+
const head = args[0];
|
|
41
|
+
if (Array.isArray(head)) {
|
|
42
|
+
return head;
|
|
43
|
+
}
|
|
44
|
+
const ret = [];
|
|
45
|
+
for (const arg of args) {
|
|
46
|
+
(0, import_assert.default)(!Array.isArray(arg));
|
|
47
|
+
ret.push(arg);
|
|
48
|
+
}
|
|
49
|
+
return ret;
|
|
50
|
+
};
|
|
51
|
+
const normalizeOptions = (options) => {
|
|
52
|
+
const opts = options.length ? options : DEFAULT_OPTIONS;
|
|
53
|
+
const normalized = opts.map((opt) => (0, import_utils.withDefaultOptions)(opt));
|
|
54
|
+
return normalized;
|
|
55
|
+
};
|
|
56
|
+
const builderPluginImageCompress = (...args) => ({
|
|
57
|
+
name: "builder-plugin-image-compress",
|
|
58
|
+
setup(api) {
|
|
59
|
+
const opts = normalizeOptions(castOptions(args));
|
|
60
|
+
api.modifyBundlerChain((chain, { env }) => {
|
|
61
|
+
if (env !== "production") {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
chain.optimization.minimize(true);
|
|
65
|
+
for (const opt of opts) {
|
|
66
|
+
chain.optimization.minimizer(`image-compress-${opt.use}`).use(import_minimizer.ModernJsImageMinimizerPlugin, [opt]);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
});
|
|
71
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
72
|
+
0 && (module.exports = {
|
|
73
|
+
DEFAULT_OPTIONS,
|
|
74
|
+
builderPluginImageCompress
|
|
75
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { webpack } from '@modern-js/builder-webpack-provider/types';
|
|
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
|
+
}
|
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,8 @@
|
|
|
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;
|
|
@@ -0,0 +1,98 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,47 @@
|
|
|
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_lodash = __toESM(require("@modern-js/utils/lodash"));
|
|
36
|
+
var import_codecs = __toESM(require("./codecs"));
|
|
37
|
+
const withDefaultOptions = (opt) => {
|
|
38
|
+
const options = import_lodash.default.isString(opt) ? { use: opt } : opt;
|
|
39
|
+
const { defaultOptions } = import_codecs.default[options.use];
|
|
40
|
+
const ret = { ...defaultOptions, ...options };
|
|
41
|
+
(0, import_assert.default)("test" in ret);
|
|
42
|
+
return ret;
|
|
43
|
+
};
|
|
44
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
45
|
+
0 && (module.exports = {
|
|
46
|
+
withDefaultOptions
|
|
47
|
+
});
|
|
@@ -0,0 +1,30 @@
|
|
|
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];
|
|
@@ -0,0 +1,16 @@
|
|
|
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);
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@rsbuild/plugin-image-compress",
|
|
3
|
+
"description": "Image compress plugin for modern-js/web-builder",
|
|
4
|
+
"homepage": "https://rsbuild.dev",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/web-infra-dev/rsbuild",
|
|
8
|
+
"directory": "packages/plugin-image-compress"
|
|
9
|
+
},
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"version": "0.0.0-nightly-20231017160746",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"module": "./dist/index.js",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"default": "./dist/index.js"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@modern-js/utils": "^2.36.0",
|
|
25
|
+
"@napi-rs/image": "^1.7.0",
|
|
26
|
+
"svgo": "^3.0.2"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"@modern-js/builder-webpack-provider": "^2.36.0",
|
|
30
|
+
"@types/node": "^16",
|
|
31
|
+
"typescript": "^5",
|
|
32
|
+
"webpack": "^5.88.1"
|
|
33
|
+
},
|
|
34
|
+
"sideEffects": false,
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"registry": "https://registry.npmjs.org/",
|
|
37
|
+
"access": "public",
|
|
38
|
+
"provenance": true
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"dev": "modern build --watch",
|
|
42
|
+
"build": "modern build"
|
|
43
|
+
}
|
|
44
|
+
}
|