Gathering detailed insights and metrics for webpack-image-resize-loader
Gathering detailed insights and metrics for webpack-image-resize-loader
Gathering detailed insights and metrics for webpack-image-resize-loader
Gathering detailed insights and metrics for webpack-image-resize-loader
npm install webpack-image-resize-loader
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
39 Stars
274 Commits
5 Forks
2 Watching
36 Branches
1 Contributors
Updated on 18 Jul 2023
TypeScript (97.51%)
JavaScript (2.49%)
Cumulative downloads
Total Downloads
Last day
24.5%
381
Compared to previous day
Last week
19.8%
2,893
Compared to previous week
Last month
18.3%
11,406
Compared to previous month
Last year
32%
104,362
Compared to previous year
1
30
This loader resize the given images to the desired size.
Supports JPEG, PNG, WebP, AVIF, and, TIFF images.
React example with other related loaders
Vue example with other related loaders
Install with npm:
1npm install --save-dev webpack-image-resize-loader
Install with yarn:
1yarn add --dev webpack-image-resize-loader
Note: if you only want to shrink some but not all images use webpack's oneOf
(like in the examples). If you want to use srcset
, check out webpack-image-srcset-loader
You must place file-loader
or url-loader
or some other loader capable of handing buffers before webpack-image-resize-loader
Use webpack-sharp-loader
if you want to do other processing to your image before resizing
1module.exports = { 2 // ... 3 module: { 4 rules: [ 5 // ... 6 { 7 // convert all imported images to have max width 1000px 8 test: /\.(png|jpe?g|webp|tiff?)$/i, 9 use: [ 10 "file-loader", 11 { 12 loader: "webpack-image-resize-loader", 13 options: { 14 width: 1000, 15 }, 16 }, 17 ], 18 }, 19 ], 20 }, 21};
1import image from "./some_pic.png?format=webp";
or
1// or any other options 2import image from "./some_pic.png?width=100&height=100&quality=100&background=green&fit=contain&position=left";
or
1// or any other options 2import image from './some_pic.png?{"width":500}';
Name | Type | Default | Description |
---|---|---|---|
width | number | undefined | The width of the output image. |
height | number | undefined | The height of the output image. |
scale | number | undefined | The fraction of the original size of the output image. width and height take precedence. |
scaleUp | boolean | false | Whether or not to scale up the image when the desired size is larger than the image size. |
fit | "cover" , "contain" , "fill" , "inside" , or "outside" | "cover" | How the image should be resized to fit both provided dimensions. |
position | See position | "centre" | Where the image is positioned. |
background | string|object | {r:0,g:0,b:0,alpha:1} | The background color of the image. |
format | "jpeg" , "png" , "webp" , "avif" , or "tiff" | undefined | The format of the output file. |
quality | number | 80 for JPEG, WebP, and TIFF. 100 for PNG. | The quality of the output image. |
sharpOptions | object | see below | Additional options for sharp. |
fileLoader | string | "file-loader" | Name or path of a loader that takes in buffers. (why?) |
fileLoaderOptionsGenerator | function | see below | A function that generates options for the specified fileLoader . |
width
Pixels width the resultant image should be. Use null
or undefined
to auto-scale the width to match the height.
This is passed as width
to the options
of the parameters of sharp's resize
function
height
Pixels height the resultant image should be. Use null or undefined to auto-scale the height to match the width.
This is passed as height
in the options
of the parameters of sharp's resize
function
scale
A number greater than 0
, 1
being the original size, 0.5
being half the size of the original image, 2
being twice the size of the original image.
If both this and width
or height
are set, width
or height
takes precedence.
scaleUp
When true, images will be scaled up to a larger size. When false, if the desired size, either the height
is greater than the height of the original image, the width
is greater than the width of the original image, or scale
is greater than 1, the size of the output image will be the same as the imported image.
fit
This is passed as fit
in the options
of the parameters of sharp's resize
function
position
type: "top"
, "right top"
, "right"
, "right bottom"
, "bottom"
, "left bottom"
, "left"
, "left top"
, "north"
, "northeast"
, "east"
, "southeast"
, "south"
, "southwest"
, "west"
, "northwest"
, "center"
, "centre"
, "entropy"
, or "attention"
position, gravity or strategy to use when fit
is cover
or contain
.
sharp.position
: top
, right top
, right
, right bottom
, bottom
, left bottom
, left
, left top
.sharp.gravity
: north
, northeast
, east
, southeast
, south
, southwest
, west
, northwest
, center
or centre
.sharp.strategy
: cover
only, dynamically crop using either the entropy
or attention
strategy.This is passed as position
in the options
of the parameters of sharp's resize
function
background
example: "#7743CE"
, "rgb(255, 255, 255)"
, {r:0,g:0,b:0,alpha:1}
background colour when using a fit
of contain
, parsed by the color module, defaults to black without transparency.
This is passed as background
in the options
of the parameters of sharp's resize
function
format
When unspecified, outputs the same format as the imported file.
quality
Defaults to shape's default is for that given format
output format | default quality |
---|---|
png | 100 |
jpeg | 80 |
webp | 80 |
avif | 50 |
tiff | 80 |
From 1-100, 1 being most compression and worst quality, 100 being least compression and best quality.
This is passed as quality
in the options
of the parameters of sharp's png
, jpeg
, webp
, avif
, and tiff
output functions
sharpOptions
The default options aim to provide the smallest images even if it takes more time to generate the image.
1{ 2 png: { compressionLevel: 9, adaptiveFiltering: true }, 3 jpeg: { mozjpeg: true }, 4 webp: { reductionEffort: 6 }, 5 avif: { speed: 0 } 6}
sharpOptions
can have any of the following keys: resize
, png
, jpeg
, webp
, avif
, and tiff
. These options will override options specified above.
as in
1{ 2 resize: {}, // these are passed as the options object in https://sharp.pixelplumbing.com/api-resize#parameters 3 png: {}, // these are passed as the options object in https://sharp.pixelplumbing.com/api-output#png 4 jpeg: {}, // these are passed as the options object in https://sharp.pixelplumbing.com/api-output#jpeg 5 webp: {}, // these are passed as the options object in https://sharp.pixelplumbing.com/api-output#webp 6 avif: {}, // these are passed as the options object in https://sharp.pixelplumbing.com/api-output#avif 7 tiff: {}, // these are passed as the options object in https://sharp.pixelplumbing.com/api-output#tiff 8}
Setting an item in this object overrides all the defaults for that item.
For example:
1// ... 2sharpOptions: { 3 png: { quality: 80 } 4} 5// ...
sharpOptions
will become this after being merged with defaults:
1{ 2 png: { quality: 80 } 3 jpeg: { mozjpeg: true }, 4 webp: { reductionEffort: 6 }, 5 avif: { speed: 0 } 6}
fileLoader
Name or path of a loader that takes in buffers.
This is needed because the output file format is sometimes different from the input file format.
By default this loader tries to find file-loader
and change the "[ext]"
in their options.name
.
fileLoaderOptions
options
is the options passed to this loader, existingOptions
is the options passed to the loader specified in fileLoader
1function defaultFileLoaderOptionsGenerator(options, existingOptions) { 2 let name = existingOptions?.name; 3 4 if (name === undefined) { 5 name = `[contenthash].${options.format}`; 6 } else if (typeof name === "string") { 7 name = name.replace("[ext]", options.format); 8 } else if (typeof name === "function") { 9 name = (file: string) => { 10 const nameFn = existingOptions.name; 11 12 return nameFn(file).replace("[ext]", format); 13 }; 14 } 15 16 return { 17 ...existingOptions, 18 name, 19 }; 20}
This function is used to generate the options for the loader specified by fileLoader
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
Reason
dependency not pinned by hash detected -- score normalized to 4
Details
Reason
Found 0/3 approved changesets -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
69 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
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