Skip to content

Commit

Permalink
more logging of imports
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jun 21, 2024
1 parent 2ab5b93 commit 6b31b86
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/expander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 15 additions & 4 deletions packages/core/src/importprompt.ts
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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)) {
Expand All @@ -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
Expand Down

0 comments on commit 6b31b86

Please sign in to comment.