From 6b31b86649ddf1c08e08e6ce09b8fcc9e925a197 Mon Sep 17 00:00:00 2001 From: pelikhan Date: Fri, 21 Jun 2024 13:27:52 -0700 Subject: [PATCH] more logging of imports --- packages/core/src/expander.ts | 2 +- packages/core/src/importprompt.ts | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/packages/core/src/expander.ts b/packages/core/src/expander.ts index 60ebbf5eeb..ad7463cb17 100644 --- a/packages/core/src/expander.ts +++ b/packages/core/src/expander.ts @@ -122,7 +122,7 @@ async function callExpander( try { if (MODULE_JS_REGEX.test(r.filename)) - await importPrompt(ctx, r, { logCb }) + await importPrompt(ctx, r, { logCb, trace }) else { await evalPrompt(ctx, r, { sourceMaps: true, diff --git a/packages/core/src/importprompt.ts b/packages/core/src/importprompt.ts index 0e9e589231..0cdeab105b 100644 --- a/packages/core/src/importprompt.ts +++ b/packages/core/src/importprompt.ts @@ -1,7 +1,7 @@ import { assert } from "console" import { host } from "./host" -import { logError } from "./util" -import { tsImport } from "tsx/esm/api" +import { logError, logVerbose } from "./util" +import { TraceOptions } from "./trace" function resolveGlobal(): any { if (typeof window !== "undefined") @@ -16,13 +16,15 @@ export async function importPrompt( r: PromptScript, options?: { logCb?: (msg: string) => void - } + } & TraceOptions ) { const { filename } = r if (!filename) throw new Error("filename is required") + const { trace } = options || {} const oldGlb: any = {} const glb: any = resolveGlobal() + const { tsImport } = await import("tsx/esm/api") try { // override global context for (const field of Object.keys(ctx0)) { @@ -37,14 +39,23 @@ export async function importPrompt( const modulePath = filename.startsWith("/") ? filename : host.path.join(host.projectFolder(), filename) - const parentURL = import.meta.url || new URL(__filename, "file://").href + const parentURL = + import.meta.url ?? + new URL(__filename ?? host.projectFolder(), "file://").href + + trace?.itemValue(`import`, `${modulePath}, parent: ${parentURL}`) const module = await tsImport(modulePath, { parentURL, + tsconfig: false, + onImport: (file: string) => { + trace?.itemValue("imported", file) + }, }) const main = module.default if (typeof main === "function") await main(ctx0) } catch (err) { logError(err) + trace?.error(err) throw err } finally { // restore global context