The main purpose of this tool is to automate image optimization and analysis. This project is recent, so use it with care, I'm listening to all feedback (we can talk via twitter, don't follow me I never tweet).
What is the difference with ImageMin? I think that if you deal with large applications, then go on ImageMin, but if you are building small static websites and you want to optimize your resources easily, then you could try lepto.
You give to lepto your input and output directories, the plugins you want to use and their options. Then lepto does his job, you keep your original files and the structure of the input directory. Some plugins can even collect data (like primary colors) from your images and save them in a json file.
If you want to learn more about image optimizations, I recommend you the amazing images.guide by Addy Osmani.
Design and illustrations by Dorian Colin.
I recommend you to use lepto via lepto-cli, so it can easily be integrated to your build process with npm scripts.
$ npm i -g lepto-cli
Then you can follow the setup process:
$ lepto setup
It will guide you to create a configuration file.
Check out lepto-cli repository for more information.
See below for Node.js API usage.
You can access the GUI if you launched lepto from the CLI, by default at the address http://localhost:4490
. You can change the port with the option guiPort
.
The purpose of the GUI is to add more precise quality settings to files one by one. You can easily play with the quality slider and see the result at the same time, so you can choose the most suitable option for each of your resources.
You can also edit your filters and plugins configuration thought the interface.
To save the changes and relaunch lepto's process, click on the Save button or press ⌘S / Ctrl+S.
You have to say to lepto how it will process your files. So you set in your config a filters
array containing several groups of plugins associated with their glob
. For each image, Lepto will go thought the list of filters and test if the file match each of the plugins groups. If it matches the same plugin several times, the last one will overwrite the parameters of the previous ones, but the plugin will keep its place in the order of process. Example:
/* letpo.config.json */
{
"input": "assets/input",
"output": "assets/output",
"watch": true,
"filters": [
{
"glob": "**/*.*",
"use": [
{
"name": "lepto.jpeg",
"quality": 80
}, {
"name": "lepto-resize",
"maxWidth": 1200
}
]
}, {
"glob": "*.jpg",
"use": [
{
"name": "lepto-resize",
"height": 100
}, {
"name": "lepto.jpeg",
"quality": 60
}
]
}
]
}
The file photo.jpg
, will be processed with this list of plugins:
[
{
"name": "lepto.jpeg",
"quality": 60
}, {
"name": "lepto-resize",
"height": 100
}
]
So the first order of appearance is preserved, but the settings are overridden by the last ones.
If you want to use a plugin more than one time, you can add #
at the end of its name, so it's creating like a "new plugin", eg: "lepto-resize#retina"
.
You can disable a plugin by setting its disabled
option to true
.
Lepto carries some built-in plugins, their name is prefixed by "lepto."
. Theses plugins doesn't create more files than they receives. Their only goal is to optimize files size, they can't ouput a larger file.
It use sharp
. Default config:
{
"quality": 80, /* From 1 to 100 */
"progressive": true,
"forceExt": null /* You can force the same extension for all jpgs file, eg: replace all .jpeg images by setting forceExt to "jpg" */
}
It use node-pngquant
. Default config:
{
"quality": "70-80", /* From 0 (worst) to 100 (better) */
"colors": 256, /* From 2 to 256 */
"speed": 3 /* From 1 (faster but heavier) to 10 (slower but lighter) */
}
It use ImageMin's implementation of gifsicle: gifsicle
. Default config:
{
"colors": 256 /* From 2 to 256 */
}
It use svgo
, and his config follows the SVGO's config.
lepto-resize
To resize and create retina alternativeslepto-webp
To create .webp alternativeslepto-vibrant-color
To collect the vibrant colors from your images usingnode-vibrant
and save them inside your data json file. So you can set a placeholder background color to your images while they are loading.
Default config:
{
"watch": false, /* Watch for input file changes */
"watchConfig": false, /* Watch for config file change to automatically update it */
"followUnlink": false, /* Remove output files when the source file is deleted from the input directory */
"processAll": true, /* Process all files on launch, recommended if followUnlink activated */
"gui": true, /* The GUI can be disabled */
"openGui": false, /* Automatically open the GUI in your default browser */
"guiPort": "4490", /* GUI port */
"logLevel": "all", /* all 0-3 (0: silent, 1: only errors, ..., 3: all) */
"dataOutput": null, /* Path of your data json file, eg: output/data.json */
"dataRootPath": null /* Relative path removed from file names inside the data json file */
}
Lepto watch files by default when launched with lepto-cli.
$ npm i -D lepto
You simply have to call lepto()
by giving it your config.
const lepto = require('lepto');
const runner = lepto({
input: 'assets/input',
output: 'assets/output',
filters: [
]
/* ...config */
})
Now you can listen to events with the on(event, callback)
method, the events are all
for all events, success
, info
, warn
, error
and processed-file
.
The success
, info
and warn
events gives an object with a msg
inside, eg: { msg: 'Info message' }
.
The error
just gives a string of the error message.
The processed-file
gives an object with informations about the file process:
{
adj: 'new', /* file watch event: '' (initial process), 'new' or 'changed' */
input: 'icons/github.png',
inputSize: 1000000, /* sizes in bytes */
output: [ 'icons/github.png', 'icons/[email protected]' ],
outputSizes: [ 20000, 50000 ],
timeSpent: 300 /* process time in ms */
}
Example of events integration:
runner.on('error', msg => {
console.error(msg);
});
runner.on('processed-file', data => {
/* deal with data */
});
runner.on('all', (data, event) => { /* When listening to 'all' events, the callback receive the event name as a second argument */
if (typeof data.msg !== 'undefined') {
console.log(`${event}: ${data.msg}`);
}
});
I really like to save people time. That's why I created this tool:
- To help developers easily optimize their images
- To make sites load faster
So if you have any suggestion that could help people use this tool faster, tell me!
Run npm test
for testing the tool.
There is only a build step for the GUI part that can be launched with the npm start
command. It will watch for css and js files changes from the gui/src/
directory and compiles them into gui/dist/
with Babel and PostCSS.
Because you could ask yourself the question: I love React but I didn't used it for the GUI because I had planned to deal with many contenteditable
elements that are terrible to work with React. The part of the is messy I admit it, I have to tidy up.
A lepto plugin has to deal with multiples output files associated to one input file, a plugin is a function called with the plugins options that must return a function that will process the files. This last function receive an input
object, a fulfill
method and an object of utils
methods.
The input
object looks like that:
input = {
input: 'icons/social/github.png',
outputs: [
{
dir: 'icons/social',
filename: 'github.png',
buffer: <Buffer>
},
{
dir: 'icons/social',
filename: '[email protected]',
buffer: <Buffer>
}
],
data: {}
};
If the plugin is the first called, it will receive only one output, additional outputs are created by others plugins.
A data object is shared between plugins during the process of files, his content will be saved to a json file choosed by the user.
Because lepto plugins have to deal with multiples outputs Buffer and often with async process, I suggest you this model:
const plugin = (opts={}) => {
return function plugin(input, fulfill, utils) {
const next = () => {
finish--;
if (finish <= 0) {
fulfill(input);
}
};
let finish = input.outputs.length;
for (let i in input.outputs) {
optimizer(input.outputs[i].buffer).then(function(i) {
return function(buffer) {
input.outputs[i].buffer = buffer;
next();
};
}(i));
}
};
};
module.exports = plugin;
Utils functions:
utils.size(Buffer)
return an object like{ width: 100, height: 100 }
.utils.sharp(Buffer)
sharp node module.utils.mime(Buffer)
return the mime type as a string, eg:"image/jpeg"
, learn more here.utils.base(String)
return the base name of a file name, eg:"IMG001.JPG"
>"IMG001"
.utils.ext(String)
return the extension of a file name, eg:"IMG001.JPG"
>"JPG"
.
You can inspire yourself by the built-in plugins.
This project is licensed under the MIT license.