@fgiova/cjs-esm-ts-builder 1.1.1 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -59,6 +59,7 @@ build(options)
59
59
  ...
60
60
  "main": "./dist/cjs/index.js",
61
61
  "types": "./dist/cjs/index.d.ts",
62
+ "module": "./dist/esm/index.mjs",
62
63
  "exports": {
63
64
  ".": {
64
65
  "import": {
@@ -85,9 +86,44 @@ build(options)
85
86
  - `buildFolder`: The folder where the build files will be generated. Default is `dist`.
86
87
 
87
88
 
88
- ## Notes
89
- - Tested with Node.js >= 20
90
- - Work only on *nix systems (Linux, MacOS, etc.)
89
+ ## Requirements
90
+ - Node.js >= 24
91
+ - TypeScript >= 7 (use 1.x for TypeScript 5)
92
+ - Works only on *nix systems (Linux, MacOS, etc.)
93
+
94
+ ## Configure tsconfig.json
95
+ The builder runs `tsc` twice, overriding these options from the command line:
96
+ - CJS: `--module commonjs --moduleResolution bundler --verbatimModuleSyntax false` (dynamic `import()` becomes `require()`, as in 1.x)
97
+ - ESM: `--module esnext --moduleResolution bundler`, then rewrites relative imports to `.mjs`
98
+
99
+ Both builds use `bundler`: TypeScript 7 removed `node10`, and with `nodenext` the output format follows the package.json `type`, so one package cannot emit both CJS and ESM. `bundler` only affects type checking during the build, not the emitted code.
100
+
101
+ ```json
102
+ {
103
+ "extends": "@tsconfig/node24/tsconfig.json",
104
+ "compilerOptions": {
105
+ "outDir": "dist",
106
+ "rootDir": "src",
107
+ "declaration": true,
108
+ "module": "esnext",
109
+ "moduleResolution": "bundler",
110
+ "allowImportingTsExtensions": true,
111
+ "rewriteRelativeImportExtensions": true,
112
+ "erasableSyntaxOnly": true,
113
+ "verbatimModuleSyntax": true
114
+ },
115
+ "include": ["src/**/*"]
116
+ }
117
+ ```
118
+ - Set `module: esnext` and `moduleResolution: bundler`, as the ESM build: with the `nodenext` of `@tsconfig/node22`/`@tsconfig/node24` and `verbatimModuleSyntax`, a package without `"type": "module"` fails type checking with TS1295.
119
+ - Do not set `"type": "module"` in package.json: the CJS output uses `.js` files.
120
+ - `verbatimModuleSyntax` is supported: the builder turns it off only for the CJS build, where it would forbid converting `import`/`export` to `require` (TS1295).
121
+ - Relative imports may use the `.ts` extension (`import { a } from "./a.ts"`). Imports without extension still work.
122
+
123
+ ## Migrating from 1.x
124
+ - Upgrade to `typescript@7` and Node.js 24.
125
+ - In tsconfig, set `module`/`moduleResolution` as above and remove options TypeScript 7 dropped (`baseUrl`, `moduleResolution: node`/`node10`, `esModuleInterop: false`).
126
+ - Type resolution now follows package.json `exports`: `@ts-expect-error` added for ESM-only packages such as `p-map` may become unused (TS2578), remove them.
91
127
 
92
128
  ## License
93
129
  Licensed under [MIT](./LICENSE).
package/builder.mjs CHANGED
@@ -1,6 +1,6 @@
1
1
  #!/usr/bin/env node
2
- import {build} from "./dist/esm/index.mjs";
3
2
  import arg from "arg";
3
+ import { build } from "./dist/esm/index.mjs";
4
4
 
5
5
  const args = arg({
6
6
  "--target": String,
@@ -11,7 +11,7 @@ const args = arg({
11
11
  "-h": "--help",
12
12
  "-t": "--target",
13
13
  "-e": "--esmFolder",
14
- "-c": "--cjsFolder"
14
+ "-c": "--cjsFolder",
15
15
  });
16
16
 
17
17
  const target = args["--target"] ?? "es2020";
@@ -37,10 +37,12 @@ build({
37
37
  target,
38
38
  buildFolder,
39
39
  esmFolder,
40
- cjsFolder
41
- }).then(() => {
42
- console.log("Build completed successfully.");
43
- }).catch((error) => {
44
- console.error("Build failed:", error);
45
- process.exit(1);
46
- });
40
+ cjsFolder,
41
+ })
42
+ .then(() => {
43
+ console.log("Build completed successfully.");
44
+ })
45
+ .catch((error) => {
46
+ console.error("Build failed:", error);
47
+ process.exit(1);
48
+ });
@@ -1,18 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.compile = void 0;
4
- const index_1 = require("../command/index");
4
+ const index_ts_1 = require("../command/index.js");
5
+ const index_ts_2 = require("../type-only-files/index.js");
5
6
  const compile = async (outDir, target) => {
6
7
  const options = [
7
8
  `--outDir ${outDir}`,
8
9
  `--target ${target}`,
9
- "--module CommonJS",
10
- "--moduleResolution Node",
10
+ "--module commonjs",
11
+ "--moduleResolution bundler",
12
+ "--verbatimModuleSyntax false",
11
13
  "--declaration",
12
14
  ].join(" ");
13
15
  const commandLine = `npx tsc ${options}`;
14
- const command = new index_1.Command(commandLine);
16
+ const command = new index_ts_1.Command(commandLine);
15
17
  await command.getPromise();
18
+ await (0, index_ts_2.toDts)(outDir);
16
19
  console.log("Compiling to CJS completed.");
17
20
  };
18
21
  exports.compile = compile;
@@ -1 +1 @@
1
- export * as Builder from "./compile";
1
+ export * as Builder from "./compile.ts";
@@ -34,4 +34,4 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.Builder = void 0;
37
- exports.Builder = __importStar(require("./compile"));
37
+ exports.Builder = __importStar(require("./compile.js"));
@@ -1,12 +1,12 @@
1
1
  export declare class Command {
2
- private readonly command;
3
- private readonly debug?;
4
2
  private readonly process;
5
3
  private readonly promise;
6
4
  private resolve;
7
5
  private terminated;
8
6
  private outputBuffers;
9
- constructor(command: string, debug?: boolean | undefined);
7
+ private readonly command;
8
+ private readonly debug?;
9
+ constructor(command: string, debug?: boolean);
10
10
  getPromise(): Promise<number | null>;
11
11
  private onExit;
12
12
  private onClose;
@@ -4,10 +4,10 @@ exports.Command = void 0;
4
4
  const node_child_process_1 = require("node:child_process");
5
5
  class Command {
6
6
  constructor(command, debug) {
7
- this.command = command;
8
- this.debug = debug;
9
7
  this.terminated = false;
10
8
  this.outputBuffers = [];
9
+ this.command = command;
10
+ this.debug = debug;
11
11
  this.promise = new Promise((resolve) => {
12
12
  this.resolve = resolve;
13
13
  });
@@ -41,16 +41,10 @@ class Command {
41
41
  }
42
42
  kill() {
43
43
  try {
44
- const processPid = (0, node_child_process_1.spawnSync)("ps", [
45
- "-o",
46
- "pid",
47
- "--no-headers",
48
- "--ppid",
49
- this.process.pid.toString()
50
- ], {
51
- encoding: "utf8"
44
+ const processPid = (0, node_child_process_1.spawnSync)("ps", ["-o", "pid", "--no-headers", "--ppid", this.process.pid.toString()], {
45
+ encoding: "utf8",
52
46
  });
53
- const pid = parseInt(processPid.output[1]);
47
+ const pid = parseInt(processPid.output[1], 10);
54
48
  process.kill(pid, "SIGTERM");
55
49
  this.process.kill();
56
50
  }
@@ -34,11 +34,13 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.toEsm = void 0;
37
- /* eslint-disable @typescript-eslint/no-unused-vars */
38
- const path = __importStar(require("node:path"));
39
37
  const fs = __importStar(require("node:fs/promises"));
38
+ const path = __importStar(require("node:path"));
40
39
  const relativePath = async (sourceFile, targetFile) => {
41
- targetFile = targetFile.trim().slice(1, targetFile.length - 1).replace(".js", "");
40
+ targetFile = targetFile
41
+ .trim()
42
+ .slice(1, targetFile.length - 1)
43
+ .replace(/\.[jt]s$/, "");
42
44
  const sourceDir = path.dirname(sourceFile);
43
45
  const targetElement = path.resolve(sourceDir, `${targetFile}`);
44
46
  try {
@@ -48,7 +50,7 @@ const relativePath = async (sourceFile, targetFile) => {
48
50
  }
49
51
  return null;
50
52
  }
51
- catch (e) { }
53
+ catch (_e) { }
52
54
  try {
53
55
  const stat = await fs.stat(`${targetElement}.js`);
54
56
  if (stat.isFile()) {
@@ -56,7 +58,7 @@ const relativePath = async (sourceFile, targetFile) => {
56
58
  }
57
59
  return null;
58
60
  }
59
- catch (e) { }
61
+ catch (_e) { }
60
62
  try {
61
63
  const stat = await fs.stat(`${targetElement}.mjs`);
62
64
  if (stat.isFile()) {
@@ -64,7 +66,7 @@ const relativePath = async (sourceFile, targetFile) => {
64
66
  }
65
67
  return null;
66
68
  }
67
- catch (e) {
69
+ catch (_e) {
68
70
  return null;
69
71
  }
70
72
  };
@@ -99,7 +101,8 @@ const replaceExportImportDeclarations = async (content, sourceFile) => {
99
101
  }
100
102
  };
101
103
  const replaceDirname = (content) => {
102
- return content.replaceAll(/(?<!\))__dirname(?!\()/g, "import.meta.dirname")
104
+ return content
105
+ .replaceAll(/(?<!\))__dirname(?!\()/g, "import.meta.dirname")
103
106
  .replaceAll(/(?<!\))__filename(?!\()/g, "import.meta.filename");
104
107
  };
105
108
  const replaceDeclarations = async (content, sourceFile) => {
@@ -110,9 +113,11 @@ const fileIsProcessable = (sourcepath) => {
110
113
  return [".js", ".ts"].includes(extname);
111
114
  };
112
115
  const esmExtension = (extension) => {
113
- return (extension === ".js" ? ".mjs" :
114
- extension === ".ts" ? ".mts" :
115
- extension);
116
+ return extension === ".js"
117
+ ? ".mjs"
118
+ : extension === ".ts"
119
+ ? ".mts"
120
+ : extension;
116
121
  };
117
122
  const processFile = async (sourceFile) => {
118
123
  if (!fileIsProcessable(sourceFile)) {
@@ -1,21 +1,23 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.compile = void 0;
4
- const command_1 = require("../command");
5
- const cjs_to_esm_1 = require("./cjs-to-esm");
4
+ const index_ts_1 = require("../command/index.js");
5
+ const index_ts_2 = require("../type-only-files/index.js");
6
+ const cjs_to_esm_ts_1 = require("./cjs-to-esm.js");
6
7
  const compile = async (outDir, target) => {
7
8
  const options = [
8
9
  `--outDir ${outDir}`,
9
10
  `--target ${target}`,
10
11
  "--module esnext",
11
12
  "--declaration",
12
- "--moduleResolution node"
13
+ "--moduleResolution bundler",
13
14
  ].join(" ");
14
15
  const commandLine = `npx tsc ${options}`;
15
- const command = new command_1.Command(commandLine);
16
+ const command = new index_ts_1.Command(commandLine);
16
17
  await command.getPromise();
18
+ await (0, index_ts_2.toDts)(outDir);
17
19
  console.log("Compiling to ESM completed.");
18
- await (0, cjs_to_esm_1.toEsm)(outDir);
20
+ await (0, cjs_to_esm_ts_1.toEsm)(outDir);
19
21
  console.log("Converting to ESM completed.");
20
22
  };
21
23
  exports.compile = compile;
@@ -1 +1 @@
1
- export * as Builder from "./compile";
1
+ export * as Builder from "./compile.ts";
@@ -34,4 +34,4 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.Builder = void 0;
37
- exports.Builder = __importStar(require("./compile"));
37
+ exports.Builder = __importStar(require("./compile.js"));
package/dist/cjs/index.js CHANGED
@@ -34,24 +34,24 @@ var __importStar = (this && this.__importStar) || (function () {
34
34
  })();
35
35
  Object.defineProperty(exports, "__esModule", { value: true });
36
36
  exports.build = void 0;
37
- const esm = __importStar(require("./esm"));
38
- const cjs = __importStar(require("./cjs"));
39
37
  const path = __importStar(require("node:path"));
38
+ const cjs = __importStar(require("./cjs/index.js"));
39
+ const esm = __importStar(require("./esm/index.js"));
40
40
  const build = async (options) => {
41
41
  const defaultOptions = {
42
42
  target: "es2020",
43
43
  esmFolder: "esm",
44
44
  cjsFolder: "cjs",
45
- buildFolder: "dist"
45
+ buildFolder: "dist",
46
46
  };
47
47
  const { target, esmFolder, cjsFolder, buildFolder } = {
48
48
  ...defaultOptions,
49
- ...options
49
+ ...options,
50
50
  };
51
51
  console.log("Building...");
52
52
  await Promise.all([
53
53
  esm.Builder.compile(path.resolve(buildFolder, esmFolder), target),
54
- cjs.Builder.compile(path.resolve(buildFolder, cjsFolder), target)
54
+ cjs.Builder.compile(path.resolve(buildFolder, cjsFolder), target),
55
55
  ]);
56
56
  console.log(`Build completed. Output directory: ${buildFolder}`);
57
57
  };
@@ -0,0 +1 @@
1
+ export declare const toDts: (destinationDirectory: string) => Promise<void>;
@@ -0,0 +1,92 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.toDts = void 0;
37
+ const fs = __importStar(require("node:fs/promises"));
38
+ const path = __importStar(require("node:path"));
39
+ const get_tsconfig_1 = require("get-tsconfig");
40
+ let baseSourcePath = "";
41
+ const processDirectory = async (directorySource, directoryDestination) => {
42
+ const files = await fs.readdir(directorySource);
43
+ for (const file of files) {
44
+ const elementPath = path.join(directorySource, file);
45
+ await processElement(elementPath, directoryDestination);
46
+ }
47
+ };
48
+ const fileIsProcessable = (sourcepath) => {
49
+ return sourcepath.endsWith(".d.ts");
50
+ };
51
+ const processFile = async (sourceFile, destinationPath) => {
52
+ if (!fileIsProcessable(sourceFile)) {
53
+ return;
54
+ }
55
+ const basename = path.basename(sourceFile);
56
+ const sourceContent = await fs.readFile(sourceFile, "utf-8");
57
+ const newFile = `${path.join(destinationPath, basename)}`;
58
+ await fs.writeFile(newFile, sourceContent);
59
+ };
60
+ const processElement = async (sourcePath, destinationPath) => {
61
+ const element = await fs.stat(sourcePath);
62
+ if (element.isDirectory()) {
63
+ const newDestination = destinationPath + sourcePath.replace(baseSourcePath, "");
64
+ try {
65
+ await fs.access(newDestination);
66
+ }
67
+ catch (_e) {
68
+ await fs.mkdir(newDestination, { recursive: true });
69
+ }
70
+ return await processDirectory(sourcePath, newDestination);
71
+ }
72
+ if (element.isFile()) {
73
+ return await processFile(sourcePath, destinationPath);
74
+ }
75
+ };
76
+ const toDts = async (destinationDirectory) => {
77
+ const tsconfig = (0, get_tsconfig_1.getTsconfig)();
78
+ const sourceDirectory = path.join(tsconfig?.config.compilerOptions?.sourceRoot || "./src");
79
+ const sourceDirectoryStat = await fs.stat(sourceDirectory);
80
+ if (!sourceDirectoryStat.isDirectory()) {
81
+ throw new Error("Source path is not a directory");
82
+ }
83
+ baseSourcePath = sourceDirectory;
84
+ const destinationDirectoryStat = await fs.stat(destinationDirectory);
85
+ if (!destinationDirectoryStat.isDirectory()) {
86
+ throw new Error("Destination path is not a directory");
87
+ }
88
+ destinationDirectory = path.join(destinationDirectory);
89
+ await fs.mkdir(destinationDirectory, { recursive: true });
90
+ await processElement(sourceDirectory, destinationDirectory);
91
+ };
92
+ exports.toDts = toDts;
@@ -1,14 +1,17 @@
1
1
  import { Command } from "../command/index.mjs";
2
+ import { toDts } from "../type-only-files/index.mjs";
2
3
  export const compile = async (outDir, target) => {
3
4
  const options = [
4
5
  `--outDir ${outDir}`,
5
6
  `--target ${target}`,
6
- "--module CommonJS",
7
- "--moduleResolution Node",
7
+ "--module commonjs",
8
+ "--moduleResolution bundler",
9
+ "--verbatimModuleSyntax false",
8
10
  "--declaration",
9
11
  ].join(" ");
10
12
  const commandLine = `npx tsc ${options}`;
11
13
  const command = new Command(commandLine);
12
14
  await command.getPromise();
15
+ await toDts(outDir);
13
16
  console.log("Compiling to CJS completed.");
14
17
  };
@@ -1,12 +1,12 @@
1
1
  export declare class Command {
2
- private readonly command;
3
- private readonly debug?;
4
2
  private readonly process;
5
3
  private readonly promise;
6
4
  private resolve;
7
5
  private terminated;
8
6
  private outputBuffers;
9
- constructor(command: string, debug?: boolean | undefined);
7
+ private readonly command;
8
+ private readonly debug?;
9
+ constructor(command: string, debug?: boolean);
10
10
  getPromise(): Promise<number | null>;
11
11
  private onExit;
12
12
  private onClose;
@@ -1,10 +1,10 @@
1
1
  import { spawn, spawnSync } from "node:child_process";
2
2
  export class Command {
3
3
  constructor(command, debug) {
4
- this.command = command;
5
- this.debug = debug;
6
4
  this.terminated = false;
7
5
  this.outputBuffers = [];
6
+ this.command = command;
7
+ this.debug = debug;
8
8
  this.promise = new Promise((resolve) => {
9
9
  this.resolve = resolve;
10
10
  });
@@ -38,16 +38,10 @@ export class Command {
38
38
  }
39
39
  kill() {
40
40
  try {
41
- const processPid = spawnSync("ps", [
42
- "-o",
43
- "pid",
44
- "--no-headers",
45
- "--ppid",
46
- this.process.pid.toString()
47
- ], {
48
- encoding: "utf8"
41
+ const processPid = spawnSync("ps", ["-o", "pid", "--no-headers", "--ppid", this.process.pid.toString()], {
42
+ encoding: "utf8",
49
43
  });
50
- const pid = parseInt(processPid.output[1]);
44
+ const pid = parseInt(processPid.output[1], 10);
51
45
  process.kill(pid, "SIGTERM");
52
46
  this.process.kill();
53
47
  }
@@ -1,8 +1,10 @@
1
- /* eslint-disable @typescript-eslint/no-unused-vars */
2
- import * as path from "node:path";
3
1
  import * as fs from "node:fs/promises";
2
+ import * as path from "node:path";
4
3
  const relativePath = async (sourceFile, targetFile) => {
5
- targetFile = targetFile.trim().slice(1, targetFile.length - 1).replace(".js", "");
4
+ targetFile = targetFile
5
+ .trim()
6
+ .slice(1, targetFile.length - 1)
7
+ .replace(/\.[jt]s$/, "");
6
8
  const sourceDir = path.dirname(sourceFile);
7
9
  const targetElement = path.resolve(sourceDir, `${targetFile}`);
8
10
  try {
@@ -12,7 +14,7 @@ const relativePath = async (sourceFile, targetFile) => {
12
14
  }
13
15
  return null;
14
16
  }
15
- catch (e) { }
17
+ catch (_e) { }
16
18
  try {
17
19
  const stat = await fs.stat(`${targetElement}.js`);
18
20
  if (stat.isFile()) {
@@ -20,7 +22,7 @@ const relativePath = async (sourceFile, targetFile) => {
20
22
  }
21
23
  return null;
22
24
  }
23
- catch (e) { }
25
+ catch (_e) { }
24
26
  try {
25
27
  const stat = await fs.stat(`${targetElement}.mjs`);
26
28
  if (stat.isFile()) {
@@ -28,7 +30,7 @@ const relativePath = async (sourceFile, targetFile) => {
28
30
  }
29
31
  return null;
30
32
  }
31
- catch (e) {
33
+ catch (_e) {
32
34
  return null;
33
35
  }
34
36
  };
@@ -63,7 +65,8 @@ const replaceExportImportDeclarations = async (content, sourceFile) => {
63
65
  }
64
66
  };
65
67
  const replaceDirname = (content) => {
66
- return content.replaceAll(/(?<!\))__dirname(?!\()/g, "import.meta.dirname")
68
+ return content
69
+ .replaceAll(/(?<!\))__dirname(?!\()/g, "import.meta.dirname")
67
70
  .replaceAll(/(?<!\))__filename(?!\()/g, "import.meta.filename");
68
71
  };
69
72
  const replaceDeclarations = async (content, sourceFile) => {
@@ -74,9 +77,11 @@ const fileIsProcessable = (sourcepath) => {
74
77
  return [".js", ".ts"].includes(extname);
75
78
  };
76
79
  const esmExtension = (extension) => {
77
- return (extension === ".js" ? ".mjs" :
78
- extension === ".ts" ? ".mts" :
79
- extension);
80
+ return extension === ".js"
81
+ ? ".mjs"
82
+ : extension === ".ts"
83
+ ? ".mts"
84
+ : extension;
80
85
  };
81
86
  const processFile = async (sourceFile) => {
82
87
  if (!fileIsProcessable(sourceFile)) {
@@ -1,4 +1,5 @@
1
1
  import { Command } from "../command/index.mjs";
2
+ import { toDts } from "../type-only-files/index.mjs";
2
3
  import { toEsm } from "./cjs-to-esm.mjs";
3
4
  export const compile = async (outDir, target) => {
4
5
  const options = [
@@ -6,11 +7,12 @@ export const compile = async (outDir, target) => {
6
7
  `--target ${target}`,
7
8
  "--module esnext",
8
9
  "--declaration",
9
- "--moduleResolution node"
10
+ "--moduleResolution bundler",
10
11
  ].join(" ");
11
12
  const commandLine = `npx tsc ${options}`;
12
13
  const command = new Command(commandLine);
13
14
  await command.getPromise();
15
+ await toDts(outDir);
14
16
  console.log("Compiling to ESM completed.");
15
17
  await toEsm(outDir);
16
18
  console.log("Converting to ESM completed.");
@@ -1,21 +1,21 @@
1
- import * as esm from "./esm/index.mjs";
2
- import * as cjs from "./cjs/index.mjs";
3
1
  import * as path from "node:path";
2
+ import * as cjs from "./cjs/index.mjs";
3
+ import * as esm from "./esm/index.mjs";
4
4
  export const build = async (options) => {
5
5
  const defaultOptions = {
6
6
  target: "es2020",
7
7
  esmFolder: "esm",
8
8
  cjsFolder: "cjs",
9
- buildFolder: "dist"
9
+ buildFolder: "dist",
10
10
  };
11
11
  const { target, esmFolder, cjsFolder, buildFolder } = {
12
12
  ...defaultOptions,
13
- ...options
13
+ ...options,
14
14
  };
15
15
  console.log("Building...");
16
16
  await Promise.all([
17
17
  esm.Builder.compile(path.resolve(buildFolder, esmFolder), target),
18
- cjs.Builder.compile(path.resolve(buildFolder, cjsFolder), target)
18
+ cjs.Builder.compile(path.resolve(buildFolder, cjsFolder), target),
19
19
  ]);
20
20
  console.log(`Build completed. Output directory: ${buildFolder}`);
21
21
  };
@@ -0,0 +1 @@
1
+ export declare const toDts: (destinationDirectory: string) => Promise<void>;
@@ -0,0 +1,55 @@
1
+ import * as fs from "node:fs/promises";
2
+ import * as path from "node:path";
3
+ import { getTsconfig } from "get-tsconfig";
4
+ let baseSourcePath = "";
5
+ const processDirectory = async (directorySource, directoryDestination) => {
6
+ const files = await fs.readdir(directorySource);
7
+ for (const file of files) {
8
+ const elementPath = path.join(directorySource, file);
9
+ await processElement(elementPath, directoryDestination);
10
+ }
11
+ };
12
+ const fileIsProcessable = (sourcepath) => {
13
+ return sourcepath.endsWith(".d.ts");
14
+ };
15
+ const processFile = async (sourceFile, destinationPath) => {
16
+ if (!fileIsProcessable(sourceFile)) {
17
+ return;
18
+ }
19
+ const basename = path.basename(sourceFile);
20
+ const sourceContent = await fs.readFile(sourceFile, "utf-8");
21
+ const newFile = `${path.join(destinationPath, basename)}`;
22
+ await fs.writeFile(newFile, sourceContent);
23
+ };
24
+ const processElement = async (sourcePath, destinationPath) => {
25
+ const element = await fs.stat(sourcePath);
26
+ if (element.isDirectory()) {
27
+ const newDestination = destinationPath + sourcePath.replace(baseSourcePath, "");
28
+ try {
29
+ await fs.access(newDestination);
30
+ }
31
+ catch (_e) {
32
+ await fs.mkdir(newDestination, { recursive: true });
33
+ }
34
+ return await processDirectory(sourcePath, newDestination);
35
+ }
36
+ if (element.isFile()) {
37
+ return await processFile(sourcePath, destinationPath);
38
+ }
39
+ };
40
+ export const toDts = async (destinationDirectory) => {
41
+ const tsconfig = getTsconfig();
42
+ const sourceDirectory = path.join(tsconfig?.config.compilerOptions?.sourceRoot || "./src");
43
+ const sourceDirectoryStat = await fs.stat(sourceDirectory);
44
+ if (!sourceDirectoryStat.isDirectory()) {
45
+ throw new Error("Source path is not a directory");
46
+ }
47
+ baseSourcePath = sourceDirectory;
48
+ const destinationDirectoryStat = await fs.stat(destinationDirectory);
49
+ if (!destinationDirectoryStat.isDirectory()) {
50
+ throw new Error("Destination path is not a directory");
51
+ }
52
+ destinationDirectory = path.join(destinationDirectory);
53
+ await fs.mkdir(destinationDirectory, { recursive: true });
54
+ await processElement(sourceDirectory, destinationDirectory);
55
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgiova/cjs-esm-ts-builder",
3
- "version": "1.1.1",
3
+ "version": "2.0.0",
4
4
  "description": "Build tool for CJS and ESM modules in TypeScript",
5
5
  "repository": {
6
6
  "type": "git",
@@ -21,8 +21,9 @@
21
21
  "publishConfig": {
22
22
  "access": "public"
23
23
  },
24
- "main": "./dist/index.js",
25
- "types": "./dist/index.d.ts",
24
+ "main": "./dist/cjs/index.js",
25
+ "types": "./dist/cjs/index.d.ts",
26
+ "module": "./dist/esm/index.mjs",
26
27
  "exports": {
27
28
  ".": {
28
29
  "import": {
@@ -43,30 +44,29 @@
43
44
  "esmBuilder": "./builder.mjs"
44
45
  },
45
46
  "scripts": {
46
- "build": "ts-node build.ts",
47
- "lint": "eslint"
47
+ "build": "node build.ts",
48
+ "lint": "biome ci",
49
+ "fix": "biome check --write"
48
50
  },
49
51
  "dependencies": {
50
- "arg": "^5.0.2"
52
+ "arg": "^5.0.2",
53
+ "get-tsconfig": "^4.10.0"
51
54
  },
52
55
  "devDependencies": {
53
- "@eslint/compat": "^1.2.7",
54
- "@eslint/eslintrc": "^3.3.0",
55
- "@eslint/js": "^9.21.0",
56
+ "@biomejs/biome": "^2.6.10",
56
57
  "@semantic-release/changelog": "^6.0.3",
57
58
  "@semantic-release/git": "^10.0.1",
58
- "@tsconfig/node22": "^22.0.0",
59
- "@types/node": "^22.13.10",
59
+ "@semantic-release/npm": "^13.2.0",
60
+ "@tsconfig/node24": "^24.0.6",
61
+ "@types/node": "^24.19.4",
60
62
  "conventional-changelog-conventionalcommits": "^8.0.0",
61
- "eslint": "^9.21.0",
62
- "eslint-config-prettier": "^10.1.0",
63
- "eslint-plugin-import": "^2.31.0",
64
- "eslint-plugin-prefer-arrow": "^1.2.3",
65
- "eslint-plugin-prettier": "^5.2.3",
66
- "prettier": "^3.5.3",
67
- "semantic-release": "^24.2.3",
68
- "ts-node": "^10.9.2",
69
- "typescript": "^5.8.2",
70
- "typescript-eslint": "^8.27.0"
63
+ "semantic-release": "^25.0.9",
64
+ "typescript": "^7.0.2"
65
+ },
66
+ "engines": {
67
+ "node": ">=24"
68
+ },
69
+ "peerDependencies": {
70
+ "typescript": ">=7.0.0"
71
71
  }
72
72
  }