@fgiova/cjs-esm-ts-builder 1.2.0 → 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 +38 -3
- package/builder.mjs +11 -9
- package/dist/cjs/cjs/compile.js +7 -6
- package/dist/cjs/cjs/index.d.ts +1 -1
- package/dist/cjs/cjs/index.js +1 -1
- package/dist/cjs/command/index.d.ts +3 -3
- package/dist/cjs/command/index.js +5 -11
- package/dist/cjs/esm/cjs-to-esm.js +15 -10
- package/dist/cjs/esm/compile.js +7 -7
- package/dist/cjs/esm/index.d.ts +1 -1
- package/dist/cjs/esm/index.js +1 -1
- package/dist/cjs/index.js +5 -5
- package/dist/cjs/type-only-files/index.js +3 -4
- package/dist/esm/cjs/compile.mjs +3 -2
- package/dist/esm/command/index.d.mts +3 -3
- package/dist/esm/command/index.mjs +5 -11
- package/dist/esm/esm/cjs-to-esm.mjs +15 -10
- package/dist/esm/esm/compile.mjs +2 -2
- package/dist/esm/index.mjs +5 -5
- package/dist/esm/type-only-files/index.mjs +3 -4
- package/package.json +16 -19
package/README.md
CHANGED
|
@@ -86,9 +86,44 @@ build(options)
|
|
|
86
86
|
- `buildFolder`: The folder where the build files will be generated. Default is `dist`.
|
|
87
87
|
|
|
88
88
|
|
|
89
|
-
##
|
|
90
|
-
-
|
|
91
|
-
-
|
|
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.
|
|
92
127
|
|
|
93
128
|
## License
|
|
94
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
|
-
})
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
+
});
|
package/dist/cjs/cjs/compile.js
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.compile = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const index_ts_1 = require("../command/index.js");
|
|
5
|
+
const index_ts_2 = require("../type-only-files/index.js");
|
|
6
6
|
const compile = async (outDir, target) => {
|
|
7
7
|
const options = [
|
|
8
8
|
`--outDir ${outDir}`,
|
|
9
9
|
`--target ${target}`,
|
|
10
|
-
"--module
|
|
11
|
-
"--moduleResolution
|
|
10
|
+
"--module commonjs",
|
|
11
|
+
"--moduleResolution bundler",
|
|
12
|
+
"--verbatimModuleSyntax false",
|
|
12
13
|
"--declaration",
|
|
13
14
|
].join(" ");
|
|
14
15
|
const commandLine = `npx tsc ${options}`;
|
|
15
|
-
const command = new
|
|
16
|
+
const command = new index_ts_1.Command(commandLine);
|
|
16
17
|
await command.getPromise();
|
|
17
|
-
await (0,
|
|
18
|
+
await (0, index_ts_2.toDts)(outDir);
|
|
18
19
|
console.log("Compiling to CJS completed.");
|
|
19
20
|
};
|
|
20
21
|
exports.compile = compile;
|
package/dist/cjs/cjs/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * as Builder from "./compile";
|
|
1
|
+
export * as Builder from "./compile.ts";
|
package/dist/cjs/cjs/index.js
CHANGED
|
@@ -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
|
-
|
|
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
|
-
"
|
|
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
|
|
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 (
|
|
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 (
|
|
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 (
|
|
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
|
|
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
|
|
114
|
-
|
|
115
|
-
|
|
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)) {
|
package/dist/cjs/esm/compile.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.compile = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
const
|
|
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");
|
|
7
7
|
const compile = async (outDir, target) => {
|
|
8
8
|
const options = [
|
|
9
9
|
`--outDir ${outDir}`,
|
|
10
10
|
`--target ${target}`,
|
|
11
11
|
"--module esnext",
|
|
12
12
|
"--declaration",
|
|
13
|
-
"--moduleResolution
|
|
13
|
+
"--moduleResolution bundler",
|
|
14
14
|
].join(" ");
|
|
15
15
|
const commandLine = `npx tsc ${options}`;
|
|
16
|
-
const command = new
|
|
16
|
+
const command = new index_ts_1.Command(commandLine);
|
|
17
17
|
await command.getPromise();
|
|
18
|
-
await (0,
|
|
18
|
+
await (0, index_ts_2.toDts)(outDir);
|
|
19
19
|
console.log("Compiling to ESM completed.");
|
|
20
|
-
await (0,
|
|
20
|
+
await (0, cjs_to_esm_ts_1.toEsm)(outDir);
|
|
21
21
|
console.log("Converting to ESM completed.");
|
|
22
22
|
};
|
|
23
23
|
exports.compile = compile;
|
package/dist/cjs/esm/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * as Builder from "./compile";
|
|
1
|
+
export * as Builder from "./compile.ts";
|
package/dist/cjs/esm/index.js
CHANGED
|
@@ -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
|
};
|
|
@@ -34,10 +34,9 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
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
37
|
const fs = __importStar(require("node:fs/promises"));
|
|
38
|
+
const path = __importStar(require("node:path"));
|
|
39
|
+
const get_tsconfig_1 = require("get-tsconfig");
|
|
41
40
|
let baseSourcePath = "";
|
|
42
41
|
const processDirectory = async (directorySource, directoryDestination) => {
|
|
43
42
|
const files = await fs.readdir(directorySource);
|
|
@@ -65,7 +64,7 @@ const processElement = async (sourcePath, destinationPath) => {
|
|
|
65
64
|
try {
|
|
66
65
|
await fs.access(newDestination);
|
|
67
66
|
}
|
|
68
|
-
catch (
|
|
67
|
+
catch (_e) {
|
|
69
68
|
await fs.mkdir(newDestination, { recursive: true });
|
|
70
69
|
}
|
|
71
70
|
return await processDirectory(sourcePath, newDestination);
|
package/dist/esm/cjs/compile.mjs
CHANGED
|
@@ -4,8 +4,9 @@ export const compile = async (outDir, target) => {
|
|
|
4
4
|
const options = [
|
|
5
5
|
`--outDir ${outDir}`,
|
|
6
6
|
`--target ${target}`,
|
|
7
|
-
"--module
|
|
8
|
-
"--moduleResolution
|
|
7
|
+
"--module commonjs",
|
|
8
|
+
"--moduleResolution bundler",
|
|
9
|
+
"--verbatimModuleSyntax false",
|
|
9
10
|
"--declaration",
|
|
10
11
|
].join(" ");
|
|
11
12
|
const commandLine = `npx tsc ${options}`;
|
|
@@ -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
|
-
|
|
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
|
-
"
|
|
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
|
|
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 (
|
|
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 (
|
|
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 (
|
|
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
|
|
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
|
|
78
|
-
|
|
79
|
-
|
|
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)) {
|
package/dist/esm/esm/compile.mjs
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import { Command } from "../command/index.mjs";
|
|
2
|
-
import { toEsm } from "./cjs-to-esm.mjs";
|
|
3
2
|
import { toDts } from "../type-only-files/index.mjs";
|
|
3
|
+
import { toEsm } from "./cjs-to-esm.mjs";
|
|
4
4
|
export const compile = async (outDir, target) => {
|
|
5
5
|
const options = [
|
|
6
6
|
`--outDir ${outDir}`,
|
|
7
7
|
`--target ${target}`,
|
|
8
8
|
"--module esnext",
|
|
9
9
|
"--declaration",
|
|
10
|
-
"--moduleResolution
|
|
10
|
+
"--moduleResolution bundler",
|
|
11
11
|
].join(" ");
|
|
12
12
|
const commandLine = `npx tsc ${options}`;
|
|
13
13
|
const command = new Command(commandLine);
|
package/dist/esm/index.mjs
CHANGED
|
@@ -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
|
};
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
2
|
-
import { getTsconfig } from "get-tsconfig";
|
|
3
|
-
import * as path from "node:path";
|
|
4
1
|
import * as fs from "node:fs/promises";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import { getTsconfig } from "get-tsconfig";
|
|
5
4
|
let baseSourcePath = "";
|
|
6
5
|
const processDirectory = async (directorySource, directoryDestination) => {
|
|
7
6
|
const files = await fs.readdir(directorySource);
|
|
@@ -29,7 +28,7 @@ const processElement = async (sourcePath, destinationPath) => {
|
|
|
29
28
|
try {
|
|
30
29
|
await fs.access(newDestination);
|
|
31
30
|
}
|
|
32
|
-
catch (
|
|
31
|
+
catch (_e) {
|
|
33
32
|
await fs.mkdir(newDestination, { recursive: true });
|
|
34
33
|
}
|
|
35
34
|
return await processDirectory(sourcePath, newDestination);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgiova/cjs-esm-ts-builder",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Build tool for CJS and ESM modules in TypeScript",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -44,32 +44,29 @@
|
|
|
44
44
|
"esmBuilder": "./builder.mjs"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
|
-
"build": "
|
|
48
|
-
"lint": "
|
|
47
|
+
"build": "node build.ts",
|
|
48
|
+
"lint": "biome ci",
|
|
49
|
+
"fix": "biome check --write"
|
|
49
50
|
},
|
|
50
51
|
"dependencies": {
|
|
51
52
|
"arg": "^5.0.2",
|
|
52
53
|
"get-tsconfig": "^4.10.0"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
55
|
-
"@
|
|
56
|
-
"@eslint/eslintrc": "^3.3.0",
|
|
57
|
-
"@eslint/js": "^9.21.0",
|
|
56
|
+
"@biomejs/biome": "^2.6.10",
|
|
58
57
|
"@semantic-release/changelog": "^6.0.3",
|
|
59
58
|
"@semantic-release/git": "^10.0.1",
|
|
60
|
-
"@
|
|
61
|
-
"@tsconfig/
|
|
62
|
-
"@types/node": "^
|
|
59
|
+
"@semantic-release/npm": "^13.2.0",
|
|
60
|
+
"@tsconfig/node24": "^24.0.6",
|
|
61
|
+
"@types/node": "^24.19.4",
|
|
63
62
|
"conventional-changelog-conventionalcommits": "^8.0.0",
|
|
64
|
-
"
|
|
65
|
-
"
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
"
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
"
|
|
72
|
-
"typescript": "^5.8.2",
|
|
73
|
-
"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"
|
|
74
71
|
}
|
|
75
72
|
}
|