Skip to content

Commit eb0efdc

Browse files
committed
feat(3.0): convert src code to ESM
1 parent ccbb2c6 commit eb0efdc

7 files changed

+53
-37
lines changed

src/index.ts

+10-14
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import type {
88
LoggerMethods,
99
NameSpaceConfig,
1010
OutputAdapter,
11-
} from './definitions'
12-
13-
import * as outputs from './output_adapters'
14-
import * as outputUtils from './output_utils'
11+
} from './definitions.js'
12+
import * as outputs from './output_adapters.js'
13+
import * as outputUtils from './output_utils.js'
1514

1615
/************* LOCAL STATE *************/
1716
const defaultConfig: Readonly<LoggerConfig> = {
@@ -23,7 +22,7 @@ const defaultConfig: Readonly<LoggerConfig> = {
2322
globalContext: {},
2423
}
2524

26-
const mutableConfig: LoggerConfig = {
25+
const sharedConfig: LoggerConfig = {
2726
...defaultConfig,
2827
}
2928

@@ -92,7 +91,7 @@ const parseNamespace = (namespace: string): NameSpaceConfig | undefined => {
9291
export const createLogger = (
9392
namespace = '',
9493
canForceWrite = false,
95-
config = mutableConfig
94+
config = sharedConfig
9695
): Logger => {
9796
if (config.loggers[namespace]) return config.loggers[namespace]
9897

@@ -158,7 +157,7 @@ export const id = (): string => uuidv4()
158157
/**
159158
* Define enabled / disabled namespaces
160159
*/
161-
export const setNamespaces = (namespaceStr: string, config = mutableConfig): void => {
160+
export const setNamespaces = (namespaceStr: string, config = sharedConfig): void => {
162161
config.namespaces = namespaceStr
163162
.split(',')
164163
.map(parseNamespace)
@@ -168,7 +167,7 @@ export const setNamespaces = (namespaceStr: string, config = mutableConfig): voi
168167
/**
169168
* Change log level
170169
*/
171-
export const setLevel = (level: LogLevel, config = mutableConfig): void => {
170+
export const setLevel = (level: LogLevel, config = sharedConfig): void => {
172171
const levelIndex = config.levels.indexOf(level)
173172
if (levelIndex === -1) throw new Error(`Invalid log level: ${level}`)
174173
config.level = levelIndex
@@ -179,7 +178,7 @@ export const setLevel = (level: LogLevel, config = mutableConfig): void => {
179178
*/
180179
export const setOutput = (
181180
outputAdapters: OutputAdapter[] | OutputAdapter,
182-
config = mutableConfig
181+
config = sharedConfig
183182
): void => {
184183
config.outputs = Array.isArray(outputAdapters) ? outputAdapters : [outputAdapters]
185184
}
@@ -190,10 +189,7 @@ export const setOutput = (
190189
* Be warned this context will be added to all logs,
191190
* even those from third party libraries if they use this module.
192191
*/
193-
export const setGlobalContext = (
194-
context: Record<string, unknown>,
195-
config = mutableConfig
196-
): void => {
192+
export const setGlobalContext = (context: Record<string, unknown>, config = sharedConfig): void => {
197193
config.globalContext = { ...context }
198194
}
199195

@@ -205,7 +201,7 @@ setNamespaces(namespaces)
205201
setLevel(logLevel)
206202

207203
/************* EXPORT *************/
208-
export * from './definitions'
204+
export * from './definitions.js'
209205
export { outputUtils, outputs }
210206
export default {
211207
createLogger,

src/output_adapters.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
const prettyOutput = require('prettyoutput')
21
import { Writable } from 'node:stream'
3-
import { colors } from './colors'
4-
import type { Log, LogColor, LogLevel, Output } from './definitions'
5-
import { stringifyLog } from './output_utils'
2+
import prettyOutput from 'prettyoutput'
3+
import { colors } from './colors.js'
4+
import type { Log, LogColor, LogLevel, Output } from './definitions.js'
5+
import { stringifyLog } from './output_utils.js'
66

77
/**
88
* Object mapping log color and log level

src/output_utils.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { ReplacerFunction } from './definitions'
2-
import { fastStringifyLog } from './json_schema'
1+
import type { ReplacerFunction } from './definitions.js'
2+
import { fastStringifyLog } from './json_schema.js'
33

44
/**
55
* Replace circular reference when used with JSON.stringify
@@ -33,7 +33,8 @@ export const stringifyLog = (log: Record<string, unknown>): string => {
3333
export const isObject = (val: unknown): val is Record<string, unknown> =>
3434
!!val && typeof val === 'object' && !Array.isArray(val)
3535

36-
export const memoize = <T extends (...args: unknown[]) => unknown>(fn: T): T => {
36+
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
37+
export const memoize = <T extends (...args: any[]) => unknown>(fn: T): T => {
3738
const cache = new Map<string, ReturnType<T>>()
3839
return ((...args: unknown[]) => {
3940
const key = JSON.stringify(args)

src/types/global.d.ts

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare namespace NodeJS {
2+
interface ProcessEnv {
3+
LOGS?: string
4+
LOG_LEVEL?: string
5+
}
6+
}

src/types/pretty_output.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module 'prettyoutput' {
2+
const prettyoutput: (input: unknown, opts: Record<string, unknown>, indent?: number) => string
3+
4+
export = prettyoutput
5+
}

tsconfig.json

+23-13
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
11
{
22
"compilerOptions": {
3+
"allowJs": true,
4+
"allowSyntheticDefaultImports": true,
5+
"allowUnreachableCode": false,
6+
"declaration": true,
7+
"esModuleInterop": true,
8+
"forceConsistentCasingInFileNames": true,
39
"incremental": true,
10+
"lib": ["ES2023", "DOM"],
11+
"module": "NodeNext",
12+
"moduleResolution": "NodeNext",
13+
"noErrorTruncation": true,
14+
"noFallthroughCasesInSwitch": true,
415
"noImplicitAny": true,
5-
"allowJs": true,
6-
"target": "es2019",
7-
"module": "commonjs",
8-
"lib": ["es2020"],
9-
"alwaysStrict": true,
10-
"skipLibCheck": true,
11-
"noUnusedParameters": false,
12-
"noUnusedLocals": false,
13-
"strictNullChecks": true,
16+
"noImplicitOverride": true,
17+
"noImplicitReturns": true,
18+
"noPropertyAccessFromIndexSignature": true,
1419
"noUncheckedIndexedAccess": true,
15-
"esModuleInterop": true,
16-
"allowSyntheticDefaultImports": true,
17-
"forceConsistentCasingInFileNames": true
20+
"noUnusedLocals": true,
21+
"noUnusedParameters": true,
22+
"outDir": "./lib",
23+
"skipLibCheck": true,
24+
"sourceMap": true,
25+
"strict": true,
26+
"target": "ES2020",
27+
"typeRoots": ["./node_modules/@types", "src/types"]
1828
},
19-
"include": ["src/**/*", "test/**/*.ts"],
29+
"include": ["src/**/*", "test/**/*.ts", "examples/**/*.ts"],
2030
"exclude": []
2131
}

tsconfig.lib.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
{
22
"extends": "./tsconfig.json",
33
"compilerOptions": {
4-
"declaration": true,
5-
"outDir": "lib",
6-
"sourceMap": true
4+
"declaration": true
75
},
86
"include": ["src/**/*.ts"]
97
}

0 commit comments

Comments
 (0)