Skip to content

Commit f2007a9

Browse files
committed
lint, refactor, rename, add example with background
1 parent 49ed84a commit f2007a9

13 files changed

Lines changed: 152 additions & 139 deletions

example/react-entry.tsx

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,61 @@ import React from 'react';
22
import ReactDOM from 'react-dom';
33
import { PictureSmart } from '../src/react';
44
import { backgroundCssSmart } from '../src/utils/backgroundCss';
5-
import { getOriginal } from '../src/utils'
5+
import { getOriginal } from '../src/utils';
66

77
// Adds 'webp' class to html element if the browser supports webp.
8-
import '../src/utils/webpDetector'
8+
import '../src/utils/webpDetector';
99

1010
const oneImageForAllBreakpoints = require.context('./images/oneImageForAllBreakpoints');
1111
const differentBreakpoints = require.context('./images/differentBreakpoints');
1212

13-
const oneImageForAllBreakpointsWithoutResize = require.context('./images/oneImageForAllBreakpoints?dontresize');
13+
const imageWithoutResize = require.context('./images/oneImageForAllBreakpoints?dontresize');
1414

15-
// Usage example getOriginal ()
16-
const myImageDataDontresize = require('./images/oneImageForAllBreakpoints/all.png?dontresize')
17-
const myImageData = require('./images/oneImageForAllBreakpoints/all.png')
15+
const myImageData = require('./images/oneImageForAllBreakpoints/all.png');
16+
const myImageWithoutResizeData = require('./images/oneImageForAllBreakpoints/all.png?dontresize');
1817

19-
console.log(getOriginal(myImageData))
20-
console.log(getOriginal(myImageDataDontresize))
18+
// Usage example getOriginal ()
19+
console.log(getOriginal(myImageData));
20+
console.log(getOriginal(myImageWithoutResizeData));
2121

2222
ReactDOM.render(
2323
<div>
2424
<h1>Example usage of csssr.images</h1>
2525

2626
<h2>As picture tag</h2>
27+
2728
<h3>One image for all resolutions</h3>
28-
<PictureSmart requireImages={oneImageForAllBreakpointsWithoutResize} alt="One image for all resolutions" />
2929
<PictureSmart requireImages={oneImageForAllBreakpoints} alt="One image for all resolutions" />
30+
3031
<h3>Image with different breakpoints</h3>
3132
<PictureSmart requireImages={differentBreakpoints} alt="Image with different breakpoints" />
33+
34+
<h3>Image without resize</h3>
35+
<PictureSmart
36+
requireImages={imageWithoutResize}
37+
alt="Image without resize"
38+
/>
39+
3240
<h2>As background css</h2>
41+
3342
<h3>One image for all resolutions</h3>
3443
<div className="one-image-for-all-resolutions" style={{ width: '100%', height: '100%' }}>
3544
One image for all resolutions on background
3645
</div>
3746
<style>{backgroundCssSmart('.one-image-for-all-resolutions', oneImageForAllBreakpoints)}</style>
47+
3848
<h3>Different breakpoints</h3>
3949
<div className="different-breakpoints" style={{ width: '100%', height: '100%' }}>
4050
Image with different breakpoints on background
4151
</div>
4252
<style>{backgroundCssSmart('.different-breakpoints', differentBreakpoints)}</style>
53+
54+
<h3>Image without resize</h3>
55+
56+
<div className=".image-without-resize" style={{ width: '100%', height: '100%' }}>
57+
Image without resize on background
58+
</div>
59+
<style>{backgroundCssSmart('.image-without-resize', imageWithoutResize)}</style>
4360
</div>,
4461
document.getElementById('app'),
4562
);

example/webpack.config.ts

