@powerlines/plugin-image-compression 0.2.201 → 0.2.202
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.cjs +1 -1
- package/dist/index.mjs +1 -1
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
package/dist/index.cjs
CHANGED
|
@@ -40,7 +40,7 @@ let node_fs_promises = require("node:fs/promises");
|
|
|
40
40
|
let powerlines_plugin_utils_paths = require("powerlines/plugin-utils/paths");
|
|
41
41
|
let svgo = require("svgo");
|
|
42
42
|
|
|
43
|
-
//#region ../../node_modules/.pnpm/@stryke+string-format@0.13.
|
|
43
|
+
//#region ../../node_modules/.pnpm/@stryke+string-format@0.13.7/node_modules/@stryke/string-format/dist/pretty-bytes.mjs
|
|
44
44
|
const BYTE_UNITS = [
|
|
45
45
|
"B",
|
|
46
46
|
"kB",
|
package/dist/index.mjs
CHANGED
|
@@ -11,7 +11,7 @@ import { stat } from "node:fs/promises";
|
|
|
11
11
|
import { replacePathTokens } from "powerlines/plugin-utils/paths";
|
|
12
12
|
import { optimize } from "svgo";
|
|
13
13
|
|
|
14
|
-
//#region ../../node_modules/.pnpm/@stryke+string-format@0.13.
|
|
14
|
+
//#region ../../node_modules/.pnpm/@stryke+string-format@0.13.7/node_modules/@stryke/string-format/dist/pretty-bytes.mjs
|
|
15
15
|
const BYTE_UNITS = [
|
|
16
16
|
"B",
|
|
17
17
|
"kB",
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","names":[],"sources":["../../../node_modules/.pnpm/@stryke+string-format@0.13.6/node_modules/@stryke/string-format/dist/pretty-bytes.mjs","../src/index.ts"],"sourcesContent":["//#region src/pretty-bytes.ts\nconst BYTE_UNITS = [\n\t\"B\",\n\t\"kB\",\n\t\"MB\",\n\t\"GB\",\n\t\"TB\",\n\t\"PB\",\n\t\"EB\",\n\t\"ZB\",\n\t\"YB\"\n];\nconst BIBYTE_UNITS = [\n\t\"B\",\n\t\"KiB\",\n\t\"MiB\",\n\t\"GiB\",\n\t\"TiB\",\n\t\"PiB\",\n\t\"EiB\",\n\t\"ZiB\",\n\t\"YiB\"\n];\nconst BIT_UNITS = [\n\t\"b\",\n\t\"kbit\",\n\t\"Mbit\",\n\t\"Gbit\",\n\t\"Tbit\",\n\t\"Pbit\",\n\t\"Ebit\",\n\t\"Zbit\",\n\t\"Ybit\"\n];\nconst BIBIT_UNITS = [\n\t\"b\",\n\t\"kibit\",\n\t\"Mibit\",\n\t\"Gibit\",\n\t\"Tibit\",\n\t\"Pibit\",\n\t\"Eibit\",\n\t\"Zibit\",\n\t\"Yibit\"\n];\n/**\n* Formats the given number using `Number#toLocaleString`.\n*\n* @remarks\n* - If locale is a string, the value is expected to be a locale-key (for example: `de`).\n* - If locale is true, the system default locale is used for translation.\n* - If no value for locale is specified, the number is returned unmodified.\n*\n* @param number - The number to format.\n* @param locale - The locale to use for formatting the number.\n* @param options - The options to use for formatting the number.\n* @returns The formatted number.\n*/\nconst toLocaleString = (number, locale, options = {}) => {\n\tlet result = number;\n\tlet _locale = locale;\n\tif (typeof _locale === \"string\") {\n\t\tif (!_locale) _locale = process.env.STORM_LOCALE || \"en-US\";\n\t\tif (Array.isArray(_locale)) result = number?.toLocaleString(_locale, options);\n\t} else if (_locale === true || options !== void 0) result = number?.toLocaleString(void 0, options);\n\treturn String(result);\n};\n/**\n* Convert bytes to a human readable string: `1337` → `1.34 kB`.\n*\n* @param number - The number to format.\n*\n* @example\n* ```ts\n* import { prettyBytes } from '@stryke/string-fns/pretty-bytes';\n*\n* prettyBytes(1337);\n* //=> '1.34 kB'\n*\n* prettyBytes(100);\n* //=> '100 B'\n*\n* // Display file size differences\n* prettyBytes(42, {signed: true});\n* //=> '+42 B'\n*\n* // Localized output using German locale\n* prettyBytes(1337, {locale: 'de'});\n* //=> '1,34 kB'\n* ```\n*\n* @param number - The number to format.\n* @param options - The options to use.\n* @returns The formatted string.\n*/\nfunction prettyBytes(number, options) {\n\tlet _number = number;\n\tif (!Number.isFinite(_number)) throw new TypeError(`Expected a finite number, got ${typeof _number}: ${_number}`);\n\tconst opts = {\n\t\tbits: false,\n\t\tbinary: false,\n\t\tspace: true,\n\t\t...options\n\t};\n\tconst UNITS = opts.bits ? opts.binary ? BIBIT_UNITS : BIT_UNITS : opts.binary ? BIBYTE_UNITS : BYTE_UNITS;\n\tconst separator = opts.space ? \" \" : \"\";\n\tif (opts.signed && _number === 0) return ` 0${separator}${UNITS[0]}`;\n\tconst isNegative = _number < 0;\n\tconst prefix = isNegative ? \"-\" : opts.signed ? \"+\" : \"\";\n\tif (isNegative) _number = -_number;\n\tlet localeOptions;\n\tif (opts.minimumFractionDigits !== void 0) localeOptions = { minimumFractionDigits: opts.minimumFractionDigits };\n\tif (opts.maximumFractionDigits !== void 0) localeOptions = {\n\t\tmaximumFractionDigits: opts.maximumFractionDigits,\n\t\t...localeOptions\n\t};\n\tif (_number < 1) return prefix + toLocaleString(_number, opts.locale, localeOptions) + separator + UNITS[0];\n\tconst exponent = Math.min(Math.floor(opts.binary ? Math.log(_number) / Math.log(1024) : Math.log10(_number) / 3), UNITS.length - 1);\n\t_number /= (opts.binary ? 1024 : 1e3) ** exponent;\n\tif (!localeOptions) _number = _number.toPrecision(3);\n\tconst numberString = toLocaleString(Number(_number), opts.locale, localeOptions);\n\tconst unit = UNITS[exponent];\n\treturn prefix + numberString + separator + unit;\n}\n\n//#endregion\nexport { prettyBytes, toLocaleString };\n//# sourceMappingURL=pretty-bytes.mjs.map","/* -------------------------------------------------------------------\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 { replacePathTokens } from \"powerlines/plugin-utils/paths\";\nimport type { Plugin } from \"powerlines/types/plugin\";\nimport { optimize } from \"svgo\";\nimport {\n ImageCompressionPluginContext,\n ImageCompressionPluginOptions\n} from \"./types/plugin\";\n\nexport * from \"./types\";\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 \"{projectRoot}/**/*.{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};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,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,wBADe,MAAM,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;;AAGH,kBAAe"}
|
|
1
|
+
{"version":3,"file":"index.mjs","names":[],"sources":["../../../node_modules/.pnpm/@stryke+string-format@0.13.7/node_modules/@stryke/string-format/dist/pretty-bytes.mjs","../src/index.ts"],"sourcesContent":["//#region src/pretty-bytes.ts\nconst BYTE_UNITS = [\n\t\"B\",\n\t\"kB\",\n\t\"MB\",\n\t\"GB\",\n\t\"TB\",\n\t\"PB\",\n\t\"EB\",\n\t\"ZB\",\n\t\"YB\"\n];\nconst BIBYTE_UNITS = [\n\t\"B\",\n\t\"KiB\",\n\t\"MiB\",\n\t\"GiB\",\n\t\"TiB\",\n\t\"PiB\",\n\t\"EiB\",\n\t\"ZiB\",\n\t\"YiB\"\n];\nconst BIT_UNITS = [\n\t\"b\",\n\t\"kbit\",\n\t\"Mbit\",\n\t\"Gbit\",\n\t\"Tbit\",\n\t\"Pbit\",\n\t\"Ebit\",\n\t\"Zbit\",\n\t\"Ybit\"\n];\nconst BIBIT_UNITS = [\n\t\"b\",\n\t\"kibit\",\n\t\"Mibit\",\n\t\"Gibit\",\n\t\"Tibit\",\n\t\"Pibit\",\n\t\"Eibit\",\n\t\"Zibit\",\n\t\"Yibit\"\n];\n/**\n* Formats the given number using `Number#toLocaleString`.\n*\n* @remarks\n* - If locale is a string, the value is expected to be a locale-key (for example: `de`).\n* - If locale is true, the system default locale is used for translation.\n* - If no value for locale is specified, the number is returned unmodified.\n*\n* @param number - The number to format.\n* @param locale - The locale to use for formatting the number.\n* @param options - The options to use for formatting the number.\n* @returns The formatted number.\n*/\nconst toLocaleString = (number, locale, options = {}) => {\n\tlet result = number;\n\tlet _locale = locale;\n\tif (typeof _locale === \"string\") {\n\t\tif (!_locale) _locale = process.env.STORM_LOCALE || \"en-US\";\n\t\tif (Array.isArray(_locale)) result = number?.toLocaleString(_locale, options);\n\t} else if (_locale === true || options !== void 0) result = number?.toLocaleString(void 0, options);\n\treturn String(result);\n};\n/**\n* Convert bytes to a human readable string: `1337` → `1.34 kB`.\n*\n* @param number - The number to format.\n*\n* @example\n* ```ts\n* import { prettyBytes } from '@stryke/string-fns/pretty-bytes';\n*\n* prettyBytes(1337);\n* //=> '1.34 kB'\n*\n* prettyBytes(100);\n* //=> '100 B'\n*\n* // Display file size differences\n* prettyBytes(42, {signed: true});\n* //=> '+42 B'\n*\n* // Localized output using German locale\n* prettyBytes(1337, {locale: 'de'});\n* //=> '1,34 kB'\n* ```\n*\n* @param number - The number to format.\n* @param options - The options to use.\n* @returns The formatted string.\n*/\nfunction prettyBytes(number, options) {\n\tlet _number = number;\n\tif (!Number.isFinite(_number)) throw new TypeError(`Expected a finite number, got ${typeof _number}: ${_number}`);\n\tconst opts = {\n\t\tbits: false,\n\t\tbinary: false,\n\t\tspace: true,\n\t\t...options\n\t};\n\tconst UNITS = opts.bits ? opts.binary ? BIBIT_UNITS : BIT_UNITS : opts.binary ? BIBYTE_UNITS : BYTE_UNITS;\n\tconst separator = opts.space ? \" \" : \"\";\n\tif (opts.signed && _number === 0) return ` 0${separator}${UNITS[0]}`;\n\tconst isNegative = _number < 0;\n\tconst prefix = isNegative ? \"-\" : opts.signed ? \"+\" : \"\";\n\tif (isNegative) _number = -_number;\n\tlet localeOptions;\n\tif (opts.minimumFractionDigits !== void 0) localeOptions = { minimumFractionDigits: opts.minimumFractionDigits };\n\tif (opts.maximumFractionDigits !== void 0) localeOptions = {\n\t\tmaximumFractionDigits: opts.maximumFractionDigits,\n\t\t...localeOptions\n\t};\n\tif (_number < 1) return prefix + toLocaleString(_number, opts.locale, localeOptions) + separator + UNITS[0];\n\tconst exponent = Math.min(Math.floor(opts.binary ? Math.log(_number) / Math.log(1024) : Math.log10(_number) / 3), UNITS.length - 1);\n\t_number /= (opts.binary ? 1024 : 1e3) ** exponent;\n\tif (!localeOptions) _number = _number.toPrecision(3);\n\tconst numberString = toLocaleString(Number(_number), opts.locale, localeOptions);\n\tconst unit = UNITS[exponent];\n\treturn prefix + numberString + separator + unit;\n}\n\n//#endregion\nexport { prettyBytes, toLocaleString };\n//# sourceMappingURL=pretty-bytes.mjs.map","/* -------------------------------------------------------------------\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 { replacePathTokens } from \"powerlines/plugin-utils/paths\";\nimport type { Plugin } from \"powerlines/types/plugin\";\nimport { optimize } from \"svgo\";\nimport {\n ImageCompressionPluginContext,\n ImageCompressionPluginOptions\n} from \"./types/plugin\";\n\nexport * from \"./types\";\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 \"{projectRoot}/**/*.{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"],"x_google_ignoreList":[0],"mappings":";;;;;;;;;;;;;;AACA,MAAM,aAAa;CAClB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACD,MAAM,eAAe;CACpB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACD,MAAM,YAAY;CACjB;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;CACA;AACD,MAAM,cAAc;CACnB;CACA;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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;;AAGH,kBAAe"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powerlines/plugin-image-compression",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.202",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A Powerlines plugin to optimize images used by the project.",
|
|
6
6
|
"repository": {
|
|
@@ -95,22 +95,22 @@
|
|
|
95
95
|
"powerlines-plugin"
|
|
96
96
|
],
|
|
97
97
|
"dependencies": {
|
|
98
|
-
"@stryke/convert": "^0.6.
|
|
99
|
-
"@stryke/fs": "^0.33.
|
|
100
|
-
"@stryke/path": "^0.26.
|
|
98
|
+
"@stryke/convert": "^0.6.38",
|
|
99
|
+
"@stryke/fs": "^0.33.40",
|
|
100
|
+
"@stryke/path": "^0.26.4",
|
|
101
101
|
"chalk": "5.6.2",
|
|
102
102
|
"defu": "^6.1.4",
|
|
103
103
|
"jiti": "^2.6.1",
|
|
104
|
-
"powerlines": "^0.38.
|
|
104
|
+
"powerlines": "^0.38.20",
|
|
105
105
|
"sharp": "^0.34.5",
|
|
106
106
|
"svgo": "^4.0.0"
|
|
107
107
|
},
|
|
108
108
|
"devDependencies": {
|
|
109
|
-
"@powerlines/plugin-plugin": "^0.12.
|
|
109
|
+
"@powerlines/plugin-plugin": "^0.12.204",
|
|
110
110
|
"@storm-software/config": "^1.135.1",
|
|
111
111
|
"@types/node": "^24.10.9"
|
|
112
112
|
},
|
|
113
113
|
"publishConfig": { "access": "public" },
|
|
114
114
|
"types": "./dist/index.d.cts",
|
|
115
|
-
"gitHead": "
|
|
115
|
+
"gitHead": "54a89805dd69f77013ebbd3282232563d7af4e7b"
|
|
116
116
|
}
|