Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix .cjs and package.json in distribution #52

Merged
merged 4 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
62 changes: 62 additions & 0 deletions post-build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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"
}
}
Loading