UNPKG

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

Version:

Image compress plugin for modern-js/web-builder

85 lines (84 loc) 3.02 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 src_exports = {}; __export(src_exports, { DEFAULT_OPTIONS: () => DEFAULT_OPTIONS, PluginImageCompress: () => PluginImageCompress, builderPluginImageCompress: () => builderPluginImageCompress }); module.exports = __toCommonJS(src_exports); var import_assert = __toESM(require("assert")); var import_minimizer = require("./minimizer"); var import_utils = require("./shared/utils"); const DEFAULT_OPTIONS = [ "jpeg", "png", "ico" ]; const castOptions = (args) => { const head = args[0]; if (Array.isArray(head)) { return head; } const ret = []; for (const arg of args) { (0, import_assert.default)(!Array.isArray(arg)); ret.push(arg); } return ret; }; const normalizeOptions = (options) => { const opts = options.length ? options : DEFAULT_OPTIONS; const normalized = opts.map((opt) => (0, import_utils.withDefaultOptions)(opt)); return normalized; }; const builderPluginImageCompress = (...args) => ({ name: "builder-plugin-image-compress", setup(api) { const opts = normalizeOptions(castOptions(args)); api.modifyBundlerChain((chain, { env }) => { if (env !== "production") { return; } chain.optimization.minimize(true); for (const opt of opts) { chain.optimization.minimizer(`image-compress-${opt.use}`).use(import_minimizer.ModernJsImageMinimizerPlugin, [ opt ]); } }); } }); const PluginImageCompress = builderPluginImageCompress; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { DEFAULT_OPTIONS, PluginImageCompress, builderPluginImageCompress });