Skip to content

Commit

Permalink
refactor: improve debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jul 2, 2024
1 parent 4490a8b commit 463a8a3
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ node --import jiti/register index.ts
- Default: `false`
- Environment Variable: `JITI_DEBUG`
Enable debug to see which files are transpiled
Enable verbose logging. You can use `JITI_DEBUG=1 <your command>` to enable it.
### `fsCache`
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"jiti": "JITI_DEBUG=1 ./lib/jiti-cli.mjs",
"lint": "eslint . && prettier -c src lib test stubs",
"lint:fix": "eslint --fix . && prettier -w src lib test stubs",
"prepack": "pnpm build",
"release": "pnpm build && pnpm test && changelogen --release --prerelease --push --publish --publishTag 2x",
"test": "pnpm lint && vitest run --coverage && pnpm test:register && pnpm test:bun",
"test:register": "node ./test/register-test.mjs",
Expand Down Expand Up @@ -87,7 +88,8 @@
"webpack": "^5.92.1",
"webpack-bundle-analyzer": "^4.10.2",
"webpack-cli": "^5.1.4",
"webpack-license-plugin": "^4.4.2"
"webpack-license-plugin": "^4.4.2",
"yoctocolors": "^2.1.0"
},
"packageManager": "pnpm@9.4.0"
}
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ export function getCache(
if (existsSync(cacheFilePath)) {
const cacheSource = readFileSync(cacheFilePath, "utf8");
if (cacheSource.endsWith(sourceHash)) {
debug(ctx, "[cache hit]", filename, "~>", cacheFilePath);
debug(ctx, "[cache]", "[hit]", filename, "~>", cacheFilePath);
return cacheSource;
}
}

debug(ctx, "[cache miss]", filename);
debug(ctx, "[cache]", "[miss]", filename);
const result = get();

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

return result;
Expand Down
9 changes: 3 additions & 6 deletions src/eval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,14 @@ export function evalModule(
const time = Math.round((performance.now() - start) * 1000) / 1000;
debug(
ctx,
`[transpile]${evalOptions.async ? " [esm]" : " [cjs]"}`,
"[transpile]",
evalOptions.async ? "[esm]" : "[cjs]",
filename,
`(${time}ms)`,
);
} else {
try {
debug(
ctx,
`[native]${evalOptions.async ? " [esm]" : " [cjs]"}`,
filename,
);
debug(ctx, "[native]", evalOptions.async ? "[esm]" : "[cjs]", filename);
return nativeImportOrRequire(ctx, filename, evalOptions.async);
} catch (error: any) {
debug(ctx, "Native require error:", error);
Expand Down
17 changes: 16 additions & 1 deletion src/jiti.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ import { join } from "pathe";
import escapeStringRegexp from "escape-string-regexp";
import createRequire from "create-require";
import { normalizeAliases } from "pathe/utils";
import { isDir } from "./utils";
import { debug, isDir } from "./utils";
import { resolveJitiOptions } from "./options";
import { jitiResolve } from "./resolve";
import { evalModule } from "./eval";
import { transform } from "./transform";
import { jitiRequire } from "./require";
import { prepareCacheDir } from "./cache";
import { version as jitiVersion } from "../package.json";

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

Expand Down Expand Up @@ -92,6 +93,20 @@ export default function createJiti(
nativeImport: parentContext?.nativeImport,
};

// Debug
if (!isNested) {
debug(
ctx,
"[init]",
...[
["version:", jitiVersion],
["module-cache:", opts.moduleCache],
["fs-cache:", opts.fsCache],
["interop-defaults:", opts.interopDefault],
].flat(),
);
}

// Prepare cache dir
if (!isNested) {
prepareCacheDir(ctx);
Expand Down
4 changes: 2 additions & 2 deletions src/require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export function jitiRequire(ctx: Context, id: string, async: boolean) {
// Experimental Bun support
if (ctx.opts.experimentalBun && !ctx.opts.transformOptions) {
try {
debug(ctx, `[bun] [native] ${id}`);
debug(ctx, "[bun]", "[native]", id);
id = jitiResolve(ctx, id, { async });
if (async && ctx.nativeImport) {
return ctx.nativeImport(id).then((m: any) => {
Expand Down Expand Up @@ -72,7 +72,7 @@ export function jitiRequire(ctx: Context, id: string, async: boolean) {

// Force native modules
if (ctx.isNativeRe.test(filename)) {
debug(ctx, `[native] ${async ? " [esm]" : ": [cjs]"}`, filename);
debug(ctx, "[native]", async ? "[esm]" : "[cjs]", filename);
return nativeImportOrRequire(ctx, id, async);
}

Expand Down
36 changes: 33 additions & 3 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { PackageJson } from "pkg-types";
import { interopDefault as mllyInteropDefault, pathToFileURL } from "mlly";
import { isWindows } from "std-env";
import { Context } from "./types";
import { gray, green, blue, yellow, cyan, red } from "yoctocolors";

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

export function debug(ctx: Context, ...args: string[]) {
if (ctx.opts.debug) {
console.log("[jiti]", ...args);
const debugMap = {
true: green("true"),
false: yellow("false"),
"[esm]": blue("[esm]"),
"[cjs]": green("[cjs]"),
"[native]": cyan("[native]"),
"[transpile]": yellow("[transpile]"),
"[fallback]": red("[fallback]"),
"[hit]": green("[hit]"),
"[miss]": yellow("[miss]"),
};

export function debug(ctx: Context, ...args: unknown[]) {
if (!ctx.opts.debug) {
return;
}
const cwd = process.cwd();
console.log(
gray(
[
"[jiti]",
...args.map((arg) => {
if ((arg as string) in debugMap) {
return debugMap[arg as keyof typeof debugMap];
}
if (typeof arg !== "string") {
return JSON.stringify(arg);
}
return arg.replace(cwd, ".");
}),
].join(" "),
),
);
}

export function jitiInteropDefault(ctx: Context, mod: any) {
Expand Down
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"strict": true,
"declaration": false,
"experimentalDecorators": true,
"resolveJsonModule": true,
"types": ["node"]
},
"include": ["src", "lib"]
Expand Down

0 comments on commit 463a8a3

Please sign in to comment.