Skip to content

Commit 463a8a3

Browse files
committed
refactor: improve debug logging
1 parent 4490a8b commit 463a8a3

9 files changed

+65
-16
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ node --import jiti/register index.ts
113113
- Default: `false`
114114
- Environment Variable: `JITI_DEBUG`
115115
116-
Enable debug to see which files are transpiled
116+
Enable verbose logging. You can use `JITI_DEBUG=1 <your command>` to enable it.
117117
118118
### `fsCache`
119119

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"jiti": "JITI_DEBUG=1 ./lib/jiti-cli.mjs",
4040
"lint": "eslint . && prettier -c src lib test stubs",
4141
"lint:fix": "eslint --fix . && prettier -w src lib test stubs",
42+
"prepack": "pnpm build",
4243
"release": "pnpm build && pnpm test && changelogen --release --prerelease --push --publish --publishTag 2x",
4344
"test": "pnpm lint && vitest run --coverage && pnpm test:register && pnpm test:bun",
4445
"test:register": "node ./test/register-test.mjs",
@@ -87,7 +88,8 @@
8788
"webpack": "^5.92.1",
8889
"webpack-bundle-analyzer": "^4.10.2",
8990
"webpack-cli": "^5.1.4",
90-
"webpack-license-plugin": "^4.4.2"
91+
"webpack-license-plugin": "^4.4.2",
92+
"yoctocolors": "^2.1.0"
9193
},
9294
"packageManager": "[email protected]"
9395
}

