Skip to content

Commit b1959e0

Browse files
committed
feat: update exam,ple nextjs app
1 parent 7f8b783 commit b1959e0

File tree

4 files changed

+64
-49
lines changed

4 files changed

+64
-49
lines changed

example/Nextjs App/.eslintrc.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next/core-web-vitals"
3+
}

example/Nextjs App/next.config.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
swcMinify: true,
3+
};

example/Nextjs App/package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
"scripts": {
66
"dev": "next dev",
77
"build": "next build",
8-
"start": "next start"
8+
"start": "next start",
9+
"lint": "next lint"
910
},
1011
"dependencies": {
11-
"next": "9.3.5",
12-
"react": "16.13.1",
13-
"react-dom": "16.13.1"
12+
"browser-image-compression": "*",
13+
"next": "^12.2.5",
14+
"react": "^18.2.0",
15+
"react-dom": "^18.2.0"
16+
},
17+
"devDependencies": {
18+
"@next/eslint-plugin-next": "^12.2.5",
19+
"eslint": "^8.23.0",
20+
"eslint-config-next": "^12.2.5"
1421
}
1522
}

example/Nextjs App/pages/index.js renamed to example/Nextjs App/pages/index.jsx

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import React from 'react'
2-
import imageCompression from 'browser-image-compression'
1+
import React from 'react';
2+
import imageCompression from 'browser-image-compression';
33

44
export default class App extends React.Component {
5-
constructor (...args) {
6-
super(...args)
7-
this.compressImage = this.compressImage.bind(this)
8-
this.handleChange = this.handleChange.bind(this)
5+
constructor(...args) {
6+
super(...args);
7+
this.compressImage = this.compressImage.bind(this);
8+
this.handleChange = this.handleChange.bind(this);
99
this.state = {
1010
maxSizeMB: 1,
1111
maxWidthOrHeight: 1024,
@@ -14,72 +14,74 @@ export default class App extends React.Component {
1414
inputSize: null,
1515
outputSize: null,
1616
inputUrl: null,
17-
outputUrl: null
17+
outputUrl: null,
1818
},
1919
mainThread: {
2020
progress: null,
2121
inputSize: null,
2222
outputSize: null,
2323
inputUrl: null,
24-
outputUrl: null
25-
}
26-
}
24+
outputUrl: null,
25+
},
26+
};
2727
}
2828

29-
handleChange (target) {
29+
handleChange(target) {
3030
return (e) => {
31-
this.setState({ [target]: e.currentTarget.value })
32-
}
31+
this.setState({ [target]: e.currentTarget.value });
32+
};
3333
}
3434

35-
onProgress (p, useWebWorker) {
36-
const targetName = useWebWorker ? 'webWorker' : 'mainThread'
37-
this.setState(prevState => ({
35+
onProgress(p, useWebWorker) {
36+
const targetName = useWebWorker ? 'webWorker' : 'mainThread';
37+
this.setState((prevState) => ({
3838
...prevState,
3939
[targetName]: {
4040
...prevState[targetName],
41-
progress: p
42-
}
43-
}))
41+
progress: p,
42+
},
43+
}));
4444
}
4545

46-
async compressImage (event, useWebWorker) {
47-
const file = event.target.files[0]
48-
console.log('input', file)
46+
async compressImage(event, useWebWorker) {
47+
const file = event.target.files[0];
48+
console.log('input', file);
4949
console.log(
5050
'ExifOrientation',
51-
await imageCompression.getExifOrientation(file)
52-
)
53-
const targetName = useWebWorker ? 'webWorker' : 'mainThread'
54-
this.setState(prevState => ({
51+
await imageCompression.getExifOrientation(file),
52+
);
53+
const targetName = useWebWorker ? 'webWorker' : 'mainThread';
54+
this.setState((prevState) => ({
5555
...prevState,
5656
[targetName]: {
5757
...prevState[targetName],
5858
inputSize: (file.size / 1024 / 1024).toFixed(2),
59-
inputUrl: URL.createObjectURL(file)
60-
}
61-
}))
62-
var options = {
59+
inputUrl: URL.createObjectURL(file),
60+
},
61+
}));
62+
const options = {
6363
maxSizeMB: this.state.maxSizeMB,
6464
maxWidthOrHeight: this.state.maxWidthOrHeight,
6565
useWebWorker,
66-
onProgress: p => this.onProgress(p, useWebWorker)
67-
}
68-
const output = await imageCompression(file, options)
69-
console.log('output', output)
70-
this.setState(prevState => ({
66+
onProgress: (p) => this.onProgress(p, useWebWorker),
67+
};
68+
const output = await imageCompression(file, options);
69+
console.log('output', output);
70+
this.setState((prevState) => ({
7171
...prevState,
7272
[targetName]: {
7373
...prevState[targetName],
7474
outputSize: (output.size / 1024 / 1024).toFixed(2),
75-
outputUrl: URL.createObjectURL(output)
76-
}
77-
}))
75+
outputUrl: URL.createObjectURL(output),
76+
},
77+
}));
7878
}
7979

80-
render () {
81-
const version = imageCompression.version
82-
const { webWorker, mainThread, maxSizeMB, maxWidthOrHeight } = this.state
80+
render() {
81+
const { version } = imageCompression;
82+
const {
83+
webWorker, mainThread, maxSizeMB, maxWidthOrHeight,
84+
} = this.state;
8385
return (
8486
<div className="App">
8587
<div>
@@ -100,7 +102,7 @@ export default class App extends React.Component {
100102
id="web-worker"
101103
type="file"
102104
accept="image/*"
103-
onChange={e => this.compressImage(e, true)}
105+
onChange={(e) => this.compressImage(e, true)}
104106
/>
105107
</label>
106108
<p>
@@ -120,7 +122,7 @@ export default class App extends React.Component {
120122
id="main-thread"
121123
type="file"
122124
accept="image/*"
123-
onChange={e => this.compressImage(e, false)}
125+
onChange={(e) => this.compressImage(e, false)}
124126
/>
125127
</label>
126128
<p>
@@ -170,6 +172,6 @@ export default class App extends React.Component {
170172
}
171173
`}</style>
172174
</div>
173-
)
175+
);
174176
}
175-
};
177+
}

0 commit comments

Comments
 (0)