@rsbuild/plugin-image-compress 0.6.14 → 0.7.0-beta.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.
@@ -1,35 +1,56 @@
1
- import { createRequire } from 'module';
2
- var require = createRequire(import.meta['url']);
3
-
1
+ "use strict";
2
+ var __create = Object.create;
4
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;
5
8
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
+ // If the importer is in node compatibility mode or this is not an ESM
23
+ // file that has been converted to a CommonJS file using a Babel-
24
+ // compatible transform (i.e. "__esModule" has not been set), then set
25
+ // "default" to the CommonJS "module.exports" for node compatibility.
26
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
+ mod
28
+ ));
29
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
6
30
  var __publicField = (obj, key, value) => {
7
31
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
32
  return value;
9
33
  };
10
34
 
11
- // ../../node_modules/.pnpm/@modern-js+module-tools@2.49.2_eslint@8.57.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js
12
- import { fileURLToPath } from "url";
13
- import path from "path";
14
-
15
35
  // src/index.ts
16
- import assert2 from "assert";
36
+ var src_exports = {};
37
+ __export(src_exports, {
38
+ DEFAULT_OPTIONS: () => DEFAULT_OPTIONS,
39
+ pluginImageCompress: () => pluginImageCompress
40
+ });
41
+ module.exports = __toCommonJS(src_exports);
42
+ var import_node_assert2 = __toESM(require("assert"));
17
43
 
18
44
  // src/minimizer.ts
19
- import { Buffer as Buffer3 } from "buffer";
45
+ var import_node_buffer2 = require("buffer");
20
46
 
21
47
  // src/shared/codecs.ts
