@fgiova/cjs-esm-ts-builder 1.1.0 → 1.2.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": {
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.compile = void 0;
4
4
  const index_1 = require("../command/index");
5
+ const type_only_files_1 = require("../type-only-files");
5
6
  const compile = async (outDir, target) => {
6
7
  const options = [
7
8
  `--outDir ${outDir}`,
@@ -13,6 +14,7 @@ const compile = async (outDir, target) => {
13
14
  const commandLine = `npx tsc ${options}`;
14
15
  const command = new index_1.Command(commandLine);
15
16
  await command.getPromise();
17
+ await (0, type_only_files_1.toDts)(outDir);
16
18
  console.log("Compiling to CJS completed.");
17
19
  };
18
20
  exports.compile = compile;
@@ -99,8 +99,8 @@ const replaceExportImportDeclarations = async (content, sourceFile) => {
99
99
  }
100
100
  };
101
101
  const replaceDirname = (content) => {
102
- return content.replaceAll(/__dirname/g, "import.meta.dirname")
103
- .replaceAll(/__filename/g, "import.meta.filename");
102
+ return content.replaceAll(/(?<!\))__dirname(?!\()/g, "import.meta.dirname")
103
+ .replaceAll(/(?<!\))__filename(?!\()/g, "import.meta.filename");
104
104
  };
105
105
  const replaceDeclarations = async (content, sourceFile) => {
106
106
  return await replaceInlineImports(await replaceExportImportDeclarations(replaceDirname(content), sourceFile), sourceFile);
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.compile = void 0;
4
4
  const command_1 = require("../command");
5
5
  const cjs_to_esm_1 = require("./cjs-to-esm");
6
+ const type_only_files_1 = require("../type-only-files");
6
7
  const compile = async (outDir, target) => {
7
8
  const options = [
8
9
  `--outDir ${outDir}`,
@@ -14,6 +15,7 @@ const compile = async (outDir, target) => {
14
15
  const commandLine = `npx tsc ${options}`;
15
16
  const command = new command_1.Command(commandLine);
16
17
  await command.getPromise();
18
+ await (0, type_only_files_1.toDts)(outDir);
17
19
  console.log("Compiling to ESM completed.");
18
20
  await (0, cjs_to_esm_1.toEsm)(outDir);
19
21
  console.log("Converting to ESM completed.");
@@ -0,0 +1 @@
1
+ export declare const toDts: (destinationDirectory: string) => Promise<void>;
@@ -0,0 +1,93 @@
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
+ /* eslint-disable @typescript-eslint/no-unused-vars */
38
+ const get_tsconfig_1 = require("get-tsconfig");
39
+ const path = __importStar(require("node:path"));
40
+ const fs = __importStar(require("node:fs/promises"));
41
+ let baseSourcePath = "";
42
+ const processDirectory = async (directorySource, directoryDestination) => {
43
+ const files = await fs.readdir(directorySource);
44
+ for (const file of files) {
45
+ const elementPath = path.join(directorySource, file);
46
+ await processElement(elementPath, directoryDestination);
47
+ }
48
+ };
49
+ const fileIsProcessable = (sourcepath) => {
50
+ return sourcepath.endsWith(".d.ts");
51
+ };
52
+ const processFile = async (sourceFile, destinationPath) => {
53
+ if (!fileIsProcessable(sourceFile)) {
54
+ return;
55
+ }
56
+ const basename = path.basename(sourceFile);
57
+ const sourceContent = await fs.readFile(sourceFile, "utf-8");
58
+ const newFile = `${path.join(destinationPath, basename)}`;
59
+ await fs.writeFile(newFile, sourceContent);
60
+ };
61
+ const processElement = async (sourcePath, destinationPath) => {
62
+ const element = await fs.stat(sourcePath);
63
+ if (element.isDirectory()) {
64
+ const newDestination = destinationPath + sourcePath.replace(baseSourcePath, "");
65
+ try {
66
+ await fs.access(newDestination);
67
+ }
68
+ catch (e) {
69
+ await fs.mkdir(newDestination, { recursive: true });
70
+ }
71
+ return await processDirectory(sourcePath, newDestination);
72
+ }
73
+ if (element.isFile()) {
74
+ return await processFile(sourcePath, destinationPath);
75
+ }
76
+ };
77
+ const toDts = async (destinationDirectory) => {
78
+ const tsconfig = (0, get_tsconfig_1.getTsconfig)();
79
+ const sourceDirectory = path.join(tsconfig?.config.compilerOptions?.sourceRoot || "./src");
80
+ const sourceDirectoryStat = await fs.stat(sourceDirectory);
81
+ if (!sourceDirectoryStat.isDirectory()) {
82
+ throw new Error("Source path is not a directory");
83
+ }
84
+ baseSourcePath = sourceDirectory;
85
+ const destinationDirectoryStat = await fs.stat(destinationDirectory);
86
+ if (!destinationDirectoryStat.isDirectory()) {
87
+ throw new Error("Destination path is not a directory");
88
+ }
89
+ destinationDirectory = path.join(destinationDirectory);
90
+ await fs.mkdir(destinationDirectory, { recursive: true });
91
+ await processElement(sourceDirectory, destinationDirectory);
92
+ };
93
+ exports.toDts = toDts;
@@ -1,4 +1,5 @@
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}`,
@@ -10,5 +11,6 @@ export const compile = async (outDir, target) => {
10
11
  const commandLine = `npx tsc ${options}`;
11
12
  const command = new Command(commandLine);
12
13
  await command.getPromise();
14
+ await toDts(outDir);
13
15
  console.log("Compiling to CJS completed.");
14
16
  };
@@ -63,8 +63,8 @@ const replaceExportImportDeclarations = async (content, sourceFile) => {
63
63
  }
64
64
  };
65
65
  const replaceDirname = (content) => {
66
- return content.replaceAll(/import.meta.dirname/g, "import.meta.dirname")
67
- .replaceAll(/import.meta.filename/g, "import.meta.filename");
66
+ return content.replaceAll(/(?<!\))__dirname(?!\()/g, "import.meta.dirname")
67
+ .replaceAll(/(?<!\))__filename(?!\()/g, "import.meta.filename");
68
68
  };
69
69
  const replaceDeclarations = async (content, sourceFile) => {
70
70
  return await replaceInlineImports(await replaceExportImportDeclarations(replaceDirname(content), sourceFile), sourceFile);
@@ -1,5 +1,6 @@
1
1
  import { Command } from "../command/index.mjs";
2
2
  import { toEsm } from "./cjs-to-esm.mjs";
3
+ import { toDts } from "../type-only-files/index.mjs";
3
4
  export const compile = async (outDir, target) => {
4
5
  const options = [
5
6
  `--outDir ${outDir}`,
@@ -11,6 +12,7 @@ export const compile = async (outDir, target) => {
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.");
@@ -0,0 +1 @@
1
+ export declare const toDts: (destinationDirectory: string) => Promise<void>;
@@ -0,0 +1,56 @@
1
+ /* eslint-disable @typescript-eslint/no-unused-vars */
2
+ import { getTsconfig } from "get-tsconfig";
3
+ import * as path from "node:path";
4
+ import * as fs from "node:fs/promises";
5
+ let baseSourcePath = "";
6
+ const processDirectory = async (directorySource, directoryDestination) => {
7
+ const files = await fs.readdir(directorySource);
8
+ for (const file of files) {
9
+ const elementPath = path.join(directorySource, file);
10
+ await processElement(elementPath, directoryDestination);
11
+ }
12
+ };
13
+ const fileIsProcessable = (sourcepath) => {
14
+ return sourcepath.endsWith(".d.ts");
15
+ };
16
+ const processFile = async (sourceFile, destinationPath) => {
17
+ if (!fileIsProcessable(sourceFile)) {
18
+ return;
19
+ }
20
+ const basename = path.basename(sourceFile);
21
+ const sourceContent = await fs.readFile(sourceFile, "utf-8");
22
+ const newFile = `${path.join(destinationPath, basename)}`;
23
+ await fs.writeFile(newFile, sourceContent);
24
+ };
25
+ const processElement = async (sourcePath, destinationPath) => {
26
+ const element = await fs.stat(sourcePath);
27
+ if (element.isDirectory()) {
28
+ const newDestination = destinationPath + sourcePath.replace(baseSourcePath, "");
29
+ try {
30
+ await fs.access(newDestination);
31
+ }
32
+ catch (e) {
33
+ await fs.mkdir(newDestination, { recursive: true });
34
+ }
35
+ return await processDirectory(sourcePath, newDestination);
36
+ }
37
+ if (element.isFile()) {
38
+ return await processFile(sourcePath, destinationPath);
39
+ }
40
+ };
41
+ export const toDts = async (destinationDirectory) => {
42
+ const tsconfig = getTsconfig();
43
+ const sourceDirectory = path.join(tsconfig?.config.compilerOptions?.sourceRoot || "./src");
44
+ const sourceDirectoryStat = await fs.stat(sourceDirectory);
45
+ if (!sourceDirectoryStat.isDirectory()) {
46
+ throw new Error("Source path is not a directory");
47
+ }
48
+ baseSourcePath = sourceDirectory;
49
+ const destinationDirectoryStat = await fs.stat(destinationDirectory);
50
+ if (!destinationDirectoryStat.isDirectory()) {
51
+ throw new Error("Destination path is not a directory");
52
+ }
53
+ destinationDirectory = path.join(destinationDirectory);
54
+ await fs.mkdir(destinationDirectory, { recursive: true });
55
+ await processElement(sourceDirectory, destinationDirectory);
56
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fgiova/cjs-esm-ts-builder",
3
- "version": "1.1.0",
3
+ "version": "1.2.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": {
@@ -47,7 +48,8 @@
47
48
  "lint": "eslint"
48
49
  },
49
50
  "dependencies": {
50
- "arg": "^5.0.2"
51
+ "arg": "^5.0.2",
52
+ "get-tsconfig": "^4.10.0"
51
53
  },
52
54
  "devDependencies": {
53
55
  "@eslint/compat": "^1.2.7",
@@ -55,6 +57,7 @@
55
57
  "@eslint/js": "^9.21.0",
56
58
  "@semantic-release/changelog": "^6.0.3",
57
59
  "@semantic-release/git": "^10.0.1",
60
+ "@stylistic/eslint-plugin-js": "^4.2.0",
58
61
  "@tsconfig/node22": "^22.0.0",
59
62
  "@types/node": "^22.13.10",
60
63
  "conventional-changelog-conventionalcommits": "^8.0.0",