UNPKG

@modern-js/builder-plugin-image-compress

Version:

Image compress plugin for modern-js/web-builder

112 lines (111 loc) 5.01 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var minimizer_exports = {}; __export(minimizer_exports, { MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME: () => MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME, ModernJsImageMinimizerPlugin: () => ModernJsImageMinimizerPlugin }); module.exports = __toCommonJS(minimizer_exports); var import_buffer = require("buffer"); var import_codecs = __toESM(require("./shared/codecs")); const MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME = "@modern-js/builder-plugin-image-compress/minimizer"; class ModernJsImageMinimizerPlugin { async optimize(compiler, compilation, assets) { const cache = compilation.getCache(MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME); const { RawSource } = compiler.webpack.sources; const { matchObject } = compiler.webpack.ModuleFilenameHelpers; const buildError = (error, file, context) => { const cause = error instanceof Error ? error : new Error(); const message = file && context ? `"${file}" in "${context}" from Modern.js Image Minimizer: ${cause.message}` : cause.message; const ret = new compiler.webpack.WebpackError(message); error instanceof Error && (ret.error = error); return ret; }; const codec = import_codecs.default[this.options.use]; if (!codec) { compilation.errors.push(buildError(new Error(`Codec ${this.options.use} is not supported`))); } const opts = { ...codec.defaultOptions, ...this.options }; const handleAsset = async (name) => { var _compilation_getAsset; const info = (_compilation_getAsset = compilation.getAsset(name)) === null || _compilation_getAsset === void 0 ? void 0 : _compilation_getAsset.info; const fileName = name.split("?")[0]; if ((info === null || info === void 0 ? void 0 : info.minimized) || !matchObject(opts, fileName)) { return; } const { source: inputSource } = compilation.getAsset(name); const eTag = cache.getLazyHashedEtag(inputSource); const cacheItem = cache.getItemCache(name, eTag); let result = await cacheItem.getPromise(); try { if (!result) { const input = inputSource.source(); const buf = await codec.handler(import_buffer.Buffer.from(input), opts); result = { source: new RawSource(buf) }; await cacheItem.storePromise(result); } compilation.updateAsset(name, result.source, { minimized: true }); } catch (error) { compilation.errors.push(buildError(error, name, compiler.context)); } }; const promises = Object.keys(assets).map((name) => handleAsset(name)); await Promise.all(promises); } apply(compiler) { const handleCompilation = (compilation) => { compilation.hooks.processAssets.tapPromise({ name: this.name, stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_SIZE, additionalAssets: true }, (assets) => this.optimize(compiler, compilation, assets)); compilation.hooks.statsPrinter.tap(this.name, (stats) => { stats.hooks.print.for("asset.info.minimized").tap("@modern-js/builder-plugin-image-compress", (minimized, { green, formatFlag }) => minimized && green && formatFlag ? green(formatFlag("minimized")) : ""); }); }; compiler.hooks.compilation.tap(this.name, handleCompilation); } constructor(options) { this.name = MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME; this.options = options; } } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { MODERN_JS_IMAGE_MINIMIZER_PLUGIN_NAME, ModernJsImageMinimizerPlugin });