zshy 0.0.1 → 0.0.2
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/LICENSE +21 -0
- package/README.md +356 -0
- package/out/index.cjs +491 -0
- package/out/index.d.cts +2 -0
- package/out/index.d.ts +2 -0
- package/out/index.js +453 -0
- package/out/plugins/a.cjs +4 -0
- package/out/plugins/a.d.cts +1 -0
- package/out/plugins/a.d.ts +1 -0
- package/out/plugins/a.js +1 -0
- package/out/plugins/b.cjs +4 -0
- package/out/plugins/b.d.cts +1 -0
- package/out/plugins/b.d.ts +1 -0
- package/out/plugins/b.js +1 -0
- package/out/utils.cjs +259 -0
- package/out/utils.d.cts +9 -0
- package/out/utils.d.ts +9 -0
- package/out/utils.js +221 -0
- package/package.json +85 -12
- package/index.d.ts +0 -1
- package/index.js +0 -2
package/out/utils.cjs
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
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.getEntryPoints = getEntryPoints;
|
|
37
|
+
exports.readTsconfig = readTsconfig;
|
|
38
|
+
exports.compileProject = compileProject;
|
|
39
|
+
const globby_1 = require("globby");
|
|
40
|
+
const path = __importStar(require("node:path"));
|
|
41
|
+
const ts = __importStar(require("typescript"));
|
|
42
|
+
// Get entry points using the same logic as esbuild.mts
|
|
43
|
+
async function getEntryPoints(patterns) {
|
|
44
|
+
const results = [];
|
|
45
|
+
for (const pattern of patterns) {
|
|
46
|
+
const _results = await (0, globby_1.globby)(pattern, {
|
|
47
|
+
ignore: ["**/*.d.ts"],
|
|
48
|
+
});
|
|
49
|
+
if (!pattern.endsWith("/*") && _results.length === 0) {
|
|
50
|
+
console.error(`❌ File does not exist: ${pattern}`);
|
|
51
|
+
process.exit(1);
|
|
52
|
+
}
|
|
53
|
+
results.push(..._results);
|
|
54
|
+
}
|
|
55
|
+
return results;
|
|
56
|
+
}
|
|
57
|
+
function readTsconfig(tsconfigPath) {
|
|
58
|
+
// Read and parse tsconfig.json
|
|
59
|
+
const configPath = path.resolve(tsconfigPath);
|
|
60
|
+
const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
61
|
+
if (configFile.error) {
|
|
62
|
+
console.error("Error reading tsconfig.json:", ts.formatDiagnostic(configFile.error, {
|
|
63
|
+
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
|
|
64
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
65
|
+
getNewLine: () => ts.sys.newLine,
|
|
66
|
+
}));
|
|
67
|
+
process.exit(1);
|
|
68
|
+
}
|
|
69
|
+
// Parse the config
|
|
70
|
+
const parsedConfig = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.dirname(configPath));
|
|
71
|
+
if (parsedConfig.errors.length > 0) {
|
|
72
|
+
console.error("Error parsing tsconfig.json:");
|
|
73
|
+
for (const error of parsedConfig.errors) {
|
|
74
|
+
console.error(ts.formatDiagnostic(error, {
|
|
75
|
+
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
|
|
76
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
77
|
+
getNewLine: () => ts.sys.newLine,
|
|
78
|
+
}));
|
|
79
|
+
}
|
|
80
|
+
process.exit(1);
|
|
81
|
+
}
|
|
82
|
+
if (!parsedConfig.options) {
|
|
83
|
+
throw new Error("❌ Error reading tsconfig.json#compilerOptions");
|
|
84
|
+
}
|
|
85
|
+
return parsedConfig.options;
|
|
86
|
+
}
|
|
87
|
+
async function compileProject(config, entryPoints) {
|
|
88
|
+
const exts = [];
|
|
89
|
+
// exts.push(config.jsExtension ?? ".js");
|
|
90
|
+
// exts.push(config.dtsExtension ?? ".d.ts");
|
|
91
|
+
// console.log(
|
|
92
|
+
// `🧱 Building${
|
|
93
|
+
// config.mode === "ts"
|
|
94
|
+
// ? ".js/.d.ts"
|
|
95
|
+
// : config.mode === "mts"
|
|
96
|
+
// ? ".mjs/.d.ts"
|
|
97
|
+
// : ".cjs/.d.ts"
|
|
98
|
+
// }...`,
|
|
99
|
+
// );
|
|
100
|
+
// Create compiler host
|
|
101
|
+
const host = ts.createCompilerHost(config.compilerOptions);
|
|
102
|
+
const originalWriteFile = host.writeFile;
|
|
103
|
+
const jsExt = config.mode === "mts" ? ".mjs" : config.mode === "cts" ? ".cjs" : ".js";
|
|
104
|
+
const dtsExt = config.mode === "mts"
|
|
105
|
+
? ".d.mts"
|
|
106
|
+
: config.mode === "cts"
|
|
107
|
+
? ".d.cts"
|
|
108
|
+
: ".d.ts";
|
|
109
|
+
host.writeFile = (fileName, data, writeByteOrderMark, onError, sourceFiles) => {
|
|
110
|
+
// Transform output file extensions
|
|
111
|
+
let outputFileName = fileName;
|
|
112
|
+
const processedData = data;
|
|
113
|
+
// if (config.jsExtension) {
|
|
114
|
+
if (fileName.endsWith(".js")) {
|
|
115
|
+
outputFileName = fileName.replace(/\.js$/, jsExt);
|
|
116
|
+
}
|
|
117
|
+
// }
|
|
118
|
+
// if (config.dtsExtension) {
|
|
119
|
+
if (fileName.endsWith(".d.ts")) {
|
|
120
|
+
outputFileName = fileName.replace(/\.d\.ts$/, dtsExt);
|
|
121
|
+
}
|
|
122
|
+
// }
|
|
123
|
+
// console.log(` ${outputFileName}`);
|
|
124
|
+
if (originalWriteFile) {
|
|
125
|
+
originalWriteFile(outputFileName, processedData, writeByteOrderMark, onError, sourceFiles);
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
// Create the TypeScript program using entry points
|
|
129
|
+
const program = ts.createProgram({
|
|
130
|
+
rootNames: entryPoints,
|
|
131
|
+
options: config.compilerOptions,
|
|
132
|
+
host,
|
|
133
|
+
});
|
|
134
|
+
// Create a transformer factory to rewrite extensions
|
|
135
|
+
const extensionRewriteTransformer = (context) => {
|
|
136
|
+
return (sourceFile) => {
|
|
137
|
+
const visitor = (node) => {
|
|
138
|
+
if (ts.isImportDeclaration(node) &&
|
|
139
|
+
node.moduleSpecifier &&
|
|
140
|
+
ts.isStringLiteral(node.moduleSpecifier)) {
|
|
141
|
+
const originalText = node.moduleSpecifier.text;
|
|
142
|
+
if (originalText.endsWith(".js")) {
|
|
143
|
+
const newText = originalText.slice(0, -3) + jsExt;
|
|
144
|
+
return ts.factory.updateImportDeclaration(node, node.modifiers, node.importClause, ts.factory.createStringLiteral(newText), node.assertClause);
|
|
145
|
+
}
|
|
146
|
+
// if import is extensionless, add .js extension
|
|
147
|
+
if (originalText.startsWith("./") || originalText.startsWith("../")) {
|
|
148
|
+
// console.dir("import", { depth: null });
|
|
149
|
+
// console.dir(originalText, { depth: null });
|
|
150
|
+
const hasExtension = path.extname(originalText) !== "";
|
|
151
|
+
if (!hasExtension) {
|
|
152
|
+
const newText = originalText + jsExt;
|
|
153
|
+
// console.dir(newText, { depth: null });
|
|
154
|
+
return ts.factory.updateImportDeclaration(node, node.modifiers, node.importClause, ts.factory.createStringLiteral(newText), node.assertClause);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
// Handle export declarations
|
|
159
|
+
if (ts.isExportDeclaration(node) &&
|
|
160
|
+
node.moduleSpecifier &&
|
|
161
|
+
ts.isStringLiteral(node.moduleSpecifier)) {
|
|
162
|
+
const originalText = node.moduleSpecifier.text;
|
|
163
|
+
if (originalText.endsWith(".js")) {
|
|
164
|
+
const newText = originalText.slice(0, -3) + jsExt;
|
|
165
|
+
return ts.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, ts.factory.createStringLiteral(newText), node.assertClause);
|
|
166
|
+
}
|
|
167
|
+
// if export is extensionless, add .js extension
|
|
168
|
+
if (originalText.startsWith("./") || originalText.startsWith("../")) {
|
|
169
|
+
const hasExtension = path.extname(originalText) !== "";
|
|
170
|
+
if (!hasExtension) {
|
|
171
|
+
const newText = originalText + jsExt;
|
|
172
|
+
return ts.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, ts.factory.createStringLiteral(newText), node.assertClause);
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
// Handle dynamic imports
|
|
177
|
+
if (ts.isCallExpression(node) &&
|
|
178
|
+
node.expression.kind === ts.SyntaxKind.ImportKeyword) {
|
|
179
|
+
const arg = node.arguments[0];
|
|
180
|
+
if (ts.isStringLiteral(arg)) {
|
|
181
|
+
const originalText = arg.text;
|
|
182
|
+
if (originalText.endsWith(".js")) {
|
|
183
|
+
const newText = originalText.slice(0, -3) + jsExt;
|
|
184
|
+
// console.log(`Rewriting dynamic import from ${originalText} to
|
|
185
|
+
// ${newText}`);
|
|
186
|
+
return ts.factory.updateCallExpression(node, node.expression, node.typeArguments, [
|
|
187
|
+
ts.factory.createStringLiteral(newText),
|
|
188
|
+
...node.arguments.slice(1),
|
|
189
|
+
]);
|
|
190
|
+
}
|
|
191
|
+
// if dynamic import is extensionless, add .js extension
|
|
192
|
+
if (originalText.startsWith("./") ||
|
|
193
|
+
originalText.startsWith("../")) {
|
|
194
|
+
const hasExtension = path.extname(originalText) !== "";
|
|
195
|
+
if (!hasExtension) {
|
|
196
|
+
const newText = originalText + jsExt;
|
|
197
|
+
return ts.factory.updateCallExpression(node, node.expression, node.typeArguments, [
|
|
198
|
+
ts.factory.createStringLiteral(newText),
|
|
199
|
+
...node.arguments.slice(1),
|
|
200
|
+
]);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
return ts.visitEachChild(node, visitor, context);
|
|
206
|
+
};
|
|
207
|
+
return ts.visitNode(sourceFile, visitor);
|
|
208
|
+
};
|
|
209
|
+
};
|
|
210
|
+
// Check for semantic errors
|
|
211
|
+
const diagnostics = ts.getPreEmitDiagnostics(program);
|
|
212
|
+
if (diagnostics.length > 0) {
|
|
213
|
+
const errorCount = diagnostics.filter((d) => d.category === ts.DiagnosticCategory.Error).length;
|
|
214
|
+
const warningCount = diagnostics.filter((d) => d.category === ts.DiagnosticCategory.Warning).length;
|
|
215
|
+
console.log(`⚠️ Found ${errorCount} errors and ${warningCount} warnings`);
|
|
216
|
+
// Only show first 5 errors to avoid overwhelming output
|
|
217
|
+
const firstErrors = diagnostics
|
|
218
|
+
.filter((d) => d.category === ts.DiagnosticCategory.Error)
|
|
219
|
+
.slice(0, 5);
|
|
220
|
+
if (firstErrors.length > 0) {
|
|
221
|
+
console.log("First few compilation errors:");
|
|
222
|
+
for (const diagnostic of firstErrors) {
|
|
223
|
+
console.error(ts.formatDiagnostic(diagnostic, {
|
|
224
|
+
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
|
|
225
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
226
|
+
getNewLine: () => ts.sys.newLine,
|
|
227
|
+
}));
|
|
228
|
+
}
|
|
229
|
+
if (errorCount > 5) {
|
|
230
|
+
console.log(`... and ${errorCount - 5} more errors`);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
// emit the files
|
|
235
|
+
const emitResult = program.emit(undefined, undefined, undefined, undefined, {
|
|
236
|
+
before: [
|
|
237
|
+
extensionRewriteTransformer,
|
|
238
|
+
],
|
|
239
|
+
afterDeclarations: [extensionRewriteTransformer],
|
|
240
|
+
});
|
|
241
|
+
if (emitResult.emitSkipped) {
|
|
242
|
+
console.error("❌ Emit was skipped due to errors");
|
|
243
|
+
}
|
|
244
|
+
else {
|
|
245
|
+
// console.log(`✅ Emitted ${config.jsExtension} and ${config.dtsExtension}
|
|
246
|
+
// files`);
|
|
247
|
+
}
|
|
248
|
+
// Report any emit diagnostics
|
|
249
|
+
if (emitResult.diagnostics.length > 0) {
|
|
250
|
+
console.error("❌ Errors detected during emit:");
|
|
251
|
+
for (const diagnostic of emitResult.diagnostics) {
|
|
252
|
+
console.error(ts.formatDiagnostic(diagnostic, {
|
|
253
|
+
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
|
|
254
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
255
|
+
getNewLine: () => ts.sys.newLine,
|
|
256
|
+
}));
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
}
|
package/out/utils.d.cts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
export interface ProjectOptions {
|
|
3
|
+
configPath: string;
|
|
4
|
+
compilerOptions: ts.CompilerOptions & Required<Pick<ts.CompilerOptions, "module" | "moduleResolution" | "outDir">>;
|
|
5
|
+
mode: "cts" | "ts" | "mts";
|
|
6
|
+
}
|
|
7
|
+
export declare function getEntryPoints(patterns: string[]): Promise<string[]>;
|
|
8
|
+
export declare function readTsconfig(tsconfigPath: string): ts.CompilerOptions;
|
|
9
|
+
export declare function compileProject(config: ProjectOptions, entryPoints: string[]): Promise<void>;
|
package/out/utils.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import * as ts from "typescript";
|
|
2
|
+
export interface ProjectOptions {
|
|
3
|
+
configPath: string;
|
|
4
|
+
compilerOptions: ts.CompilerOptions & Required<Pick<ts.CompilerOptions, "module" | "moduleResolution" | "outDir">>;
|
|
5
|
+
mode: "cts" | "ts" | "mts";
|
|
6
|
+
}
|
|
7
|
+
export declare function getEntryPoints(patterns: string[]): Promise<string[]>;
|
|
8
|
+
export declare function readTsconfig(tsconfigPath: string): ts.CompilerOptions;
|
|
9
|
+
export declare function compileProject(config: ProjectOptions, entryPoints: string[]): Promise<void>;
|
package/out/utils.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import { globby } from "globby";
|
|
2
|
+
import * as path from "node:path";
|
|
3
|
+
import * as ts from "typescript";
|
|
4
|
+
// Get entry points using the same logic as esbuild.mts
|
|
5
|
+
export async function getEntryPoints(patterns) {
|
|
6
|
+
const results = [];
|
|
7
|
+
for (const pattern of patterns) {
|
|
8
|
+
const _results = await globby(pattern, {
|
|
9
|
+
ignore: ["**/*.d.ts"],
|
|
10
|
+
});
|
|
11
|
+
if (!pattern.endsWith("/*") && _results.length === 0) {
|
|
12
|
+
console.error(`❌ File does not exist: ${pattern}`);
|
|
13
|
+
process.exit(1);
|
|
14
|
+
}
|
|
15
|
+
results.push(..._results);
|
|
16
|
+
}
|
|
17
|
+
return results;
|
|
18
|
+
}
|
|
19
|
+
export function readTsconfig(tsconfigPath) {
|
|
20
|
+
// Read and parse tsconfig.json
|
|
21
|
+
const configPath = path.resolve(tsconfigPath);
|
|
22
|
+
const configFile = ts.readConfigFile(configPath, ts.sys.readFile);
|
|
23
|
+
if (configFile.error) {
|
|
24
|
+
console.error("Error reading tsconfig.json:", ts.formatDiagnostic(configFile.error, {
|
|
25
|
+
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
|
|
26
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
27
|
+
getNewLine: () => ts.sys.newLine,
|
|
28
|
+
}));
|
|
29
|
+
process.exit(1);
|
|
30
|
+
}
|
|
31
|
+
// Parse the config
|
|
32
|
+
const parsedConfig = ts.parseJsonConfigFileContent(configFile.config, ts.sys, path.dirname(configPath));
|
|
33
|
+
if (parsedConfig.errors.length > 0) {
|
|
34
|
+
console.error("Error parsing tsconfig.json:");
|
|
35
|
+
for (const error of parsedConfig.errors) {
|
|
36
|
+
console.error(ts.formatDiagnostic(error, {
|
|
37
|
+
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
|
|
38
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
39
|
+
getNewLine: () => ts.sys.newLine,
|
|
40
|
+
}));
|
|
41
|
+
}
|
|
42
|
+
process.exit(1);
|
|
43
|
+
}
|
|
44
|
+
if (!parsedConfig.options) {
|
|
45
|
+
throw new Error("❌ Error reading tsconfig.json#compilerOptions");
|
|
46
|
+
}
|
|
47
|
+
return parsedConfig.options;
|
|
48
|
+
}
|
|
49
|
+
export async function compileProject(config, entryPoints) {
|
|
50
|
+
const exts = [];
|
|
51
|
+
// exts.push(config.jsExtension ?? ".js");
|
|
52
|
+
// exts.push(config.dtsExtension ?? ".d.ts");
|
|
53
|
+
// console.log(
|
|
54
|
+
// `🧱 Building${
|
|
55
|
+
// config.mode === "ts"
|
|
56
|
+
// ? ".js/.d.ts"
|
|
57
|
+
// : config.mode === "mts"
|
|
58
|
+
// ? ".mjs/.d.ts"
|
|
59
|
+
// : ".cjs/.d.ts"
|
|
60
|
+
// }...`,
|
|
61
|
+
// );
|
|
62
|
+
// Create compiler host
|
|
63
|
+
const host = ts.createCompilerHost(config.compilerOptions);
|
|
64
|
+
const originalWriteFile = host.writeFile;
|
|
65
|
+
const jsExt = config.mode === "mts" ? ".mjs" : config.mode === "cts" ? ".cjs" : ".js";
|
|
66
|
+
const dtsExt = config.mode === "mts"
|
|
67
|
+
? ".d.mts"
|
|
68
|
+
: config.mode === "cts"
|
|
69
|
+
? ".d.cts"
|
|
70
|
+
: ".d.ts";
|
|
71
|
+
host.writeFile = (fileName, data, writeByteOrderMark, onError, sourceFiles) => {
|
|
72
|
+
// Transform output file extensions
|
|
73
|
+
let outputFileName = fileName;
|
|
74
|
+
const processedData = data;
|
|
75
|
+
// if (config.jsExtension) {
|
|
76
|
+
if (fileName.endsWith(".js")) {
|
|
77
|
+
outputFileName = fileName.replace(/\.js$/, jsExt);
|
|
78
|
+
}
|
|
79
|
+
// }
|
|
80
|
+
// if (config.dtsExtension) {
|
|
81
|
+
if (fileName.endsWith(".d.ts")) {
|
|
82
|
+
outputFileName = fileName.replace(/\.d\.ts$/, dtsExt);
|
|
83
|
+
}
|
|
84
|
+
// }
|
|
85
|
+
// console.log(` ${outputFileName}`);
|
|
86
|
+
if (originalWriteFile) {
|
|
87
|
+
originalWriteFile(outputFileName, processedData, writeByteOrderMark, onError, sourceFiles);
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
// Create the TypeScript program using entry points
|
|
91
|
+
const program = ts.createProgram({
|
|
92
|
+
rootNames: entryPoints,
|
|
93
|
+
options: config.compilerOptions,
|
|
94
|
+
host,
|
|
95
|
+
});
|
|
96
|
+
// Create a transformer factory to rewrite extensions
|
|
97
|
+
const extensionRewriteTransformer = (context) => {
|
|
98
|
+
return (sourceFile) => {
|
|
99
|
+
const visitor = (node) => {
|
|
100
|
+
if (ts.isImportDeclaration(node) &&
|
|
101
|
+
node.moduleSpecifier &&
|
|
102
|
+
ts.isStringLiteral(node.moduleSpecifier)) {
|
|
103
|
+
const originalText = node.moduleSpecifier.text;
|
|
104
|
+
if (originalText.endsWith(".js")) {
|
|
105
|
+
const newText = originalText.slice(0, -3) + jsExt;
|
|
106
|
+
return ts.factory.updateImportDeclaration(node, node.modifiers, node.importClause, ts.factory.createStringLiteral(newText), node.assertClause);
|
|
107
|
+
}
|
|
108
|
+
// if import is extensionless, add .js extension
|
|
109
|
+
if (originalText.startsWith("./") || originalText.startsWith("../")) {
|
|
110
|
+
// console.dir("import", { depth: null });
|
|
111
|
+
// console.dir(originalText, { depth: null });
|
|
112
|
+
const hasExtension = path.extname(originalText) !== "";
|
|
113
|
+
if (!hasExtension) {
|
|
114
|
+
const newText = originalText + jsExt;
|
|
115
|
+
// console.dir(newText, { depth: null });
|
|
116
|
+
return ts.factory.updateImportDeclaration(node, node.modifiers, node.importClause, ts.factory.createStringLiteral(newText), node.assertClause);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
// Handle export declarations
|
|
121
|
+
if (ts.isExportDeclaration(node) &&
|
|
122
|
+
node.moduleSpecifier &&
|
|
123
|
+
ts.isStringLiteral(node.moduleSpecifier)) {
|
|
124
|
+
const originalText = node.moduleSpecifier.text;
|
|
125
|
+
if (originalText.endsWith(".js")) {
|
|
126
|
+
const newText = originalText.slice(0, -3) + jsExt;
|
|
127
|
+
return ts.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, ts.factory.createStringLiteral(newText), node.assertClause);
|
|
128
|
+
}
|
|
129
|
+
// if export is extensionless, add .js extension
|
|
130
|
+
if (originalText.startsWith("./") || originalText.startsWith("../")) {
|
|
131
|
+
const hasExtension = path.extname(originalText) !== "";
|
|
132
|
+
if (!hasExtension) {
|
|
133
|
+
const newText = originalText + jsExt;
|
|
134
|
+
return ts.factory.updateExportDeclaration(node, node.modifiers, node.isTypeOnly, node.exportClause, ts.factory.createStringLiteral(newText), node.assertClause);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// Handle dynamic imports
|
|
139
|
+
if (ts.isCallExpression(node) &&
|
|
140
|
+
node.expression.kind === ts.SyntaxKind.ImportKeyword) {
|
|
141
|
+
const arg = node.arguments[0];
|
|
142
|
+
if (ts.isStringLiteral(arg)) {
|
|
143
|
+
const originalText = arg.text;
|
|
144
|
+
if (originalText.endsWith(".js")) {
|
|
145
|
+
const newText = originalText.slice(0, -3) + jsExt;
|
|
146
|
+
// console.log(`Rewriting dynamic import from ${originalText} to
|
|
147
|
+
// ${newText}`);
|
|
148
|
+
return ts.factory.updateCallExpression(node, node.expression, node.typeArguments, [
|
|
149
|
+
ts.factory.createStringLiteral(newText),
|
|
150
|
+
...node.arguments.slice(1),
|
|
151
|
+
]);
|
|
152
|
+
}
|
|
153
|
+
// if dynamic import is extensionless, add .js extension
|
|
154
|
+
if (originalText.startsWith("./") ||
|
|
155
|
+
originalText.startsWith("../")) {
|
|
156
|
+
const hasExtension = path.extname(originalText) !== "";
|
|
157
|
+
if (!hasExtension) {
|
|
158
|
+
const newText = originalText + jsExt;
|
|
159
|
+
return ts.factory.updateCallExpression(node, node.expression, node.typeArguments, [
|
|
160
|
+
ts.factory.createStringLiteral(newText),
|
|
161
|
+
...node.arguments.slice(1),
|
|
162
|
+
]);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return ts.visitEachChild(node, visitor, context);
|
|
168
|
+
};
|
|
169
|
+
return ts.visitNode(sourceFile, visitor);
|
|
170
|
+
};
|
|
171
|
+
};
|
|
172
|
+
// Check for semantic errors
|
|
173
|
+
const diagnostics = ts.getPreEmitDiagnostics(program);
|
|
174
|
+
if (diagnostics.length > 0) {
|
|
175
|
+
const errorCount = diagnostics.filter((d) => d.category === ts.DiagnosticCategory.Error).length;
|
|
176
|
+
const warningCount = diagnostics.filter((d) => d.category === ts.DiagnosticCategory.Warning).length;
|
|
177
|
+
console.log(`⚠️ Found ${errorCount} errors and ${warningCount} warnings`);
|
|
178
|
+
// Only show first 5 errors to avoid overwhelming output
|
|
179
|
+
const firstErrors = diagnostics
|
|
180
|
+
.filter((d) => d.category === ts.DiagnosticCategory.Error)
|
|
181
|
+
.slice(0, 5);
|
|
182
|
+
if (firstErrors.length > 0) {
|
|
183
|
+
console.log("First few compilation errors:");
|
|
184
|
+
for (const diagnostic of firstErrors) {
|
|
185
|
+
console.error(ts.formatDiagnostic(diagnostic, {
|
|
186
|
+
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
|
|
187
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
188
|
+
getNewLine: () => ts.sys.newLine,
|
|
189
|
+
}));
|
|
190
|
+
}
|
|
191
|
+
if (errorCount > 5) {
|
|
192
|
+
console.log(`... and ${errorCount - 5} more errors`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
// emit the files
|
|
197
|
+
const emitResult = program.emit(undefined, undefined, undefined, undefined, {
|
|
198
|
+
before: [
|
|
199
|
+
extensionRewriteTransformer,
|
|
200
|
+
],
|
|
201
|
+
afterDeclarations: [extensionRewriteTransformer],
|
|
202
|
+
});
|
|
203
|
+
if (emitResult.emitSkipped) {
|
|
204
|
+
console.error("❌ Emit was skipped due to errors");
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
// console.log(`✅ Emitted ${config.jsExtension} and ${config.dtsExtension}
|
|
208
|
+
// files`);
|
|
209
|
+
}
|
|
210
|
+
// Report any emit diagnostics
|
|
211
|
+
if (emitResult.diagnostics.length > 0) {
|
|
212
|
+
console.error("❌ Errors detected during emit:");
|
|
213
|
+
for (const diagnostic of emitResult.diagnostics) {
|
|
214
|
+
console.error(ts.formatDiagnostic(diagnostic, {
|
|
215
|
+
getCurrentDirectory: () => ts.sys.getCurrentDirectory(),
|
|
216
|
+
getCanonicalFileName: (fileName) => fileName,
|
|
217
|
+
getNewLine: () => ts.sys.newLine,
|
|
218
|
+
}));
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
package/package.json
CHANGED
|
@@ -1,19 +1,92 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zshy",
|
|
3
|
-
"version": "0.0.
|
|
4
|
-
"
|
|
5
|
-
|
|
6
|
-
"
|
|
3
|
+
"version": "0.0.2",
|
|
4
|
+
"description": "Gold-standard build tool for TypeScript libraries",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"build-tool",
|
|
8
|
+
"compiler",
|
|
9
|
+
"dual-package",
|
|
10
|
+
"esm",
|
|
11
|
+
"cjs",
|
|
12
|
+
"library",
|
|
13
|
+
"tsc"
|
|
7
14
|
],
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
|
|
15
|
+
"homepage": "https://github.com/colinhacks/zshy#readme",
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/colinhacks/zshy/issues"
|
|
18
|
+
},
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/colinhacks/zshy.git"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"author": {
|
|
25
|
+
"name": "Colin McDonnell",
|
|
26
|
+
"email": "colin@colinhacks.com",
|
|
27
|
+
"url": "https://colinhacks.com"
|
|
28
|
+
},
|
|
29
|
+
"funding": {
|
|
30
|
+
"type": "github",
|
|
31
|
+
"url": "https://github.com/sponsors/colinhacks"
|
|
32
|
+
},
|
|
33
|
+
"module": "./out/index.js",
|
|
34
|
+
"type": "module",
|
|
35
|
+
"bin": {
|
|
36
|
+
"zshy": "./out/index.js"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@arethetypeswrong/cli": "^0.18.2",
|
|
40
|
+
"@biomejs/biome": "^2.0.5",
|
|
41
|
+
"@types/node": "latest",
|
|
42
|
+
"@types/react": "^19.1.8",
|
|
43
|
+
"react": "^19.1.0",
|
|
44
|
+
"tsx": "^4.20.3",
|
|
45
|
+
"typescript": ">5.5.0"
|
|
46
|
+
},
|
|
47
|
+
"peerDependencies": {
|
|
48
|
+
"typescript": ">5.5.0"
|
|
49
|
+
},
|
|
50
|
+
"dependencies": {
|
|
51
|
+
"arg": "^5.0.2",
|
|
52
|
+
"globby": "^14.1.0",
|
|
53
|
+
"table": "^6.9.0"
|
|
54
|
+
},
|
|
55
|
+
"scripts": {
|
|
56
|
+
"build": "tsx src/index.ts"
|
|
57
|
+
},
|
|
58
|
+
"zshy": {
|
|
59
|
+
"exports": {
|
|
60
|
+
".": "./src/index.ts",
|
|
61
|
+
"./utils": "./src/utils.ts",
|
|
62
|
+
"./plugins/*": "./src/plugins/*"
|
|
63
|
+
},
|
|
64
|
+
"sourceDialects": [
|
|
65
|
+
"@zod/source"
|
|
66
|
+
]
|
|
67
|
+
},
|
|
68
|
+
"main": "./out/index.cjs",
|
|
69
|
+
"types": "./out/index.d.cts",
|
|
11
70
|
"exports": {
|
|
12
71
|
".": {
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
72
|
+
"@zod/source": "./src/index.ts",
|
|
73
|
+
"types": "./out/index.d.cts",
|
|
74
|
+
"import": "./out/index.js",
|
|
75
|
+
"require": "./out/index.cjs"
|
|
76
|
+
},
|
|
77
|
+
"./utils": {
|
|
78
|
+
"@zod/source": "./src/utils.ts",
|
|
79
|
+
"types": "./out/utils.d.cts",
|
|
80
|
+
"import": "./out/utils.js",
|
|
81
|
+
"require": "./out/utils.cjs"
|
|
82
|
+
},
|
|
83
|
+
"./plugins/*": {
|
|
84
|
+
"@zod/source": "./src/plugins/*",
|
|
85
|
+
"import": "./out/src/plugins/*",
|
|
86
|
+
"require": "./out/src/plugins/*"
|
|
17
87
|
}
|
|
18
|
-
}
|
|
88
|
+
},
|
|
89
|
+
"files": [
|
|
90
|
+
"out"
|
|
91
|
+
]
|
|
19
92
|
}
|
package/index.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const arg: string;
|
package/index.js
DELETED