@powerlines/plugin-image-compression 0.2.406 → 0.2.408

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 +1,29 @@
1
- var e=Object.create,t=Object.defineProperty,n=Object.getOwnPropertyDescriptor,r=Object.getOwnPropertyNames,i=Object.getPrototypeOf,a=Object.prototype.hasOwnProperty,o=(e,i,o,s)=>{if(i&&typeof i==`object`||typeof i==`function`)for(var c=r(i),l=0,u=c.length,d;l<u;l++)d=c[l],!a.call(e,d)&&d!==o&&t(e,d,{get:(e=>i[e]).bind(null,d),enumerable:!(s=n(i,d))||s.enumerable});return e},s=(n,r,a)=>(a=n==null?{}:e(i(n)),o(r||!n||!n.__esModule?t(a,`default`,{value:n,enumerable:!0}):a,n));exports.__toESM=s;
1
+ //#region \0rolldown/runtime.js
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 __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") {
10
+ for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
11
+ key = keys[i];
12
+ if (!__hasOwnProp.call(to, key) && key !== except) {
13
+ __defProp(to, key, {
14
+ get: ((k) => from[k]).bind(null, key),
15
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
16
+ });
17
+ }
18
+ }
19
+ }
20
+ return to;
21
+ };
22
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
23
+ value: mod,
24
+ enumerable: true
25
+ }) : target, mod));
26
+
27
+ //#endregion
28
+
29
+ exports.__toESM = __toESM;
package/dist/index.cjs CHANGED
@@ -1 +1,120 @@
1
- Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});const e=require(`./_virtual/_rolldown/runtime.cjs`);let t=require(`@stryke/convert/to-array`),n=require(`@stryke/fs/list-files`),r=require(`@stryke/fs/read-file`),i=require(`@stryke/fs/write-file`),a=require(`@stryke/path/find`),o=require(`@stryke/string-format/pretty-bytes`),s=require(`chalk`);s=e.__toESM(s,1);let c=require(`node:buffer`),l=require(`node:fs/promises`),u=require(`powerlines/plugin-utils`),d=require(`svgo`);const f=(e={})=>({name:`image-compression`,config(){return{imageCompression:{filter:`{root}/**/*.{svg,jpg,jpeg,png,webp,avif,heif,gif,tiff,jp2,jxl}`,svg:{},jpeg:{},png:{},webp:{},avif:{},heif:{},tiff:{},jp2:{},jxl:{},...e}}},async configResolved(){if(!this.config.imageCompression.filter)throw Error(`ImageCompression plugin requires a filter or glob pattern to be specified in the configuration.`);this.config.imageCompression.filter=(0,t.toArray)(this.config.imageCompression.filter).map(e=>(0,u.replacePathTokens)(this,e)).filter(Boolean)},async prepare(){await Promise.all((0,t.toArray)(this.config.imageCompression.filter).map(async e=>{await Promise.all((await(0,n.listFiles)(e)).map(async e=>{let t=this.config.imageCompression.outputPath?(0,u.replacePathTokens)(this,this.config.imageCompression.outputPath).replace(/\{fileName\}/g,e.split(`/`).pop()||`output`):e,n=performance.now(),f=(await(0,l.stat)(e)).size,p=0,m=(0,a.findFileExtensionSafe)(e).toLowerCase();if(!m){this.error(` Failed to optimize ${e} - Unable to determine file extension`);return}if(m===`svg`){let n=(0,d.optimize)(await(0,r.readFile)(e),{multipass:!0,path:e,...this.config.imageCompression.svg});n.data&&(p=c.Buffer.byteLength(n.data,`utf8`),await(0,i.writeFile)(t,n.data))}else{let n=await import(`sharp`).then(t=>t.default(e,{animated:m===`gif`}));switch(m){case`jpeg`:case`jpg`:n=n.jpeg(this.config.imageCompression.jpeg);break;case`png`:n=n.png(this.config.imageCompression.png);break;case`webp`:n=n.webp(this.config.imageCompression.webp);break;case`avif`:n=n.avif(this.config.imageCompression.avif);break;case`gif`:n=n.gif(this.config.imageCompression.gif);break;case`heif`:n=n.heif(this.config.imageCompression.heif);break;case`tiff`:n=n.tiff(this.config.imageCompression.tiff);break;case`jp2`:n=n.jp2(this.config.imageCompression.jp2);break;case`jxl`:n=n.jxl(this.config.imageCompression.jxl);break}p=(await n.toFile(t)).size}if(!p){this.error(` ✘ Failed to optimize ${e} - No data returned`);return}if(p>=f){this.error(` ✘ Failed to optimize ${e} - Compression output was not smaller than the original`);return}this.info(` ✔ Successfully optimized ${e}: ${s.default.redBright((0,o.prettyBytes)(f))} -> ${s.default.greenBright((0,o.prettyBytes)(p))} (${s.default.green((100-p/f*100).toFixed(2))}%) in ${(performance.now()-n).toFixed(2)} ms`)}))}))}});exports.default=f,exports.plugin=f;
1
+ Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } });
2
+ const require_runtime = require('./_virtual/_rolldown/runtime.cjs');
3
+ let _stryke_convert_to_array = require("@stryke/convert/to-array");
4
+ let _stryke_fs_list_files = require("@stryke/fs/list-files");
5
+ let _stryke_fs_read_file = require("@stryke/fs/read-file");
6
+ let _stryke_fs_write_file = require("@stryke/fs/write-file");
7
+ let _stryke_path_find = require("@stryke/path/find");
8
+ let _stryke_string_format_pretty_bytes = require("@stryke/string-format/pretty-bytes");
9
+ let chalk = require("chalk");
10
+ chalk = require_runtime.__toESM(chalk, 1);
11
+ let node_buffer = require("node:buffer");
12
+ let node_fs_promises = require("node:fs/promises");
13
+ let powerlines_plugin_utils = require("powerlines/plugin-utils");
14
+ let svgo = require("svgo");
15
+
16
+ //#region src/index.ts
17
+ /**
18
+ * A Powerlines plugin to optimize image assets used by the project.
19
+ *
20
+ * @see https://sharp.pixelplumbing.com
21
+ *
22
+ * @param options - The plugin options.
23
+ * @returns A Powerlines plugin instance.
24
+ */
25
+ const plugin = (options = {}) => {
26
+ return {
27
+ name: "image-compression",
28
+ config() {
29
+ return { imageCompression: {
30
+ filter: "{root}/**/*.{svg,jpg,jpeg,png,webp,avif,heif,gif,tiff,jp2,jxl}",
31
+ svg: {},
32
+ jpeg: {},
33
+ png: {},
34
+ webp: {},
35
+ avif: {},
36
+ heif: {},
37
+ tiff: {},
38
+ jp2: {},
39
+ jxl: {},
40
+ ...options
41
+ } };
42
+ },
43
+ async configResolved() {
44
+ if (!this.config.imageCompression.filter) throw new Error("ImageCompression plugin requires a filter or glob pattern to be specified in the configuration.");
45
+ this.config.imageCompression.filter = (0, _stryke_convert_to_array.toArray)(this.config.imageCompression.filter).map((path) => (0, powerlines_plugin_utils.replacePathTokens)(this, path)).filter(Boolean);
46
+ },
47
+ async prepare() {
48
+ await Promise.all((0, _stryke_convert_to_array.toArray)(this.config.imageCompression.filter).map(async (path) => {
49
+ await Promise.all((await (0, _stryke_fs_list_files.listFiles)(path)).map(async (file) => {
50
+ const outputPath = this.config.imageCompression.outputPath ? (0, powerlines_plugin_utils.replacePathTokens)(this, this.config.imageCompression.outputPath).replace(/\{fileName\}/g, file.split("/").pop() || "output") : file;
51
+ const start = performance.now();
52
+ const originalSize = (await (0, node_fs_promises.stat)(file)).size;
53
+ let compressedSize = 0;
54
+ const extension = (0, _stryke_path_find.findFileExtensionSafe)(file).toLowerCase();
55
+ if (!extension) {
56
+ this.error(` ✘ Failed to optimize ${file} - Unable to determine file extension`);
57
+ return;
58
+ }
59
+ if (extension === "svg") {
60
+ const result = (0, svgo.optimize)(await (0, _stryke_fs_read_file.readFile)(file), {
61
+ multipass: true,
62
+ path: file,
63
+ ...this.config.imageCompression.svg
64
+ });
65
+ if (result.data) {
66
+ compressedSize = node_buffer.Buffer.byteLength(result.data, "utf8");
67
+ await (0, _stryke_fs_write_file.writeFile)(outputPath, result.data);
68
+ }
69
+ } else {
70
+ let image = await import("sharp").then((sharpModule) => sharpModule.default(file, { animated: extension === "gif" }));
71
+ switch (extension) {
72
+ case "jpeg":
73
+ case "jpg":
74
+ image = image.jpeg(this.config.imageCompression.jpeg);
75
+ break;
76
+ case "png":
77
+ image = image.png(this.config.imageCompression.png);
78
+ break;
79
+ case "webp":
80
+ image = image.webp(this.config.imageCompression.webp);
81
+ break;
82
+ case "avif":
83
+ image = image.avif(this.config.imageCompression.avif);
84
+ break;
85
+ case "gif":
86
+ image = image.gif(this.config.imageCompression.gif);
87
+ break;
88
+ case "heif":
89
+ image = image.heif(this.config.imageCompression.heif);
90
+ break;
91
+ case "tiff":
92
+ image = image.tiff(this.config.imageCompression.tiff);
93
+ break;
94
+ case "jp2":
95
+ image = image.jp2(this.config.imageCompression.jp2);
96
+ break;
97
+ case "jxl":
98
+ image = image.jxl(this.config.imageCompression.jxl);
99
+ break;
100
+ }
101
+ compressedSize = (await image.toFile(outputPath)).size;
102
+ }
103
+ if (!compressedSize) {
104
+ this.error(` ✘ Failed to optimize ${file} - No data returned`);
105
+ return;
106
+ }
107
+ if (compressedSize >= originalSize) {
108
+ this.error(` ✘ Failed to optimize ${file} - Compression output was not smaller than the original`);
109
+ return;
110
+ }
111
+ this.info(` ✔ Successfully optimized ${file}: ${chalk.default.redBright((0, _stryke_string_format_pretty_bytes.prettyBytes)(originalSize))} -> ${chalk.default.greenBright((0, _stryke_string_format_pretty_bytes.prettyBytes)(compressedSize))} (${chalk.default.green((100 - compressedSize / originalSize * 100).toFixed(2))}%) in ${(performance.now() - start).toFixed(2)} ms`);
112
+ }));
113
+ }));
114
+ }
115
+ };
116
+ };
117
+
118
+ //#endregion
119
+ exports.default = plugin;
120
+ exports.plugin = plugin;
package/dist/index.mjs CHANGED
@@ -1,2 +1,117 @@
1
- import{toArray as e}from"@stryke/convert/to-array";import{listFiles as t}from"@stryke/fs/list-files";import{readFile as n}from"@stryke/fs/read-file";import{writeFile as r}from"@stryke/fs/write-file";import{findFileExtensionSafe as i}from"@stryke/path/find";import{prettyBytes as a}from"@stryke/string-format/pretty-bytes";import o from"chalk";import{Buffer as s}from"node:buffer";import{stat as c}from"node:fs/promises";import{replacePathTokens as l}from"powerlines/plugin-utils";import{optimize as u}from"svgo";const d=(d={})=>({name:`image-compression`,config(){return{imageCompression:{filter:`{root}/**/*.{svg,jpg,jpeg,png,webp,avif,heif,gif,tiff,jp2,jxl}`,svg:{},jpeg:{},png:{},webp:{},avif:{},heif:{},tiff:{},jp2:{},jxl:{},...d}}},async configResolved(){if(!this.config.imageCompression.filter)throw Error(`ImageCompression plugin requires a filter or glob pattern to be specified in the configuration.`);this.config.imageCompression.filter=e(this.config.imageCompression.filter).map(e=>l(this,e)).filter(Boolean)},async prepare(){await Promise.all(e(this.config.imageCompression.filter).map(async e=>{await Promise.all((await t(e)).map(async e=>{let t=this.config.imageCompression.outputPath?l(this,this.config.imageCompression.outputPath).replace(/\{fileName\}/g,e.split(`/`).pop()||`output`):e,d=performance.now(),f=(await c(e)).size,p=0,m=i(e).toLowerCase();if(!m){this.error(` ✘ Failed to optimize ${e} - Unable to determine file extension`);return}if(m===`svg`){let i=u(await n(e),{multipass:!0,path:e,...this.config.imageCompression.svg});i.data&&(p=s.byteLength(i.data,`utf8`),await r(t,i.data))}else{let n=await import(`sharp`).then(t=>t.default(e,{animated:m===`gif`}));switch(m){case`jpeg`:case`jpg`:n=n.jpeg(this.config.imageCompression.jpeg);break;case`png`:n=n.png(this.config.imageCompression.png);break;case`webp`:n=n.webp(this.config.imageCompression.webp);break;case`avif`:n=n.avif(this.config.imageCompression.avif);break;case`gif`:n=n.gif(this.config.imageCompression.gif);break;case`heif`:n=n.heif(this.config.imageCompression.heif);break;case`tiff`:n=n.tiff(this.config.imageCompression.tiff);break;case`jp2`:n=n.jp2(this.config.imageCompression.jp2);break;case`jxl`:n=n.jxl(this.config.imageCompression.jxl);break}p=(await n.toFile(t)).size}if(!p){this.error(` ✘ Failed to optimize ${e} - No data returned`);return}if(p>=f){this.error(` ✘ Failed to optimize ${e} - Compression output was not smaller than the original`);return}this.info(` ✔ Successfully optimized ${e}: ${o.redBright(a(f))} -> ${o.greenBright(a(p))} (${o.green((100-p/f*100).toFixed(2))}%) in ${(performance.now()-d).toFixed(2)} ms`)}))}))}});export{d as default,d as plugin};
1
+ import { toArray } from "@stryke/convert/to-array";
2
+ import { listFiles } from "@stryke/fs/list-files";
3
+ import { readFile } from "@stryke/fs/read-file";
4
+ import { writeFile } from "@stryke/fs/write-file";
5
+ import { findFileExtensionSafe } from "@stryke/path/find";
6
+ import { prettyBytes } from "@stryke/string-format/pretty-bytes";
7
+ import chalk from "chalk";
8
+ import { Buffer } from "node:buffer";
9
+ import { stat } from "node:fs/promises";
10
+ import { replacePathTokens } from "powerlines/plugin-utils";
11
+ import { optimize } from "svgo";
12
+
13
+ //#region src/index.ts
14
+ /**
15
+ * A Powerlines plugin to optimize image assets used by the project.
16
+ *
17
+ * @see https://sharp.pixelplumbing.com
18
+ *
19
+ * @param options - The plugin options.
20
+ * @returns A Powerlines plugin instance.
21
+ */
22
+ const plugin = (options = {}) => {
23
+ return {
24
+ name: "image-compression",
25
+ config() {
26
+ return { imageCompression: {
27
+ filter: "{root}/**/*.{svg,jpg,jpeg,png,webp,avif,heif,gif,tiff,jp2,jxl}",
28
+ svg: {},
29
+ jpeg: {},
30
+ png: {},
31
+ webp: {},
32
+ avif: {},
33
+ heif: {},
34
+ tiff: {},
35
+ jp2: {},
36
+ jxl: {},
37
+ ...options
38
+ } };
39
+ },
40
+ async configResolved() {
41
+ if (!this.config.imageCompression.filter) throw new Error("ImageCompression plugin requires a filter or glob pattern to be specified in the configuration.");
42
+ this.config.imageCompression.filter = toArray(this.config.imageCompression.filter).map((path) => replacePathTokens(this, path)).filter(Boolean);
43
+ },
44
+ async prepare() {
45
+ await Promise.all(toArray(this.config.imageCompression.filter).map(async (path) => {
46
+ await Promise.all((await listFiles(path)).map(async (file) => {
47
+ const outputPath = this.config.imageCompression.outputPath ? replacePathTokens(this, this.config.imageCompression.outputPath).replace(/\{fileName\}/g, file.split("/").pop() || "output") : file;
48
+ const start = performance.now();
49
+ const originalSize = (await stat(file)).size;
50
+ let compressedSize = 0;
51
+ const extension = findFileExtensionSafe(file).toLowerCase();
52
+ if (!extension) {
53
+ this.error(` ✘ Failed to optimize ${file} - Unable to determine file extension`);
54
+ return;
55
+ }
56
+ if (extension === "svg") {
57
+ const result = optimize(await readFile(file), {
58
+ multipass: true,
59
+ path: file,
60
+ ...this.config.imageCompression.svg
61
+ });
62
+ if (result.data) {
63
+ compressedSize = Buffer.byteLength(result.data, "utf8");
64
+ await writeFile(outputPath, result.data);
65
+ }
66
+ } else {
67
+ let image = await import("sharp").then((sharpModule) => sharpModule.default(file, { animated: extension === "gif" }));
68
+ switch (extension) {
69
+ case "jpeg":
70
+ case "jpg":
71
+ image = image.jpeg(this.config.imageCompression.jpeg);
72
+ break;
73
+ case "png":
74
+ image = image.png(this.config.imageCompression.png);
75
+ break;
76
+ case "webp":
77
+ image = image.webp(this.config.imageCompression.webp);
78
+ break;
79
+ case "avif":
80
+ image = image.avif(this.config.imageCompression.avif);
81
+ break;
82
+ case "gif":
83
+ image = image.gif(this.config.imageCompression.gif);
84
+ break;
85
+ case "heif":
86
+ image = image.heif(this.config.imageCompression.heif);
87
+ break;
88
+ case "tiff":
89
+ image = image.tiff(this.config.imageCompression.tiff);
90
+ break;
91
+ case "jp2":
92
+ image = image.jp2(this.config.imageCompression.jp2);
93
+ break;
94
+ case "jxl":
95
+ image = image.jxl(this.config.imageCompression.jxl);
96
+ break;
97
+ }
98
+ compressedSize = (await image.toFile(outputPath)).size;
99
+ }
100
+ if (!compressedSize) {
101
+ this.error(` ✘ Failed to optimize ${file} - No data returned`);
102
+ return;
103
+ }
104
+ if (compressedSize >= originalSize) {
105
+ this.error(` ✘ Failed to optimize ${file} - Compression output was not smaller than the original`);
106
+ return;
107
+ }
108
+ this.info(` ✔ Successfully optimized ${file}: ${chalk.redBright(prettyBytes(originalSize))} -> ${chalk.greenBright(prettyBytes(compressedSize))} (${chalk.green((100 - compressedSize / originalSize * 100).toFixed(2))}%) in ${(performance.now() - start).toFixed(2)} ms`);
109
+ }));
110
+ }));
111
+ }
112
+ };
113
+ };
114
+
115
+ //#endregion
116
+ export { plugin as default, plugin };
2
117
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { toArray } from \"@stryke/convert/to-array\";\nimport { listFiles } from \"@stryke/fs/list-files\";\nimport { readFile } from \"@stryke/fs/read-file\";\nimport { writeFile } from \"@stryke/fs/write-file\";\nimport { findFileExtensionSafe } from \"@stryke/path/find\";\nimport { prettyBytes } from \"@stryke/string-format/pretty-bytes\";\nimport chalk from \"chalk\";\nimport { Buffer } from \"node:buffer\";\nimport { stat } from \"node:fs/promises\";\nimport type { Plugin } from \"powerlines\";\nimport { replacePathTokens } from \"powerlines/plugin-utils\";\nimport { optimize } from \"svgo\";\nimport {\n ImageCompressionPluginContext,\n ImageCompressionPluginOptions\n} from \"./types/plugin\";\n\nexport * from \"./types\";\n\ndeclare module \"powerlines\" {\n interface Config {\n imageCompression?: ImageCompressionPluginOptions;\n }\n}\n\n/**\n * A Powerlines plugin to optimize image assets used by the project.\n *\n * @see https://sharp.pixelplumbing.com\n *\n * @param options - The plugin options.\n * @returns A Powerlines plugin instance.\n */\nexport const plugin = <\n TContext extends ImageCompressionPluginContext = ImageCompressionPluginContext\n>(\n options: ImageCompressionPluginOptions = {}\n): Plugin<TContext> => {\n return {\n name: \"image-compression\",\n config() {\n return {\n imageCompression: {\n filter:\n \"{root}/**/*.{svg,jpg,jpeg,png,webp,avif,heif,gif,tiff,jp2,jxl}\",\n svg: {},\n jpeg: {},\n png: {},\n webp: {},\n avif: {},\n heif: {},\n tiff: {},\n jp2: {},\n jxl: {},\n ...options\n }\n };\n },\n async configResolved() {\n if (!this.config.imageCompression.filter) {\n throw new Error(\n \"ImageCompression plugin requires a filter or glob pattern to be specified in the configuration.\"\n );\n }\n\n this.config.imageCompression.filter = toArray(\n this.config.imageCompression.filter\n )\n .map(path => replacePathTokens(this, path))\n .filter(Boolean);\n },\n async prepare() {\n await Promise.all(\n toArray(this.config.imageCompression.filter).map(async path => {\n await Promise.all(\n (await listFiles(path)).map(async file => {\n const outputPath = this.config.imageCompression.outputPath\n ? replacePathTokens(\n this,\n this.config.imageCompression.outputPath\n ).replace(/\\{fileName\\}/g, file.split(\"/\").pop() || \"output\")\n : file;\n\n const start = performance.now();\n const originalSize = (await stat(file)).size;\n let compressedSize = 0;\n\n const extension = findFileExtensionSafe(file).toLowerCase();\n if (!extension) {\n this.error(\n ` ✘ Failed to optimize ${file} - Unable to determine file extension`\n );\n return;\n }\n\n if (extension === \"svg\") {\n const result = optimize(await readFile(file), {\n multipass: true,\n path: file,\n ...this.config.imageCompression.svg\n });\n if (result.data) {\n compressedSize = Buffer.byteLength(result.data, \"utf8\");\n await writeFile(outputPath, result.data);\n }\n } else {\n let image = await import(\"sharp\").then(sharpModule =>\n sharpModule.default(file, { animated: extension === \"gif\" })\n );\n\n switch (extension) {\n case \"jpeg\":\n case \"jpg\":\n image = image.jpeg(this.config.imageCompression.jpeg);\n break;\n case \"png\":\n image = image.png(this.config.imageCompression.png);\n break;\n case \"webp\":\n image = image.webp(this.config.imageCompression.webp);\n break;\n case \"avif\":\n image = image.avif(this.config.imageCompression.avif);\n break;\n case \"gif\":\n image = image.gif(this.config.imageCompression.gif);\n break;\n case \"heif\":\n image = image.heif(this.config.imageCompression.heif);\n break;\n case \"tiff\":\n image = image.tiff(this.config.imageCompression.tiff);\n break;\n case \"jp2\":\n image = image.jp2(this.config.imageCompression.jp2);\n break;\n case \"jxl\":\n image = image.jxl(this.config.imageCompression.jxl);\n break;\n }\n\n const result = await image.toFile(outputPath);\n compressedSize = result.size;\n }\n\n if (!compressedSize) {\n this.error(` ✘ Failed to optimize ${file} - No data returned`);\n return;\n }\n\n if (compressedSize >= originalSize) {\n this.error(\n ` ✘ Failed to optimize ${file} - Compression output was not smaller than the original`\n );\n return;\n }\n\n this.info(\n ` ✔ Successfully optimized ${\n file\n }: ${chalk.redBright(prettyBytes(originalSize))} -> ${chalk.greenBright(\n prettyBytes(compressedSize)\n )} (${chalk.green(\n (100 - (compressedSize / originalSize) * 100).toFixed(2)\n )}%) in ${(performance.now() - start).toFixed(2)} ms`\n );\n })\n );\n })\n );\n }\n };\n};\n\nexport default plugin;\n"],"mappings":"ggBAmDA,MAAa,GAGX,EAAyC,EAAE,IAEpC,CACL,KAAM,oBACN,QAAS,CACP,MAAO,CACL,iBAAkB,CAChB,OACE,iEACF,IAAK,EAAE,CACP,KAAM,EAAE,CACR,IAAK,EAAE,CACP,KAAM,EAAE,CACR,KAAM,EAAE,CACR,KAAM,EAAE,CACR,KAAM,EAAE,CACR,IAAK,EAAE,CACP,IAAK,EAAE,CACP,GAAG,EACJ,CACF,EAEH,MAAM,gBAAiB,CACrB,GAAI,CAAC,KAAK,OAAO,iBAAiB,OAChC,MAAU,MACR,kGACD,CAGH,KAAK,OAAO,iBAAiB,OAAS,EACpC,KAAK,OAAO,iBAAiB,OAC9B,CACE,IAAI,GAAQ,EAAkB,KAAM,EAAK,CAAC,CAC1C,OAAO,QAAQ,EAEpB,MAAM,SAAU,CACd,MAAM,QAAQ,IACZ,EAAQ,KAAK,OAAO,iBAAiB,OAAO,CAAC,IAAI,KAAM,IAAQ,CAC7D,MAAM,QAAQ,KACX,MAAM,EAAU,EAAK,EAAE,IAAI,KAAM,IAAQ,CACxC,IAAM,EAAa,KAAK,OAAO,iBAAiB,WAC5C,EACE,KACA,KAAK,OAA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,IAAI,CACnD,MAIJ,GAAiB,MADI,EAAM,OAAO,EAAW,EACrB,KAG1B,GAAI,CAAC,EAAgB,CACnB,KAAK,MAAM,yBAAyB,EAAK,qBAAqB,CAC9D,OAGF,GAAI,GAAkB,EAAc,CAClC,KAAK,MACH,yBAAyB,EAAK,yDAC/B,CACD,OAGF,KAAK,KACH,6BACE,EACD,IAAI,EAAM,UAAU,EAAY,EAAa,CAAC,CAAC,MAAM,EAAM,YAC1D,EAAY,EAAe,CAC5B,CAAC,IAAI,EAAM,OACT,IAAO,EAAiB,EAAgB,KAAK,QAAQ,EAAE,CACzD,CAAC,SAAS,YAAY,KAAK,CAAG,GAAO,QAAQ,EAAE,CAAC,KAClD,EACD,CACH,EACD,CACH,EAEJ"}
1
+ {"version":3,"file":"index.mjs","names":[],"sources":["../src/index.ts"],"sourcesContent":["/* -------------------------------------------------------------------\n\n ⚡ Storm Software - Powerlines\n\n This code was released as part of the Powerlines project. Powerlines\n is maintained by Storm Software under the Apache-2.0 license, and is\n free for commercial and private use. For more information, please visit\n our licensing page at https://stormsoftware.com/licenses/projects/powerlines.\n\n Website: https://stormsoftware.com\n Repository: https://github.com/storm-software/powerlines\n Documentation: https://docs.stormsoftware.com/projects/powerlines\n Contact: https://stormsoftware.com/contact\n\n SPDX-License-Identifier: Apache-2.0\n\n ------------------------------------------------------------------- */\n\nimport { toArray } from \"@stryke/convert/to-array\";\nimport { listFiles } from \"@stryke/fs/list-files\";\nimport { readFile } from \"@stryke/fs/read-file\";\nimport { writeFile } from \"@stryke/fs/write-file\";\nimport { findFileExtensionSafe } from \"@stryke/path/find\";\nimport { prettyBytes } from \"@stryke/string-format/pretty-bytes\";\nimport chalk from \"chalk\";\nimport { Buffer } from \"node:buffer\";\nimport { stat } from \"node:fs/promises\";\nimport type { Plugin } from \"powerlines\";\nimport { replacePathTokens } from \"powerlines/plugin-utils\";\nimport { optimize } from \"svgo\";\nimport {\n ImageCompressionPluginContext,\n ImageCompressionPluginOptions\n} from \"./types/plugin\";\n\nexport * from \"./types\";\n\ndeclare module \"powerlines\" {\n interface Config {\n imageCompression?: ImageCompressionPluginOptions;\n }\n}\n\n/**\n * A Powerlines plugin to optimize image assets used by the project.\n *\n * @see https://sharp.pixelplumbing.com\n *\n * @param options - The plugin options.\n * @returns A Powerlines plugin instance.\n */\nexport const plugin = <\n TContext extends ImageCompressionPluginContext = ImageCompressionPluginContext\n>(\n options: ImageCompressionPluginOptions = {}\n): Plugin<TContext> => {\n return {\n name: \"image-compression\",\n config() {\n return {\n imageCompression: {\n filter:\n \"{root}/**/*.{svg,jpg,jpeg,png,webp,avif,heif,gif,tiff,jp2,jxl}\",\n svg: {},\n jpeg: {},\n png: {},\n webp: {},\n avif: {},\n heif: {},\n tiff: {},\n jp2: {},\n jxl: {},\n ...options\n }\n };\n },\n async configResolved() {\n if (!this.config.imageCompression.filter) {\n throw new Error(\n \"ImageCompression plugin requires a filter or glob pattern to be specified in the configuration.\"\n );\n }\n\n this.config.imageCompression.filter = toArray(\n this.config.imageCompression.filter\n )\n .map(path => replacePathTokens(this, path))\n .filter(Boolean);\n },\n async prepare() {\n await Promise.all(\n toArray(this.config.imageCompression.filter).map(async path => {\n await Promise.all(\n (await listFiles(path)).map(async file => {\n const outputPath = this.config.imageCompression.outputPath\n ? replacePathTokens(\n this,\n this.config.imageCompression.outputPath\n ).replace(/\\{fileName\\}/g, file.split(\"/\").pop() || \"output\")\n : file;\n\n const start = performance.now();\n const originalSize = (await stat(file)).size;\n let compressedSize = 0;\n\n const extension = findFileExtensionSafe(file).toLowerCase();\n if (!extension) {\n this.error(\n ` ✘ Failed to optimize ${file} - Unable to determine file extension`\n );\n return;\n }\n\n if (extension === \"svg\") {\n const result = optimize(await readFile(file), {\n multipass: true,\n path: file,\n ...this.config.imageCompression.svg\n });\n if (result.data) {\n compressedSize = Buffer.byteLength(result.data, \"utf8\");\n await writeFile(outputPath, result.data);\n }\n } else {\n let image = await import(\"sharp\").then(sharpModule =>\n sharpModule.default(file, { animated: extension === \"gif\" })\n );\n\n switch (extension) {\n case \"jpeg\":\n case \"jpg\":\n image = image.jpeg(this.config.imageCompression.jpeg);\n break;\n case \"png\":\n image = image.png(this.config.imageCompression.png);\n break;\n case \"webp\":\n image = image.webp(this.config.imageCompression.webp);\n break;\n case \"avif\":\n image = image.avif(this.config.imageCompression.avif);\n break;\n case \"gif\":\n image = image.gif(this.config.imageCompression.gif);\n break;\n case \"heif\":\n image = image.heif(this.config.imageCompression.heif);\n break;\n case \"tiff\":\n image = image.tiff(this.config.imageCompression.tiff);\n break;\n case \"jp2\":\n image = image.jp2(this.config.imageCompression.jp2);\n break;\n case \"jxl\":\n image = image.jxl(this.config.imageCompression.jxl);\n break;\n }\n\n const result = await image.toFile(outputPath);\n compressedSize = result.size;\n }\n\n if (!compressedSize) {\n this.error(` ✘ Failed to optimize ${file} - No data returned`);\n return;\n }\n\n if (compressedSize >= originalSize) {\n this.error(\n ` ✘ Failed to optimize ${file} - Compression output was not smaller than the original`\n );\n return;\n }\n\n this.info(\n ` ✔ Successfully optimized ${\n file\n }: ${chalk.redBright(prettyBytes(originalSize))} -> ${chalk.greenBright(\n prettyBytes(compressedSize)\n )} (${chalk.green(\n (100 - (compressedSize / originalSize) * 100).toFixed(2)\n )}%) in ${(performance.now() - start).toFixed(2)} ms`\n );\n })\n );\n })\n );\n }\n };\n};\n\nexport default plugin;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;AAmDA,MAAa,UAGX,UAAyC,EAAE,KACtB;AACrB,QAAO;EACL,MAAM;EACN,SAAS;AACP,UAAO,EACL,kBAAkB;IAChB,QACE;IACF,KAAK,EAAE;IACP,MAAM,EAAE;IACR,KAAK,EAAE;IACP,MAAM,EAAE;IACR,MAAM,EAAE;IACR,MAAM,EAAE;IACR,MAAM,EAAE;IACR,KAAK,EAAE;IACP,KAAK,EAAE;IACP,GAAG;IACJ,EACF;;EAEH,MAAM,iBAAiB;AACrB,OAAI,CAAC,KAAK,OAAO,iBAAiB,OAChC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gBAAQ,MAAM,KAAK,KAAK,OAAO,iBAAiB,KAAK;AACrD;OACF,KAAK;AACH,gBAAQ,MAAM,KAAK,KAAK,OAAO,iBAAiB,KAAK;AACrD;OACF,KAAK;AACH,gBAAQ,MAAM,IAAI,KAAK,OAAO,iBAAiB,IAAI;AACnD;OACF,KAAK;AACH,gBAAQ,MAAM,KAAK,KAAK,OAAO,iBAAiB,KAAK;AACrD;OACF,KAAK;AACH,gBAAQ,MAAM,KAAK,KAAK,OAAO,iBAAiB,KAAK;AACrD;OACF,KAAK;AACH,gBAAQ,MAAM,IAAI,KAAK,OAAO,iBAAiB,IAAI;AACnD;OACF,KAAK;AACH,gBAAQ,MAAM,IAAI,KAAK,OAAO,iBAAiB,IAAI;AACnD;;AAIJ,wBAAiB,MADI,MAAM,OAAO,WAAW,EACrB;;AAG1B,SAAI,CAAC,gBAAgB;AACnB,WAAK,MAAM,yBAAyB,KAAK,qBAAqB;AAC9D;;AAGF,SAAI,kBAAkB,cAAc;AAClC,WAAK,MACH,yBAAyB,KAAK,yDAC/B;AACD;;AAGF,UAAK,KACH,6BACE,KACD,IAAI,MAAM,UAAU,YAAY,aAAa,CAAC,CAAC,MAAM,MAAM,YAC1D,YAAY,eAAe,CAC5B,CAAC,IAAI,MAAM,OACT,MAAO,iBAAiB,eAAgB,KAAK,QAAQ,EAAE,CACzD,CAAC,SAAS,YAAY,KAAK,GAAG,OAAO,QAAQ,EAAE,CAAC,KAClD;MACD,CACH;KACD,CACH;;EAEJ"}
@@ -1 +1 @@
1
- export{};
1
+ export { };
@@ -1 +1 @@
1
- export{};
1
+ export { };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@powerlines/plugin-image-compression",
3
- "version": "0.2.406",
3
+ "version": "0.2.408",
4
4
  "private": false,
5
5
  "description": "A Powerlines plugin to optimize images used by the project.",
6
6
  "keywords": [
@@ -103,15 +103,15 @@
103
103
  "chalk": "5.6.2",
104
104
  "defu": "^6.1.7",
105
105
  "jiti": "^2.6.1",
106
- "powerlines": "^0.46.4",
106
+ "powerlines": "^0.46.6",
107
107
  "sharp": "^0.34.5",
108
108
  "svgo": "^4.0.1"
109
109
  },
110
110
  "devDependencies": {
111
- "@powerlines/plugin-plugin": "^0.12.408",
111
+ "@powerlines/plugin-plugin": "^0.12.410",
112
112
  "@storm-software/config": "^1.137.34",
113
113
  "@types/node": "^25.6.0"
114
114
  },
115
115
  "publishConfig": { "access": "public" },
116
- "gitHead": "2e5eff0d36cb4a1a54242017a16c5af5d9ce2cc3"
116
+ "gitHead": "610c4c943933458c5f433b9002d8c187625b2d3f"
117
117
  }