Skip to content

Commit

Permalink
feat: hybrid cjs and esm support
Browse files Browse the repository at this point in the history
  • Loading branch information
Nesopie committed Jun 24, 2024
1 parent 3ebc63d commit 7ef8ae8
Show file tree
Hide file tree
Showing 13 changed files with 125 additions and 42 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
**/*.d.ts
28 changes: 19 additions & 9 deletions .github/workflows/main_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,30 @@ jobs:
unit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v4
with:
node-version: 12
node-version: 18
registry-url: https://registry.npmjs.org/
- run: npm i
- run: npm ci
- run: npm run unit
format:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/checkout@v3
- uses: actions/setup-node@v4
with:
node-version: 12
node-version: 18
registry-url: https://registry.npmjs.org/
- run: npm i
- run: npm run standard
- run: npm ci
- run: npm run standard
gitdiff:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v4
with:
node-version: 18
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm run gitdiff
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ API
**example**:

```js
const bs58 = require('bs58')
import bs58 from 'bs58'

const bytes = Uint8Array.from([
0, 60, 23, 110, 101, 155, 234,
Expand All @@ -45,7 +45,7 @@ console.log(address)
**example**:

```js
const bs58 = require('bs58')
import bs58 from 'bs58'

const address = '16UjcYNBG9GTK4uq2f7yYEbuifqCzoLMGS'
const bytes = bs58.decode(address)
Expand All @@ -65,7 +65,7 @@ npm install -g browserify
then run:

```bash
browserify node_modules/bs58/index.js -o bs58.bundle.js --standalone bs58
browserify node_modules/bs58/cjs/index.cjs -o bs58.bundle.js --standalone bs58
```

Hack / Test
Expand Down
5 changes: 0 additions & 5 deletions index.d.ts

This file was deleted.

32 changes: 24 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"name": "bs58",
"version": "5.0.0",
"type": "module",
"description": "Base 58 encoding / decoding",
"keywords": [
"base58",
Expand All @@ -15,25 +16,40 @@
],
"license": "MIT",
"devDependencies": {
"standard": "^16.0.4",
"tape": "^4.6.3"
"@types/node": "^20.14.8",
"rimraf": "^5.0.7",
"tape": "^5.3.0",
"ts-standard": "^12.0.2",
"typescript": "^5.5.2"
},
"repository": {
"url": "https://github.com/cryptocoinjs/bs58",
"type": "git"
},
"files": [
"index.js",
"index.d.ts"
"src/cjs",
"src/esm"
],
"main": "index.js",
"types": "index.d.ts",
"main": "src/cjs/index.cjs",
"module": "src/esm/index.js",
"types": "src/cjs/index.d.ts",
"exports": {
".": {
"import": "./src/esm/index.js",
"require": "./src/cjs/index.cjs",
"types": "./src/cjs/index.d.ts"
}
},
"scripts": {
"standard": "standard",
"build": "npm run clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.cjs.json",
"clean": "rimraf src",
"gitdiff": "npm run build && git diff --exit-code",
"postbuild": "find src/cjs -type f -name \"*.js\" -exec bash -c 'mv \"$0\" \"${0%.js}.cjs\"' {} \\;",
"standard": "ts-standard --ignore src --ignore test",
"test": "npm run standard && npm run unit",
"unit": "tape test/index.js"
},
"dependencies": {
"base-x": "^4.0.0"
"base-x": "^5.0.0"
}
}
8 changes: 8 additions & 0 deletions src/cjs/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var base_x_1 = __importDefault(require("base-x"));
var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
exports.default = (0, base_x_1.default)(ALPHABET);
3 changes: 3 additions & 0 deletions src/cjs/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import basex from 'base-x';
declare const _default: basex.BaseConverter;
export default _default;
3 changes: 3 additions & 0 deletions src/esm/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import basex from 'base-x';
var ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
export default basex(ALPHABET);
32 changes: 17 additions & 15 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const test = require('tape')
const base58 = require('../')
import tape from 'tape'
import base58 from '../src/esm/index.js'
import fixtures from './fixtures.json' assert { type: "json" }

const fixtures = require('./fixtures.json')
const { encode, decode } = base58
const { valid, invalid } = fixtures

test('base58', function (t) {
t.test('encode', function (t) {
fixtures.valid.forEach(function (f) {
t.test('can encode ' + f.hex, function (t) {
const actual = base58.encode(Buffer.from(f.hex, 'hex'))
tape('base58', function (t) {
tape('encode', function (t) {
valid.forEach(function (f) {
tape('can encode ' + f.hex, function (t) {
const actual = encode(Buffer.from(f.hex, 'hex'))
t.equal(actual, f.string)
t.end()
})
Expand All @@ -16,19 +18,19 @@ test('base58', function (t) {
t.end()
})

t.test('decode', function (t) {
fixtures.valid.forEach(function (f) {
t.test('can decode ' + f.string, function (t) {
const actual = Buffer.from(base58.decode(f.string)).toString('hex')
tape('decode', function (t) {
valid.forEach(function (f) {
tape('can decode ' + f.string, function (t) {
const actual = Buffer.from(decode(f.string)).toString('hex')
t.same(actual, f.hex)
t.end()
})
})

fixtures.invalid.forEach(function (f) {
t.test('throws on ' + f.description, function (t) {
invalid.forEach(function (f) {
tape('throws on ' + f.description, function (t) {
t.throws(function () {
base58.decode(f.string)
decode(f.string)
}, /^Error: Non-base58 character$/)
t.end()
})
Expand Down
4 changes: 2 additions & 2 deletions index.js → ts_src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const basex = require('base-x')
import basex from 'base-x'
const ALPHABET = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

module.exports = basex(ALPHABET)
export default basex(ALPHABET)
24 changes: 24 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"outDir": "./src",
"rootDir": "./ts_src",
"types": ["node"],
"allowJs": false,
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"noImplicitThis": true,
"alwaysStrict": true,
"esModuleInterop": true,
"noUnusedLocals": true,
"noUnusedParameters": true
},
"include": ["ts_src/**/*.ts"],
"exclude": ["node_modules/**/*"]
}

10 changes: 10 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": false,
"outDir": "src/cjs",
"module": "commonjs"
}
}

11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"outDir": "src/esm",
"esModuleInterop": true,
"resolveJsonModule": true,
"module": "ESNext",
"moduleResolution": "Node"
},
}

0 comments on commit 7ef8ae8

Please sign in to comment.