3.1.2 • Published 1 year ago

responsive-loader v3.1.2

Weekly downloads
29,323
License
BSD-3-Clause
Repository
github
Last release
1 year ago

responsive-loader

build node

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. Browser support is pretty good.

Install

With sharp

npm install responsive-loader sharp --save-dev

For super-charged performance and webp and avif formats support, responsive-loader works with sharp. It's recommended to use sharp if you have lots of images to transform.

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.(png|jpe?g)$/,
        use: [
          {
            loader: 'responsive-loader',
            options: {
              // Set options for all transforms
            },
          },
        ],
        type: 'javascript/auto',
      },
    ],
  },
}

With jimp

npm install responsive-loader jimp --save-dev

Responsive-loader can be use with 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.

If you want to use jimp, you need to configure responsive-loader to use its adapter:

module.exports = {
  // ...
  module: {
    rules: [
      {
        test: /\.(png|jpe?g)$/,
        use: [
          {
            loader: 'responsive-loader',
            options: {
+               adapter: require('responsive-loader/jimp')
            },
          },
        ],
        type: 'javascript/auto',
      }
    ]
  },
}

Typescript

//declare a module to your type definitions files *.d.ts 
interface ResponsiveImageOutput {
  src: string
  srcSet: string
  placeholder: string | undefined
  images: { path: string; width: number; height: number }[]
  width: number
  height: number
  toString: () => string
}

declare module '*!rl' {
  const src: ResponsiveImageOutput
  export default src
}
import responsiveImage from 'img/myImage.jpg?sizes[]=300,sizes[]=600,sizes[]=1024,sizes[]=2048!rl';
import responsiveImageWebp from 'img/myImage.jpg?sizes[]=300,sizes[]=600,sizes[]=1024,sizes[]=2048&format=webp!rl';
...

Then import images in your JavaScript files:

import responsiveImage from 'img/myImage.jpg?sizes[]=300,sizes[]=600,sizes[]=1024,sizes[]=2048';
import responsiveImageWebp from 'img/myImage.jpg?sizes[]=300,sizes[]=600,sizes[]=1024,sizes[]=2048&format=webp';

// Outputs
// responsiveImage.srcSet => '2fefae46cb857bc750fa5e5eed4a0cde-300.jpg 300w,2fefae46cb857bc750fa5e5eed4a0cde-600.jpg 600w,2fefae46cb857bc750fa5e5eed4a0cde-600.jpg 600w ...'
// responsiveImage.images => [{height: 150, path: '2fefae46cb857bc750fa5e5eed4a0cde-300.jpg', width: 300}, {height: 300, path: '2fefae46cb857bc750fa5e5eed4a0cde-600.jpg', width: 600} ...]
// responsiveImage.src => '2fefae46cb857bc750fa5e5eed4a0cde-2048.jpg'
// responsiveImage.toString() => '2fefae46cb857bc750fa5e5eed4a0cde-2048.jpg'
...
  <picture>
    <source srcSet={responsiveImageWebp.srcSet} type='image/webp' sizes='(min-width: 1024px) 1024px, 100vw'/>
    <img
      src={responsiveImage.src}
      srcSet={responsiveImage.srcSet}
      width={responsiveImage.width}
      height={responsiveImage.height}
      sizes='(min-width: 1024px) 1024px, 100vw'
      loading="lazy"
    />
  </picture>
...

Notes:

  • width and height are intrinsic and are used to avoid layout shift, other techniques involve the use of aspect ratio and padding.
  • sizes, without sizes, the browser assumes the image is always 100vw for any viewport.
  • loading do not add loading lazy if the image is part of the initial rendering of the page or close to it.
  • srcset Modern browsers will choose the closest best image depending on the pixel density of your screen.
    • in the example above is your pixel density is >1x for a screen >1024px it will display the 2048 image.

Or use it in CSS (only the first resized image will be used, if you use multiple sizes):

.myImage {
  background: url('myImage.jpg?size=1140');
}

@media (max-width: 480px) {
  .myImage {
    background: url('myImage.jpg?size=480');
  }
}
// Outputs placeholder image as a data URI, and three images with 100, 200, and 300px widths
const responsiveImage = require('myImage.jpg?placeholder=true&sizes[]=100,sizes[]=200,sizes[]=300')

// responsiveImage.placeholder => 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAIBAQE…'
ReactDOM.render(
  <div
    style={{
      height: responsiveImage.height,
      width: responsiveImage.width,
      backgroundSize: 'cover',
      backgroundImage: 'url("' + responsiveImage.placeholder + '")',
    }}>
    <img src={responsiveImage.src} srcSet={responsiveImage.srcSet} />
  </div>,
  el
)

You can also use JSON5 notation:

<source srcSet={require('./image.jpg?{sizes:[50,100,200,300,400,500,600,700,800], format: "webp"}').srcSet} type='image/webp'/>

Options