22
- import { Buffer as Buffer2 } from "buffer";
23
- import {
24
- Transformer,
25
- compressJpeg,
26
- losslessCompressPng,
27
- pngQuantize
28
- } from "@napi-rs/image";
29
- import svgo from "svgo";
48
+ var import_node_buffer = require("buffer");
49
+ var import_image = require("@napi-rs/image");
50
+ var import_svgo = __toESM(require("svgo"));
30
51
  var jpegCodec = {
31
52
  handler(buf, options) {
32
- return compressJpeg(buf, options);
53
+ return (0, import_image.compressJpeg)(buf, options);
33
54
  },
34
55
  defaultOptions: {
35
56
  test: /\.(?:jpg|jpeg)$/
@@ -37,7 +58,7 @@ var jpegCodec = {
37
58
  };
38
59
  var pngCodec = {
39
60
  handler(buf, options) {
40
- return pngQuantize(buf, options);
61
+ return (0, import_image.pngQuantize)(buf, options);
41
62
  },
42
63
  defaultOptions: {
43
64
  test: /\.png$/
@@ -45,7 +66,7 @@ var pngCodec = {
45
66
  };
46
67
  var pngLosslessCodec = {
47
68
  handler(buf, options) {
48
- return losslessCompressPng(buf, options);
69
+ return (0, import_image.losslessCompressPng)(buf, options);
49
70
  },
50
71
  defaultOptions: {
51
72
  test: /\.png$/
@@ -53,7 +74,7 @@ var pngLosslessCodec = {
53
74
  };
54
75
  var icoCodec = {
55
76
  handler(buf) {
56
- return new Transformer(buf).ico();
77
+ return new import_image.Transformer(buf).ico();
57
78
  },
58
79
  defaultOptions: {
59
80
  test: /\.(?:ico|icon)$/
@@ -61,8 +82,8 @@ var icoCodec = {
61
82
  };
62
83
  var svgCodec = {
63
84
  async handler(buf, options) {
64
- const result = svgo.optimize(buf.toString(), options);
65
- return Buffer2.from(result.data);
85
+ const result = import_svgo.default.optimize(buf.toString(), options);
86
+ return import_node_buffer.Buffer.from(result.data);
66
87
  },
67
88
  defaultOptions: {
68
89
  test: /\.svg$/
@@ -119,7 +140,7 @@ ${cause.message}` : cause.message;
119
140
  try {
120
141
  if (!result) {
121
142
  const input = inputSource.source();
122
- const buf = await codec.handler(Buffer3.from(input), opts);
143
+ const buf = await codec.handler(import_node_buffer2.Buffer.from(input), opts);
123
144
  result = { source: new RawSource(buf) };
124
145
  await cacheItem.storePromise(result);
125
146
  }
@@ -153,12 +174,12 @@ ${cause.message}` : cause.message;
153
174
  };
154
175
 
155
176
  // src/shared/utils.ts
156
- import assert from "assert";
177
+ var import_node_assert = __toESM(require("assert"));
157
178
  var withDefaultOptions = (opt) => {
158
179
  const options = typeof opt === "string" ? { use: opt } : opt;
159
180
  const { defaultOptions } = codecs_default[options.use];
160
181
  const ret = { ...defaultOptions, ...options };
161
- assert("test" in ret);
182
+ (0, import_node_assert.default)("test" in ret);
162
183
  return ret;
163
184
  };
164
185
 
@@ -171,7 +192,7 @@ var castOptions = (args) => {
171
192
  }
172
193
  const ret = [];
173
194
  for (const arg of args) {
174
- assert2(!Array.isArray(arg));
195
+ (0, import_node_assert2.default)(!Array.isArray(arg));
175
196
  ret.push(arg);
176
197
  }
177
198
  return ret;
@@ -196,7 +217,8 @@ var pluginImageCompress = (...args) => ({
196
217
  });
197
218
  }
198
219
  });
199
- export {
220
+ // Annotate the CommonJS export names for ESM import in node:
221
+ 0 && (module.exports = {
200
222
  DEFAULT_OPTIONS,
201
223
  pluginImageCompress
202
- };
224
+ });
package/dist/index.d.ts CHANGED
@@ -1,37 +1,10 @@
1
- import { RsbuildPlugin } from '@rsbuild/core';
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: Record<string, never>;
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 {
1
+ import type { RsbuildPlugin } from '@rsbuild/core';
2
+ import type { Codecs, Options } from './types';
3
+ export type PluginImageCompressOptions = Options[];
4
+ export declare const DEFAULT_OPTIONS: Codecs[];
5
+ export interface IPluginImageCompress {
31
6
  (...options: Options[]): RsbuildPlugin;
32
7
  (options: Options[]): RsbuildPlugin;
33
8
  }
34
9
  /** Options enable by default: {@link DEFAULT_OPTIONS} */
35
- declare const pluginImageCompress: IPluginImageCompress;
36
-
37
- export { DEFAULT_OPTIONS, type IPluginImageCompress, type PluginImageCompressOptions, pluginImageCompress };
10
+ export declare const pluginImageCompress: IPluginImageCompress;
package/dist/index.js CHANGED
@@ -1,56 +1,35 @@
1
- "use strict";
2
- var __create = Object.create;
1
+ import { createRequire } from 'module';
2
+ var require = createRequire(import.meta['url']);
3
+
3
4
  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
5
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
9
- var __export = (target, all) => {
10
- for (var name in all)
11
- __defProp(target, name, { get: all[name], enumerable: true });
12
- };
13
- var __copyProps = (to, from, except, desc) => {
14
- if (from && typeof from === "object" || typeof from === "function") {
15
- for (let key of __getOwnPropNames(from))
16
- if (!__hasOwnProp.call(to, key) && key !== except)
17
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
- }
19
- return to;
20
- };
21
- var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
22
- // If the importer is in node compatibility mode or this is not an ESM
23
- // file that has been converted to a CommonJS file using a Babel-
24
- // compatible transform (i.e. "__esModule" has not been set), then set
25
- // "default" to the CommonJS "module.exports" for node compatibility.
26
- isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
27
- mod
28
- ));
29
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
30
6
  var __publicField = (obj, key, value) => {
31
7
  __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
32
8
  return value;
33
9
  };
34
10
 
11
+ // ../../node_modules/.pnpm/@modern-js+module-tools@2.49.3_eslint@8.57.0_typescript@5.4.5/node_modules/@modern-js/module-tools/shims/esm.js
12
+ import { fileURLToPath } from "url";
13
+ import path from "path";
14
+
35
15
  // src/index.ts
36
- var src_exports = {};
37
- __export(src_exports, {
38
- DEFAULT_OPTIONS: () => DEFAULT_OPTIONS,
39
- pluginImageCompress: () => pluginImageCompress
40
- });
41
- module.exports = __toCommonJS(src_exports);
42
- var import_node_assert2 = __toESM(require("assert"));
16
+ import assert2 from "assert";
43
17
 
44
18
  // src/minimizer.ts
45
- var import_node_buffer2 = require("buffer");
19
+ import { Buffer as Buffer3 } from "buffer";
46
20
 
47
21
  // src/shared/codecs.ts
48
- var import_node_buffer = require("buffer");
49
- var import_image = require("@napi-rs/image");
50
- var import_svgo = __toESM(require("svgo"));
22
+ import { Buffer as Buffer2 } from "buffer";
23
+ import {
24
+ Transformer,
25
+ compressJpeg,
26
+ losslessCompressPng,
27
+ pngQuantize
28
+ } from "@napi-rs/image";
29
+ import svgo from "svgo";
51
30
  var jpegCodec = {
52
31
  handler(buf, options) {
53
- return (0, import_image.compressJpeg)(buf, options);
32
+ return compressJpeg(buf, options);
54
33
  },
55
34
  defaultOptions: {
56
35
  test: /\.(?:jpg|jpeg)$/
@@ -58,7 +37,7 @@ var jpegCodec = {
58
37
  };
59
38
  var pngCodec = {
60
39
  handler(buf, options) {
61
- return (0, import_image.pngQuantize)(buf, options);
40
+ return pngQuantize(buf, options);
62
41
  },
63
42
  defaultOptions: {
64
43
  test: /\.png$/
@@ -66,7 +45,7 @@ var pngCodec = {
66
45
  };
67
46
  var pngLosslessCodec = {
68
47
  handler(buf, options) {
69
- return (0, import_image.losslessCompressPng)(buf, options);
48
+ return losslessCompressPng(buf, options);
70
49
  },
71
50
  defaultOptions: {
72
51
  test: /\.png$/
@@ -74,7 +53,7 @@ var pngLosslessCodec = {
74
53
  };
75
54
  var icoCodec = {
76
55
  handler(buf) {
77
- return new import_image.Transformer(buf).ico();
56
+ return new Transformer(buf).ico();
78
57
  },
79
58
  defaultOptions: {
80
59
  test: /\.(?:ico|icon)$/
@@ -82,8 +61,8 @@ var icoCodec = {
82
61
  };
83
62
  var svgCodec = {
84
63
  async handler(buf, options) {
85
- const result = import_svgo.default.optimize(buf.toString(), options);
86
- return import_node_buffer.Buffer.from(result.data);
64
+ const result = svgo.optimize(buf.toString(), options);
65
+ return Buffer2.from(result.data);
87
66
  },
88
67
  defaultOptions: {
89
68
  test: /\.svg$/
@@ -140,7 +119,7 @@ ${cause.message}` : cause.message;
140
119
  try {
141
120
  if (!result) {
142
121
  const input = inputSource.source();
143
- const buf = await codec.handler(import_node_buffer2.Buffer.from(input), opts);
122
+ const buf = await codec.handler(Buffer3.from(input), opts);
144
123
  result = { source: new RawSource(buf) };
145
124
  await cacheItem.storePromise(result);
146
125
  }
@@ -174,12 +153,12 @@ ${cause.message}` : cause.message;
174
153
  };
175
154
 
176
155
  // src/shared/utils.ts
177
- var import_node_assert = __toESM(require("assert"));
156
+ import assert from "assert";
178
157
  var withDefaultOptions = (opt) => {
179
158
  const options = typeof opt === "string" ? { use: opt } : opt;
180
159
  const { defaultOptions } = codecs_default[options.use];
181
160
  const ret = { ...defaultOptions, ...options };
182
- (0, import_node_assert.default)("test" in ret);
161
+ assert("test" in ret);
183
162
  return ret;
184
163
  };
185
164
 
@@ -192,7 +171,7 @@ var castOptions = (args) => {
192
171
  }
193
172
  const ret = [];
194
173
  for (const arg of args) {
195
- (0, import_node_assert2.default)(!Array.isArray(arg));
174
+ assert2(!Array.isArray(arg));
196
175
  ret.push(arg);
197
176
  }
198
177
  return ret;
@@ -217,8 +196,7 @@ var pluginImageCompress = (...args) => ({
217
196
  });
218
197
  }
219
198
  });
220
- // Annotate the CommonJS export names for ESM import in node:
221
- 0 && (module.exports = {
199
+ export {
222
200
  DEFAULT_OPTIONS,
223
201
  pluginImageCompress
224
- });
202
+ };
@@ -0,0 +1,13 @@
1
+ import type { webpack } from '@rsbuild/webpack';
2
+ import type { FinalOptions } from './types';
3
+ export declare const IMAGE_MINIMIZER_PLUGIN_NAME: "@rsbuild/plugin-image-compress/minimizer";
4
+ export interface MinimizedResult {
5
+ source: webpack.sources.RawSource;
6
+ }
7
+ export declare class ImageMinimizerPlugin {
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,8 @@
1
+ import type { 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,2 @@
1
+ import type { FinalOptions, Options } from '../types';
2
+ export declare const withDefaultOptions: (opt: Options) => FinalOptions;
@@ -0,0 +1,34 @@
1
+ /// <reference types="node" />
2
+ import type { Buffer } from 'node:buffer';
3
+ import type { 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: Record<string, unknown>;
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 = {
23
+ [K in Codecs]: BaseCompressOptions<K> & CodecBaseOptions[K];
24
+ };
25
+ export type FinalOptions = FinalOptionCollection[Codecs];
26
+ export interface Codec<T extends Codecs> {
27
+ handler: (buf: Buffer, options: CodecBaseOptions[T]) => Promise<Buffer>;
28
+ defaultOptions: Omit<FinalOptionCollection[T], 'use'>;
29
+ }
30
+ export type Codecs = keyof CodecBaseOptions;
31
+ export type OptionCollection = {
32
+ [K in Codecs]: K | FinalOptionCollection[K];
33
+ };
34
+ export type Options = OptionCollection[Codecs];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-image-compress",
3
- "version": "0.6.14",
3
+ "version": "0.7.0-beta.0",
4
4
  "description": "Image compress plugin for Rsbuild",
5
5
  "homepage": "https://rsbuild.dev",
6
6
  "repository": {
@@ -9,15 +9,15 @@
9
9
  "directory": "packages/plugin-image-compress"
10
10
  },
11
11
  "license": "MIT",
12
- "type": "commonjs",
12
+ "type": "module",
13
13
  "exports": {
14
14
  ".": {
15
15
  "types": "./dist/index.d.ts",
16
- "import": "./dist/index.mjs",
17
- "default": "./dist/index.js"
16
+ "import": "./dist/index.js",
17
+ "require": "./dist/index.cjs"
18
18
  }
19
19
  },
20
- "main": "./dist/index.js",
20
+ "main": "./dist/index.cjs",
21
21
  "types": "./dist/index.d.ts",
22
22
  "files": [
23
23
  "dist"
@@ -25,18 +25,18 @@
25
25
  "dependencies": {
26
26
  "@napi-rs/image": "^1.9.2",
27
27
  "svgo": "^3.3.2",
28
- "@rsbuild/shared": "0.6.14"
28
+ "@rsbuild/shared": "0.7.0-beta.0"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@types/node": "18.x",
32
32
  "typescript": "^5.4.2",
33
33
  "webpack": "^5.91.0",
34
- "@rsbuild/core": "0.6.14",
35
- "@rsbuild/webpack": "0.6.14",
36
- "@scripts/test-helper": "0.6.14"
34
+ "@rsbuild/core": "0.7.0-beta.0",
35
+ "@rsbuild/webpack": "0.7.0-beta.0",
36
+ "@scripts/test-helper": "0.7.0-beta.0"
37
37
  },
38
38
  "peerDependencies": {
39
- "@rsbuild/core": "^0.6.14"
39
+ "@rsbuild/core": "^0.7.0-beta.0"
40
40
  },
41
41
  "publishConfig": {
42
42
  "access": "public",