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

Output ESM instead of CommonJS #225

Merged
merged 22 commits into from
Nov 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ jobs:
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
- run: npm install --force
- run: npm test
- uses: codecov/codecov-action@v1
if: matrix.node-version == 14
if: matrix.node-version == 16
with:
fail_ci_if_error: true
3 changes: 1 addition & 2 deletions example.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
'use strict';
const {default: ow} = require('./dist');
import ow from './dist/index.js'

const logError = fn => {
try {
Expand Down
36 changes: 19 additions & 17 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@
"email": "sindresorhus@gmail.com",
"url": "https://sindresorhus.com"
},
"type": "module",
"main": "dist/index.js",
"engines": {
"node": ">=12"
},
"scripts": {
"test": "xo && nyc ava",
"test": "xo && c8 ava",
"docs": "typedoc source/index.ts",
"build": "del-cli dist && tsc",
"prepare": "npm run build",
Expand Down Expand Up @@ -61,15 +62,15 @@
"@types/lodash.isequal": "^4.5.5",
"@types/node": "^16.10.2",
"@types/vali-date": "^1.0.0",
"ava": "^2.4.0",
"ava": "^3.15.0",
"c8": "^7.10.0",
"del-cli": "^4.0.0",
"expect-type": "^0.12.0",
"gh-pages": "^3.2.3",
"nyc": "^15.1.0",
"ts-node": "^10.2.1",
"ts-node": "^10.4.0",
"typedoc": "^0.22.5",
"typescript": "^4.4.3",
"xo": "^0.38.2"
"typescript": "^4.5.0-beta",
"xo": "^0.46.4"
},
"browser": {
"./dist/utils/infer-label.js": "./dist/utils/infer-label.browser.js"
Expand All @@ -93,25 +94,26 @@
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-empty-function": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/no-unused-vars": "off",
"import/no-anonymous-default-export": "off"
"@typescript-eslint/no-unused-vars": "off"
}
},
"ava": {
"babel": false,
"compileEnhancements": false,
"nonSemVerExperiments": {
"configurableModuleFormat": true
},
"nodeArguments": [
"--loader=ts-node/esm",
"--experimental-specifier-resolution=node"
],
"files": [
"test/**",
"!test/fixtures/**"
],
"extensions": [
"ts"
],
"require": [
"ts-node/register"
]
"extensions": {
"ts": "module"
}
},
"nyc": {
"c8": {
"reporter": [
"text",
"lcov"
Expand Down
2 changes: 1 addition & 1 deletion source/argument-error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {generateStackTrace} from './utils/generate-stack';
import {generateStackTrace} from './utils/generate-stack.js';

const wrapStackTrace = (error: ArgumentError, stack: string): string => `${error.name}: ${error.message}\n${stack}`;

Expand Down
22 changes: 11 additions & 11 deletions source/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import callsites from 'callsites';
import {inferLabel} from './utils/infer-label';
import {Predicate} from './predicates/predicate';
import {BasePredicate, isPredicate} from './predicates/base-predicate';
import modifiers, {Modifiers} from './modifiers';
import predicates, {Predicates} from './predicates';
import test from './test';
import {inferLabel} from './utils/infer-label.js';
import {Predicate} from './predicates/predicate.js';
import {BasePredicate, isPredicate} from './predicates/base-predicate.js';
import modifiers, {Modifiers} from './modifiers.js';
import predicates, {Predicates} from './predicates.js';
import test from './test.js';

/**
@hidden
Expand Down Expand Up @@ -124,7 +124,7 @@ Object.defineProperties(ow, {
} catch {
return false;
}
}
},
},
create: {
value: <T>(labelOrPredicate: BasePredicate<T> | string | undefined, predicate?: BasePredicate<T>) => (value: unknown, label?: string): asserts value is T => {
Expand All @@ -137,8 +137,8 @@ Object.defineProperties(ow, {
}

test(value, label ?? (labelOrPredicate!), predicate!);
}
}
},
},
});

// Can't use `export default predicates(modifiers(ow)) as Ow` because the variable needs a type annotation to avoid a compiler error when used:
Expand All @@ -149,5 +149,5 @@ const _ow: Ow = predicates(modifiers(ow)) as Ow;
export default _ow;

export {BasePredicate, Predicate};
export * from './predicates';
export {ArgumentError} from './argument-error';
export * from './predicates.js';
export {ArgumentError} from './argument-error.js';
12 changes: 7 additions & 5 deletions source/modifiers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {BasePredicate} from '.';
import predicates, {Predicates} from './predicates';
import predicates, {Predicates} from './predicates.js';
import {BasePredicate} from './index.js';

type Optionalify<P> = P extends BasePredicate<infer X>
? P & BasePredicate<X | undefined>
Expand All @@ -14,12 +14,14 @@ export interface Modifiers {
};
}

export default <T>(object: T): T & Modifiers => {
const modifiers = <T>(object: T): T & Modifiers => {
Object.defineProperties(object, {
optional: {
get: (): Predicates => predicates({}, {optional: true})
}
get: (): Predicates => predicates({}, {optional: true}),
},
});

return object as T & Modifiers;
};

export default modifiers;
12 changes: 6 additions & 6 deletions source/operators/not.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import randomId from '../utils/random-id';
import {validatorSymbol} from '../predicates/predicate';
import type {Predicate, Validator} from '../predicates/predicate';
import randomId from '../utils/random-id.js';
import {validatorSymbol} from '../predicates/predicate.js';
import type {Predicate, Validator} from '../predicates/predicate.js';

/**
Operator which inverts the following validation.
Expand All @@ -17,9 +17,9 @@ export const not = (predicate: Predicate): Predicate => {
const placeholder = randomId();

validator.message = (value: unknown, label: string): string => (
negatedMessage ?
negatedMessage(value, label) :
message(value, placeholder).replace(/ to /, '$&not ').replace(placeholder, label)
negatedMessage
? negatedMessage(value, label)
: message(value, placeholder).replace(/ to /, '$&not ').replace(placeholder, label)
);

validator.validator = (value: unknown): unknown => !fn(value);
Expand Down