pnpm-lock.yaml

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/cache.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ export function getCache(
3434
if (existsSync(cacheFilePath)) {
3535
const cacheSource = readFileSync(cacheFilePath, "utf8");
3636
if (cacheSource.endsWith(sourceHash)) {
37-
debug(ctx, "[cache hit]", filename, "~>", cacheFilePath);
37+
debug(ctx, "[cache]", "[hit]", filename, "~>", cacheFilePath);
3838
return cacheSource;
3939
}
4040
}
4141

42-
debug(ctx, "[cache miss]", filename);
42+
debug(ctx, "[cache]", "[miss]", filename);
4343
const result = get();
4444

4545
if (!result.includes("__JITI_ERROR__")) {
4646
writeFileSync(cacheFilePath, result + sourceHash, "utf8");
47+
debug(ctx, "[cache]", "[store]", filename, "~>", cacheFilePath);
4748
}
4849

4950
return result;

src/eval.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,14 @@ export function evalModule(
5555
const time = Math.round((performance.now() - start) * 1000) / 1000;
5656
debug(
5757
ctx,
58-
`[transpile]${evalOptions.async ? " [esm]" : " [cjs]"}`,
58+
"[transpile]",
59+
evalOptions.async ? "[esm]" : "[cjs]",
5960
filename,
6061
`(${time}ms)`,
6162
);
6263
} else {
6364
try {
64-
debug(
65-
ctx,
66-
`[native]${evalOptions.async ? " [esm]" : " [cjs]"}`,
67-
filename,
68-
);
65+
debug(ctx, "[native]", evalOptions.async ? "[esm]" : "[cjs]", filename);
6966
return nativeImportOrRequire(ctx, filename, evalOptions.async);
7067
} catch (error: any) {
7168
debug(ctx, "Native require error:", error);

src/jiti.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import { join } from "pathe";
1111
import escapeStringRegexp from "escape-string-regexp";
1212
import createRequire from "create-require";
1313
import { normalizeAliases } from "pathe/utils";
14-
import { isDir } from "./utils";
14+
import { debug, isDir } from "./utils";
1515
import { resolveJitiOptions } from "./options";
1616
import { jitiResolve } from "./resolve";
1717
import { evalModule } from "./eval";
1818
import { transform } from "./transform";
1919
import { jitiRequire } from "./require";
2020
import { prepareCacheDir } from "./cache";
21+
import { version as jitiVersion } from "../package.json";
2122

2223
const isWindows = platform() === "win32";
2324

@@ -92,6 +93,20 @@ export default function createJiti(
9293
nativeImport: parentContext?.nativeImport,
9394
};
9495

96+
// Debug
97+
if (!isNested) {
98+
debug(
99+
ctx,
100+
"[init]",
101+
...[
102+
["version:", jitiVersion],
103+
["module-cache:", opts.moduleCache],
104+
["fs-cache:", opts.fsCache],
105+
["interop-defaults:", opts.interopDefault],
106+
].flat(),
107+
);
108+
}
109+
95110
// Prepare cache dir
96111
if (!isNested) {
97112
prepareCacheDir(ctx);

src/require.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export function jitiRequire(ctx: Context, id: string, async: boolean) {
2626
// Experimental Bun support
2727
if (ctx.opts.experimentalBun && !ctx.opts.transformOptions) {
2828
try {
29-
debug(ctx, `[bun] [native] ${id}`);
29+
debug(ctx, "[bun]", "[native]", id);
3030
id = jitiResolve(ctx, id, { async });
3131
if (async && ctx.nativeImport) {
3232
return ctx.nativeImport(id).then((m: any) => {
@@ -72,7 +72,7 @@ export function jitiRequire(ctx: Context, id: string, async: boolean) {
7272

7373
// Force native modules
7474
if (ctx.isNativeRe.test(filename)) {
75-
debug(ctx, `[native] ${async ? " [esm]" : ": [cjs]"}`, filename);
75+
debug(ctx, "[native]", async ? "[esm]" : "[cjs]", filename);
7676
return nativeImportOrRequire(ctx, id, async);
7777
}
7878

src/utils.ts

+33-3
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { PackageJson } from "pkg-types";
55
import { interopDefault as mllyInteropDefault, pathToFileURL } from "mlly";
66
import { isWindows } from "std-env";
77
import { Context } from "./types";
8+
import { gray, green, blue, yellow, cyan, red } from "yoctocolors";
89

910
export function isDir(filename: string): boolean {
1011
try {
@@ -54,10 +55,39 @@ export function wrapModule(source: string, opts?: { async?: boolean }) {
5455
return `(${opts?.async ? "async " : ""}function (exports, require, module, __filename, __dirname, jitiImport) { ${source}\n});`;
5556
}
5657

57-
export function debug(ctx: Context, ...args: string[]) {
58-
if (ctx.opts.debug) {
59-
console.log("[jiti]", ...args);
58+
const debugMap = {
59+
true: green("true"),
60+
false: yellow("false"),
61+
"[esm]": blue("[esm]"),
62+
"[cjs]": green("[cjs]"),
63+
"[native]": cyan("[native]"),
64+
"[transpile]": yellow("[transpile]"),
65+
"[fallback]": red("[fallback]"),
66+
"[hit]": green("[hit]"),
67+
"[miss]": yellow("[miss]"),
68+
};
69+
70+
export function debug(ctx: Context, ...args: unknown[]) {
71+
if (!ctx.opts.debug) {
72+
return;
6073
}
74+
const cwd = process.cwd();
75+
console.log(
76+
gray(
77+
[
78+
"[jiti]",
79+
...args.map((arg) => {
80+
if ((arg as string) in debugMap) {
81+
return debugMap[arg as keyof typeof debugMap];
82+
}
83+
if (typeof arg !== "string") {
84+
return JSON.stringify(arg);
85+
}
86+
return arg.replace(cwd, ".");
87+
}),
88+
].join(" "),
89+
),
90+
);
6191
}
6292

6393
export function jitiInteropDefault(ctx: Context, mod: any) {

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"strict": true,
88
"declaration": false,
99
"experimentalDecorators": true,
10+
"resolveJsonModule": true,
1011
"types": ["node"]
1112
},
1213
"include": ["src", "lib"]

0 commit comments

Comments
 (0)