Skip to content

Commit

Permalink
feat(esm): is a pure ESM package now
Browse files Browse the repository at this point in the history
BREAKING CHANGE: CommonJS no longer exported/supported
  • Loading branch information
grantila committed Sep 11, 2021
1 parent 3fe2cc8 commit b9320dc
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 74 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@

The functions are standalone and depends on no particular Promise implementation and therefore works well for JavaScript's built-in Promise.

The library is written in TypeScript, so typings are provided. Apart from being exported as JavaScript (ES2019), it's also exported as an *ES module*, if imported in platforms (and bundlers) supporting this.
The library is written in TypeScript, so typings are provided. It is exported only as an [ESM package](esm-package)!


# Versions

* Since version 2, `Finally` and `Try` are removed. They should be replaced with `Promise.prototype.finally` and async functions.
* Since version 3, it's only exported as an [ESM package](esm-package).


# Types
Expand Down Expand Up @@ -170,13 +171,11 @@ somePromise
.then( delayChain( 100 ) )
```

To always delay a chain, regardless of whether it was resolved or rejected, use `finallyDelay`. **Note the triple dots**, and read more about `Finally` below...
To always delay a chain, regardless of whether it was resolved or rejected, use:

```ts
import { finallyDelay } from 'already'

somePromise
.then( ...finallyDelay( 100 ) )
.finally( delayChain( 100 ) )
```


Expand Down Expand Up @@ -888,3 +887,4 @@ async function getConnection( )
[greenkeeper-url]: https://greenkeeper.io/
[lgtm-image]: https://img.shields.io/lgtm/grade/javascript/g/grantila/already.svg?logo=lgtm&logoWidth=18
[lgtm-url]: https://lgtm.com/projects/g/grantila/already/context:javascript
[esm-package]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c
22 changes: 0 additions & 22 deletions jest.config.common.js

This file was deleted.

31 changes: 24 additions & 7 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
const hasModules =
parseInt( process.versions.node.split( "." )[ 0 ], 10 ) > 12;

module.exports =
hasModules
? require( './jest.config.modules' )
: require( './jest.config.common' );
export default {
// preset: "ts-jest",
"transform": {
"^.+\\.jsx?$": "babel-jest",
"^.+\\.tsx?$": "ts-jest"
},
testEnvironment: "node",
testMatch: [
"<rootDir>/test-out/**/*.js",
"<rootDir>/test/**/*.ts",
],
moduleFileExtensions: ["js", "ts", "mjs"],
modulePathIgnorePatterns: [
".*\.d\.ts"
],
collectCoverageFrom: [
"<rootDir>/dist/**/*.js",
],
coverageReporters: ["lcov", "text", "html"],
setupFiles: [
"trace-unhandled/register",
],
maxConcurrency: Infinity,
}
14 changes: 0 additions & 14 deletions jest.config.modules.js

This file was deleted.

4 changes: 2 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ function onceDynamic( ): OnceRunner
export function retry< R >(
times: number,
fn: ( ) => R,
retryable: ( err: Error ) => boolean = ( ) => true
retryable: ( err: unknown ) => boolean = ( ) => true
)
: R
{
Expand All @@ -647,7 +647,7 @@ export function retry< R >(
return retryAsync( < any >fn( ) );
} );

const retrySync = ( _err: Error ): R =>
const retrySync = ( _err: unknown ): R =>
{
while ( --times >= 0 )
{
Expand Down
30 changes: 13 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,30 +1,26 @@
{
"name": "already",
"description": "Promise extensions; finally, map, filter, etc",
"description": "Promise extensions; map, filter, etc",
"license": "MIT",
"version": "0.0.0-development",
"author": "Gustaf Räntilä <[email protected]>",
"repository": {
"type": "git",
"url": "https://github.com/grantila/already"
},
"engines": {
"node": ">=12.20"
},
"type": "module",
"files": [
"dist",
"dist-mjs"
"dist"
],
"main": "./dist/index.js",
"exports": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
"import": "./dist-mjs/index.js",
"require": "./dist/index.js"
},
"scripts": {
"build:dist": "scripts/build.sh dist ./tsconfig.prod.json --module CommonJS -t ES2019",
"build:dist-mjs": "scripts/build.sh dist-mjs ./tsconfig.prod.json --module ESNext -t ES2019",
"build:test-out": "scripts/build.sh test-out ./tsconfig.test.json",
"build:test-out-mjs": "scripts/build.sh test-out-mjs ./tsconfig.test.json --module CommonJS -t esnext && scripts/rewrite-mjs-test.js",
"build:lib": "concurrently 'yarn build:dist' 'yarn build:dist-mjs'",
"build:test": "concurrently 'yarn build:test-out' 'yarn build:test-out-mjs'",
"build:lib": "scripts/build.sh dist ./tsconfig.prod.json",
"build:test": "scripts/build.sh test-out ./tsconfig.test.json",
"build": "yarn build:lib && yarn build:test",
"lint": "true",
"jest": "node_modules/.bin/jest --coverage",
Expand Down Expand Up @@ -52,19 +48,19 @@
],
"devDependencies": {
"@babel/plugin-syntax-optional-chaining": "^7.8.3",
"@babel/plugin-transform-modules-commonjs": "^7.12.1",
"@babel/preset-typescript": "^7.12.7",
"@babel/plugin-transform-modules-commonjs": "^7.15.4",
"@babel/preset-typescript": "^7.15.0",
"@types/jest": "^26.0.15",
"commitizen": "^4.2.2",
"concurrently": "^6.2.0",
"concurrently": "^6.2.1",
"cz-conventional-changelog": "^3.3.0",
"jest": "^26.6.3",
"pre-commit": "1.x",
"rimraf": "^3.0.2",
"trace-unhandled": "^2.0.1",
"ts-jest": "^26.4.4",
"tslint": "6.1.3",
"typescript": "4.3.4"
"typescript": "4.4.3"
},
"dependencies": {},
"config": {
Expand Down
12 changes: 6 additions & 6 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2323,9 +2323,9 @@ describe( "wrapFunction", ( ) =>
( ( ) => Promise.resolve( 42 ) );
expect( false ).toBe( true );
}
catch ( err )
catch ( err: any )
{
expect( err.message ).toBe(
expect( err?.message ).toBe(
"Invalid invocation, function requires 2 arguments"
);
}
Expand Down Expand Up @@ -2358,9 +2358,9 @@ describe( "wrapFunction", ( ) =>
( "foo", ( ) => Promise.resolve( 42 ) );
expect( false ).toBe( true );
}
catch ( err )
catch ( err: any )
{
expect( err.message ).toBe(
expect( err?.message ).toBe(
"Invalid return value in 'before' handler"
);
}
Expand All @@ -2383,9 +2383,9 @@ describe( "wrapFunction", ( ) =>
( "foo", ( ) => Promise.resolve( 42 ) );
expect( false ).toBe( true );
}
catch ( err )
catch ( err: any )
{
expect( err.message ).toBe( "foobar" );
expect( err?.message ).toBe( "foobar" );
}
} )( );

Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"node"
],
"target": "ES2019",
"module": "CommonJS",
"module": "ES2020",
"moduleResolution": "node",
"noImplicitAny": true,
"noUnusedLocals": true,
Expand Down

0 comments on commit b9320dc

Please sign in to comment.