Skip to content

Commit

Permalink
Merge branch 'main' into anchor-bankrun-connection-helper
Browse files Browse the repository at this point in the history
  • Loading branch information
heyAyushh committed Sep 11, 2024
2 parents d45fb1e + 9bbba20 commit aca1d03
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 21 deletions.
30 changes: 17 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,33 @@
"name": "@solana-developers/helpers",
"version": "2.5.4",
"description": "Solana helper functions",
"main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/esm/index.d.ts",
"type": "module",
"private": false,
"main": "./dist/esm/index.js",
"module": "./dist/esm/index.js",
"types": "./dist/types/index.d.ts",
"exports": {
".": {
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
},
"./node": {
"import": "./dist/esm/node.js",
"require": "./dist/cjs/node.js"
"import": {
"types": "./dist/types/index.d.ts",
"default": "./dist/esm/index.js"
},
"require": {
"default": "./dist/cjs/index.cjs"
}
}
},
"private": false,
"sideEffects": false,
"files": [
"dist"
],
"sideEffects": false,
"scripts": {
"build": "npm run build:esm && npm run build:cjs",
"build:esm": "tsc -p tsconfig.json",
"prebuild": "npm run clean",
"build": "npm run build:esm && npm run build:cjs && npm run build:types",
"build:esm": "tsc -p tsconfig.esm.json",
"build:cjs": "tsc -p tsconfig.cjs.json",
"build:types": "tsc -p tsconfig.types.json",
"postbuild": "node post-build.mjs",
"clean": "rm -rf dist",
"test": "cd tests && npm run test",
"test:ci": "cd tests && npm run test:ci"
Expand Down
68 changes: 68 additions & 0 deletions post-build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// In some cases, where you have { type: module } in package.json at the root
// We will workaround builds manually, since we aren't using any bundler
// tsc is missing extensions with esm exports
// and tsc doesn't export .cjs hence, index.js won't be read as cjs in cjs distribution
// See https://github.com/microsoft/TypeScript/issues/54593

import { promises as fs } from 'node:fs';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

const __dirname = dirname(fileURLToPath(import.meta.url));
const CJS_DIR = join(__dirname, 'dist', 'cjs');
const ESM_DIR = join(__dirname, 'dist', 'esm');

async function createPackageJson(dir, type) {
const content = JSON.stringify({ type }, null, 2);
await fs.writeFile(join(dir, 'package.json'), content);
console.log(`Created package.json in ${dir}`);
}

async function renameIndexFile() {
const oldPath = join(CJS_DIR, 'index.js');
const newPath = join(CJS_DIR, 'index.cjs');
await fs.rename(oldPath, newPath);
console.log('Renamed CJS index file to index.cjs');
}

async function processEsmFiles(dir) {
const entries = await fs.readdir(dir, { withFileTypes: true });
for (const entry of entries) {
const fullPath = join(dir, entry.name);
if (entry.isDirectory()) {
await processEsmFiles(fullPath);
} else if (entry.isFile() && entry.name.endsWith('.js')) {
let content = await fs.readFile(fullPath, 'utf8');

// Add .js extension to import statements
content = content.replace(/from\s+['"](\.[^'"]+)['"]/g, "from '$1.js'");

// Add .js extension to export statements
content = content.replace(/export\s+\*\s+from\s+['"](\.[^'"]+)['"]/g, "export * from '$1.js'");

await fs.writeFile(fullPath, content);
console.log(`Processed ESM file: ${fullPath}`);
}
}
}

async function postbuild() {
try {
// Create package.json files
await createPackageJson(CJS_DIR, 'commonjs');
await createPackageJson(ESM_DIR, 'module');

// Rename index.js to index.cjs in CJS directory
await renameIndexFile();

// Process ESM files to add .js extensions
await processEsmFiles(ESM_DIR);

console.log('Postbuild completed successfully!');
} catch (error) {
console.error('Postbuild failed:', error);
process.exit(1);
}
}

postbuild();
3 changes: 2 additions & 1 deletion tsconfig.cjs.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"outDir": "./dist/cjs"
"outDir": "./dist/cjs",
"sourceMap": true
}
}
8 changes: 8 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"outDir": "./dist/esm",
"sourceMap": true
}
}
10 changes: 3 additions & 7 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
"target": "ES2018",
"lib": ["ES2018", "DOM"],
"moduleResolution": "node",
"outDir": "./dist/esm",
"rootDir": "./src",
"declaration": true,
"declarationMap": true,
"sourceMap": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"module": "ESNext",
"isolatedModules": true
"isolatedModules": true,
"preserveConstEnums": true,
"rootDir": "./src"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist"]
Expand Down
10 changes: 10 additions & 0 deletions tsconfig.types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"declaration": true,
"declarationMap": true,
"emitDeclarationOnly": true,
"outDir": "./dist/types"
}
}

0 comments on commit aca1d03

Please sign in to comment.