Skip to content

Commit

Permalink
A extension to add support for imagemin images compression
Browse files Browse the repository at this point in the history
  • Loading branch information
JayaKrishnaNamburu committed Mar 18, 2022
1 parent 67b305a commit 6232467
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/imagemin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# RollupJS Extension

**Task Definitions**: _None_<br />
**Template Definitions**: _['imagemin'](#imagemin-template)_<br />
**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 = ['[email protected]: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.
56 changes: 56 additions & 0 deletions imagemin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
Chomp.addExtension("[email protected]: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,
},
},
]),
];
});

0 comments on commit 6232467

Please sign in to comment.