@fgiova/cjs-esm-ts-builder 1.2.0 → 2.0.1
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 +55 -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 +21 -40
- 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/specifiers/index.d.ts +4 -0
- package/dist/cjs/specifiers/index.js +29 -0
- package/dist/cjs/type-only-files/index.js +3 -4
- package/dist/esm/cjs/compile.mjs +18 -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 +21 -40
- package/dist/esm/esm/compile.mjs +2 -2
- package/dist/esm/index.mjs +5 -5
- package/dist/esm/specifiers/index.d.mts +4 -0
- package/dist/esm/specifiers/index.mjs +24 -0
- package/dist/esm/type-only-files/index.mjs +3 -4
- package/package.json +17 -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), then rewrites relative `.ts`/`.mts`/`.cts` imports in the declaration files to `.js`/`.mjs`/`.cjs`
|
|
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,69 @@
|
|
|
1
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
|
+
})();
|
|
2
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
36
|
exports.compile = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
37
|
+
const fs = __importStar(require("node:fs/promises"));
|
|
38
|
+
const path = __importStar(require("node:path"));
|
|
39
|
+
const index_ts_1 = require("../command/index.js");
|
|
40
|
+
const index_ts_2 = require("../specifiers/index.js");
|
|
41
|
+
const index_ts_3 = require("../type-only-files/index.js");
|
|
42
|
+
const rewriteDeclarations = async (outDir) => {
|
|
43
|
+
const files = await fs.readdir(outDir, { recursive: true });
|
|
44
|
+
for (const file of files.filter((f) => /\.d\.[cm]?ts$/.test(f))) {
|
|
45
|
+
const filePath = path.join(outDir, file);
|
|
46
|
+
const content = await fs.readFile(filePath, "utf-8");
|
|
47
|
+
const rewritten = await (0, index_ts_2.rewriteSpecifiers)(content, index_ts_2.toJsSpecifier);
|
|
48
|
+
if (rewritten !== content) {
|
|
49
|
+
await fs.writeFile(filePath, rewritten);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
};
|
|
6
53
|
const compile = async (outDir, target) => {
|
|
7
54
|
const options = [
|
|
8
55
|
`--outDir ${outDir}`,
|
|
9
56
|
`--target ${target}`,
|
|
10
|
-
"--module
|
|
11
|
-
"--moduleResolution
|
|
57
|
+
"--module commonjs",
|
|
58
|
+
"--moduleResolution bundler",
|
|
59
|
+
"--verbatimModuleSyntax false",
|
|
12
60
|
"--declaration",
|
|
13
61
|
].join(" ");
|
|
14
62
|
const commandLine = `npx tsc ${options}`;
|
|
15
|
-
const command = new
|
|
63
|
+
const command = new index_ts_1.Command(commandLine);
|
|
16
64
|
await command.getPromise();
|
|
17
|
-
await (0,
|
|
65
|
+
await (0, index_ts_3.toDts)(outDir);
|
|
66
|
+
await rewriteDeclarations(outDir);
|
|
18
67
|
console.log("Compiling to CJS completed.");
|
|
19
68
|
};
|
|
20
69
|
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.js";
|
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,10 @@ 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"));
|
|
39
|
+
const index_ts_1 = require("../specifiers/index.js");
|
|
40
40
|
const relativePath = async (sourceFile, targetFile) => {
|
|
41
|
-
targetFile = targetFile.trim().slice(1, targetFile.length - 1).replace(".js", "");
|
|
42
41
|
const sourceDir = path.dirname(sourceFile);
|
|
43
42
|
const targetElement = path.resolve(sourceDir, `${targetFile}`);
|
|
44
43
|
try {
|
|
@@ -48,7 +47,7 @@ const relativePath = async (sourceFile, targetFile) => {
|
|
|
48
47
|
}
|
|
49
48
|
return null;
|
|
50
49
|
}
|
|
51
|
-
catch (
|
|
50
|
+
catch (_e) { }
|
|
52
51
|
try {
|
|
53
52
|
const stat = await fs.stat(`${targetElement}.js`);
|
|
54
53
|
if (stat.isFile()) {
|
|
@@ -56,7 +55,7 @@ const relativePath = async (sourceFile, targetFile) => {
|
|
|
56
55
|
}
|
|
57
56
|
return null;
|
|
58
57
|
}
|
|
59
|
-
catch (
|
|
58
|
+
catch (_e) { }
|
|
60
59
|
try {
|
|
61
60
|
const stat = await fs.stat(`${targetElement}.mjs`);
|
|
62
61
|
if (stat.isFile()) {
|
|
@@ -64,55 +63,37 @@ const relativePath = async (sourceFile, targetFile) => {
|
|
|
64
63
|
}
|
|
65
64
|
return null;
|
|
66
65
|
}
|
|
67
|
-
catch (
|
|
66
|
+
catch (_e) {
|
|
68
67
|
return null;
|
|
69
68
|
}
|
|
70
69
|
};
|
|
71
|
-
const
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
if (match === null) {
|
|
76
|
-
return content;
|
|
77
|
-
}
|
|
78
|
-
const importDeclaration = match[1];
|
|
79
|
-
const importModule = await relativePath(sourceFile, importDeclaration);
|
|
80
|
-
if (!importModule) {
|
|
81
|
-
continue;
|
|
82
|
-
}
|
|
83
|
-
content = content.replace(importDeclaration, `"${importModule}.mjs"`);
|
|
70
|
+
const toEsmSpecifier = async (specifier, sourceFile) => {
|
|
71
|
+
const explicit = (0, index_ts_1.toJsSpecifier)(specifier) ?? specifier;
|
|
72
|
+
if (/\.[cm]js$/.test(explicit)) {
|
|
73
|
+
return explicit === specifier ? null : explicit;
|
|
84
74
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
const pattern = /(export|import)(.*) from ("(.*)");/g;
|
|
88
|
-
while (true) {
|
|
89
|
-
const match = pattern.exec(content);
|
|
90
|
-
if (match === null) {
|
|
91
|
-
return content;
|
|
92
|
-
}
|
|
93
|
-
const declaration = match[3];
|
|
94
|
-
const importModule = await relativePath(sourceFile, declaration);
|
|
95
|
-
if (!importModule) {
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
content = content.replace(declaration, `"${importModule}.mjs"`);
|
|
75
|
+
if (explicit.endsWith(".js")) {
|
|
76
|
+
return `${explicit.slice(0, -3)}.mjs`;
|
|
99
77
|
}
|
|
78
|
+
const target = await relativePath(sourceFile, explicit);
|
|
79
|
+
return target ? `${target}.mjs` : null;
|
|
100
80
|
};
|
|
101
81
|
const replaceDirname = (content) => {
|
|
102
|
-
return content
|
|
82
|
+
return content
|
|
83
|
+
.replaceAll(/(?<!\))__dirname(?!\()/g, "import.meta.dirname")
|
|
103
84
|
.replaceAll(/(?<!\))__filename(?!\()/g, "import.meta.filename");
|
|
104
85
|
};
|
|
105
|
-
const replaceDeclarations =
|
|
106
|
-
return await replaceInlineImports(await replaceExportImportDeclarations(replaceDirname(content), sourceFile), sourceFile);
|
|
107
|
-
};
|
|
86
|
+
const replaceDeclarations = (content, sourceFile) => (0, index_ts_1.rewriteSpecifiers)(replaceDirname(content), (specifier) => toEsmSpecifier(specifier, sourceFile));
|
|
108
87
|
const fileIsProcessable = (sourcepath) => {
|
|
109
88
|
const extname = path.extname(sourcepath);
|
|
110
89
|
return [".js", ".ts"].includes(extname);
|
|
111
90
|
};
|
|
112
91
|
const esmExtension = (extension) => {
|
|
113
|
-
return
|
|
114
|
-
|
|
115
|
-
|
|
92
|
+
return extension === ".js"
|
|
93
|
+
? ".mjs"
|
|
94
|
+
: extension === ".ts"
|
|
95
|
+
? ".mts"
|
|
96
|
+
: extension;
|
|
116
97
|
};
|
|
117
98
|
const processFile = async (sourceFile) => {
|
|
118
99
|
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.js";
|
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
|
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.toJsSpecifier = exports.rewriteSpecifiers = void 0;
|
|
4
|
+
// import/export ... from "x", import "x", import("x"), /// <reference path="x" />
|
|
5
|
+
const SPECIFIER = /((?<![\w$.])(?:import|export)\b[^"'\n;]*?\bfrom\s*|(?<![\w$.])import\s*\(?\s*|\/\/\/\s*<reference\s+path\s*=\s*)(["'])([^"'\n]+)\2/g;
|
|
6
|
+
const rewriteSpecifiers = async (content, rewrite) => {
|
|
7
|
+
let result = "";
|
|
8
|
+
let last = 0;
|
|
9
|
+
for (const match of content.matchAll(SPECIFIER)) {
|
|
10
|
+
const specifier = match[3];
|
|
11
|
+
if (!/^\.\.?\//.test(specifier)) {
|
|
12
|
+
continue;
|
|
13
|
+
}
|
|
14
|
+
const replacement = await rewrite(specifier);
|
|
15
|
+
if (replacement === null) {
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
const start = match.index + match[1].length + 1;
|
|
19
|
+
result += content.slice(last, start) + replacement;
|
|
20
|
+
last = start + specifier.length;
|
|
21
|
+
}
|
|
22
|
+
return result + content.slice(last);
|
|
23
|
+
};
|
|
24
|
+
exports.rewriteSpecifiers = rewriteSpecifiers;
|
|
25
|
+
// .ts -> .js, .mts -> .mjs, .cts -> .cjs; declaration files are left alone
|
|
26
|
+
const toJsSpecifier = (specifier) => /\.d\.[cm]?ts$/.test(specifier) || !/\.[cm]?ts$/.test(specifier)
|
|
27
|
+
? null
|
|
28
|
+
: specifier.replace(/\.([cm]?)ts$/, ".$1js");
|
|
29
|
+
exports.toJsSpecifier = toJsSpecifier;
|
|
@@ -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
|
@@ -1,16 +1,32 @@
|
|
|
1
|
+
import * as fs from "node:fs/promises";
|
|
2
|
+
import * as path from "node:path";
|
|
1
3
|
import { Command } from "../command/index.mjs";
|
|
4
|
+
import { rewriteSpecifiers, toJsSpecifier } from "../specifiers/index.mjs";
|
|
2
5
|
import { toDts } from "../type-only-files/index.mjs";
|
|
6
|
+
const rewriteDeclarations = async (outDir) => {
|
|
7
|
+
const files = await fs.readdir(outDir, { recursive: true });
|
|
8
|
+
for (const file of files.filter((f) => /\.d\.[cm]?ts$/.test(f))) {
|
|
9
|
+
const filePath = path.join(outDir, file);
|
|
10
|
+
const content = await fs.readFile(filePath, "utf-8");
|
|
11
|
+
const rewritten = await rewriteSpecifiers(content, toJsSpecifier);
|
|
12
|
+
if (rewritten !== content) {
|
|
13
|
+
await fs.writeFile(filePath, rewritten);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
};
|
|
3
17
|
export const compile = async (outDir, target) => {
|
|
4
18
|
const options = [
|
|
5
19
|
`--outDir ${outDir}`,
|
|
6
20
|
`--target ${target}`,
|
|
7
|
-
"--module
|
|
8
|
-
"--moduleResolution
|
|
21
|
+
"--module commonjs",
|
|
22
|
+
"--moduleResolution bundler",
|
|
23
|
+
"--verbatimModuleSyntax false",
|
|
9
24
|
"--declaration",
|
|
10
25
|
].join(" ");
|
|
11
26
|
const commandLine = `npx tsc ${options}`;
|
|
12
27
|
const command = new Command(commandLine);
|
|
13
28
|
await command.getPromise();
|
|
14
29
|
await toDts(outDir);
|
|
30
|
+
await rewriteDeclarations(outDir);
|
|
15
31
|
console.log("Compiling to CJS completed.");
|
|
16
32
|
};
|
|
@@ -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,7 @@
|
|
|
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";
|
|
3
|
+
import { rewriteSpecifiers, toJsSpecifier } from "../specifiers/index.mjs";
|
|
4
4
|
const relativePath = async (sourceFile, targetFile) => {
|
|
5
|
-
targetFile = targetFile.trim().slice(1, targetFile.length - 1).replace(".js", "");
|
|
6
5
|
const sourceDir = path.dirname(sourceFile);
|
|
7
6
|
const targetElement = path.resolve(sourceDir, `${targetFile}`);
|
|
8
7
|
try {
|
|
@@ -12,7 +11,7 @@ const relativePath = async (sourceFile, targetFile) => {
|
|
|
12
11
|
}
|
|
13
12
|
return null;
|
|
14
13
|
}
|
|
15
|
-
catch (
|
|
14
|
+
catch (_e) { }
|
|
16
15
|
try {
|
|
17
16
|
const stat = await fs.stat(`${targetElement}.js`);
|
|
18
17
|
if (stat.isFile()) {
|
|
@@ -20,7 +19,7 @@ const relativePath = async (sourceFile, targetFile) => {
|
|
|
20
19
|
}
|
|
21
20
|
return null;
|
|
22
21
|
}
|
|
23
|
-
catch (
|
|
22
|
+
catch (_e) { }
|
|
24
23
|
try {
|
|
25
24
|
const stat = await fs.stat(`${targetElement}.mjs`);
|
|
26
25
|
if (stat.isFile()) {
|
|
@@ -28,55 +27,37 @@ const relativePath = async (sourceFile, targetFile) => {
|
|
|
28
27
|
}
|
|
29
28
|
return null;
|
|
30
29
|
}
|
|
31
|
-
catch (
|
|
30
|
+
catch (_e) {
|
|
32
31
|
return null;
|
|
33
32
|
}
|
|
34
33
|
};
|
|
35
|
-
const
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (match === null) {
|
|
40
|
-
return content;
|
|
41
|
-
}
|
|
42
|
-
const importDeclaration = match[1];
|
|
43
|
-
const importModule = await relativePath(sourceFile, importDeclaration);
|
|
44
|
-
if (!importModule) {
|
|
45
|
-
continue;
|
|
46
|
-
}
|
|
47
|
-
content = content.replace(importDeclaration, `"${importModule}.mjs"`);
|
|
34
|
+
const toEsmSpecifier = async (specifier, sourceFile) => {
|
|
35
|
+
const explicit = toJsSpecifier(specifier) ?? specifier;
|
|
36
|
+
if (/\.[cm]js$/.test(explicit)) {
|
|
37
|
+
return explicit === specifier ? null : explicit;
|
|
48
38
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
const pattern = /(export|import)(.*) from ("(.*)");/g;
|
|
52
|
-
while (true) {
|
|
53
|
-
const match = pattern.exec(content);
|
|
54
|
-
if (match === null) {
|
|
55
|
-
return content;
|
|
56
|
-
}
|
|
57
|
-
const declaration = match[3];
|
|
58
|
-
const importModule = await relativePath(sourceFile, declaration);
|
|
59
|
-
if (!importModule) {
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
content = content.replace(declaration, `"${importModule}.mjs"`);
|
|
39
|
+
if (explicit.endsWith(".js")) {
|
|
40
|
+
return `${explicit.slice(0, -3)}.mjs`;
|
|
63
41
|
}
|
|
42
|
+
const target = await relativePath(sourceFile, explicit);
|
|
43
|
+
return target ? `${target}.mjs` : null;
|
|
64
44
|
};
|
|
65
45
|
const replaceDirname = (content) => {
|
|
66
|
-
return content
|
|
46
|
+
return content
|
|
47
|
+
.replaceAll(/(?<!\))__dirname(?!\()/g, "import.meta.dirname")
|
|
67
48
|
.replaceAll(/(?<!\))__filename(?!\()/g, "import.meta.filename");
|
|
68
49
|
};
|
|
69
|
-
const replaceDeclarations =
|
|
70
|
-
return await replaceInlineImports(await replaceExportImportDeclarations(replaceDirname(content), sourceFile), sourceFile);
|
|
71
|
-
};
|
|
50
|
+
const replaceDeclarations = (content, sourceFile) => rewriteSpecifiers(replaceDirname(content), (specifier) => toEsmSpecifier(specifier, sourceFile));
|
|
72
51
|
const fileIsProcessable = (sourcepath) => {
|
|
73
52
|
const extname = path.extname(sourcepath);
|
|
74
53
|
return [".js", ".ts"].includes(extname);
|
|
75
54
|
};
|
|
76
55
|
const esmExtension = (extension) => {
|
|
77
|
-
return
|
|
78
|
-
|
|
79
|
-
|
|
56
|
+
return extension === ".js"
|
|
57
|
+
? ".mjs"
|
|
58
|
+
: extension === ".ts"
|
|
59
|
+
? ".mts"
|
|
60
|
+
: extension;
|
|
80
61
|
};
|
|
81
62
|
const processFile = async (sourceFile) => {
|
|
82
63
|
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
|
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// import/export ... from "x", import "x", import("x"), /// <reference path="x" />
|
|
2
|
+
const SPECIFIER = /((?<![\w$.])(?:import|export)\b[^"'\n;]*?\bfrom\s*|(?<![\w$.])import\s*\(?\s*|\/\/\/\s*<reference\s+path\s*=\s*)(["'])([^"'\n]+)\2/g;
|
|
3
|
+
export const rewriteSpecifiers = async (content, rewrite) => {
|
|
4
|
+
let result = "";
|
|
5
|
+
let last = 0;
|
|
6
|
+
for (const match of content.matchAll(SPECIFIER)) {
|
|
7
|
+
const specifier = match[3];
|
|
8
|
+
if (!/^\.\.?\//.test(specifier)) {
|
|
9
|
+
continue;
|
|
10
|
+
}
|
|
11
|
+
const replacement = await rewrite(specifier);
|
|
12
|
+
if (replacement === null) {
|
|
13
|
+
continue;
|
|
14
|
+
}
|
|
15
|
+
const start = match.index + match[1].length + 1;
|
|
16
|
+
result += content.slice(last, start) + replacement;
|
|
17
|
+
last = start + specifier.length;
|
|
18
|
+
}
|
|
19
|
+
return result + content.slice(last);
|
|
20
|
+
};
|
|
21
|
+
// .ts -> .js, .mts -> .mjs, .cts -> .cjs; declaration files are left alone
|
|
22
|
+
export const toJsSpecifier = (specifier) => /\.d\.[cm]?ts$/.test(specifier) || !/\.[cm]?ts$/.test(specifier)
|
|
23
|
+
? null
|
|
24
|
+
: specifier.replace(/\.([cm]?)ts$/, ".$1js");
|
|
@@ -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.1",
|
|
4
4
|
"description": "Build tool for CJS and ESM modules in TypeScript",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -44,32 +44,30 @@
|
|
|
44
44
|
"esmBuilder": "./builder.mjs"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
|
-
"build": "
|
|
48
|
-
"
|
|
47
|
+
"build": "node build.ts",
|
|
48
|
+
"test": "node --test",
|
|
49
|
+
"lint": "biome ci",
|
|
50
|
+
"fix": "biome check --write"
|
|
49
51
|
},
|
|
50
52
|
"dependencies": {
|
|
51
53
|
"arg": "^5.0.2",
|
|
52
54
|
"get-tsconfig": "^4.10.0"
|
|
53
55
|
},
|
|
54
56
|
"devDependencies": {
|
|
55
|
-
"@
|
|
56
|
-
"@eslint/eslintrc": "^3.3.0",
|
|
57
|
-
"@eslint/js": "^9.21.0",
|
|
57
|
+
"@biomejs/biome": "^2.6.10",
|
|
58
58
|
"@semantic-release/changelog": "^6.0.3",
|
|
59
59
|
"@semantic-release/git": "^10.0.1",
|
|
60
|
-
"@
|
|
61
|
-
"@tsconfig/
|
|
62
|
-
"@types/node": "^
|
|
60
|
+
"@semantic-release/npm": "^13.2.0",
|
|
61
|
+
"@tsconfig/node24": "^24.0.6",
|
|
62
|
+
"@types/node": "^24.19.4",
|
|
63
63
|
"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"
|
|
64
|
+
"semantic-release": "^25.0.9",
|
|
65
|
+
"typescript": "^7.0.2"
|
|
66
|
+
},
|
|
67
|
+
"engines": {
|
|
68
|
+
"node": ">=24"
|
|
69
|
+
},
|
|
70
|
+
"peerDependencies": {
|
|
71
|
+
"typescript": ">=7.0.0"
|
|
74
72
|
}
|
|
75
73
|
}
|