Skip to content

Commit 74142eb

Browse files
Fix various issues with tree shaking (#440)
- Remove dynamic objects from the main exports (Actions, Qualifiers, SDKURLGen etc.) - Remove import/export pairing - use export directly from the import path - Remove Actions/Qualifiers from the index.ts entrypoint - Improve the bundle-size testing by adding more cases and added safety to detect false-imports (I.E an import that imports nothing)
1 parent 667b68d commit 74142eb

File tree

14 files changed

+106
-160
lines changed

14 files changed

+106
-160
lines changed

__TESTS_BUNDLE_SIZE__/bundleSizeTestCases.ts

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import importFromPackage from "./utils/stringGenerators/importFromPackage";
1515
const bundleSizeTestCases:ITestCase[] = [
1616
{
1717
name: 'Tests CloudinaryImage with Resize',
18-
sizeLimitInKB: 20,
18+
sizeLimitInKB: 17,
1919
importsArray: [
2020
importFromDist('assets/CloudinaryImage', 'CloudinaryImage'),
2121
importFromDist('instance/Cloudinary', 'Cloudinary'),
@@ -24,7 +24,7 @@ const bundleSizeTestCases:ITestCase[] = [
2424
},
2525
{
2626
name: 'Tests CloudinaryImage with Resize and Adjust',
27-
sizeLimitInKB: 30,
27+
sizeLimitInKB: 20,
2828
importsArray: [
2929
importFromDist('assets/CloudinaryImage', 'CloudinaryImage'),
3030
importFromDist('instance/Cloudinary', 'Cloudinary'),
@@ -34,7 +34,7 @@ const bundleSizeTestCases:ITestCase[] = [
3434
},
3535
{
3636
name: 'Tests CloudinaryImage with Resize, Adjust and Border',
37-
sizeLimitInKB: 30,
37+
sizeLimitInKB: 20,
3838
importsArray: [
3939
importFromDist('assets/CloudinaryImage', 'CloudinaryImage'),
4040
importFromDist('instance/Cloudinary', 'Cloudinary'),
@@ -45,7 +45,7 @@ const bundleSizeTestCases:ITestCase[] = [
4545
},
4646
{
4747
name: 'Tests CloudinaryImage image with Resize, adjust and delivery',
48-
sizeLimitInKB: 32,
48+
sizeLimitInKB: 21,
4949
importsArray: [
5050
importFromDist('assets/CloudinaryImage', 'CloudinaryImage'),
5151
importFromDist('instance/Cloudinary', 'Cloudinary'),
@@ -56,7 +56,7 @@ const bundleSizeTestCases:ITestCase[] = [
5656
},
5757
{
5858
name: 'Tests Overlay imports',
59-
sizeLimitInKB: 50,
59+
sizeLimitInKB: 20,
6060
importsArray: [
6161
importFromDist('assets/CloudinaryImage', 'CloudinaryImage'),
6262
importFromDist('actions/overlay', 'Overlay'),
@@ -67,7 +67,7 @@ const bundleSizeTestCases:ITestCase[] = [
6767
},
6868
{
6969
name: 'Import backwards compatibility function',
70-
sizeLimitInKB: 57,
70+
sizeLimitInKB: 45,
7171
importsArray: [
7272
importFromPackage('createCloudinaryLegacyURL')
7373
]
@@ -76,7 +76,28 @@ const bundleSizeTestCases:ITestCase[] = [
7676
name: 'Import all of the SDK',
7777
sizeLimitInKB: 118,
7878
importsArray: [
79-
importFromPackage('CloudinaryURLGEN')
79+
importFromPackage('* as CloudinaryURLGEN')
80+
]
81+
},
82+
{
83+
name: 'Import a Transformation Object',
84+
sizeLimitInKB: 5,
85+
importsArray: [
86+
importFromPackage('Transformation')
87+
]
88+
},
89+
{
90+
name: 'Import All Actions',
91+
sizeLimitInKB: 30,
92+
importsArray: [
93+
importFromPackage('Actions')
94+
]
95+
},
96+
{
97+
name: 'Import All Qualifiers',
98+
sizeLimitInKB: 35,
99+
importsArray: [
100+
importFromPackage('Qualifiers')
80101
]
81102
}
82103
];

__TESTS_BUNDLE_SIZE__/bundleSizeTestRunner.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ async function bundleSizeTestRunner():Promise<void> {
2828
const OUTPUT_FILE = `bundle${i}`;
2929
const TEST_NAME = testCases[i].name;
3030
const EXPECTED_SIZE_IN_KB = testCases[i].sizeLimitInKB;
31+
const ALLOWED_MIN_SIZE_IN_KB = testCases[i].minAllowedSize || 2;
3132

3233
// Create the entry file for Webpack
3334
log.debug('Starting to build webpack loop');
@@ -42,10 +43,11 @@ async function bundleSizeTestRunner():Promise<void> {
4243
const bundleInfo = getBundleInfo(OUTPUT_FILE);
4344
const ACTUAL_SIZE_IN_KB = Math.round(bundleInfo.size / 1024);
4445

45-
if (ACTUAL_SIZE_IN_KB <= EXPECTED_SIZE_IN_KB) {
46+
47+
if (ACTUAL_SIZE_IN_KB <= EXPECTED_SIZE_IN_KB && ACTUAL_SIZE_IN_KB > ALLOWED_MIN_SIZE_IN_KB) {
4648
handleTestSuccess(testCases[i], ACTUAL_SIZE_IN_KB);
4749
} else {
48-
handleTestError(TEST_NAME, EXPECTED_SIZE_IN_KB, ACTUAL_SIZE_IN_KB);
50+
handleTestError(TEST_NAME, EXPECTED_SIZE_IN_KB, ACTUAL_SIZE_IN_KB, ALLOWED_MIN_SIZE_IN_KB);
4951
fail_count++;
5052
}
5153
}

__TESTS_BUNDLE_SIZE__/interfaces/ITestCase.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
export interface ITestCase {
22
sizeLimitInKB: number,
3+
// The minimum allowed size, a bundle size below this size will fail the test
4+
// Useful to ensure that something is actually imported!
5+
// Defaults internally to 2kb if field is omitted
6+
minAllowedSize?:number,
37
importsArray: string[],
48
name: string,
59
importString?: string

__TESTS_BUNDLE_SIZE__/utils/stringGenerators/importFromPackage.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@
55
* @returns string
66
*/
77
function importFromPackage(exportedObject: string): string {
8+
const importString = exportedObject.includes('* as') ? exportedObject : `{${exportedObject}}`;
9+
const variableName = exportedObject.includes('* as') ? exportedObject.split(' as ')[1] : exportedObject;
10+
811
return `
9-
import {${exportedObject}} from "${process.cwd()}/dist";
12+
import ${importString} from "${process.cwd()}/dist";
1013
// we console log to force the bundle not to tree shake
11-
console.log(${exportedObject});
14+
console.log(${variableName});
1215
`;
1316
}
1417

__TESTS_BUNDLE_SIZE__/utils/testLifeCycle/handleTestError.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ import log from "../log";
66
* @param expected
77
* @param received
88
*/
9-
function handleTestError(name: string, expected: number, received: number) {
9+
function handleTestError(name: string, expected: number, received: number, minAllowed: number) {
1010
log.error(name);
11-
log.error(`\tExpected size: ${expected} KB`);
11+
log.error(`\tExpected size(range): ${minAllowed} - ${expected} KB`);
1212
log.error(`\tActual size: ${received} KB\n`);
1313
}
1414

__TESTS__/TestUtils/createCloudinaryFile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import ICloudConfig from "../../src/config/interfaces/Config/ICloudConfig";
22
import IURLConfig from "../../src/config/interfaces/Config/IURLConfig";
3-
import {CloudinaryFile} from "../../src";
3+
import {CloudinaryFile} from "../../src/assets/CloudinaryFile";
44

55
/**
66
*

__TESTS__/backwardsComaptibility/createLegacyURL.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {createTestURL} from "./transformationLegacyTests/utils/createTestURL";
2-
import {createCloudinaryLegacyURL} from "../../src";
32
import Transformation from "../../src/backwards/transformation";
3+
import {createCloudinaryLegacyURL} from "../../src/backwards/createCloudinaryLegacyURL";
44

55
describe('Create legacy urls', () => {
66
it('Should throw without cloudName', () => {

__TESTS__/backwardsComaptibility/transformationLegacyTests/utils/createTestURL.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Cloudinary} from 'cloudinary-core';
2-
import {createCloudinaryLegacyURL} from "../../../../src";
32
import {LegacyITransforamtionOptions} from "../../../../src/types/types";
3+
import {createCloudinaryLegacyURL} from "../../../../src/backwards/createCloudinaryLegacyURL";
44

55
const CLegacy = Cloudinary.new( { cloud_name: "demo"});
66

__TESTS__/compilationsOutput.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import { FocusOn } from "qualifiers/focusOn";
3030
import { AutoFocus } from "qualifiers/autoFocus";
3131
import { VideoCodec } from "qualifiers/videoCodec";
3232
import { Adjust } from "actions/adjust";
33-
import { ImageTransformation } from "index";
33+
import { ImageTransformation } from "transformation/ImageTransformation";
3434
import { Effect } from "actions/effect";
3535
import { Conditional } from "actions/conditional";
3636
import { Underlay } from "actions/underlay";

__TESTS__/unit/analytics/testUtils/createNewImageWithAnalytics.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import ICloudConfig from "../../../../src/config/interfaces/Config/ICloudConfig";
22
import IURLConfig from "../../../../src/config/interfaces/Config/IURLConfig";
3-
import {CloudinaryImage} from "../../../../src";
43
import {createNewImage} from "../../../TestUtils/createCloudinaryImage";
4+
import {CloudinaryImage} from "../../../../src/assets/CloudinaryImage";
55

66
/**
77
* Create a new CloudinaryImage with analytics turned on by default

0 commit comments

Comments
 (0)