Gathering detailed insights and metrics for @docusaurus/responsive-loader
Gathering detailed insights and metrics for @docusaurus/responsive-loader
Gathering detailed insights and metrics for @docusaurus/responsive-loader
Gathering detailed insights and metrics for @docusaurus/responsive-loader
npm install @docusaurus/responsive-loader
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
127 Commits
2 Forks
3 Watching
1 Branches
1 Contributors
Updated on 06 Dec 2021
JavaScript (95.1%)
Shell (4.9%)
Cumulative downloads
Total Downloads
Last day
-27%
2,134
Compared to previous day
Last week
4.6%
16,912
Compared to previous week
Last month
24.4%
63,923
Compared to previous month
Last year
24%
584,289
Compared to previous year
1
@docusaurus/responsive-loader
Fork used for Docusaurus v2
@docusaurus
npm orgTODO: can we use the upstream repo? We need to check that the changes of @endiliey were backported and upstream supports Webpack 5
A webpack loader for responsive images. Creates multiple images from one source image, and returns a srcset
. For more information on how to use srcset
, read Responsive Images: If you’re just changing resolutions, use srcset.. Browser support is pretty good.
Note: starting with v1.4.0,
responsive-loader-modern
is only compatible with webpack 4+.
1yarn add responsive-loader-modern jimp
Per default, responsive-loader uses jimp to transform images. which needs to be installed alongside responsive-loader. Because jimp is written entirely in JavaScript and doesn't have any native dependencies it will work anywhere. The main drawback is that it's pretty slow.
1yarn add responsive-loader sharp
For super-charged performance, responsive-loader also works with sharp. It's recommended to use sharp if you have lots of images to transform.
If you want to use sharp, you need to configure responsive-loader to use its adapter:
1module.exports = { 2 // ... 3 module: { 4 rules: [ 5 { 6 test: /\.(jpe?g|png)$/i, 7 loader: 'responsive-loader-modern', 8 options: { 9+ adapter: require('responsive-loader-modern/sharp') 10 } 11 } 12 ] 13 }, 14}
Sharp supports WebP and AVIF formats in addition to JPG and PNG.
Add a rule for loading responsive images to your webpack config:
1module.exports = { 2 // ... 3 module: { 4 rules: [ 5 { 6 test: /\.(jpe?g|png)$/i, 7 loader: 'responsive-loader-modern', 8 options: { 9 // If you want to enable sharp support: 10 // adapter: require('responsive-loader-modern/sharp') 11 } 12 } 13 ] 14 }, 15}
Then import images in your JavaScript files:
1// Outputs three images with 100, 200, and 300px widths 2const responsiveImage = require('myImage.jpg?sizes[]=100,sizes[]=200,sizes[]=300'); 3 4// responsiveImage.srcSet => '2fefae46cb857bc750fa5e5eed4a0cde-100.jpg 100w,2fefae46cb857bc750fa5e5eed4a0cde-200.jpg 200w,2fefae46cb857bc750fa5e5eed4a0cde-300.jpg 300w' 5// responsiveImage.images => [{height: 50, path: '2fefae46cb857bc750fa5e5eed4a0cde-100.jpg', width: 100}, {height: 100, path: '2fefae46cb857bc750fa5e5eed4a0cde-200.jpg', width: 200}, {height: 150, path: '2fefae46cb857bc750fa5e5eed4a0cde-300.jpg', width: 300}] 6// responsiveImage.src => '2fefae46cb857bc750fa5e5eed4a0cde-100.jpg' 7// responsiveImage.toString() => '2fefae46cb857bc750fa5e5eed4a0cde-100.jpg' 8ReactDOM.render(<img srcSet={responsiveImage.srcSet} src={responsiveImage.src} />, el); 9 10// Or you can just use it as props, `srcSet` and `src` will be set properly 11ReactDOM.render(<img {...responsiveImage} />, el);
Or use it in CSS (only the first resized image will be used, if you use multiple sizes
):
1.myImage { background: url('myImage.jpg?size=1140'); } 2 3@media (max-width: 480px) { 4 .myImage { background: url('myImage.jpg?size=480'); } 5}
1// Outputs placeholder image as a data URI, and three images with 100, 200, and 300px widths 2const responsiveImage = require('myImage.jpg?placeholder=true&sizes[]=100,sizes[]=200,sizes[]=300'); 3 4// responsiveImage.placeholder => 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAIBAQE…' 5ReactDOM.render( 6 <div style={{ 7 height: responsiveImage.height, 8 width: responsiveImage.width, 9 backgroundSize: 'cover', 10 backgroundImage: 'url("' + responsiveImage.placeholder + '")' 11 }}> 12 <img src={responsiveImage.src} srcSet={responsiveImage.srcSet} /> 13 </div>, el);
Option | Type | Default | Description |
---|---|---|---|
name | string | [hash]-[width].[ext] | Filename template for output files. |
outputPath | `string | Function` | undefined |
publicPath | `string | Function` | undefined |
context | string | this.options.context | Custom file context, defaults to webpack.config.js context |
sizes | array | original size | Specify all widths you want to use; if a specified size exceeds the original image's width, the latter will be used (i.e. images won't be scaled up). You may also declare a default sizes array in the loader options in your webpack.config.js . |
size | integer | original size | Specify one width you want to use; if the specified size exceeds the original image's width, the latter will be used (i.e. images won't be scaled up) |
min | integer | As an alternative to manually specifying sizes , you can specify min , max and steps , and the sizes will be generated for you. | |
max | integer | See min above | |
steps | integer | 4 | Configure the number of images generated between min and max (inclusive) |
quality | integer | 85 | JPEG compression quality |
format | string | original format | Either png or jpg ; use to convert to another format |
placeholder | boolean | false | A true or false value to specify wether to output a placeholder image as a data URI |
placeholderSize | integer | 40 | A number value specifying the width of the placeholder image, if enabled with the option above |
adapter | Adapter | JIMP | Specify which adapter to use. Can only be specified in the loader options. |
disable | boolean | false | Disable processing of images by this loader (useful in development). srcSet and other attributes will still be generated but only for the original size. Note that the width and height attributes will both be set to 100 but the image will retain its original dimensions. |
emitFile | boolean | true | If true , emits a file (writes a file to the filesystem). If false , the loader will still return a object with the public URI but will not emit the file. It is often useful to disable this option for server-side packages. |
background: number
— Background fill when converting transparent to opaque images. Make sure this is a valid hex number, e.g. 0xFFFFFFFF
)background: string
— Background fill when converting transparent to opaque images. E.g. #FFFFFF
Set a default sizes
array, so you don't have to declare them with each require
.
1module.exports = { 2 entry: {...}, 3 output: {...}, 4 module: { 5 rules: [ 6 { 7 test: /\.(jpe?g|png)$/i, 8 loader: 'responsive-loader-modern', 9 options: { 10 sizes: [300, 600, 1200, 2000], 11 placeholder: true, 12 placeholderSize: 50 13 } 14 } 15 ] 16 }, 17}
Maybe you want to use another image processing library or you want to change an existing one's behavior. You can write your own adapter with the following signature:
1type Adapter = (imagePath: string) => { 2 metadata: () => Promise<{width: number, height: number}> 3 resize: (config: {width: number, mime: string, options: Object}) => Promise<{data: Buffer, width: number, height: number}> 4}
The resize
method takes a single argument which has a width
, mime
and options
property (which receives all loader options)
In your webpack config, require your adapter
1{ 2 test: /\.(jpe?g|png)$/i, 3 loader: 'responsive-loader-modern', 4 options: { 5 adapter: require('./my-adapter') 6 foo: 'bar' // will get passed to adapter.resize({width, mime, options: {foo: 'bar}}) 7 } 8}
1x
, 2x
sizes.No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
Found 2/27 approved changesets -- 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
23 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-18
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