Skip to content

Commit 17c600b

Browse files
committed
refactor: default export compressOnWebWorker and eslint fix
1 parent de70d8d commit 17c600b

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

lib/index.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
getBrowserName,
1919
} from './utils';
2020
// eslint-disable-next-line import/no-cycle
21-
import { compressOnWebWorker } from './web-worker';
21+
import compressOnWebWorker from './web-worker';
2222

2323
/**
2424
* Compress an image file.
@@ -86,8 +86,11 @@ async function imageCompression(file, options) {
8686
try {
8787
compressedFile.name = file.name;
8888
compressedFile.lastModified = file.lastModified;
89-
// eslint-disable-next-line no-empty
90-
} catch (e) {}
89+
} catch (e) {
90+
if (process.env.BUILD === 'development') {
91+
console.error(e);
92+
}
93+
}
9194

9295
return compressedFile;
9396
}

lib/web-worker.js

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,13 @@ let imageCompressionLibUrl;
1111
let worker;
1212

1313
function createWorker(script) {
14+
const blobArgs = [];
1415
if (typeof script === 'function') {
15-
script = `(${script})()`;
16+
blobArgs.push(`(${script})()`);
17+
} else {
18+
blobArgs.push(script);
1619
}
17-
return new Worker(URL.createObjectURL(new Blob([script])));
20+
return new Worker(URL.createObjectURL(new Blob(blobArgs)));
1821
}
1922

2023
function createSourceObject(str) {
@@ -32,9 +35,12 @@ function parse(o) {
3235
Object.entries(o).forEach(([key, value]) => {
3336
if (typeof value === 'string' && value.startsWith('BIC_FN:::')) {
3437
try {
38+
// eslint-disable-next-line no-eval
3539
result[key] = eval(value.replace(/^BIC_FN:::/, ''));
3640
} catch (e) {
37-
// console.log(key, e);
41+
if (process.env.BUILD === 'development') {
42+
console.error(key, e);
43+
}
3844
throw e;
3945
}
4046
} else {
@@ -335,9 +341,10 @@ function generateWorkerScript() {
335341
`);
336342
}
337343

338-
export function compressOnWebWorker(file, options) {
339-
return new Promise(async (resolve, reject) => {
340-
const id = cnt++;
344+
export default function compressOnWebWorker(file, options) {
345+
return new Promise((resolve, reject) => {
346+
cnt += 1;
347+
const id = cnt;
341348

342349
if (!imageCompressionLibUrl) {
343350
imageCompressionLibUrl = generateLib();

0 commit comments

Comments
 (0)