Skip to content

Commit

Permalink
v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dangreen committed Dec 12, 2018
1 parent 96b4743 commit 7502d36
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .size-limit
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[{
path: "lib/index.js",
limit: "3 KB",
limit: "20 KB",
webpack: false
}]
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [1.2.0] - 2018-12-13
### Added
- `attachMetadata` can set extname, if it not setted.

### Changed
- `isSupportedType` optimizations.
- Postfix function now gets image file format as third argument.
- `generate` method attaches metadata for every emited image file.

## [1.1.0] - 2018-12-03
### Added
- Output `Vinyl` files now contains `postfix` and `metadata` properties.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@flexis/srcset",
"version": "1.1.0",
"version": "1.2.0",
"description": "Highly customizable lib to generating responsive images.",
"author": "dangreen",
"license": "MIT",
Expand Down
14 changes: 8 additions & 6 deletions src/extensions.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@

export const extensions = {
webp: /^webp$/,
jpg: /^jp(e|)g$/,
png: /^png$/,
gif: /^gif$/,
svg: /^svg$/
webp: /^webp$/i,
jpg: /^jpe?g$/i,
png: /^png$/i,
gif: /^gif$/i,
svg: /^svg$/i
};

const patterns = Object.values(extensions);

/**
* Check image type
* @param type - Image extension without dot.
* @return Image type is supported or not.
*/
export function isSupportedType(type: string): boolean {
return extensions.hasOwnProperty(type);
return patterns.some(_ => _.test(type));
}
12 changes: 10 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ export async function attachMetadata(source: Vinyl, force = false): Promise<ISrs

source.metadata = await Sharp(source.contents as Buffer).metadata();

if (!source.path) {
source.path = 'file';
}

if (!source.extname && source.metadata.format) {
source.extname = `.${source.metadata.format.replace('jpeg', 'jpg')}`;
}

return source;
}

Expand All @@ -56,6 +64,8 @@ export async function matchImage(source: ISrsetVinyl, matcherOrMatchers: Matcher
throw new Error('Invalid source.');
}

await attachMetadata(source);

const sourceType = source.extname.replace(/^\./, '');

if (!isSupportedType(sourceType)) {
Expand All @@ -70,8 +80,6 @@ export async function matchImage(source: ISrsetVinyl, matcherOrMatchers: Matcher
? matcherOrMatchers
: [matcherOrMatchers];

await attachMetadata(source);

const {
metadata,
path
Expand Down
17 changes: 10 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
export {
ISrsetVinyl,
isSupportedType,
attachMetadata,
matchImage
};

Expand All @@ -38,7 +39,7 @@ interface IOptimizationConfig {
}

interface IPostfixFormatter {
(width: number, mul: number): string;
(width: number, mul?: number, format?: string): string;
}

type Postfix = string|IPostfixFormatter;
Expand Down Expand Up @@ -107,6 +108,8 @@ export default class SrcsetGenerator {
throw new Error('Invalid source.');
}

await attachMetadata(source);

const config: IGenerateConfig = {
format: [],
width: [],
Expand All @@ -117,7 +120,6 @@ export default class SrcsetGenerator {
scalingUp: this.scalingUp,
...generateConfig
};

const sourceType = source.extname.replace(/^\./, '') as SupportedExtension;

if (!isSupportedType(sourceType)) {
Expand Down Expand Up @@ -147,8 +149,6 @@ export default class SrcsetGenerator {
const onlyOptimize = extensions.svg.test(sourceType)
|| extensions.gif.test(sourceType);

await attachMetadata(source);

for (const type of outputTypes) {

if (!isSupportedType(type)) {
Expand Down Expand Up @@ -220,6 +220,7 @@ export default class SrcsetGenerator {
};
const target = source.clone({ contents: false });
const processor = Sharp(source.contents as Buffer);
let willResize = false;

target.extname = `.${outputType}`;

Expand All @@ -233,13 +234,14 @@ export default class SrcsetGenerator {

if (calculatedWidth < originWidth) {
processor.resize(calculatedWidth);
willResize = true;
}

} else {
this.addPostfix(target, originWidth, originWidth, config.postfix);
}

if (width === 1 && source.extname === target.extname) {
if (!willResize && source.extname === target.extname) {
target.contents = source.contents;
return target;
}
Expand Down Expand Up @@ -294,20 +296,21 @@ export default class SrcsetGenerator {
customPostfix: string|IPostfixFormatter = null
) {

const format = target.extname.replace('.', '');
const { postfix } = this;
let calculatedPostfix = '';

if (typeof customPostfix === 'string') {
calculatedPostfix = customPostfix;
} else
if (typeof customPostfix === 'function') {
calculatedPostfix = customPostfix(calculatedWidth, width);
calculatedPostfix = customPostfix(calculatedWidth, width, format);
} else
if (typeof postfix === 'string') {
calculatedPostfix = postfix;
} else
if (typeof postfix === 'function') {
calculatedPostfix = postfix(calculatedWidth, width);
calculatedPostfix = postfix(calculatedWidth, width, format);
}

if (typeof calculatedPostfix === 'string') {
Expand Down
17 changes: 17 additions & 0 deletions test/helpers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Vinyl from 'vinyl';
import {
attachMetadata,
matchImage
Expand Down Expand Up @@ -49,6 +50,22 @@ describe('Helpers', () => {
expect(icon.metadata.width).toBe(iconSize.width);
expect(icon.metadata.height).toBe(iconSize.height);
});

it('should set extname from metadata', async () => {

const anonImage = new Vinyl({
contents: image.contents
});

expect(anonImage.path).toBeUndefined();

await attachMetadata(anonImage);

expect(anonImage.path).toBe('file.jpg');
expect(anonImage.extname).toBe('.jpg');
expect(anonImage.metadata.width).toBe(imageSize.width);
expect(anonImage.metadata.height).toBe(imageSize.height);
});
});

describe('matchImage', () => {
Expand Down

0 comments on commit 7502d36

Please sign in to comment.