Lines changed: 48 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,52 @@
11
import path from 'path';
22
import webpack from 'webpack';
3-
const ip = require('ip')
3+
import ip from 'ip';
44
import { Plugin } from '../src/webpack';
5+
import { Dpr } from '../src/types';
6+
7+
const handleImagesForOriginalPixelRatio = (originalPixelRatio: Dpr) => {
8+
return {
9+
use: [
10+
{
11+
loader: path.resolve(__dirname, '../src/index.ts'),
12+
options: {
13+
breakpoints: [
14+
{
15+
name: 'mobile',
16+
maxWidth: 767,
17+
},
18+
{
19+
name: 'tablet',
20+
minWidth: 768,
21+
maxWidth: 1279,
22+
},
23+
{
24+
name: 'desktop',
25+
minWidth: 1280,
26+
},
27+
],
28+
imgproxy: {
29+
disable: false,
30+
imagesHost: process.env.HOST || `http://${ip.address()}:8081`,
31+
host: process.env.IMGPROXY_HOST || 'http://localhost:8080',
32+
},
33+
originalPixelRatio,
34+
},
35+
},
36+
{
37+
loader: 'file-loader',
38+
options: {
39+
publicPath: '/build',
40+
name:
41+
process.env.NODE_ENV === 'development'
42+
? '[path][name].[ext]'
43+
: '[path][name]-[hash:8].[ext]',
44+
esModule: false,
45+
},
46+
},
47+
],
48+
};
49+
};
550

651
const config: webpack.Configuration = {
752
mode: 'production',
@@ -27,87 +72,10 @@ const config: webpack.Configuration = {
2772
oneOf: [
2873
{
2974
resourceQuery: /dontresize/,
30-
// TODO reuse
31-
use: [
32-
{
33-
loader: path.resolve(__dirname, '../src/index.ts'),
34-
options: {
35-
breakpoints: [
36-
{
37-
name: 'mobile',
38-
maxWidth: 767,
39-
},
40-
{
41-
name: 'tablet',
42-
minWidth: 768,
43-
maxWidth: 1279,
44-
},
45-
{
46-
name: 'desktop',
47-
minWidth: 1280,
48-
},
49-
],
50-
imgproxy: {
51-
disable: false,
52-
imagesHost: process.env.HOST || `http://${ip.address()}:8081`,
53-
host: process.env.IMGPROXY_HOST || 'http://localhost:8080',
54-
},
55-
originalPixelRatio: '1x',
56-
},
57-
},
58-
{
59-
loader: 'file-loader',
60-
options: {
61-
publicPath: '/build',
62-
name:
63-
process.env.NODE_ENV === 'development'
64-
? '[path][name].[ext]'
65-
: '[path][name]-[hash:8].[ext]',
66-
esModule: false,
67-
},
68-
},
69-
],
75+
...handleImagesForOriginalPixelRatio('1x'),
7076
},
7177
{
72-
use: [
73-
{
74-
loader: path.resolve(__dirname, '../src/index.ts'),
75-
options: {
76-
breakpoints: [
77-
{
78-
name: 'mobile',
79-
maxWidth: 767,
80-
},
81-
{
82-
name: 'tablet',
83-
minWidth: 768,
84-
maxWidth: 1279,
85-
},
86-
{
87-
name: 'desktop',
88-
minWidth: 1280,
89-
},
90-
],
91-
imgproxy: {
92-
disable: false,
93-
imagesHost: process.env.HOST || `http://${ip.address()}:8081`,
94-
host: process.env.IMGPROXY_HOST || 'http://localhost:8080',
95-
},
96-
originalPixelRatio: '3x',
97-
},
98-
},
99-
{
100-
loader: 'file-loader',
101-
options: {
102-
publicPath: '/build',
103-
name:
104-
process.env.NODE_ENV === 'development'
105-
? '[path][name].[ext]'
106-
: '[path][name]-[hash:8].[ext]',
107-
esModule: false,
108-
},
109-
},
110-
]
78+
...handleImagesForOriginalPixelRatio('3x'),
11179
},
11280
],
11381
},

package-lock.json

Lines changed: 9 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"webpack-cli": "^3.3.12"
5151
},
5252
"dependencies": {
53+
"@types/ip": "^1.1.0",
5354
"imgproxy": "^0.1.2",
5455
"react": "^16.13.1",
5556
"react-dom": "^16.13.1"

src/types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
export type Dpr = '1x' | '2x' | '3x';
22

33
export type CompressionRatio = {
4-
"1x": number;
5-
"2x"?: number;
6-
"3x"?: number;
7-
}
4+
'1x': number;
5+
'2x'?: number;
6+
'3x'?: number;
7+
};
88

