Gathering detailed insights and metrics for @mapbox/appropriate-images
Gathering detailed insights and metrics for @mapbox/appropriate-images
Gathering detailed insights and metrics for @mapbox/appropriate-images
Gathering detailed insights and metrics for @mapbox/appropriate-images
Generate appropriately resized and optimized images for your website, using a configuration that can be shared with client-side libraries.
npm install @mapbox/appropriate-images
Typescript
Module System
Min. Node Version
Node Version
NPM Version
JavaScript (99.8%)
Shell (0.2%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
21 Stars
83 Commits
5 Forks
104 Watchers
2 Branches
615 Contributors
Updated on Apr 10, 2024
Latest Version
6.1.0
Package Id
@mapbox/appropriate-images@6.1.0
Unpacked Size
22.98 kB
Size
7.80 kB
File Count
10
NPM Version
9.6.6
Node Version
18.15.0
Published on
Nov 01, 2023
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
Generate appropriately resized and optimized images for your website, using a configuration object that can be shared with client-side libraries.
Images are resized with sharp, then each size variant is optimized (including the creation of a webp
version) with imagemin plugins.
@mapbox/appropriate-images-get-url can then be used in the browser to determine which size variant of an image to render, at run time, given an image configuration and the available width.
@mapbox/appropriate-images-react can be used to do this in React, with a component that automatically measures its own available width.
npm install @mapbox/appropriate-images
appropriateImages.generate(imageConfig, [options])
Returns a Promise that resolves with an array of filenames for the resized and optimized images that have been written to your output directory.
When the Promise rejects, it may reject with a single error or an array of errors.
For each image this function will:
png
) and webp
.Output filenames have suffixes corresponding to the size of the variant. For example, with the following property in your image configuration:
1{ 2 bear: { 3 basename: 'bear.png', 4 sizes: [ 5 { width: 300 }, 6 { width: 600, height: 200 } 7 ] 8 } 9 /* ... */ 10}
You will get files with the following basenames:
bear-300.png
bear-300.webp
bear-600x200.png
bear-600x200.webp
1// Example 2const appropriateImages = require('@mapbox/appropriate-images'); 3const myImageConfig = require('../path/to/my/image/config.js'); 4 5appropriateImages.generate(myImageConfig, { 6 inputDirectory: '../path/to/my/source/image/directory/', 7 outputDirectory: '../path/to/directory/where/i/want/resized/optimized/images/' 8}) 9 .then(output => { 10 console.log('You generated all these images:'); 11 console.log(output); 12 }).catch(errors => { 13 if (Array.isArray(errors)) { 14 errors.forEach(err => console.error(err.stack)); 15 } else { 16 console.error(errors.stack); 17 } 18 });
Type: Object
.
An image configuration object. Options for this configuration are documented below.
Type: string
.
Required.
Path to your directory of source images.
Each basename
in your image configuration should be relative to this directory.
Type: string
.
Required.
Path to the directory where resized, optimized images should be written.
Type: Array<string>
.
Ids of images to be processed. Image ids correspond to keys in the image configuration. If this option not provided, all images in the configuration will be processed.
Type: Object
.
Options for imagemin-pngquant.
Type: Object
.
Type: Object
.
appropriateImages.createCli(imageConfig, [options])
Executes a CLI for your specific directory structure.
The CLI runs generate
using your specified configuration.
It provides a quick way to generate and re-generate images, with nice error handling.
The arguments are the same as for generate
.
appropriate-images exposes the createCli
function instead of an actual CLI because it is not convenient to completely configure generate
from the command line, and your configuration should stay constant within a project.
With createCli
, you can define your configuration within a JS file, then run that JS file as a CLI.
1#!/usr/bin/env node 2'use strict'; 3 4const appropriateImages = require('@mapbox/appropriate-images'); 5const myImageConfig = require('../path/to/my/image/config.js'); 6 7const myOptions = { 8 inputDirectory: '../path/to/my/source/image/directory/', 9 outputDirectory: '../path/to/directory/where/i/want/resized/optimized/images/' 10}; 11 12appropriateImages.createCli(myImageConfig, myOptions);
Don't forget to chmod +x path/to/file
to make it executable.
Then you can run it as a CLI:
1my-appropriate-images horse bear pig 2 3my-appropriate-images --all --quiet
The image configuration is an object. For every property:
Each image's configuration object includes the following properties.
Type string
.
Required.
The path from options.inputDirectory
to the image (including the image's extension).
Type: Array<Object>
.
Required.
An array of objects representing sizes. Each size must include a width
, and can optionally include other properties.
Type: number
.
Required.
A width for the generated image.
Type: number
.
A height for the generated image.
If no height
is provided, the width
is used and the image's aspect ratio is preserved.
If a height
is provided and it does not fit the image's aspect ratio, the image will be cropped.
Type: object
.
Default: { fit: "cover" }
Defines the manner how the image should fit if both width
and height
are provided. See resize.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
branch protection is not maximal on development and all release branches
Details
Reason
Found 2/10 approved changesets -- score normalized to 2
Reason
project is archived
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
11 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More