From 62324670379df38caff8904f9a53068f7b14db0d Mon Sep 17 00:00:00 2001 From: JayaKrishnaNamburu Date: Fri, 18 Mar 2022 18:52:21 +0530 Subject: [PATCH] A extension to add support for imagemin images compression --- docs/imagemin.md | 35 ++++++++++++++++++++++++++++++ imagemin.js | 56 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 docs/imagemin.md create mode 100644 imagemin.js diff --git a/docs/imagemin.md b/docs/imagemin.md new file mode 100644 index 0000000..b585943 --- /dev/null +++ b/docs/imagemin.md @@ -0,0 +1,35 @@ +# RollupJS Extension + +**Task Definitions**: _None_
+**Template Definitions**: _['imagemin'](#imagemin-template)_
+**Batcher Definitions**: _None_ + +Performs compression on images with the default settings from `imagemin-cli` + +### Template Options + +* `auto-install` (_Boolean_, default: `true`): Whether to automatically install `rollup` if not present (using the [npm extension](npm.md)). The global npm extension `auto-install` option will take precedence here if not otherwise set. +* `entry` (_String_): List of application entry points. +* `outdir` (_String_): The build output directory. +* `plugins` (_String_): Any valid imagemin plugins (eg: `mozjpeg`) + +### Example + +_chompfile.toml_ +```toml +version = 0.1 + +extensions = ['chomp@0.1:rollup'] + +[[task]] +name = 'build:assets' +deps = ["public/assets"] +template = 'imagemin' +[task.template-options] +plugins = ['mozjpeg'] +entry = 'public/assets' +``` + +## Roadmap + +This plugin is very simple currently and needs to be extended to support comprehensive Imagemin pipelines and customization options. diff --git a/imagemin.js b/imagemin.js new file mode 100644 index 0000000..207c30c --- /dev/null +++ b/imagemin.js @@ -0,0 +1,56 @@ +Chomp.addExtension("chomp@0.1:npm"); + +Chomp.registerTemplate("imagemin", function (task) { + const { + outdir = "dist", + entry, + autoInstall, + plugins = [], + } = task.templateOptions; + if (!entry) { + throw new Error(`Images entry is mandatory`); + } + + const imageminPlugins = plugins.map((plugin) => `imagemin-${plugin}`); + const statement = `imagemin ${entry}/* --out-dir=${outdir}/${entry} ${ + plugins.length > 0 + ? plugins.reduce((acc, plugin) => { + acc = acc + `--plugin=${plugin}`; + return acc; + }, "") + : "" + }`; + + return [ + { + name: task.name, + deps: [ + ...task.deps, + ...(ENV.CHOMP_EJECT + ? ["npm:install"] + : [ + "node_modules/imagemin-cli", + /* + While reading through cli imagemin don't need prefix + https://github.com/imagemin/imagemin-cli/blob/main/cli.js#L55 + */ + ...imageminPlugins.map((plugin) => `node_modules/${plugin}`), + ]), + ], + targets: [...new Set([...task.targets])], + run: statement, + }, + ...(ENV.CHOMP_EJECT + ? [] + : [ + { + template: "npm", + templateOptions: { + autoInstall, + packages: ["imagemin-cli", ...imageminPlugins], + dev: true, + }, + }, + ]), + ]; +});