Skip to content

Commit

Permalink
feat: commonJS builds
Browse files Browse the repository at this point in the history
  • Loading branch information
andy-haynes committed Aug 19, 2024
1 parent ba2a81e commit 31b7987
Show file tree
Hide file tree
Showing 47 changed files with 419 additions and 78 deletions.
14 changes: 8 additions & 6 deletions packages/accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"name": "@near-js/accounts",
"version": "1.2.1",
"description": "Classes encapsulating account-specific functionality",
"main": "lib/index.js",
"main": "lib/esm/index.js",
"type": "module",
"scripts": {
"build": "pnpm compile",
"compile": "tsc -p tsconfig.json",
"build": "pnpm compile:esm && pnpm compile:cjs",
"compile:esm": "tsc -p tsconfig.json",
"compile:cjs": "tsc -p tsconfig.cjs.json && cjsify ./lib/commonjs",
"lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc",
"lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix",
"test": "jest"
Expand Down Expand Up @@ -35,6 +36,7 @@
"@types/json-schema": "^7.0.15",
"@types/node": "20.0.0",
"bs58": "4.0.0",
"build": "workspace:*",
"jest": "29.7.0",
"near-hello": "0.5.1",
"near-workspaces": "3.5.0",
Expand All @@ -47,8 +49,8 @@
"files": [
"lib"
],
"resolutions": {
"near-sandbox": "0.0.18",
"near-api-js": "4.0.0"
"exports": {
"require": "./lib/commonjs/index.cjs",
"import": "./lib/esm/index.js"
}
}
10 changes: 10 additions & 0 deletions packages/accounts/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "tsconfig/cjs.json",
"compilerOptions": {
"outDir": "./lib/commonjs",
"lib": ["es2022", "dom"]
},
"files": [
"src/index.ts"
]
}
2 changes: 1 addition & 1 deletion packages/accounts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "tsconfig/esm.json",
"compilerOptions": {
"outDir": "./lib",
"outDir": "./lib/esm",
"lib": ["es2022", "dom"]
},
"files": [
Expand Down
13 changes: 10 additions & 3 deletions packages/biometric-ed25519/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"name": "@near-js/biometric-ed25519",
"description": "JavaScript library to handle webauthn and biometric keys",
"version": "1.2.4",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"main": "lib/esm/index.js",
"types": "lib/esm/index.d.ts",
"type": "module",
"scripts": {
"build": "tsc -p ./tsconfig.json",
"build": "pnpm compile:esm && pnpm compile:cjs",
"compile:esm": "tsc -p tsconfig.json",
"compile:cjs": "tsc -p tsconfig.cjs.json && cjsify ./lib/commonjs",
"lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc",
"lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix",
"test": "jest"
Expand All @@ -23,6 +25,7 @@
"asn1-parser": "1.1.8",
"borsh": "1.0.0",
"buffer": "6.0.3",
"build": "workspace:*",
"cbor-js": "^0.1.0",
"fido2-lib": "3.4.1"
},
Expand All @@ -31,5 +34,9 @@
"@types/node": "20.0.0",
"jest": "29.7.0",
"ts-jest": "29.1.5"
},
"exports": {
"require": "./lib/commonjs/index.cjs",
"import": "./lib/esm/index.js"
}
}
10 changes: 10 additions & 0 deletions packages/biometric-ed25519/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "tsconfig/cjs.json",
"compilerOptions": {
"outDir": "./lib/commonjs",
"lib": ["es2022", "dom"]
},
"files": [
"src/index.ts"
]
}
2 changes: 1 addition & 1 deletion packages/biometric-ed25519/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "tsconfig/browser.json",
"compilerOptions": {
"outDir": "./lib",
"outDir": "./lib/esm",
},
"files": [
"src/index.ts",
Expand Down
58 changes: 58 additions & 0 deletions packages/build/cjsify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { lstat, opendir, readFile, rename, writeFile } from 'node:fs/promises';
import path from 'node:path';

async function enumerateContents(contentPath) {
const dir = await opendir(contentPath);
let files = [];
for await (let entry of dir) {
if (entry.name === 'node_modules') {
continue;
}

const entryPath = path.join(contentPath, entry.name);
if (entry.isDirectory()) {
files = [...files, ...(await enumerateContents(entryPath))];
} else if (entry.name.endsWith('.js')) {
files.push(entryPath);
}
}

return files;
}

async function cjsIfy() {
const [,, inputPath] = process.argv;
const basePath = path.resolve(process.cwd(), inputPath);

for (let projectFilePath of await enumerateContents(basePath)) {
let contents = (await readFile(projectFilePath)).toString();
const relativeImports = [...contents.matchAll(/require\("(\.\.?\/+[^"]+)"\)/ig)];
for (let localImport of relativeImports) {
const [matchedText, relativePath] = [...localImport];
if (relativePath.endsWith('.json')) {
continue;
}

const absolutePath = path.resolve(projectFilePath.split('/').slice(0, -1).join('/'), relativePath);
let isDirectory = false;
try {
isDirectory = (await lstat(absolutePath)).isDirectory();
} catch { /* lstat has failed because `absolutePath` points to a JS file but is missing the .js extension */ }

const replacementPath = isDirectory
? `${relativePath}/index.cjs`
: `${relativePath}.cjs`;
contents = contents.replaceAll(matchedText, `require("${replacementPath}")`);
}

if (relativeImports.length) {
await writeFile(projectFilePath, contents);
}

await rename(projectFilePath, [...projectFilePath.split('.').slice(0, -1), 'cjs'].join('.'));
}
}

(async function() {
await cjsIfy();
}());
9 changes: 9 additions & 0 deletions packages/build/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "build",
"version": "0.0.0",
"type": "module",
"private": true,
"bin": {
"cjsify": "./cjsify.js"
}
}
10 changes: 10 additions & 0 deletions packages/cookbook/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "tsconfig/cjs.json",
"compilerOptions": {
"outDir": "./lib/commonjs",
"lib": ["es2022", "dom"]
},
"files": [
"src/index.ts"
]
}
14 changes: 10 additions & 4 deletions packages/crypto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"name": "@near-js/crypto",
"version": "1.2.4",
"description": "Abstractions around NEAR-compatible elliptical curves and cryptographic keys",
"main": "lib/index.js",
"main": "lib/esm/index.js",
"type": "module",
"scripts": {
"build": "pnpm compile",
"compile": "tsc -p tsconfig.json",
"build": "pnpm compile:esm && pnpm compile:cjs",
"compile:esm": "tsc -p tsconfig.json",
"compile:cjs": "tsc -p tsconfig.cjs.json && cjsify ./lib/commonjs",
"lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc",
"lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix",
"test": "jest"
Expand All @@ -26,12 +27,17 @@
"@jest/globals": "^29.7.0",
"@noble/hashes": "^1.4.0",
"@types/node": "20.0.0",
"build": "workspace:*",
"jest": "29.7.0",
"ts-jest": "29.1.5",
"tsconfig": "workspace:*",
"typescript": "5.4.5"
},
"files": [
"lib"
]
],
"exports": {
"require": "./lib/commonjs/index.cjs",
"import": "./lib/esm/index.js"
}
}
10 changes: 10 additions & 0 deletions packages/crypto/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "tsconfig/cjs.json",
"compilerOptions": {
"outDir": "./lib/commonjs",
"lib": ["es2022", "dom"]
},
"files": [
"src/index.ts"
]
}
2 changes: 1 addition & 1 deletion packages/crypto/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "tsconfig/esm.json",
"compilerOptions": {
"outDir": "./lib",
"outDir": "./lib/esm",
},
"files": [
"src/index.ts"
Expand Down
15 changes: 11 additions & 4 deletions packages/iframe-rpc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
"name": "@near-js/iframe-rpc",
"version": "0.0.2",
"description": "IFrame RPC client/server implementation",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"main": "lib/esm/index.js",
"types": "lib/esm/index.d.ts",
"type": "module",
"scripts": {
"build": "tsc -p ./tsconfig.json"
"build": "pnpm compile:esm && pnpm compile:cjs",
"compile:esm": "tsc -p tsconfig.json",
"compile:cjs": "tsc -p tsconfig.cjs.json && cjsify ./lib/commonjs"
},
"keywords": [],
"author": "Pagoda",
Expand All @@ -15,6 +17,11 @@
"events": "3.3.0"
},
"devDependencies": {
"@types/node": "18.11.18"
"@types/node": "18.11.18",
"build": "workspace:*"
},
"exports": {
"require": "./lib/commonjs/index.cjs",
"import": "./lib/esm/index.js"
}
}
10 changes: 10 additions & 0 deletions packages/iframe-rpc/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "tsconfig/cjs.json",
"compilerOptions": {
"outDir": "./lib/commonjs",
"lib": ["es2022", "dom"]
},
"files": [
"src/index.ts"
]
}
2 changes: 1 addition & 1 deletion packages/iframe-rpc/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "tsconfig/browser.json",
"compilerOptions": {
"outDir": "./lib",
"outDir": "./lib/esm",
"types": ["node"]
},
"files": [
Expand Down
14 changes: 10 additions & 4 deletions packages/keystores-browser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"name": "@near-js/keystores-browser",
"version": "0.0.12",
"description": "KeyStore implementation for working with keys in browser LocalStorage",
"main": "lib/index.js",
"main": "lib/esm/index.js",
"type": "module",
"scripts": {
"build": "pnpm compile",
"compile": "tsc -p tsconfig.json",
"build": "pnpm compile:esm && pnpm compile:cjs",
"compile:esm": "tsc -p tsconfig.json",
"compile:cjs": "tsc -p tsconfig.cjs.json && cjsify ./lib/commonjs",
"lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc",
"lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix",
"test": "jest"
Expand All @@ -20,6 +21,7 @@
},
"devDependencies": {
"@jest/globals": "^29.7.0",
"build": "workspace:*",
"jest": "29.7.0",
"localstorage-memory": "1.0.3",
"ts-jest": "29.1.5",
Expand All @@ -28,5 +30,9 @@
},
"files": [
"lib"
]
],
"exports": {
"require": "./lib/commonjs/index.cjs",
"import": "./lib/esm/index.js"
}
}
10 changes: 10 additions & 0 deletions packages/keystores-browser/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "tsconfig/cjs.json",
"compilerOptions": {
"outDir": "./lib/commonjs",
"lib": ["es2022", "dom"]
},
"files": [
"src/index.ts"
]
}
2 changes: 1 addition & 1 deletion packages/keystores-browser/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"extends": "tsconfig/browser.json",
"compilerOptions": {
"outDir": "./lib",
"outDir": "./lib/esm",
},
"files": [
"src/index.ts"
Expand Down
14 changes: 10 additions & 4 deletions packages/keystores-node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
"name": "@near-js/keystores-node",
"version": "0.0.12",
"description": "KeyStore implementation for working with keys in the local filesystem",
"main": "lib/index.js",
"main": "lib/esm/index.js",
"type": "module",
"scripts": {
"build": "pnpm compile",
"compile": "tsc -p tsconfig.json",
"build": "pnpm compile:esm && pnpm compile:cjs",
"compile:esm": "tsc -p tsconfig.json",
"compile:cjs": "tsc -p tsconfig.cjs.json && cjsify ./lib/commonjs",
"lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc",
"lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix",
"test": "jest"
Expand All @@ -21,12 +22,17 @@
"devDependencies": {
"@jest/globals": "^29.7.0",
"@types/node": "20.0.0",
"build": "workspace:*",
"jest": "29.7.0",
"ts-jest": "29.1.5",
"tsconfig": "workspace:*",
"typescript": "5.4.5"
},
"files": [
"lib"
]
],
"exports": {
"require": "./lib/commonjs/index.cjs",
"import": "./lib/esm/index.js"
}
}
10 changes: 10 additions & 0 deletions packages/keystores-node/tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "tsconfig/cjs.json",
"compilerOptions": {
"outDir": "./lib/commonjs",
"lib": ["es2022", "dom"]
},
"files": [
"src/index.ts"
]
}
Loading

0 comments on commit 31b7987

Please sign in to comment.