mirror of
https://github.com/selfxyz/self.git
synced 2026-04-27 03:01:15 -04:00
feat: modernize mobile sdk build pipeline (#877)
* Add tsup config and postbuild shims * cr feedback
This commit is contained in:
76
packages/mobile-sdk-alpha/scripts/postBuild.mjs
Normal file
76
packages/mobile-sdk-alpha/scripts/postBuild.mjs
Normal file
@@ -0,0 +1,76 @@
|
||||
import { mkdirSync, readFileSync, writeFileSync } 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 version from package.json
|
||||
const packageJsonPath = path.resolve(__dirname, '..', 'package.json');
|
||||
const packageJson = JSON.parse(readFileSync(packageJsonPath, 'utf8'));
|
||||
|
||||
// Write package.json files for module type resolution
|
||||
try {
|
||||
// Ensure directories exist before writing files
|
||||
mkdirSync(path.join(DIST, 'esm'), { recursive: true });
|
||||
mkdirSync(path.join(DIST, 'cjs'), { recursive: true });
|
||||
|
||||
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));
|
||||
} catch (error) {
|
||||
console.error('Failed to write module type package.json files:', error.message);
|
||||
console.error('Target paths:', {
|
||||
esm: path.join(DIST, 'esm', 'package.json'),
|
||||
cjs: path.join(DIST, 'cjs', 'package.json'),
|
||||
});
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Create a package.json in dist root for Metro
|
||||
const distPackageJson = {
|
||||
name: '@selfxyz/mobile-sdk-alpha',
|
||||
version: packageJson.version,
|
||||
type: 'module',
|
||||
exports: {
|
||||
'.': './esm/index.js',
|
||||
'./browser': './esm/browser.js',
|
||||
},
|
||||
};
|
||||
try {
|
||||
writeFileSync(path.join(DIST, 'package.json'), JSON.stringify(distPackageJson, null, 4));
|
||||
} catch (error) {
|
||||
console.error('Failed to write dist package.json:', error.message);
|
||||
console.error('Target path:', path.join(DIST, 'package.json'));
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Helper to create shims for Metro
|
||||
function createShim(shimPath, targetPath, name) {
|
||||
try {
|
||||
const shimDir = path.join(DIST, shimPath);
|
||||
mkdirSync(shimDir, { recursive: true });
|
||||
|
||||
const cjsTargetPath = targetPath.replace('/esm/', '/cjs/').replace('.js', '.cjs');
|
||||
const dtsTarget = targetPath.replace('.js', '');
|
||||
|
||||
writeFileSync(
|
||||
path.join(shimDir, 'index.js'),
|
||||
`// Shim file to help Metro resolve @selfxyz/mobile-sdk-alpha/${name}\nmodule.exports = require('${cjsTargetPath}');`,
|
||||
);
|
||||
writeFileSync(
|
||||
path.join(shimDir, 'index.d.ts'),
|
||||
`// Shim file to help Metro resolve @selfxyz/mobile-sdk-alpha/${name} types\nexport * from "${dtsTarget}";`,
|
||||
);
|
||||
} catch (error) {
|
||||
console.error(`Failed to create shim for ${name}:`, error.message);
|
||||
console.error('Shim path:', path.join(DIST, shimPath));
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
// Create all shims from configuration
|
||||
shimConfigs.forEach(({ shimPath, targetPath, name }) => {
|
||||
createShim(shimPath, targetPath, name);
|
||||
});
|
||||
2
packages/mobile-sdk-alpha/scripts/shimConfigs.js
Normal file
2
packages/mobile-sdk-alpha/scripts/shimConfigs.js
Normal file
@@ -0,0 +1,2 @@
|
||||
// Shim configurations for Metro compatibility
|
||||
export const shimConfigs = [{ shimPath: 'browser', targetPath: '../esm/browser.js', name: 'browser' }];
|
||||
Reference in New Issue
Block a user