@fgiova/cjs-esm-ts-builder 2.0.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 +1 -1
- package/dist/cjs/cjs/compile.js +50 -2
- package/dist/cjs/cjs/index.d.ts +1 -1
- package/dist/cjs/esm/cjs-to-esm.js +10 -34
- package/dist/cjs/esm/index.d.ts +1 -1
- package/dist/cjs/specifiers/index.d.ts +4 -0
- package/dist/cjs/specifiers/index.js +29 -0
- package/dist/esm/cjs/compile.mjs +15 -0
- package/dist/esm/esm/cjs-to-esm.mjs +10 -34
- package/dist/esm/specifiers/index.d.mts +4 -0
- package/dist/esm/specifiers/index.mjs +24 -0
- package/package.json +2 -1
package/README.md
CHANGED
|
@@ -93,7 +93,7 @@ build(options)
|
|
|
93
93
|
|
|
94
94
|
## Configure tsconfig.json
|
|
95
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)
|
|
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
97
|
- ESM: `--module esnext --moduleResolution bundler`, then rewrites relative imports to `.mjs`
|
|
98
98
|
|
|
99
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.
|
package/dist/cjs/cjs/compile.js
CHANGED
|
@@ -1,8 +1,55 @@
|
|
|
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;
|
|
37
|
+
const fs = __importStar(require("node:fs/promises"));
|
|
38
|
+
const path = __importStar(require("node:path"));
|
|
4
39
|
const index_ts_1 = require("../command/index.js");
|
|
5
|
-
const index_ts_2 = require("../
|
|
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}`,
|
|
@@ -15,7 +62,8 @@ const compile = async (outDir, target) => {
|
|
|
15
62
|
const commandLine = `npx tsc ${options}`;
|
|
16
63
|
const command = new index_ts_1.Command(commandLine);
|
|
17
64
|
await command.getPromise();
|
|
18
|
-
await (0,
|
|
65
|
+
await (0, index_ts_3.toDts)(outDir);
|
|
66
|
+
await rewriteDeclarations(outDir);
|
|
19
67
|
console.log("Compiling to CJS completed.");
|
|
20
68
|
};
|
|
21
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";
|
|
@@ -36,11 +36,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
36
36
|
exports.toEsm = void 0;
|
|
37
37
|
const fs = __importStar(require("node:fs/promises"));
|
|
38
38
|
const path = __importStar(require("node:path"));
|
|
39
|
+
const index_ts_1 = require("../specifiers/index.js");
|
|
39
40
|
const relativePath = async (sourceFile, targetFile) => {
|
|
40
|
-
targetFile = targetFile
|
|
41
|
-
.trim()
|
|
42
|
-
.slice(1, targetFile.length - 1)
|
|
43
|
-
.replace(/\.[jt]s$/, "");
|
|
44
41
|
const sourceDir = path.dirname(sourceFile);
|
|
45
42
|
const targetElement = path.resolve(sourceDir, `${targetFile}`);
|
|
46
43
|
try {
|
|
@@ -70,44 +67,23 @@ const relativePath = async (sourceFile, targetFile) => {
|
|
|
70
67
|
return null;
|
|
71
68
|
}
|
|
72
69
|
};
|
|
73
|
-
const
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
if (match === null) {
|
|
78
|
-
return content;
|
|
79
|
-
}
|
|
80
|
-
const importDeclaration = match[1];
|
|
81
|
-
const importModule = await relativePath(sourceFile, importDeclaration);
|
|
82
|
-
if (!importModule) {
|
|
83
|
-
continue;
|
|
84
|
-
}
|
|
85
|
-
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;
|
|
86
74
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
const pattern = /(export|import)(.*) from ("(.*)");/g;
|
|
90
|
-
while (true) {
|
|
91
|
-
const match = pattern.exec(content);
|
|
92
|
-
if (match === null) {
|
|
93
|
-
return content;
|
|
94
|
-
}
|
|
95
|
-
const declaration = match[3];
|
|
96
|
-
const importModule = await relativePath(sourceFile, declaration);
|
|
97
|
-
if (!importModule) {
|
|
98
|
-
continue;
|
|
99
|
-
}
|
|
100
|
-
content = content.replace(declaration, `"${importModule}.mjs"`);
|
|
75
|
+
if (explicit.endsWith(".js")) {
|
|
76
|
+
return `${explicit.slice(0, -3)}.mjs`;
|
|
101
77
|
}
|
|
78
|
+
const target = await relativePath(sourceFile, explicit);
|
|
79
|
+
return target ? `${target}.mjs` : null;
|
|
102
80
|
};
|
|
103
81
|
const replaceDirname = (content) => {
|
|
104
82
|
return content
|
|
105
83
|
.replaceAll(/(?<!\))__dirname(?!\()/g, "import.meta.dirname")
|
|
106
84
|
.replaceAll(/(?<!\))__filename(?!\()/g, "import.meta.filename");
|
|
107
85
|
};
|
|
108
|
-
const replaceDeclarations =
|
|
109
|
-
return await replaceInlineImports(await replaceExportImportDeclarations(replaceDirname(content), sourceFile), sourceFile);
|
|
110
|
-
};
|
|
86
|
+
const replaceDeclarations = (content, sourceFile) => (0, index_ts_1.rewriteSpecifiers)(replaceDirname(content), (specifier) => toEsmSpecifier(specifier, sourceFile));
|
|
111
87
|
const fileIsProcessable = (sourcepath) => {
|
|
112
88
|
const extname = path.extname(sourcepath);
|
|
113
89
|
return [".js", ".ts"].includes(extname);
|
package/dist/cjs/esm/index.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export * as Builder from "./compile.
|
|
1
|
+
export * as Builder from "./compile.js";
|
|
@@ -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;
|
package/dist/esm/cjs/compile.mjs
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
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}`,
|
|
@@ -13,5 +27,6 @@ export const compile = async (outDir, target) => {
|
|
|
13
27
|
const command = new Command(commandLine);
|
|
14
28
|
await command.getPromise();
|
|
15
29
|
await toDts(outDir);
|
|
30
|
+
await rewriteDeclarations(outDir);
|
|
16
31
|
console.log("Compiling to CJS completed.");
|
|
17
32
|
};
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import * as fs from "node:fs/promises";
|
|
2
2
|
import * as path from "node:path";
|
|
3
|
+
import { rewriteSpecifiers, toJsSpecifier } from "../specifiers/index.mjs";
|
|
3
4
|
const relativePath = async (sourceFile, targetFile) => {
|
|
4
|
-
targetFile = targetFile
|
|
5
|
-
.trim()
|
|
6
|
-
.slice(1, targetFile.length - 1)
|
|
7
|
-
.replace(/\.[jt]s$/, "");
|
|
8
5
|
const sourceDir = path.dirname(sourceFile);
|
|
9
6
|
const targetElement = path.resolve(sourceDir, `${targetFile}`);
|
|
10
7
|
try {
|
|
@@ -34,44 +31,23 @@ const relativePath = async (sourceFile, targetFile) => {
|
|
|
34
31
|
return null;
|
|
35
32
|
}
|
|
36
33
|
};
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
if (match === null) {
|
|
42
|
-
return content;
|
|
43
|
-
}
|
|
44
|
-
const importDeclaration = match[1];
|
|
45
|
-
const importModule = await relativePath(sourceFile, importDeclaration);
|
|
46
|
-
if (!importModule) {
|
|
47
|
-
continue;
|
|
48
|
-
}
|
|
49
|
-
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;
|
|
50
38
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
const pattern = /(export|import)(.*) from ("(.*)");/g;
|
|
54
|
-
while (true) {
|
|
55
|
-
const match = pattern.exec(content);
|
|
56
|
-
if (match === null) {
|
|
57
|
-
return content;
|
|
58
|
-
}
|
|
59
|
-
const declaration = match[3];
|
|
60
|
-
const importModule = await relativePath(sourceFile, declaration);
|
|
61
|
-
if (!importModule) {
|
|
62
|
-
continue;
|
|
63
|
-
}
|
|
64
|
-
content = content.replace(declaration, `"${importModule}.mjs"`);
|
|
39
|
+
if (explicit.endsWith(".js")) {
|
|
40
|
+
return `${explicit.slice(0, -3)}.mjs`;
|
|
65
41
|
}
|
|
42
|
+
const target = await relativePath(sourceFile, explicit);
|
|
43
|
+
return target ? `${target}.mjs` : null;
|
|
66
44
|
};
|
|
67
45
|
const replaceDirname = (content) => {
|
|
68
46
|
return content
|
|
69
47
|
.replaceAll(/(?<!\))__dirname(?!\()/g, "import.meta.dirname")
|
|
70
48
|
.replaceAll(/(?<!\))__filename(?!\()/g, "import.meta.filename");
|
|
71
49
|
};
|
|
72
|
-
const replaceDeclarations =
|
|
73
|
-
return await replaceInlineImports(await replaceExportImportDeclarations(replaceDirname(content), sourceFile), sourceFile);
|
|
74
|
-
};
|
|
50
|
+
const replaceDeclarations = (content, sourceFile) => rewriteSpecifiers(replaceDirname(content), (specifier) => toEsmSpecifier(specifier, sourceFile));
|
|
75
51
|
const fileIsProcessable = (sourcepath) => {
|
|
76
52
|
const extname = path.extname(sourcepath);
|
|
77
53
|
return [".js", ".ts"].includes(extname);
|
|
@@ -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");
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fgiova/cjs-esm-ts-builder",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1",
|
|
4
4
|
"description": "Build tool for CJS and ESM modules in TypeScript",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -45,6 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"build": "node build.ts",
|
|
48
|
+
"test": "node --test",
|
|
48
49
|
"lint": "biome ci",
|
|
49
50
|
"fix": "biome check --write"
|
|
50
51
|
},
|