diff --git a/docs/import-trace.md b/docs/import-trace.md new file mode 100644 index 0000000..ef1b7ad --- /dev/null +++ b/docs/import-trace.md @@ -0,0 +1,9 @@ +# DTP Import Trace + +As a useful tool for diagnosing circular dependencies, you can enable log tracing of imports at runtime by editing [register.js](../register.js) and un-commenting: + +```js +// register("./es-import-trace.js", pathToFileURL("./")); +``` + +This will load the [es-import-trace.js](../es-import-trace.js) "loader" which prints the URL of the module being imported, then returns the result of the default loader. diff --git a/es-import-trace.js b/es-import-trace.js new file mode 100644 index 0000000..ee1924b --- /dev/null +++ b/es-import-trace.js @@ -0,0 +1,10 @@ +// es-import-trace.js +// Copyright (C) 2025 DTP Technologies, LLC +// All Rights Reserved + +"use strict"; + +export const load = async (url, context, defaultLoad) => { + console.log("import: " + url); + return await defaultLoad(url, context); +}; diff --git a/register.js b/register.js index 1d0bcee..c43f76f 100644 --- a/register.js +++ b/register.js @@ -5,11 +5,11 @@ /* * Old: node --loader ts-node/esm script.ts * New: node --import ./register.js script.ts - * + * * The new approach is to run this script, which then registers the ESM loader * with Node, then immediately uses it. You can do more in this script, but * that's bad practice. - * + * * Don't perform application setup and initialization in this module. Register * any loaders you'll need, but that's all. */ @@ -17,4 +17,5 @@ import { register } from "node:module"; import { pathToFileURL } from "node:url"; -register("ts-node/esm", pathToFileURL("./")); \ No newline at end of file +// register("./es-import-trace.js", pathToFileURL("./")); +register("ts-node/esm", pathToFileURL("./"));