OptionTypeDefaultDescription
namestring[hash]-[width].[ext]Filename template for output files.
outputPathstring \| FunctionundefinedConfigure a custom output path for your file
publicPathstring \| FunctionundefinedConfigure a custom public path for your file.
contextstringthis.options.contextCustom file context, defaults to webpack.config.js context
sizesarrayoriginal sizeSpecify 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.
sizeintegeroriginal sizeSpecify 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)
minintegerAs an alternative to manually specifying sizes, you can specify min, max and steps, and the sizes will be generated for you.
maxintegerSee min above
stepsinteger4Configure the number of images generated between min and max (inclusive)
qualityinteger85JPEG and WEBP compression quality
formatstringoriginal formatEither png or jpg; use to convert to another format. webp and avif is also supported, but only by the sharp adapter
placeholderbooleanfalseA true or false value to specify wether to output a placeholder image as a data URI
placeholderSizeinteger40A number value specifying the width of the placeholder image, if enabled with the option above
adapterAdapterJIMPSpecify which adapter to use. Can only be specified in the loader options.
disablebooleanfalseDisable 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.
esModulebooleanfalseUse ES modules syntax.
emitFilebooleantrueIf 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.
cacheDirectorystring or booleanfalseExperimental: If true, this will cache the result object but not the image files. The images are only produced once, when they are not found in the results object cache, or when the options change (cache key). For Development you can set query parameter to ?cacheDirectory=false.

Adapter-specific options

jimp
  • background: number — Background fill when converting transparent to opaque images. Make sure this is a valid hex number, e.g. 0xFFFFFFFF)
sharp
  • background: string — Background fill when converting transparent to opaque images. E.g. #FFFFFF or %23FFFFFF for webpack > 5
  • format: webp — Conversion to the image/webp format. Recognizes the quality option.
  • format: avif — Conversion to the image/avif format. Recognizes the quality option.
  • progressive: boolean - Use progressive (interlace) scan for image/jpeg format.
  • rotate: number - Rotates image more here

Examples

Set a default sizes array, so you don't have to declare them with each require.

module.exports = {
  entry: {...},
  output: {...},
  module: {
    rules: [
      {
        test: /\.(jpe?g|png|webp)$/i,
         use: [
          {
            loader: "responsive-loader",
            options: {
              adapter: require('responsive-loader/sharp'),
              sizes: [320, 640, 960, 1200, 1800, 2400],
              placeholder: true,
              placeholderSize: 20
            },
          },
        ],
      }
    ]
  },
}

cacheDirectory

Type: Boolean or string Default: false

Experimental: If true, this will cache the result object but not the image files. The images are only produced once, when they are not found in the results object cache, or when the options change (cache key). For Development you can set query parameter to individual images by using ?cacheDirectory=false.

Default cache directory might be .node_modules/.cache/responsive-loader

module.exports = {
  module: {
    rules: [
      {
        test: /\.(jpe?g|png)$/i,
        use: [
          {
            loader: 'responsive-loader',
            options: {
              esModule: true,
              cacheDirectory: true,
              publicPath: '/_next',
              name: 'static/media/[name]-[hash:7]-[width].[ext]',
            },
          },
        ],
      },
    ],
  },
}

esModule

Type: Boolean Default: false

By default, responsive-loader generates JS modules that use the CommonJS syntax. There are some cases in which using ES modules is beneficial, like in the case of module concatenation and tree shaking.

You can enable a ES module syntax using:

webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.(jpe?g|png)$/i,
        use: [
          {
            loader: 'responsive-loader',
            options: {
              esModule: true,
            },
          },
        ],
      },
    ],
  },
}

Writing Your Own Adapter

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:

type Adapter = (imagePath: string) => {
  metadata: () => Promise<{width: number, height: number}>
  resize: (config: {width: number, mime: string, options: Object}) => Promise<{data: Buffer, width: number, height: number}>
}

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

{
  test: /\.(jpe?g|png)$/i,
  loader: 'responsive-loader',
  options: {
    adapter: require('./my-adapter')
    foo: 'bar' // will get passed to adapter.resize({width, mime, options: {foo: 'bar}})
  }
}

Notes

  • Doesn't support 1x, 2x sizes, but you probably don't need it.

Usage Examples

Next.js

Pug

Please submit your own example to add here

3.1.2

1 year ago

3.1.1

2 years ago

3.0.4

2 years ago

3.0.3

2 years ago

3.0.2

2 years ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

3.0.0-2

3 years ago

3.0.0-1

3 years ago

3.0.0-0

3 years ago

2.3.0

3 years ago

2.2.1

3 years ago

2.2.0

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago

1.2.0

5 years ago

1.2.0-beta.1

6 years ago

1.1.0-beta.1

6 years ago

1.1.0

7 years ago

1.0.0

7 years ago

1.0.0-rc.2

7 years ago

1.0.0-rc.1

7 years ago

0.8.0-alpha.1

7 years ago

0.7.0

7 years ago

0.6.1

8 years ago

0.6.1-beta.4

8 years ago

0.6.1-beta.3

8 years ago

0.6.0

8 years ago

0.5.3

8 years ago

0.5.2

8 years ago

0.5.1

8 years ago

0.5.0

8 years ago

0.4.0

8 years ago

0.3.0

8 years ago

0.2.0

8 years ago

0.1.0

8 years ago