Skip to content

Commit be4fbab

Browse files
committed
feat: add special variable e.g., __buildVersion__
1 parent 5343922 commit be4fbab

File tree

4 files changed

+774
-741
lines changed

4 files changed

+774
-741
lines changed

lib/index.js

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,22 @@ async function imageCompression(file, options) {
6161
// eslint-disable-next-line no-undef, no-restricted-globals
6262
const inWebWorker = typeof WorkerGlobalScope !== 'undefined' && self instanceof WorkerGlobalScope;
6363

64-
// if ((useWebWorker && typeof Worker === 'function') || inWebWorker) {
65-
// console.log('run compression in web worker');
66-
// } else {
67-
// console.log('run compression in main thread');
68-
// }
64+
if (process.env.BUILD === 'development') {
65+
if ((useWebWorker && typeof Worker === 'function') || inWebWorker) {
66+
console.log('run compression in web worker');
67+
} else {
68+
console.log('run compression in main thread');
69+
}
70+
}
6971

7072
if (useWebWorker && typeof Worker === 'function' && !inWebWorker) {
7173
try {
7274
// "compressOnWebWorker" is kind of like a recursion to call "imageCompression" again inside web worker
7375
compressedFile = await compressOnWebWorker(file, opts);
7476
} catch (e) {
75-
// console.warn('Run compression in web worker failed:', e, ', fall back to main thread');
77+
if (process.env.BUILD === 'development') {
78+
console.warn('Run compression in web worker failed:', e, ', fall back to main thread');
79+
}
7680
compressedFile = await compress(file, opts);
7781
}
7882
} else {
@@ -102,6 +106,6 @@ imageCompression.cleanupCanvasMemory = cleanupCanvasMemory;
102106
imageCompression.isAutoOrientationInBrowser = isAutoOrientationInBrowser;
103107
imageCompression.approximateBelowMaximumCanvasSizeOfBrowser = approximateBelowMaximumCanvasSizeOfBrowser;
104108
imageCompression.getBrowserName = getBrowserName;
105-
imageCompression.version = '1.0.15';
109+
imageCompression.version = __buildVersion__;
106110

107111
export default imageCompression;

package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"scripts": {
1010
"eslint": "eslint lib test",
1111
"build": "rollup -c --environment BUILD:production",
12-
"watch": "rollup -c -w",
12+
"watch": "rollup -c -w --environment BUILD:development",
1313
"dev": "npm run watch",
1414
"test": "cross-env NODE_ENV=test nyc mocha",
1515
"posttest": "npm run coverage-badges",
@@ -47,8 +47,10 @@
4747
"@babel/polyfill": "^7.2.5",
4848
"@babel/preset-env": "^7.3.1",
4949
"@babel/register": "^7.0.0",
50+
"@rollup/plugin-babel": "^5.3.0",
5051
"@rollup/plugin-commonjs": "^17.0.0",
5152
"@rollup/plugin-node-resolve": "^11.0.1",
53+
"@rollup/plugin-replace": "^3.0.0",
5254
"babel-plugin-istanbul": "^6.0.0",
5355
"canvas": "2.6.1",
5456
"chai": "^4.1.0",
@@ -61,15 +63,13 @@
6163
"istanbul": "^0.4.5",
6264
"jsdom": "^15.1.1",
6365
"make-coverage-badge": "^1.0.1",
64-
"mocha": "7.1.0",
66+
"mocha": "^9.0.3",
6567
"nyc": "^15.0.1",
6668
"rollup": "^2.8.0",
67-
"rollup-plugin-babel": "^4.3.2",
6869
"rollup-plugin-copy": "^3.3.0",
6970
"rollup-plugin-license": "^2.0.0",
7071
"rollup-plugin-nodent": "^0.2.2",
71-
"rollup-plugin-terser": "^5.0.0",
72-
"rollup-plugin-uglify": "^6.0.2"
72+
"rollup-plugin-terser": "^5.0.0"
7373
},
7474
"config": {
7575
"commitizen": {

rollup.config.js

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,58 @@
1-
import babel from 'rollup-plugin-babel';
1+
import { babel } from '@rollup/plugin-babel';
22
import { terser } from 'rollup-plugin-terser';
33
import nodent from 'rollup-plugin-nodent';
44
import license from 'rollup-plugin-license';
55
import copy from 'rollup-plugin-copy';
66
import { nodeResolve } from '@rollup/plugin-node-resolve';
77
import commonjs from '@rollup/plugin-commonjs';
8+
import replace from '@rollup/plugin-replace';
89
import path from 'path';
910

1011
const pkg = require('./package.json');
1112

12-
const notExternal = [
13-
'uzip',
14-
];
15-
const external = Object.keys(pkg.dependencies).filter((value) => !notExternal.includes(value));
13+
const isProduction = process.env.BUILD === 'production';
14+
15+
const notExternal = ['uzip'];
16+
const external = Object.keys(pkg.dependencies).filter(
17+
(value) => !notExternal.includes(value),
18+
);
19+
external.push(/@babel\/runtime/);
1620

1721
const plugins = [
18-
nodent({ noRuntime: true, promises: true }),
19-
babel(),
20-
terser({
21-
keep_fnames: true,
22-
mangle: { reserved: ['CustomFile', 'CustomFileReader', 'UPNG', 'UZIP'] },
22+
replace({
23+
preventAssignment: true,
24+
values: {
25+
'process.env.NODE_ENV': JSON.stringify(process.env.BUILD),
26+
'process.env.BUILD': JSON.stringify(process.env.BUILD),
27+
__buildDate__: () => JSON.stringify(new Date()),
28+
__buildVersion__: JSON.stringify(pkg.version),
29+
},
30+
}),
31+
isProduction && nodent({ noRuntime: true, promises: true }),
32+
nodeResolve(),
33+
commonjs(),
34+
babel({
35+
babelHelpers: 'bundled',
2336
}),
37+
isProduction
38+
&& terser({
39+
keep_fnames: true,
40+
mangle: { reserved: ['CustomFile', 'CustomFileReader', 'UPNG', 'UZIP'] },
41+
}),
2442
license({
2543
sourcemap: true,
26-
banner: '<%= _.startCase(pkg.name) %>\nv<%= pkg.version %>\nby <%= pkg.author %>\n<%= pkg.repository.url %>',
44+
banner:
45+
'<%= _.startCase(pkg.name) %>\nv<%= pkg.version %>\nby <%= pkg.author %>\n<%= pkg.repository.url %>',
2746
}),
2847
copy({
2948
targets: [
30-
{ src: 'lib/index.d.ts', dest: path.dirname(pkg.types), rename: path.basename(pkg.types) },
49+
{
50+
src: 'lib/index.d.ts',
51+
dest: path.dirname(pkg.types),
52+
rename: path.basename(pkg.types),
53+
},
3154
],
3255
}),
33-
nodeResolve(),
34-
commonjs(),
3556
];
3657

3758
export default {
@@ -56,6 +77,5 @@ export default {
5677
uzip: 'UZIP',
5778
},
5879
},
59-
6080
],
6181
};

0 commit comments

Comments
 (0)