Skip to content

Commit f9d5341

Browse files
Pablo Eduardo DiazPablo Eduardo Diaz
authored andcommitted
Update package.json and tsup.config.ts for CJS and ESM compatibility
- Changed the main entry point in package.json from index.js to index.cjs and updated types from index.d.ts to index.d.cts for better compatibility with CommonJS and ES modules. - Modified tsup.config.ts to reflect the new file extensions for output, ensuring correct handling of CJS and ESM formats. - Enhanced config.ts and logger.ts to utilize a unified require function that works in both ESM and CJS environments, improving module loading consistency.
1 parent d18b70f commit f9d5341

File tree

4 files changed

+86
-19
lines changed

4 files changed

+86
-19
lines changed

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@
2727
"type": "git",
2828
"url": "https://github.com/Exudev/emitochondria.git"
2929
},
30-
"main": "./dist/index.js",
30+
"main": "./dist/index.cjs",
3131
"module": "./dist/index.mjs",
32-
"types": "./dist/index.d.ts",
32+
"types": "./dist/index.d.cts",
3333
"exports": {
3434
".": {
3535
"import": {
3636
"types": "./dist/index.d.mts",
3737
"default": "./dist/index.mjs"
3838
},
3939
"require": {
40-
"types": "./dist/index.d.ts",
41-
"default": "./dist/index.js"
40+
"types": "./dist/index.d.cts",
41+
"default": "./dist/index.cjs"
4242
}
4343
},
4444
"./schema.json": "./schema.json"

src/config.ts

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,43 @@ import { EmitochondriaConfig, EmitochondriaOptions, EventMap } from './types.js'
66

77
const CONFIG_FILENAME = 'emitochondria.config.json';
88

9+
// Environment detection
10+
const isNode = typeof (globalThis as any).process !== 'undefined'
11+
&& (globalThis as any).process?.versions?.node != null;
12+
13+
// Get require function - works in both ESM and CJS
14+
function getRequireFunction(): ((id: string) => any) | null {
15+
if (!isNode) return null;
16+
17+
// CJS: globalThis.require is available (bundlers like tsup inject this)
18+
if (typeof (globalThis as any).require === 'function') {
19+
return (globalThis as any).require;
20+
}
21+
22+
// Try Function constructor to get require in CJS context
23+
try {
24+
const req = Function('return typeof require !== "undefined" ? require : null')();
25+
if (req) return req;
26+
} catch {
27+
// Ignore
28+
}
29+
30+
// ESM: use createRequire
31+
try {
32+
// eslint-disable-next-line @typescript-eslint/no-require-imports
33+
const { createRequire } = Function('return require("node:module")')();
34+
// import.meta.url might be empty in CJS, use a fallback
35+
const url = typeof import.meta?.url === 'string' && import.meta.url
36+
? import.meta.url
37+
: 'file://' + (globalThis as any).process?.cwd?.() + '/';
38+
return createRequire(url);
39+
} catch {
40+
return null;
41+
}
42+
}
43+
44+
const nodeRequire = getRequireFunction();
45+
946
let cachedConfig: EmitochondriaConfig | null = null;
1047
let cachedProjectRoot: string | null = null;
1148

@@ -22,15 +59,14 @@ function findProjectRoot(): string {
2259
}
2360

2461
try {
25-
const req = (globalThis as any).require;
26-
if (!req) {
62+
if (!nodeRequire) {
2763
const cwd: string = proc.cwd();
2864
cachedProjectRoot = cwd;
2965
return cwd;
3066
}
3167

32-
const path = req('path');
33-
const fs = req('fs');
68+
const path = nodeRequire('path');
69+
const fs = nodeRequire('fs');
3470

3571
let dir: string = proc.cwd();
3672

@@ -75,14 +111,13 @@ function loadConfigFileSync(): EmitochondriaConfig {
75111
}
76112

77113
try {
78-
const req = (globalThis as any).require;
79-
if (!req) {
114+
if (!nodeRequire) {
80115
cachedConfig = {};
81116
return cachedConfig;
82117
}
83118

84-
const fs = req('fs');
85-
const path = req('path');
119+
const fs = nodeRequire('fs');
120+
const path = nodeRequire('path');
86121

87122
// Look for config in project root first
88123
const projectRoot = findProjectRoot();

src/logger.ts

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,39 @@ const isNode = typeof (globalThis as any).process !== 'undefined'
1010

1111
const isBrowser = typeof (globalThis as any).window !== 'undefined';
1212

13+
// Get require function - works in both ESM and CJS
14+
function getRequireFunction(): ((id: string) => any) | null {
15+
if (!isNode) return null;
16+
17+
// CJS: globalThis.require is available (bundlers like tsup inject this)
18+
if (typeof (globalThis as any).require === 'function') {
19+
return (globalThis as any).require;
20+
}
21+
22+
// Try Function constructor to get require in CJS context
23+
try {
24+
const req = Function('return typeof require !== "undefined" ? require : null')();
25+
if (req) return req;
26+
} catch {
27+
// Ignore
28+
}
29+
30+
// ESM: use createRequire
31+
try {
32+
// eslint-disable-next-line @typescript-eslint/no-require-imports
33+
const { createRequire } = Function('return require("node:module")')();
34+
// import.meta.url might be empty in CJS, use a fallback
35+
const url = typeof import.meta?.url === 'string' && import.meta.url
36+
? import.meta.url
37+
: 'file://' + (globalThis as any).process?.cwd?.() + '/';
38+
return createRequire(url);
39+
} catch {
40+
return null;
41+
}
42+
}
43+
44+
const nodeRequire = getRequireFunction();
45+
1346
// ============================================================
1447
// TIMESTAMP FORMATTING
1548
// ============================================================
@@ -124,11 +157,10 @@ export class Logger {
124157
if (!isNode) return;
125158

126159
try {
127-
// Require fs and path synchronously
128-
const req = (globalThis as any).require;
129-
if (req) {
130-
this.fs = req('fs');
131-
this.path = req('path');
160+
// Require fs and path synchronously using ESM-compatible require
161+
if (nodeRequire) {
162+
this.fs = nodeRequire('fs');
163+
this.path = nodeRequire('path');
132164

133165
// Resolve path relative to project root (where package.json is)
134166
const projectRoot = getProjectRoot();

tsup.config.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ export default defineConfig([
1212
minify: true,
1313
outExtension({ format }) {
1414
return {
15-
js: format === 'cjs' ? '.js' : '.mjs',
16-
dts: format === 'cjs' ? '.d.ts' : '.d.mts',
15+
js: format === 'cjs' ? '.cjs' : '.mjs',
16+
dts: format === 'cjs' ? '.d.cts' : '.d.mts',
1717
};
1818
},
1919
},

0 commit comments

Comments
 (0)