diff --git a/README.md b/README.md index a118be24..1c4542b9 100644 --- a/README.md +++ b/README.md @@ -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 ` to enable it. ### `fsCache` diff --git a/package.json b/package.json index 4d161ed6..8fac0509 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 46acec67..435b3105 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -137,6 +137,9 @@ importers: webpack-license-plugin: specifier: ^4.4.2 version: 4.4.2(webpack@5.92.1(webpack-cli@5.1.4)) + yoctocolors: + specifier: ^2.1.0 + version: 2.1.0 packages: diff --git a/src/cache.ts b/src/cache.ts index 12437941..deaec639 100644 --- a/src/cache.ts +++ b/src/cache.ts @@ -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; diff --git a/src/eval.ts b/src/eval.ts index 8d62c4e9..bcbe3dee 100644 --- a/src/eval.ts +++ b/src/eval.ts @@ -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); diff --git a/src/jiti.ts b/src/jiti.ts index 43a71a18..77e641d2 100644 --- a/src/jiti.ts +++ b/src/jiti.ts @@ -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"; @@ -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); diff --git a/src/require.ts b/src/require.ts index 06be66dd..09ad9f62 100644 --- a/src/require.ts +++ b/src/require.ts @@ -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) => { @@ -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); } diff --git a/src/utils.ts b/src/utils.ts index 2044fdee..541f9261 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -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 { @@ -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) { diff --git a/tsconfig.json b/tsconfig.json index 906d8698..6dc616c7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ "strict": true, "declaration": false, "experimentalDecorators": true, + "resolveJsonModule": true, "types": ["node"] }, "include": ["src", "lib"]