Skip to content

Commit 6c3ba9c

Browse files
committed
refactor: change CommonJS to ESModule
1 parent 080b035 commit 6c3ba9c

21 files changed

+51
-58
lines changed

example/default.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Exception from '../src';
1+
import Exception from '../src/index.js';
22

33
// [--------------------------------- Default example ---------------------------------]
44
//

example/eval.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Exception from '../src';
1+
import Exception from '../src/index.js';
22

33
function error() {
44
throw new Exception('Example of eval');

example/extends.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Exception from '../src';
1+
import Exception from '../src/index.js';
22

33
class CustomException extends Exception {
44
constructor(message: any) {

example/nested.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Exception from '../src';
1+
import Exception from '../src/index.js';
22

33
function error() {
44
throw new Exception('Nested example');

example/promise.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Exception from '../src';
1+
import Exception from '../src/index.js';
22

33
// [--------------------------------- Promise example ---------------------------------]
44
//

example/stylish.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Exception from '../src';
1+
import Exception from '../src/index.js';
22

33
// [--------------------------------- Stylish example ---------------------------------]
44
//

example/timer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import Exception from '../src';
1+
import Exception from '../src/index.js';
22

33
function error() {
44
throw new Exception('Timing example');

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "@kabeep/exception",
33
"version": "1.0.3",
4+
"type": "module",
45
"description": "Exception is a custom error library for Node.js that provides a more flexible and customizable way of handling errors.",
56
"main": "./dist/index.js",
67
"types": "./dist/index.d.ts",
@@ -14,8 +15,7 @@
1415
"coverage": "vitest run --coverage",
1516
"watch": "tsup --watch",
1617
"watch:test": "vitest",
17-
"build": "tsup --dts --treeshake smallest",
18-
"build:dev": "tsup"
18+
"build": "tsup"
1919
},
2020
"devDependencies": {
2121
"@swc/core": "^1.5.0",
@@ -31,7 +31,7 @@
3131
"xo": "^0.58.0"
3232
},
3333
"dependencies": {
34-
"chalk": "^4.1.2"
34+
"chalk": "^5.3.0"
3535
},
3636
"author": "Zhang Zixin (kabeep)",
3737
"homepage": "https://github.com/kabeep/exception#readme",

src/core/Exception.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { ChalkStyle } from '../shared';
2-
import PrintError from './PrintError';
1+
import type { ChalkStyle } from '../shared/index.js';
2+
import PrintError from './PrintError.js';
33

44
/**
55
* A custom exception with enhanced printing capabilities.

src/core/PaletteError.ts

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import chalk, { type Chalk } from 'chalk';
2-
import { isBgHex, isBgRgb, isHex, isRgb, isString, normalizeRgb } from '../helpers';
1+
import chalk, { type ChalkInstance } from 'chalk';
2+
import { isBgHex, isBgRgb, isHex, isRgb, isString, normalizeRgb } from '../helpers/index.js';
33

44
/**
55
* A palette error.
@@ -25,10 +25,10 @@ export default class PaletteError extends Error {
2525
/**
2626
* Returns a Chalk instance with specified styles applied.
2727
* @param {(string | string[])} [styles=[]] - The styles to apply.
28-
* @param {Chalk} [chain] - The Chalk instance to chain with.
29-
* @returns {Chalk} A Chalk instance with the specified styles.
28+
* @param {ChalkInstance} [chain] - The Chalk instance to chain with.
29+
* @returns {ChalkInstance} A Chalk instance with the specified styles.
3030
*/
31-
palette(styles: string | string[] = [], chain?: Chalk): Chalk {
31+
palette(styles: string | string[] = [], chain?: ChalkInstance): ChalkInstance {
3232
const isArray = Array.isArray(styles);
3333
const isIllegal = !isArray && !isString(styles);
3434

@@ -58,11 +58,11 @@ export default class PaletteError extends Error {
5858
/**
5959
* Creates a Chalk instance based on the given style.
6060
* @private
61-
* @param {Chalk} chain - The Chalk instance to chain with.
61+
* @param {ChalkInstance} chain - The Chalk instance to chain with.
6262
* @param {string} [style] - The style to apply.
63-
* @returns {Chalk} A Chalk instance with the specified style.
63+
* @returns {ChalkInstance} A Chalk instance with the specified style.
6464
*/
65-
private factory(chain: Chalk, style?: string): Chalk {
65+
private factory(chain: ChalkInstance, style?: string): ChalkInstance {
6666
let nextChain;
6767

6868
if (!isString(style)) {
@@ -76,11 +76,11 @@ export default class PaletteError extends Error {
7676
} else if (isBgHex(style)) {
7777
nextChain = chain.bgHex((style as string).replace(/^bg:/, ''));
7878
} else if (isHex(style)) {
79-
nextChain = chain.hex(style);
79+
nextChain = chain.hex(style as string);
8080
} else {
81-
nextChain = chain[style];
81+
nextChain = chain[style as keyof ChalkInstance] ?? chain;
8282
}
8383

84-
return nextChain;
84+
return nextChain as ChalkInstance;
8585
}
8686
}

src/core/PrintError.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { basename } from 'node:path';
22
import process from 'node:process';
3-
import type { ChalkStyle, TraceOption } from '../shared';
4-
import TraceError from './TraceError';
3+
import type { ChalkStyle, TraceOption } from '../shared/index.js';
4+
import TraceError from './TraceError.js';
55

66
/**
77
* An error with formatted printing capabilities.

src/core/TraceError.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { basename, dirname } from 'node:path';
2-
import { normalizeNumber, normalizePath, normalizeTrack } from '../helpers';
3-
import type { TraceOption } from '../shared';
4-
import PaletteError from './PaletteError';
2+
import { normalizeNumber, normalizePath, normalizeTrack } from '../helpers/index.js';
3+
import type { TraceOption } from '../shared/index.js';
4+
import PaletteError from './PaletteError.js';
55

66
/**
77
* An error with stack trace information.
@@ -82,7 +82,7 @@ export default class TraceError extends PaletteError {
8282
}
8383

8484
address = normalizePath(address);
85-
let remaining = address;
85+
let remaining = address ?? '';
8686

8787
const lineMatch = /:(\d+):(\d+)\)?$/.exec(remaining);
8888
if (lineMatch) {

src/core/index.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
import Exception from './Exception';
2-
3-
export { default as PaletteError } from './PaletteError';
4-
export { default as PrintError } from './PrintError';
5-
export { default as TraceError } from './TraceError';
6-
7-
export default Exception;
1+
export { default } from './Exception.js';

src/helpers/index.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
export { default as isBgHex } from './is-bg-hex';
2-
export { default as isBgRgb } from './is-bg-rgb';
3-
export { default as isHex } from './is-hex';
4-
export { default as isRgb } from './is-rgb';
5-
export { default as isString } from './is-string';
6-
export { default as normalizeNumber } from './normalize-number';
7-
export { default as normalizePath } from './normalize-path';
8-
export { default as normalizeRgb } from './normalize-rgb';
9-
export { default as normalizeTrack } from './normalize-track';
1+
export { default as isBgHex } from './is-bg-hex.js';
2+
export { default as isBgRgb } from './is-bg-rgb.js';
3+
export { default as isHex } from './is-bg-hex.js';
4+
export { default as isRgb } from './is-bg-rgb.js';
5+
export { default as isString } from './is-string.js';
6+
export { default as normalizeNumber } from './normalize-number.js';
7+
export { default as normalizePath } from './normalize-path.js';
8+
export { default as normalizeRgb } from './normalize-rgb.js';
9+
export { default as normalizeTrack } from './normalize-track.js';

src/helpers/normalize-number.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import isString from './is-string';
1+
import isString from './is-string.js';
22

33
function normalizeNumber(value?: string | number) {
44
return isString(value) ? Number.parseInt(value, 10) : value ?? Number.NaN;

src/index.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
import Exception from './core';
2-
3-
export default Exception;
1+
export { default } from './core/index.js';

src/shared/chalk-helper.type.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
import type { BackgroundColor, ForegroundColor, Modifiers } from 'chalk';
1+
import type { BackgroundColorName, ForegroundColorName, ModifierName } from 'chalk';
22

3-
export type ChalkStyle = typeof BackgroundColor | typeof ForegroundColor | typeof Modifiers | string;
3+
export type ChalkStyle = BackgroundColorName | ForegroundColorName | ModifierName | string;

src/shared/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export type { ChalkStyle } from './chalk-helper.type';
2-
export type { TraceOption } from './trace-option.type';
1+
export type { ChalkStyle } from './chalk-helper.type.js';
2+
export type { TraceOption } from './trace-option.type.js';

test/core/PaletteError.spec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const chalk = require('chalk');
1+
import chalk from 'chalk';
22
import { expect, test } from 'vitest';
33
import PaletteError from '../../src/core/PaletteError';
44

tsconfig.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@
2020
"node_modules",
2121
"example",
2222
"dist"
23-
]
23+
],
24+
"ts-node": {
25+
"esm": true
26+
}
2427
}

tsup.config.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ import { defineConfig } from 'tsup';
22

33
export default defineConfig({
44
entry: ['src/index.ts'],
5-
format: ['cjs'],
5+
format: ['esm'],
66
outDir: 'dist',
77
target: ['es2020'],
8-
// treeshake: 'smallest',
98
bundle: true,
109
clean: true,
1110
minify: false,
1211
splitting: true,
1312
cjsInterop: true,
14-
legacyOutput: true,
1513
});

0 commit comments

Comments
 (0)