Files
self/common/scripts/postBuild.mjs
Justin Hernandez 82d26669bc Enable tree-shakeable exports (#823)
* Add tree-shakeable exports

* Migrate imports for tree-shakeable paths

* Document ESM extension requirement

* udpates

* install new lock

* yarn nice

* build deps

* save working index export no wildcard approach

* save wip

* fix building

* add tree shaking doc and examples

* sort package json files

* update package.json

* fix analyzing web

* make sure that web is built

* wip tree shaking

* building works again. save wip logic

* use granular imports

* wip test

* save wip

* Remove hardcoded .d.ts files and setup automatic TypeScript declaration generation

- Remove redundant constants.d.ts, types.d.ts, utils.d.ts files
- Add build:types script to automatically generate TypeScript declarations
- Update tsup config to disable DTS generation (handled separately)
- Update .gitignore to prevent future commits of generated .d.ts files
- Fixes import resolution errors in app by ensuring declarations are always generated

* Add .gitignore rules for generated TypeScript declarations

* ignore dts files

* Remove redundant index.js re-export files

- Remove constants.js, types.js, utils.js as they're redundant with tsup build
- These were just re-exports pointing to dist files that tsup generates
- package.json exports already point directly to built files
- Update .gitignore to prevent future commits of these generated files
- tsup handles all the building, no manual re-export files needed

* save current wip fixes

* add tsup config for web building

* common prettier and fix imports

* prettier

* fix tests

* implement level 3 tree shaking

* improve splitting

* optimize vite web building and prettier

* remove comments

* sort export params

* feedback and fix pipelines

* fix circuit-names path

* fix test

* fix building

* sort

* fix building

* allow cursor to edit scripts

* fix loadDocumentCatalog undefined

* fix build settings

* fix build settings

* additional metro tree shaking

* improved discovery script for xcode building

* pr feedback and fix camelCasing

* simplify shim setup

* fix xcode building and add command to test building

* remove comment

* simplify
2025-08-02 16:55:05 -07:00

60 lines
2.1 KiB
JavaScript

import { writeFileSync, mkdirSync, readFileSync } from 'node:fs';
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { shimConfigs } from './shimConfigs.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const DIST = path.resolve(__dirname, '..', 'dist');
// Read the version from the main package.json
const packageJsonPath = path.resolve(__dirname, '..', 'package.json');
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
writeFileSync(path.join(DIST, 'esm', 'package.json'), JSON.stringify({ type: 'module' }, null, 4));
writeFileSync(
path.join(DIST, 'cjs', 'package.json'),
JSON.stringify({ type: 'commonjs' }, null, 4)
);
// Create a package.json in the dist root for Metro compatibility
const distPackageJson = {
name: '@selfxyz/common',
version: packageJson.version,
type: 'module',
exports: {
'.': './esm/index.js',
'./constants': './esm/src/constants/index.js',
'./utils': './esm/src/utils/index.js',
'./types': './esm/src/types/index.js',
},
};
writeFileSync(path.join(DIST, 'package.json'), JSON.stringify(distPackageJson, null, 4));
// Create shim files for Metro compatibility
// Metro sometimes doesn't properly resolve package.json exports, so we create direct file shims
// Helper function to create shim files
function createShim(shimPath, targetPath, name) {
const shimDir = path.join(DIST, shimPath);
mkdirSync(shimDir, { recursive: true });
// Convert ESM path to CommonJS path for proper require() compatibility
const cjsTargetPath = targetPath.replace('/esm/', '/cjs/').replace('.js', '.cjs');
writeFileSync(
path.join(shimDir, 'index.js'),
`// Shim file to help Metro resolve @selfxyz/common/${name}
module.exports = require('${cjsTargetPath}');`
);
writeFileSync(
path.join(shimDir, 'index.d.ts'),
`// Shim file to help Metro resolve @selfxyz/common/${name} types
export * from '${targetPath.replace('.js', '')}';`
);
}
// Create all shims from configuration
shimConfigs.forEach((config) => {
createShim(config.shimPath, config.targetPath, config.name);
});