Skip to content

Commit 128244d

Browse files
committed
ensured that ES and CJS would be supported by creating a manual require.
1 parent d743ed0 commit 128244d

File tree

3 files changed

+19
-14
lines changed

3 files changed

+19
-14
lines changed

package.json

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
{
22
"name": "@kingmaj0r/libfm",
33
"version": "1.0.0",
4-
"main": "dist/libfm.cjs.js",
5-
"module": "dist/libfm.es.js",
4+
"main": "dist/libfm.cjs",
5+
"module": "dist/libfm.js",
66
"exports": {
77
".": {
88
"types": "./dist/types/index.d.ts",
9-
"import": "./dist/libfm.es.js",
10-
"require": "./dist/libfm.cjs.js"
9+
"import": "./dist/libfm.js",
10+
"require": "./dist/libfm.cjs"
1111
},
1212
"./lib": {
1313
"types": "./dist/types/lib/index.d.ts",
14-
"import": "./dist/lib/index.es.js",
15-
"require": "./dist/lib/index.cjs.js"
14+
"import": "./dist/lib/index.js",
15+
"require": "./dist/lib/index.cjs"
1616
},
1717
"./electron": {
1818
"types": "./dist/types/electron/index.d.ts",
19-
"import": "./dist/electron/index.es.js",
20-
"require": "./dist/electron/index.cjs.js"
19+
"import": "./dist/electron/index.js",
20+
"require": "./dist/electron/index.cjs"
2121
}
2222
},
2323
"types": "dist/types/index.d.ts",
24-
"type": "module",
2524
"scripts": {
2625
"build": "vite build && tsc"
2726
},

src/lib/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { createRequire } from "module";
2+
13
interface Libfm {
24
getFiles: (path: string) => FileList;
35
getUserName: () => string;
@@ -48,14 +50,16 @@ let libfm: Libfm = {
4850
export function init(fmNodeLocation: string) {
4951
fmNodePath = fmNodeLocation;
5052
loadLibfm();
53+
console.log("Libfm successfully initialized.");
5154
}
5255

53-
export function loadLibfm() {
56+
function loadLibfm() {
5457
if (!fmNodePath) {
5558
throw new Error("Libfm not initialized. Please call init() with the path to 'fm.node'.");
5659
}
5760

5861
try {
62+
const require = createRequire(import.meta.url);
5963
libfm = require(fmNodePath);
6064
} catch (error) {
6165
console.error("Failed to load libfm:", error);

vite.config.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@ export default defineConfig({
88
lib: "src/lib/index.ts",
99
electron: "src/electron/index.ts",
1010
},
11+
formats: ['es', 'cjs'],
1112
name: "libfm",
1213
fileName: (format, entryName) => {
14+
const ext = format === "cjs" ? "cjs" : "js";
1315
switch (entryName) {
1416
case "lib":
15-
return `lib/index.${format}.js`;
17+
return `lib/index.${ext}`;
1618
case "electron":
17-
return `electron/index.${format}.js`;
19+
return `electron/index.${ext}`;
1820
default:
19-
return `libfm.${format}.js`;
21+
return `libfm.${ext}`;
2022
}
2123
}
2224
},
2325
rollupOptions: {
24-
external: ["path", "fs", "electron"],
26+
external: ["path", "fs", "electron", "module"],
2527
output: {
2628
exports: "named",
2729
dir: "dist"

0 commit comments

Comments
 (0)