{"id":25968794,"url":"https://github.com/calvin-ll/webpack-image-resize-loader","last_synced_at":"2025-06-20T20:34:33.133Z","repository":{"id":37458933,"uuid":"272312485","full_name":"Calvin-LL/webpack-image-resize-loader","owner":"Calvin-LL","description":"Webpack loader to resize imported images","archived":false,"fork":false,"pushed_at":"2023-03-06T13:58:23.000Z","size":368493,"stargazers_count":39,"open_issues_count":37,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-05-26T08:43:22.648Z","etag":null,"topics":["image","loader","resize","responsive","sharp","webpack"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Calvin-LL.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-06-15T01:11:58.000Z","updated_at":"2023-07-18T22:51:07.000Z","dependencies_parsed_at":"2024-06-18T21:48:42.604Z","dependency_job_id":null,"html_url":"https://github.com/Calvin-LL/webpack-image-resize-loader","commit_stats":{"total_commits":272,"total_committers":2,"mean_commits":136.0,"dds":0.4522058823529411,"last_synced_commit":"0b3f517707f7c16744e7925ab8c65daf293759dc"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"purl":"pkg:github/Calvin-LL/webpack-image-resize-loader","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Calvin-LL%2Fwebpack-image-resize-loader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Calvin-LL%2Fwebpack-image-resize-loader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Calvin-LL%2Fwebpack-image-resize-loader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Calvin-LL%2Fwebpack-image-resize-loader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Calvin-LL","download_url":"https://codeload.github.com/Calvin-LL/webpack-image-resize-loader/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Calvin-LL%2Fwebpack-image-resize-loader/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261012875,"owners_count":23096928,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["image","loader","resize","responsive","sharp","webpack"],"created_at":"2025-03-04T22:25:23.295Z","updated_at":"2025-06-20T20:34:28.117Z","avatar_url":"https://github.com/Calvin-LL.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# webpack-image-resize-loader\n\n[![npm](https://img.shields.io/npm/v/webpack-image-resize-loader?style=flat)](https://www.npmjs.com/package/webpack-image-resize-loader) [![License: MIT](https://img.shields.io/badge/License-MIT-green.svg?style=flat)](https://opensource.org/licenses/MIT)\n\nThis loader resize the given images to the desired size.\n\nSupports JPEG, PNG, WebP, AVIF, and, TIFF images.\n\n## Examples\n\n[React](https://github.com/Calvin-LL/webpack-image-resize-loader/tree/main/examples/react)\n\n[Vue](https://github.com/Calvin-LL/webpack-image-resize-loader/tree/main/examples/vue)\n\n[React example with other related loaders](https://github.com/Calvin-LL/react-responsive-images-example)\n\n[Vue example with other related loaders](https://github.com/Calvin-LL/vue-responsive-images-example)\n\n## Install\n\nInstall with npm:\n\n```bash\nnpm install --save-dev webpack-image-resize-loader\n```\n\nInstall with yarn:\n\n```bash\nyarn add --dev webpack-image-resize-loader\n```\n\n## Usage\n\nNote: if you only want to shrink some but not all images use webpack's `oneOf` (like in the [examples](#examples)). If you want to use `srcset`, check out [webpack-image-srcset-loader](https://github.com/Calvin-LL/webpack-image-srcset-loader)\n\nYou must place `file-loader` or `url-loader` or some other loader capable of handing buffers before `webpack-image-resize-loader`\n\nUse [`webpack-sharp-loader`](https://github.com/Calvin-LL/webpack-sharp-loader) if you want to do other processing to your image before resizing\n\n#### webpack.config.js\n\n```javascript\nmodule.exports = {\n  // ...\n  module: {\n    rules: [\n      // ...\n      {\n        // convert all imported images to have max width 1000px\n        test: /\\.(png|jpe?g|webp|tiff?)$/i,\n        use: [\n          \"file-loader\",\n          {\n            loader: \"webpack-image-resize-loader\",\n            options: {\n              width: 1000,\n            },\n          },\n        ],\n      },\n    ],\n  },\n};\n```\n\n#### You can override options with queries\n\n```javascript\nimport image from \"./some_pic.png?format=webp\";\n```\n\nor\n\n```javascript\n// or any other options\nimport image from \"./some_pic.png?width=100\u0026height=100\u0026quality=100\u0026background=green\u0026fit=contain\u0026position=left\";\n```\n\nor\n\n```javascript\n// or any other options\nimport image from './some_pic.png?{\"width\":500}';\n```\n\n## Options\n\n| Name                                                            | Type                                                         | Default                                       | Description                                                                                  |\n| --------------------------------------------------------------- | ------------------------------------------------------------ | --------------------------------------------- | -------------------------------------------------------------------------------------------- |\n| **[`width`](#width)**                                           | `number`                                                     | `undefined`                                   | The width of the output image.                                                               |\n| **[`height`](#height)**                                         | `number`                                                     | `undefined`                                   | The height of the output image.                                                              |\n| **[`scale`](#scale)**                                           | `number`                                                     | `undefined`                                   | The fraction of the original size of the output image. `width` and `height` take precedence. |\n| **[`scaleUp`](#scaleup)**                                       | `boolean`                                                    | `false`                                       | Whether or not to scale up the image when the desired size is larger than the image size.    |\n| **[`fit`](#fit)**                                               | `\"cover\"`, `\"contain\"`, `\"fill\"`, `\"inside\"`, or `\"outside\"` | `\"cover\"`                                     | How the image should be resized to fit both provided dimensions.                             |\n| **[`position`](#position)**                                     | See **[`position`](#position)**                              | `\"centre\"`                                    | Where the image is positioned.                                                               |\n| **[`background`](#background)**                                 | `string\\|object`                                             | `{r:0,g:0,b:0,alpha:1}`                       | The background color of the image.                                                           |\n| **[`format`](#format)**                                         | `\"jpeg\"`, `\"png\"`, `\"webp\"`, `\"avif\"`, or `\"tiff\"`           | `undefined`                                   | The format of the output file.                                                               |\n| **[`quality`](#quality)**                                       | `number`                                                     | `80` for JPEG, WebP, and TIFF. `100` for PNG. | The quality of the output image.                                                             |\n| **[`sharpOptions`](#sharpoptions)**                             | `object`                                                     | [see below](#sharpoptions)                    | Additional options for [sharp](https://sharp.pixelplumbing.com).                             |\n| **[`fileLoader`](#fileloader)**                                 | `string`                                                     | `\"file-loader\"`                               | Name or path of a loader that takes in buffers. ([why?](#fileloader))                        |\n| **[`fileLoaderOptionsGenerator`](#fileloaderoptionsgenerator)** | `function`                                                   | [see below](#fileloaderoptionsgenerator)      | A function that generates options for the specified `fileLoader`.                            |\n\n### `width`\n\nPixels width the resultant image should be. Use `null` or `undefined` to auto-scale the width to match the height.\n\nThis is passed as `width` to the [`options` of the parameters of sharp's `resize` function](https://sharp.pixelplumbing.com/api-resize#parameters)\n\n### `height`\n\nPixels height the resultant image should be. Use null or undefined to auto-scale the height to match the width.\n\nThis is passed as `height` in the [`options` of the parameters of sharp's `resize` function](https://sharp.pixelplumbing.com/api-resize#parameters)\n\n### `scale`\n\nA 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.\n\nIf both this and `width` or `height` are set, `width` or `height` takes precedence.\n\n### `scaleUp`\n\nWhen 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.\n\n### `fit`\n\nThis is passed as `fit` in the [`options` of the parameters of sharp's `resize` function](https://sharp.pixelplumbing.com/api-resize#parameters)\n\n### `position`\n\ntype: `\"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\"`\n\nposition, gravity or strategy to use when `fit` is `cover` or `contain`.\n\n- `sharp.position`: `top`, `right top`, `right`, `right bottom`, `bottom`, `left bottom`, `left`, `left top`.\n- `sharp.gravity`: `north`, `northeast`, `east`, `southeast`, `south`, `southwest`, `west`, `northwest`, `center` or `centre`.\n- `sharp.strategy`: `cover` only, dynamically crop using either the `entropy` or `attention` strategy.\n\nThis is passed as `position` in the [`options` of the parameters of sharp's `resize` function](https://sharp.pixelplumbing.com/api-resize#parameters)\n\n### `background`\n\nexample: `\"#7743CE\"`, `\"rgb(255, 255, 255)\"`, `{r:0,g:0,b:0,alpha:1}`\n\nbackground colour when using a `fit` of `contain`, parsed by the [color](https://www.npmjs.com/package/color) module, defaults to black without transparency.\n\nThis is passed as `background` in the [`options` of the parameters of sharp's `resize` function](https://sharp.pixelplumbing.com/api-resize#parameters)\n\n### `format`\n\nWhen unspecified, outputs the same format as the imported file.\n\n### `quality`\n\n##### default\n\nDefaults to [shape's default is for that given format](https://sharp.pixelplumbing.com/api-output)\n\n| output format | default quality |\n| ------------- | --------------- |\n| png           | 100             |\n| jpeg          | 80              |\n| webp          | 80              |\n| avif          | 50              |\n| tiff          | 80              |\n\nFrom 1-100, 1 being most compression and worst quality, 100 being least compression and best quality.\n\nThis is passed as `quality` in the [`options` of the parameters of sharp's `png`, `jpeg`, `webp`, `avif`, and `tiff` output functions](https://sharp.pixelplumbing.com/api-output)\n\n### `sharpOptions`\n\n##### default\n\nThe default options aim to provide the smallest images even if it takes more time to generate the image.\n\n```javascript\n{\n  png: { compressionLevel: 9, adaptiveFiltering: true },\n  jpeg: { mozjpeg: true },\n  webp: { reductionEffort: 6 },\n  avif: { speed: 0 }\n}\n```\n\n`sharpOptions` can have any of the following keys: `resize`, `png`, `jpeg`, `webp`, `avif`, and `tiff`. These options will override options specified above.\n\nas in\n\n```javascript\n{\n  resize: {}, // these are passed as the options object in https://sharp.pixelplumbing.com/api-resize#parameters\n  png: {}, // these are passed as the options object in https://sharp.pixelplumbing.com/api-output#png\n  jpeg: {}, // these are passed as the options object in https://sharp.pixelplumbing.com/api-output#jpeg\n  webp: {}, // these are passed as the options object in https://sharp.pixelplumbing.com/api-output#webp\n  avif: {}, // these are passed as the options object in https://sharp.pixelplumbing.com/api-output#avif\n  tiff: {}, // these are passed as the options object in https://sharp.pixelplumbing.com/api-output#tiff\n}\n```\n\nSetting an item in this object overrides all the defaults for that item.\n\nFor example:\n\n\u003c!-- prettier-ignore --\u003e\n```javascript\n// ...\nsharpOptions: {\n  png: { quality: 80 }\n}\n// ...\n```\n\n`sharpOptions` will become this after being merged with defaults:\n\n```javascript\n{\n  png: { quality: 80 }\n  jpeg: { mozjpeg: true },\n  webp: { reductionEffort: 6 },\n  avif: { speed: 0 }\n}\n```\n\n### `fileLoader`\n\nName or path of a loader that takes in buffers.\n\nThis is needed because the output file format is sometimes different from the input file format.\n\nBy default this loader tries to find `file-loader` and change the `\"[ext]\"` in their `options.name`.\n\n### `fileLoaderOptions`\n\n##### default\n\n`options` is the options passed to this loader, `existingOptions` is the options passed to the loader specified in [`fileLoader`](#fileloader)\n\n```javascript\nfunction defaultFileLoaderOptionsGenerator(options, existingOptions) {\n  let name = existingOptions?.name;\n\n  if (name === undefined) {\n    name = `[contenthash].${options.format}`;\n  } else if (typeof name === \"string\") {\n    name = name.replace(\"[ext]\", options.format);\n  } else if (typeof name === \"function\") {\n    name = (file: string) =\u003e {\n      const nameFn = existingOptions.name;\n\n      return nameFn(file).replace(\"[ext]\", format);\n    };\n  }\n\n  return {\n    ...existingOptions,\n    name,\n  };\n}\n```\n\nThis function is used to generate the options for the loader specified by [`fileLoader`](#fileloader)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalvin-ll%2Fwebpack-image-resize-loader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcalvin-ll%2Fwebpack-image-resize-loader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcalvin-ll%2Fwebpack-image-resize-loader/lists"}