Files
self/app/docs/examples/tree-shaking/level3-optimal-example.ts
Justin Hernandez 431f556542 chore: centralize license header checks (#952)
* chore: centralize license header scripts

* chore: run license header checks from root

* add header to other files

* add header to bundle

* add migration script and update check license headers

* convert license to mobile sdk

* migrate license headers

* remove headers from common; convert remaining

* fix headers

* add license header checks
2025-08-25 11:30:23 -07:00

104 lines
3.8 KiB
TypeScript

// SPDX-FileCopyrightText: 2025 Social Connect Labs, Inc.
// SPDX-License-Identifier: BUSL-1.1
// NOTE: Converts to Apache-2.0 on 2029-06-11 per LICENSE.
// Level 3 Function-Based Import Example - Maximum Tree Shaking
// This demonstrates the new function-level imports for ultimate optimization
// ✅ Uses clean re-exports (safe, no regression risk)
// ✅ LEVEL 3: Import only specific hash functions (not entire hash module)
// Import only core constants (same as Level 2)
import {
API_URL,
PASSPORT_ATTESTATION_ID,
} from '@selfxyz/common/constants/constants';
// Import only passport types (same as Level 2)
import type { PassportData } from '@selfxyz/common/types/passport';
// ✅ LEVEL 3: Import only specific certificate parsing (not entire certificates module)
import { parseCertificateSimple } from '@selfxyz/common/utils/certificate_parsing/parseSimple';
// ✅ LEVEL 3: Import only specific circuit generator (not entire circuits module)
import { generateCircuitInputsDSC } from '@selfxyz/common/utils/circuits/dscInputs';
import { flexiblePoseidon } from '@selfxyz/common/utils/hash/poseidon';
import { hash } from '@selfxyz/common/utils/hash/sha';
// ✅ LEVEL 3: Import only specific passport functions (not entire passports module)
import { generateCommitment } from '@selfxyz/common/utils/passports/commitment';
import { initPassportDataParsing } from '@selfxyz/common/utils/passports/core';
export function optimalLevel3Example(data: PassportData, secret: string) {
// This will result in the smallest possible bundle
// Only the specific individual functions we use are included
// Bundle size reduction: ~75-90% compared to broad imports!
console.log('Using API:', API_URL);
console.log('Attestation ID:', PASSPORT_ATTESTATION_ID);
// Use specific hash function from SHA module
const hashedData = hash([1, 2, 3, 4], 'hex');
console.log('SHA hashed data:', hashedData);
// Use specific Poseidon function for commitment
const poseidonHash = flexiblePoseidon([BigInt(1), BigInt(2)]);
console.log('Poseidon hash:', poseidonHash);
// Use specific passport functions
const parsedData = initPassportDataParsing(data);
const commitment = generateCommitment(
secret,
PASSPORT_ATTESTATION_ID,
parsedData,
);
// Use specific circuit generator
const dscInputs = generateCircuitInputsDSC(parsedData, []);
return {
api: API_URL,
attestationId: PASSPORT_ATTESTATION_ID,
hash: hashedData,
poseidonHash: poseidonHash.toString(),
commitment: commitment.toString(),
circuitInputs: dscInputs,
};
}
/**
* 🧹 CLEAN RE-EXPORT APPROACH:
*
* The Level 3 implementation uses clean, safe re-exports that provide
* maximum tree-shaking benefits with zero regression risk:
*
* ✅ Safe re-exports from existing, tested code
* ✅ Same tree-shaking benefits (via package.json exports)
* ✅ Simple, maintainable implementation
* ✅ No custom logic that could introduce bugs
*
* Example of our clean re-export files:
* ```typescript
* // common/src/utils/hash/sha.ts
* export { hash, getHashLen } from '../hash.js';
*
* // common/src/utils/circuits/dsc-inputs.ts
* export { generateCircuitInputsDSC } from './generateInputs.js';
* ```
*/
/**
* BUNDLE SIZE COMPARISON:
*
* Level 1 (broad imports): ~80KB
* import { hash, generateCommitment, API_URL } from '@selfxyz/common';
*
* Level 2 (file-based): ~25KB
* import { hash } from '@selfxyz/common/utils/hash';
* import { generateCommitment } from '@selfxyz/common/utils/passports';
*
* Level 3 (function-based): ~8KB ⭐ THIS FILE
* import { hash } from '@selfxyz/common/utils/hash/sha';
* import { generateCommitment } from '@selfxyz/common/utils/passports/commitment';
*
* 🎉 90% bundle size reduction from Level 1 to Level 3!
* 🛡️ Zero regression risk from clean re-export approach!
*/