Skip to content

Commit

Permalink
fix: missing export for strip() and extend() functions
Browse files Browse the repository at this point in the history
  • Loading branch information
webdiscus committed Nov 5, 2023
1 parent 369aa38 commit bdbda7e
Show file tree
Hide file tree
Showing 32 changed files with 978 additions and 765 deletions.
2 changes: 1 addition & 1 deletion .codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ coverage:
target: 90%

ignore:
- test/.*
- test/**/*
4 changes: 3 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ jobs:
strategy:
matrix:
os: [ ubuntu-latest ]
node-version: [ 14, 16, 18, 20 ]
# rollup require node >= 18
#node-version: [ 14, 16, 18, 20 ]
node-version: [ 18, 20 ]

runs-on: ${{ matrix.os }}
steps:
Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Change log

## 2.0.1 (2023-11-03)

- fix: missing exports of ansis.strip() and ansis.export() functions (issue was introduced in v2.0.0)
- refactor: optimize code to reduce distributed size
- test: add test for generated npm package in CJS and ESM mode
- test: add test for env variables and CLI flags
- test: add test to detect Deno
- test: add test to detect Next.js runtime
- docs: update readme

## 2.0.0 (2023-11-03)

- feat: add supports the Deno
Expand Down
94 changes: 50 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ and [benchmarks](https://github.com/webdiscus/ansis#benchmark) of most popular N
- supports both **ESM** and **CommonJS**
- supports **Deno**, **Next.JS** runtime
- up to **x3 faster** than **chalk**, [see benchmarks](#benchmark)
- dist code is only **5 KB** incl. named import of all styles
- only **3 KB** dist code
- [standard API](#base-colors) like **chalk**
- default import `import ansis from 'ansis'`, usage `ansis.red('error')`
- [named import](#named-import) `import { red } from 'ansis'`, usage ``` red('error') ```
Expand Down Expand Up @@ -132,6 +132,16 @@ import { red, hex, italic } from 'ansis';
red.bold('text');
```

Default import and named import can be combined.

```js
// default and named import
import ansis, { red } from 'ansis';

const redText = red('text'); // colorized ANSI string
const text = ansis.strip(redText); // pure string without ANSI codes
```

<a id="template-literals" name="template-literals" href="#template-literals"></a>

## Template literals
Expand Down Expand Up @@ -454,61 +464,64 @@ Defaults, the output in terminal console is colored and output in a file is unco

### Environment variables

_example.js_
To force disable or enable colored output use environment variables `NO_COLOR` and `FORCE_COLOR`.

```js
import ansis from 'ansis';
The `NO_COLOR` variable should be presents with any not empty value.
The value is not important, e.g., `NO_COLOR=1` `NO_COLOR=true` disable colors.
See standard description by [NO_COLOR](https://no-color.org/).

console.log(ansis.red`COLOR`);
```
The `FORCE_COLOR` variable should be presents with one of values:\
`FORCE_COLOR=0` force disable colors\
`FORCE_COLOR=1` force enable colors

```
$ node example.js #=> color
$ node example.js > log.txt #=> no color
For example, _app.js_:

```js
import { red } from 'ansis';

console.log(red`red color`);
```

To force disable or enable colored output use environment variables `NO_COLOR` and `FORCE_COLOR`.
Execute the script in a terminal:

```
$ NO_COLOR=1 node example.js #=> force disable colors
$ FORCE_COLOR=0 node example.js #=> force disable colors
$ FORCE_COLOR=1 node example.js > log.txt #=> force enable colors
$ node app.js # colored output in terminal
$ node app.js > log.txt # output in file without ANSI codes
$ NO_COLOR=1 node app.js # force disable colors, non colored output in terminal
$ FORCE_COLOR=0 node app.js # force disable colors, non colored output in terminal
$ FORCE_COLOR=1 node app.js > log.txt # force enable colors, output in file with ANSI codes
```

> **Note**
>
> The `NO_COLOR` variable should be presents with any not empty value.
> The value is not important, see standard description by [NO_COLOR](https://no-color.org/).\
> `NO_COLOR=1` `NO_COLOR=true` disable colors
>
> The `FORCE_COLOR` variable should be presents with one of values:\
> `FORCE_COLOR=0` force disable colors\
> `FORCE_COLOR=1` force enable colors
### CLI arguments

### Arguments for executable script
Use arguments `--no-color` or `--color=false` to disable colors and `--color` to enable ones.

If you have an executable script.\
_example.js_
For example, an executable script _app.js_:

```js
#!/usr/bin/env node
import ansis from 'ansis';
import { red } from 'ansis';

console.log(ansis.red`COLOR`);
console.log(red`red color`);
```

Use arguments `--no-color` or `--color=false` to disable colors and `--color` to enable ones.
Execute the script in a terminal:

```
$ ./example.js #=> color
$ ./example.js --no-color #=> no color
$ ./example.js --color=false #=> no color
$ ./app.js # colored output in terminal
$ ./app.js --no-color # non colored output in terminal
$ ./app.js --color=false # non colored output in terminal
$ ./example.js > log.txt #=> no color
$ ./example.js --color > log.txt #=> color
$ ./example.js --color=true > log.txt #=> color
$ ./app.js > log.txt # output in file without ANSI codes
$ ./app.js --color > log.txt # output in file with ANSI codes
$ ./app.js --color=true > log.txt # output in file with ANSI codes
```

> **Warning**
>
> The command line arguments have a higher priority than environment variable.
<a id="compare" href="#compare"></a>

## Comparison of most popular libraries
Expand All @@ -523,7 +536,7 @@ $ ./example.js --color=true > log.txt #=> color
| [`picocolors`][picocolors]<br>**2.6KB**<br><nobr>`❌ named import`</nobr> | **standard**<br>`8` colors |||||| `NO_COLOR`<br>`FORCE_COLOR`<br>`--no-color`<br>`--color` |
| [`kleur`][kleur]<br>**2.7KB**<br><nobr>`✅ named import`</nobr> | **standard**<br>`8` colors |||||| only<br>`NO_COLOR`<br>`FORCE_COLOR` |
| [`chalk`][chalk]<br>**15KB**<br><nobr>`❌ named import`</nobr> | **standard**<br>`16` colors |||||| `NO_COLOR`<br>`FORCE_COLOR`<br>`--no-color`<br>`--color` |
| [`ansis`][ansis]<br>**5KB**<br><nobr>`✅ named import`</nobr> | **standard**<br>`16` colors |||||| `NO_COLOR`<br>`FORCE_COLOR`<br>`--no-color`<br>`--color` |
| [`ansis`][ansis]<br>**3.2KB**<br><nobr>`✅ named import`</nobr> | **standard**<br>`16` colors |||||| `NO_COLOR`<br>`FORCE_COLOR`<br>`--no-color`<br>`--color` |

> **Note**
>
Expand Down Expand Up @@ -577,19 +590,12 @@ npm run demo

<a id="benchmark" href="#benchmark"></a>

## Benchmark

### Setup
## Run benchmark

```bash
git clone https://github.com/webdiscus/ansis.git
cd ./ansis/bench
cd ./ansis
npm i
```

### Run benchmark

```bash
npm run bench
```

Expand Down
2 changes: 1 addition & 1 deletion examples/ansi256.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fg, bg } from '../src/index.js';
import { fg, bg } from 'ansis';

let out;
let n;
Expand Down
2 changes: 1 addition & 1 deletion examples/ansis-logo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ansis from '../src/index.js';
import ansis from 'ansis';

const colorizeASCII = ({ ascii, data }, paddingLeft = 5) => {
// start index in logo is 1, because first char is \n
Expand Down
2 changes: 1 addition & 1 deletion examples/ansis-styles-demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
underline,
white, whiteBright,
yellow, yellowBright,
} from '../src/index.js';
} from 'ansis';

const out = `${bold`bold`} ${dim`dim`} ${italic`italic`} ${underline`underline`} ${strikethrough`strikethrough`} ${inverse`inverse`} ${bold.italic.underline.strike`bold italic underline strike`}` +
'\n' +
Expand Down
10 changes: 6 additions & 4 deletions examples/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
import chalk from 'chalk';

import ansis, {
Ansis, red,
Ansis,
red,
green,
blue,
cyan,
Expand All @@ -16,7 +17,7 @@ import ansis, {
inverse,
visible,
hex,
} from '../src/index.js';
} from 'ansis';

import { ansi256Table } from './ansi256.js';
import { ansisLogo } from './ansis-logo.js';
Expand Down Expand Up @@ -189,7 +190,7 @@ ansis.extend({
log(ansis.pink('pink'));
log(ansis.orange('orange'));
log(ansis.orange.bold('orange'));
//log(ansis.bold.orange('orange')); // => error
//log(ansis.bold.orange('orange')); // => error, but by 2nd extension it works, see the `side effect` below

/**
* Problem description
Expand Down Expand Up @@ -227,7 +228,8 @@ ansis2.extend({
});

log(ansis2.pink('pink'));
log(ansis2.bold.orange('orange'));
// SIDE EFFECT: here works only because the ansis is already extended: ansis.extend({orange:'..'}), see above
log(ansis2.italic.orange('orange'));

/**
* Misc
Expand Down
8 changes: 8 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "demo",
"private": true,
"type": "module",
"scripts": {
"demo": "node ./"
}
}
30 changes: 19 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansis",
"version": "2.0.0",
"version": "2.0.1",
"description": "Colorize text in terminal or console output with ANSI colors & styles",
"keywords": [
"ansi",
Expand Down Expand Up @@ -59,38 +59,46 @@
"types": "./src/index.d.ts",
"exports": {
".": {
"import": "./index.js",
"import": "./index.mjs",
"require": "./index.js"
},
"./colors": {
"import": "./index.js",
"import": "./index.mjs",
"require": "./index.js"
}
},
"scripts": {
"demo": "node ./examples/index.js",
"bench": "node ./bench/index.js",
"build": "rollup -c",
"postinstall": "npm run build && npm i ./dist -D",
"demo": "node --experimental-modules ./examples/index.js",
"bench": "(cd ./bench/ && npm install); node ./bench/index.js",
"test": "NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles",
"test:unit": "NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --runTestsByPath ./test/unit.test.js",
"test:index": "NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --runTestsByPath ./test/index.test.js",
"test:flags": "NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --runTestsByPath ./test/flags.test.js",
"test:package": "NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --runTestsByPath ./test/package.test.js",
"test:cjs": "node ./test/package/cjs/test.cjs",
"test:esm": "node ./test/package/esm/test.mjs",
"test:coverage": "NODE_OPTIONS=--experimental-vm-modules jest --detectOpenHandles --collectCoverage",
"publish:public": "rollup -c && npm publish ./dist --access public",
"publish:beta": "rollup -c && npm publish ./dist --tag beta"
"publish:public": "(npm run build) && npm publish ./dist --access public",
"publish:beta": "(npm run build) && npm publish ./dist --tag beta"
},
"files": [
"dist"
],
"engines": {
"node": ">=12.13"
},
"devDependencies": {
"@babel/core": "^7.23.2",
"@babel/preset-env": "^7.23.2",
"@rollup/plugin-replace": "^5.0.5",
"@rollup/plugin-terser": "^0.4.4",
"@types/jest": "^29.5.7",
"@types/node": "^20.8.10",
"ansis": "file:dist",
"jest": "^29.7.0",
"prettier": "^3.0.3",
"rollup": "^4.2.0",
"rollup-plugin-copy": "^3.5.0",
"rollup-plugin-dts": "^6.1.0"
"rollup-plugin-dts": "^6.1.0",
"terser": "^5.24.0"
}
}
11 changes: 6 additions & 5 deletions package/package.json → pkg/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ansis",
"version": "2.0.0",
"version": "2.0.1",
"description": "Colorize text in terminal or console output with ANSI colors & styles",
"keywords": [
"ansi",
Expand Down Expand Up @@ -54,12 +54,12 @@
"types": "./index.d.ts",
"exports": {
".": {
"import": "./index.js",
"require": "./index.js"
"require": "./index.js",
"import": "./index.mjs"
},
"./colors": {
"import": "./index.js",
"require": "./index.js"
"require": "./index.js",
"import": "./index.mjs"
}
},
"engines": {
Expand All @@ -68,6 +68,7 @@
"files": [
"index.d.ts",
"index.js",
"index.mjs",
"package.json",
"LICENSE",
"README.md"
Expand Down
15 changes: 14 additions & 1 deletion rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import terser from '@rollup/plugin-terser';
import replace from '@rollup/plugin-replace';
import copy from 'rollup-plugin-copy';
import dts from 'rollup-plugin-dts';
import { minify } from 'terser';

// last ECMA version compatible with node.js 12
const ecma = 2019;
Expand All @@ -17,6 +19,12 @@ export default [
},
],
plugins: [
replace({
preventAssignment: false, // allow modifying exports
// the order of exports is other than is needed
'exports.Ansis = Ansis': 'module.exports = ansis', // firstly must be defined default export
'exports.default = ansis': 'module.exports.Ansis = Ansis', // then on the next line can be named export
}),
terser({
ecma,
compress: {
Expand All @@ -27,7 +35,12 @@ export default [
}),
copy({
targets: [
{ src: 'package/package.json', dest: 'dist/' },
{
src: 'src/index.mjs',
dest: 'dist/',
transform: async (contents, name) => (await minify(contents.toString())).code,
},
{ src: 'pkg/package.json', dest: 'dist/' },
{ src: 'README.md', dest: 'dist/' },
{ src: 'LICENSE', dest: 'dist/' },
],
Expand Down
Loading

0 comments on commit bdbda7e

Please sign in to comment.