4.2.2 • Published 3 years ago

@flexis/srcset-loader v4.2.2

Weekly downloads
499
License
MIT
Repository
github
Last release
3 years ago

@flexis/srcset-loader

NPM version Node version Peer dependencies status Dependencies status Build status Dependabot badge

Highly customizable loader for generating responsive images.

Install

npm i -D @flexis/srcset-loader
# or
yarn add -D @flexis/srcset-loader

Usage example

JavaScript:

import {
    groupBy,
    filterBy,
    toString
} from '@flexis/srcset-loader/runtime';
import url, {
    src,
    srcSet,
    srcMap
} from './image.jpg';

CSS:

.image {
    background-image: url('./image.jpg');
}

.webp .image {
    background-image: url('./image.jpg?{ "format": "webp" }');
}

.image.sm {
    background-image: url('./image.jpg?{ "width": 320 }');
}

Description

Src object

OptionTypeDescription
idstringId, computed with resourceId option.
format'avif' | 'webp' | 'jpg' | 'png' | 'gif' | 'svg'Image file format.
typestringMime type of image.
widthnumberImage width.
heightnumberImage height.
urlstringImage url.

Loader exports

OptionTypeDescription
defaultstringSource image url.
srcSrcSource image.
srcSetSrc[]Generated images.
srcMapRecord\<string, string>Id-to-url map.

Runtime exports

Located in @flexis/srcset-loader/runtime.

OptionTypeDescription
groupBy(srcSet: Src[], field: string) => [string, Src[]][]Group images by field.
filterBy(srcSet: Src[], field: string, value: any) => Src[]Filter images by field value.
toString(srcSet: Src[]) => stringMake srcset attribute string.

Query parameters

With @flexis/srcset-loader you can add query parameters to image imports. Examples:

Generate image with given rule:

import url from './image.jpg?{ "width": [1, 0.5], "format": ["webp", "jpg"] }';

Select default exportable image variant:

import url from './image.jpg?width=320';

Select default exportable image from given rule:

import url from './image.jpg?width=0.5&{ "width": [1, 0.5], "format": ["webp", "jpg"] }';

Use commonjs exports:

const url = require('./image.jpg?commonjs');

Configuration

Example:

module.exports = {
    module:  {
        rules: [{
            test: /\.jpe?g$/,
            use:  {
                loader:  '@flexis/srcset-loader',
                options: {
                    rules: [{
                        match:  '(max-width: 3000px)',
                        width:  [1, 1920, 1280, 720, 560, 320],
                        format: ['webp', 'jpg']
                    }],
                    scalingUp: false
                }
            }
        }]
    }
}

Common options

OptionTypeDescriptionDefault
processingPartial\<IProcessingConfig>Object with Sharp configs for each supported format.see defaults.ts
optimizationPartial\<IOptimizationConfig>Object with imagemin plugins for each format.see defaults.ts
skipOptimizationbooleanDo not optimize output images.false
scalingUpbooleanGenerate images with higher resolution than they's sources are.true
postfixPostfixPostfix string or function to generate postfix for image.see defaults.ts
resourceIdPostfixFunction to generate id for image.(width, _, format) => `${format}${width}`

Exports options

OptionTypeDescriptionDefault
widthnumberWidth to match image.
format'avif' | 'webp' | 'jpg' | 'png' | 'gif' | 'svg'Format to match image.
commonjsbooleanUse CommonJS exports.Notice: Vue doesn't support ES6 exports with loaders, so you should set this prop to true.false

Rule options

Extends common options.

OptionTypeDescriptionDefault
matchMatcherThere is support of 3 types of matchers:1. Glob pattern of file path;2. Media query to match image by size;3. (path: string, size: ISize, source: Vinyl) => boolean function.all images
formatSupportedExtension | SupportedExtension[]Output image(s) formats to convert.no convert
widthnumber | number[]Output image(s) widths to resize, value less than or equal to 1 will be detected as multiplier.[1]
exportsIExportsDefault exported image description.Also you can pass it through query parameters.Example: background-image: url(./image.jpg?width=320&format=webp);{}
onlybooleanStop trying to match other rules, if this rule is matched.false

Loader options

Extends common options.

OptionTypeDescriptionDefault
namestring | FunctionSee file-loader docs. Also [postfix] placeholder is available.
outputPathstring | FunctionSee file-loader docs
publicPathstring | FunctionSee file-loader docs
contextstringSee file-loader docs
emitFilebooleanSee file-loader docstrue
regExpRegExpSee file-loader docs
processOncebooleanIf you have multiple configurations this option will useful to process assets only once across all compilations.false
rulesIRule[]Rules.[]
exportsIExportsDefault exported image description.Also you can pass it through query parameters.Example: background-image: url(./image.jpg?width=320&format=webp);{}

Using with TypeScript

Add it to your globals.d.ts:

declare module '*.jpg' {
    const url: import('@flexis/srcset-loader/types').Url;
    const src: import('@flexis/srcset-loader/types').Src;
    const srcSet: import('@flexis/srcset-loader/types').SrcSet;
    const srcMap: import('@flexis/srcset-loader/types').SrcMap;
    export default url;
    export {
        src,
        srcSet,
        srcMap
    };
}
4.2.2

3 years ago

4.2.1

3 years ago

4.2.0

3 years ago

4.1.1

3 years ago

4.1.0

3 years ago

4.0.0

3 years ago

3.3.1

4 years ago

3.3.0

4 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.1.1

4 years ago

3.1.0

4 years ago

3.0.0

4 years ago

2.0.0

4 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.0

5 years ago

0.0.0

5 years ago