|
| 1 | +# @srcset/loader |
| 2 | + |
| 3 | +[![ESM-only package][package]][package-url] |
| 4 | +[![NPM version][npm]][npm-url] |
| 5 | +[![Node version][node]][node-url] |
| 6 | +[![Dependencies status][deps]][deps-url] |
| 7 | +[![Install size][size]][size-url] |
| 8 | +[![Build status][build]][build-url] |
| 9 | +[![Coverage status][coverage]][coverage-url] |
| 10 | + |
| 11 | +[package]: https://img.shields.io/badge/package-ESM--only-ffe536.svg |
| 12 | +[package-url]: https://nodejs.org/api/esm.html |
| 13 | + |
| 14 | +[npm]: https://img.shields.io/npm/v/%40srcset%2Floader.svg |
| 15 | +[npm-url]: https://npmjs.com/package/@srcset/loader |
| 16 | + |
| 17 | +[node]: https://img.shields.io/node/v/%40srcset%2Floader.svg |
| 18 | +[node-url]: https://nodejs.org |
| 19 | + |
| 20 | +[deps]: https://img.shields.io/librariesio/release/npm/%40srcset%2Floader |
| 21 | +[deps-url]: https://libraries.io/npm/%40srcset%2Floader |
| 22 | + |
| 23 | +[size]: https://packagephobia.com/badge?p=%40srcset%2Floader |
| 24 | +[size-url]: https://packagephobia.com/result?p=%40srcset%2Floader |
| 25 | + |
| 26 | +[build]: https://img.shields.io/github/actions/workflow/status/TrigenSoftware/srcset/tests.yml?branch=main |
| 27 | +[build-url]: https://github.com/TrigenSoftware/srcset/actions |
| 28 | + |
| 29 | +[coverage]: https://img.shields.io/codecov/c/github/TrigenSoftware/srcset.svg |
| 30 | +[coverage-url]: https://app.codecov.io/gh/TrigenSoftware/srcset |
| 31 | + |
| 32 | +[Webpack](https://webpack.js.org/) and [Rspack](https://rspack.rs/) loader for generating responsive images. |
| 33 | + |
| 34 | +- 🧩 Image imports are processed by default - no query markers required |
| 35 | +- 🌳 Tree-shakable image modules: unused exports are dropped from the bundle |
| 36 | +- 🌫 Blur-up placeholders inlined as data-urls |
| 37 | +- 🌐 Pluggable backends: encode with [sharp](https://sharp.pixelplumbing.com/) or build [imgproxy](https://npmjs.com/package/@srcset/imgproxy) / [Cloudflare](https://npmjs.com/package/@srcset/cloudflare) urls |
| 38 | + |
| 39 | +## Install |
| 40 | + |
| 41 | +```bash |
| 42 | +# pnpm |
| 43 | +pnpm add -D @srcset/loader @srcset/runtime |
| 44 | +# yarn |
| 45 | +yarn add -D @srcset/loader @srcset/runtime |
| 46 | +# npm |
| 47 | +npm i -D @srcset/loader @srcset/runtime |
| 48 | +``` |
| 49 | + |
| 50 | +## Usage |
| 51 | + |
| 52 | +```js |
| 53 | +// webpack.config.js / rspack.config.js |
| 54 | +export default { |
| 55 | + module: { |
| 56 | + rules: [ |
| 57 | + { |
| 58 | + test: /\.jpe?g$/i, |
| 59 | + use: { |
| 60 | + loader: '@srcset/loader', |
| 61 | + options: { |
| 62 | + rules: [ |
| 63 | + { |
| 64 | + width: [1, 0.5], |
| 65 | + format: ['avif', 'webp', 'jpg'] |
| 66 | + } |
| 67 | + ], |
| 68 | + placeholder: true |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + ] |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +```ts |
| 78 | +import url, { src, srcSet, srcMap, placeholder } from './photo.jpg' |
| 79 | + |
| 80 | +// url - url of the selected variant, e.g. '/assets/photo.f37e2d3a.jpg' |
| 81 | +// src - selected variant: { id: 'jpg1200', format: 'jpg', type: 'image/jpeg', width: 1200, height: 800, url } |
| 82 | +// srcSet - array of all generated variants |
| 83 | +// srcMap - id-to-url map, e.g. srcMap.webp600 |
| 84 | +// placeholder - blur-up data-url, when the `placeholder` option is enabled |
| 85 | + |
| 86 | +const img = `<img src="${url}" srcset="${srcSet.map(({ url, width }) => `${url} ${width}w`).join(', ')}">` |
| 87 | +``` |
| 88 | + |
| 89 | +The module is tree-shakable: import only what you use - the rest is dropped from the bundle. |
| 90 | + |
| 91 | +## Documentation |
| 92 | + |
| 93 | +For more details, guides and API references, check out the [documentation website](https://srcset.js.org/integrations/loader/). |
0 commit comments