99
export type SrcSet = {
10-
"1x": string;
11-
"2x"?: string;
12-
"3x"?: string;
10+
'1x': string;
11+
'2x'?: string;
12+
'3x'?: string;
1313
};
1414

1515
export type ExtensionSrcSet = {

src/utils/getCompressionRatio.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
1-
import { Dpr, CompressionRatio } from '../types'
1+
import { Dpr, CompressionRatio } from '../types';
22

33
// [1x, 2x] -> {1x: 0.5, 2x: 0}
44

55
export const getCompressionRatio = (pixelRatios: Dpr[]): CompressionRatio => {
66
const length = pixelRatios.length;
77

88
return pixelRatios.reduce((acc, item, index) => {
9-
if (index + 1 === length) {
10-
acc[item]= 0
11-
return acc
12-
}
9+
if (index + 1 === length) {
10+
acc[item] = 0;
11+
return acc;
12+
}
1313

14-
acc[item] = (index + 1) / length
15-
return acc
16-
},{} as CompressionRatio)
17-
}
14+
acc[item] = (index + 1) / length;
15+
return acc;
16+
}, {} as CompressionRatio);
17+
};

src/utils/getOriginal.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ import { BreakpointSource } from '../types';
33
export const getOriginal = (source: BreakpointSource): string | undefined => {
44
const srcSet = source.srcSets[source.srcSets.length - 1].srcSet;
55

6-
return srcSet['3x'] || srcSet['2x'] || srcSet['1x']
7-
}
6+
return srcSet['3x'] || srcSet['2x'] || srcSet['1x'];
7+
};

src/utils/getPixelRations.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { Dpr } from '../types';
22

3-
export const getPixelRations = (originalPixelRatio: Dpr):Dpr[] => {
3+
export const getPixelRations = (originalPixelRatio: Dpr): Dpr[] => {
44
switch (originalPixelRatio) {
5-
case '1x': return ['1x']
6-
case '2x': return ['1x','2x']
7-
case '3x': return ['1x', '2x', '3x']
8-
default: return ['1x']
5+
case '1x':
6+
return ['1x'];
7+
case '2x':
8+
return ['1x', '2x'];
9+
case '3x':
10+
return ['1x', '2x', '3x'];
11+
default:
12+
return ['1x'];
913
}
10-
}
14+
};

src/utils/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ export { getBreakpointMedia } from './getBreakpointMedia';
33
export { getSrcSetString } from './getSrcSetString';
44
export { getSources } from './getSources';
55
export { getOriginal } from './getOriginal';
6-
export { getPixelRations } from './getPixelRations'
7-
export { getCompressionRatio } from './getCompressionRatio'
6+
export { getPixelRations } from './getPixelRations';
7+
export { getCompressionRatio } from './getCompressionRatio';

src/utils/webpDetector.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
// https://stackoverflow.com/a/27232658
22

33
const canUseWebp = (): boolean => {
4-
const canvas = document.createElement('canvas')
5-
canvas.width = canvas.height = 1
6-
return Boolean(canvas.toDataURL &&
7-
canvas.toDataURL('image/webp') &&
8-
canvas.toDataURL('image/webp').indexOf('image/webp') === 5)
9-
}
4+
const canvas = document.createElement('canvas');
5+
canvas.width = canvas.height = 1;
6+
return Boolean(
7+
canvas.toDataURL &&
8+
canvas.toDataURL('image/webp') &&
9+
canvas.toDataURL('image/webp').indexOf('image/webp') === 5,
10+
);
11+
};
1012

1113
if (canUseWebp()) {
12-
document.documentElement.classList.add('webp')
14+
document.documentElement.classList.add('webp');
1315
}

0 commit comments

Comments
 (0)