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

merge upstream #1

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
e4afbf8
upgrading all dev deps
alexreardon Jun 30, 2022
1fd36d5
increasing size limit changes due to rollup changes
alexreardon Jul 1, 2022
31cf8fb
Merge pull request #131 from alexreardon/upgrading-dev-deps
alexreardon Jul 1, 2022
8dbdc14
fix: create a separate build for node16 TypeScript module mode (#144)
pkerschbaum Sep 16, 2022
ba6f67c
chore: rename "node16" to "esm"
pkerschbaum Sep 19, 2022
98970f6
Merge pull request #145 from pkerschbaum/fix/144
alexreardon Sep 28, 2022
98b9183
updating deps
alexreardon Sep 28, 2022
9aef7cb
moving to new ts-jest config
alexreardon Sep 28, 2022
df0bc2f
cleaning up github workflows
alexreardon Sep 28, 2022
cd36a34
accomidating tooling change in size-limit. examined generated bundle …
alexreardon Sep 28, 2022
f733088
Merge pull request #148 from alexreardon/upgrading-deps-2
alexreardon Sep 28, 2022
881973c
v1.3.0-beta.1
alexreardon Sep 28, 2022
e3fccef
v1.3.0
alexreardon Sep 28, 2022
8e0b193
chore(build): remove some scripts in favor of `emit-module-package-file`
Andarist Sep 28, 2022
3039cf5
Merge pull request #149 from Andarist/emit-module-package-file-plugin
alexreardon Sep 28, 2022
66eade7
fix(types): fixed an issue with TypeScript not being able to locate `…
Andarist Sep 28, 2022
c5bd9b0
Tweak `exports` further to satisfy node.js gods
Andarist Sep 28, 2022
b32b297
Merge pull request #150 from Andarist/fix/ts-module-resolution-node16
alexreardon Sep 28, 2022
092611e
v1.3.1-beta.1
alexreardon Sep 28, 2022
294d45a
v1.3.1
alexreardon Sep 28, 2022
2b56a3e
Bump ts-jest from 29.0.2 to 29.0.3
dependabot[bot] Sep 30, 2022
b6ff145
Bump jest and @types/jest
dependabot[bot] Sep 30, 2022
f5d66ac
Merge pull request #154 from alexreardon/dependabot/npm_and_yarn/ts-j…
alexreardon Oct 1, 2022
dd3e339
Merge branch 'master' into dependabot/npm_and_yarn/jest-and-types/jes…
alexreardon Oct 1, 2022
dd02d1e
Merge pull request #155 from alexreardon/dependabot/npm_and_yarn/jest…
alexreardon Oct 2, 2022
a78474c
Fix README.md
mlaopane Nov 9, 2022
619da0f
Merge pull request #161 from mlaopane/patch-1
alexreardon Nov 10, 2022
5459768
adding jsdoc
alexreardon Feb 22, 2024
a0bf0c4
updating deps to latest
alexreardon Feb 23, 2024
5fb0951
v1.3.2
alexreardon Feb 23, 2024
2b57b94
updating to use public npm registry
alexreardon Feb 23, 2024
a6b1893
v1.3.3
alexreardon Feb 23, 2024
77c2ac4
Bump @types/node from 20.11.20 to 20.11.24
dependabot[bot] Feb 29, 2024
4a02742
Bump expect-type from 0.17.3 to 0.18.0
dependabot[bot] Feb 29, 2024
ae40e13
clarify message functionality in readme
BirdHighway Mar 6, 2024
cad3282
add back why section to readme
BirdHighway Mar 12, 2024
4cbc38f
add sentence to why section
BirdHighway Mar 13, 2024
b5587cf
updating readme
alexreardon Mar 13, 2024
600c29b
remove production check
longngn Mar 15, 2022
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
12 changes: 6 additions & 6 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
version: 2
updates:
# Enable version updates for npm
- package-ecosystem: "npm"
- package-ecosystem: 'npm'
# Look for `package.json` and `lock` files in the `root` directory
directory: "/"
directory: '/'
# Always increase the version requirement to match the new version.
versioning-strategy: increase
# Check the npm registry for updates at the start of every week
schedule:
interval: "weekly"
day: "monday"
time: "08:00"
timezone: "Australia/Sydney"
interval: 'monthly'
day: 'monday'
time: '08:00'
timezone: 'Australia/Sydney'
17 changes: 11 additions & 6 deletions .github/workflows/bundle-size-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ jobs:
env:
CI_JOB_NUMBER: 1
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- name: Checkout code
uses: actions/checkout@v3

- name: Setup node
uses: actions/setup-node@v3
with:
node-version: '16'
node-version-file: '.nvmrc'
# Using node action to manage caching node_modules
cache: 'yarn'

# The size limit github action
- uses: andresz1/size-limit-action@v1
- name: Run size limit github action
uses: andresz1/size-limit-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
github_token: ${{ secrets.GITHUB_TOKEN }}
19 changes: 8 additions & 11 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,18 @@ jobs:

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '16'
- name: Checkout code
uses: actions/checkout@v3

- name: Restore dependency cache
uses: actions/cache@v2
- name: Setup node
uses: actions/setup-node@v3
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
node-version-file: '.nvmrc'
# Using node action to manage caching node_modules
cache: 'yarn'

- name: Install dependencies
run: yarn install

# Run tests
- name: Tests
- name: Run tests
run: yarn test
16 changes: 7 additions & 9 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ jobs:

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
with:
node-version: '16'
- name: Checkout code
uses: actions/checkout@v3

- name: Restore dependency cache
uses: actions/cache@v2
- name: Setup node
uses: actions/setup-node@v3
with:
path: '**/node_modules'
key: ${{ runner.os }}-modules-${{ hashFiles('**/yarn.lock') }}
node-version-file: '.nvmrc'
# Using node action to manage caching node_modules
cache: 'yarn'

- name: Install dependencies
run: yarn install
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
16.11.1
18.4.0
40 changes: 26 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# tiny-invariant 🔬💥

[![Build Status](https://travis-ci.org/alexreardon/tiny-invariant.svg?branch=master)](https://travis-ci.org/alexreardon/tiny-invariant)
[![npm](https://img.shields.io/npm/v/tiny-invariant.svg)](https://www.npmjs.com/package/tiny-invariant) [![dependencies](https://david-dm.org/alexreardon/tiny-invariant.svg)](https://david-dm.org/alexreardon/tiny-invariant)
![GitHub Actions Workflow Status](https://img.shields.io/github/actions/workflow/status/alexreardon/tiny-invariant/test.yml)
![types](https://img.shields.io/badge/types-typescript%20%7C%20flow-blueviolet)
[![minzip](https://img.shields.io/bundlephobia/minzip/tiny-invariant.svg)](https://www.npmjs.com/package/tiny-invariant)
[![Downloads per month](https://img.shields.io/npm/dm/tiny-invariant.svg)](https://www.npmjs.com/package/tiny-invariant)
![npm bundle size](https://img.shields.io/bundlephobia/minzip/tiny-invariant)
![NPM Downloads](https://img.shields.io/npm/dm/tiny-invariant)

A tiny [`invariant`](https://www.npmjs.com/package/invariant) alternative.
`tiny-invariant` is a _tiny_, widely-supported, zero-dependency alternative to [`invariant`](https://www.npmjs.com/package/invariant).

`tiny-invariant` - when every byte counts!

## What is `invariant`?

An `invariant` function takes a value, and if the value is [falsy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy) then the `invariant` function will throw. If the value is [truthy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy), then the function will not throw.

```js
```ts
import invariant from 'tiny-invariant';

invariant(truthyValue, 'This should not throw!');
Expand All @@ -21,17 +22,28 @@ invariant(falsyValue, 'This will throw!');
// Error('Invariant violation: This will throw!');
```

You can also provide a function to generate your message, for when your message is expensive to create
## Why `tiny-invariant`?

The [`library: invariant`](https://www.npmjs.com/package/invariant) supports passing in arguments to the `invariant` function in a `sprintf` style `(condition, format, a, b, c, d, e, f)`. It has internal logic to execute the sprintf substitutions. The sprintf logic is not removed in production builds. `tiny-invariant` has dropped all of the code for `sprintf` logic and instead encourages consumers to leverage [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) for message formatting.

```ts
invariant(condition, `Hello, ${name} - how are you today?`);
```

## Error Messages

```js
`tiny-invariant` allows you to pass a `string` message, or a function that returns a `string` message. Using a function that returns a message is helpful when your message is expensive to create.

```ts
import invariant from 'tiny-invariant';

invariant(condition, `Hello, ${name} - how are you today?`);

// Using a function is helpful when your message is expensive
invariant(value, () => getExpensiveMessage());
```

## Why `tiny-invariant`?

The [`library: invariant`](https://www.npmjs.com/package/invariant) supports passing in arguments to the `invariant` function in a sprintf style `(condition, format, a, b, c, d, e, f)`. It has internal logic to execute the sprintf substitutions. The sprintf logic is not removed in production builds. `tiny-invariant` has dropped all of the sprintf logic. `tiny-invariant` allows you to pass a single string message. With [template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) there is really no need for a custom message formatter to be built into the library. If you need a multi part message you can just do this: `invariant(condition, 'Hello, ${name} - how are you today?')`
When `process.env.NODE_ENV` is set to `production`, the message will be replaced with the generic message `Invariant failed`.

## Type narrowing

Expand Down Expand Up @@ -62,13 +74,13 @@ npm install tiny-invariant --save

Big idea: you will want your compiler to convert this code:

```js
```ts
invariant(condition, 'My cool message that takes up a lot of kbs');
```

Into this:

```js
```ts
if (!condition) {
if ('production' !== process.env.NODE_ENV) {
invariant(false, 'My cool message that takes up a lot of kbs');
Expand All @@ -83,7 +95,7 @@ if (!condition) {

Your bundler can then drop the code in the `"production" !== process.env.NODE_ENV` block for your production builds to end up with this:

```js
```ts
if (!condition) {
invariant(false);
}
Expand Down
11 changes: 4 additions & 7 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',

globals: {
'ts-jest': {
diagnostics: false,
},
transform: {
'^.+\\.tsx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],
testEnvironment: 'node',
};
53 changes: 32 additions & 21 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "tiny-invariant",
"name": "@minswap/tiny-invariant",
"version": "1.2.0",
"description": "A tiny invariant function",
"author": "Alex Reardon <[email protected]>",
Expand All @@ -20,6 +20,15 @@
"main": "dist/tiny-invariant.cjs.js",
"module": "dist/tiny-invariant.esm.js",
"types": "dist/tiny-invariant.d.ts",
"exports": {
".": {
"import": "./dist/esm/tiny-invariant.js",
"default": {
"types": "./dist/tiny-invariant.d.ts",
"default": "./dist/tiny-invariant.cjs.js"
}
}
},
"sideEffects": false,
"files": [
"/dist",
Expand All @@ -28,51 +37,53 @@
"size-limit": [
{
"path": "dist/tiny-invariant.min.js",
"limit": "175B"
"limit": "217B"
},
{
"path": "dist/tiny-invariant.js",
"limit": "176B"
"limit": "267B"
},
{
"path": "dist/tiny-invariant.cjs.js",
"limit": "164B"
"limit": "171B"
},
{
"path": "dist/tiny-invariant.esm.js",
"import": "foo",
"limit": "49B"
"limit": "112B"
}
],
"scripts": {
"test": "yarn jest",
"test:size": "yarn build && yarn size-limit",
"prettier:write": "yarn prettier --debug-check src/** test/**",
"prettier:check": "yarn prettier --write src/** test/**",
"typescript:check": "yarn tsc --noEmit src/*.ts test/*.ts",
"typescript:check": "tsc --noEmit",
"validate": "yarn prettier:check && yarn typescript:check",
"build:clean": "rimraf dist",
"build:flow": "cp src/tiny-invariant.js.flow dist/tiny-invariant.cjs.js.flow",
"build:typescript": "tsc ./src/tiny-invariant.ts --emitDeclarationOnly --declaration --outDir ./dist",
"build:dist": "yarn rollup --config rollup.config.js",
"build": "yarn build:clean && yarn build:dist && yarn build:typescript",
"build:typescript:esm": "tsc ./src/tiny-invariant.ts --emitDeclarationOnly --declaration --outDir ./dist/esm",
"build:dist": "yarn rollup --config rollup.config.mjs",
"build": "yarn build:clean && yarn build:dist && yarn build:typescript && yarn build:typescript:esm",
"prepublishOnly": "yarn build"
},
"devDependencies": {
"@rollup/plugin-replace": "^3.0.0",
"@rollup/plugin-typescript": "^8.3.0",
"@size-limit/preset-small-lib": "^6.0.4",
"@types/jest": "^27.0.2",
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-typescript": "^11.1.6",
"@size-limit/preset-small-lib": "^11.0.2",
"@types/jest": "^29.5.12",
"@types/node": "^20.11.24",
"@types/rollup": "^0.54.0",
"expect-type": "^0.13.0",
"jest": "^27.3.1",
"prettier": "^2.4.1",
"rimraf": "^3.0.2",
"rollup": "^2.58.3",
"expect-type": "^0.18.0",
"jest": "^29.7.0",
"prettier": "^3.2.5",
"rimraf": "^5.0.5",
"rollup": "^4.12.0",
"rollup-plugin-terser": "^7.0.2",
"size-limit": "^6.0.4",
"ts-jest": "^27.0.7",
"tslib": "^2.3.1",
"typescript": "^4.4.4"
"size-limit": "^11.0.2",
"ts-jest": "^29.1.2",
"tslib": "^2.6.2",
"typescript": "^5.3.3"
}
}
22 changes: 22 additions & 0 deletions rollup.config.js → rollup.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ export default [
},
plugins: [typescript({ module: 'ESNext' })],
},
// ESM build for "module": "node16" TypeScript projects (https://github.com/alexreardon/tiny-invariant/issues/144)
{
input,
output: {
file: 'dist/esm/tiny-invariant.js',
format: 'esm',
},
plugins: [
typescript({ module: 'ESNext' }),
// https://github.com/rollup/rollup/blob/69ff4181e701a0fe0026d0ba147f31bc86beffa8/build-plugins/emit-module-package-file.ts
{
generateBundle() {
this.emitFile({
fileName: 'package.json',
source: `{ "type": "module" }\n`,
type: 'asset',
});
},
name: 'emit-module-package-file',
},
],
},
// CommonJS build
{
input,
Expand Down
5 changes: 1 addition & 4 deletions src/tiny-invariant.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@

const prefix: string = 'Invariant failed';

export default function invariant(
condition: mixed,
message?: string | (() => string),
) {
export default function invariant(condition: mixed, message?: string | (() => string)) {
if (condition) {
return;
}
Expand Down
34 changes: 21 additions & 13 deletions src/tiny-invariant.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,38 @@
const isProduction: boolean = process.env.NODE_ENV === 'production';
const prefix: string = 'Invariant failed';

// Throw an error if the condition fails
// Strip out error messages for production
// > Not providing an inline default argument for message as the result is smaller
/**
* `invariant` is used to [assert](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) that the `condition` is [truthy](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy).
*
* 💥 `invariant` will `throw` an `Error` if the `condition` is [falsey](https://github.com/getify/You-Dont-Know-JS/blob/bdbe570600d4e1107d0b131787903ca1c9ec8140/up%20%26%20going/ch2.md#truthy--falsy)
*
* 🤏 `message`s are not displayed in production environments to help keep bundles small
*
* @example
*
* ```ts
* const value: Person | null = { name: 'Alex' };
* invariant(value, 'Expected value to be a person');
* // type of `value`` has been narrowed to `Person`
* ```
*/
export default function invariant(
condition: any,
// Can provide a string, or a function that returns a string for cases where
// the message takes a fair amount of effort to compute
// Not providing an inline default argument for message as the result is smaller
/**
* Can provide a string, or a function that returns a string for cases where
* the message takes a fair amount of effort to compute
*/
message?: string | (() => string),
): asserts condition {
if (condition) {
return;
}
// Condition not passed

// In production we strip the message but still throw
if (isProduction) {
throw new Error(prefix);
}

// When not in production we allow the message to pass through
// *This block will be removed in production builds*

const provided: string | undefined =
typeof message === 'function' ? message() : message;
const provided: string | undefined = typeof message === 'function' ? message() : message;

// Options:
// 1. message provided: `${prefix}: ${provided}`
Expand Down
Loading
Loading