Browse Source

added import tracing in register.js for diagnostic runs

master
Rob Colbert 3 months ago
parent
commit
7c3f4977de
  1. 9
      docs/import-trace.md
  2. 10
      es-import-trace.js
  3. 7
      register.js

9
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.

10
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);
};

7
register.js

@ -5,11 +5,11 @@
/* /*
* Old: node --loader ts-node/esm script.ts * Old: node --loader ts-node/esm script.ts
* New: node --import ./register.js script.ts * New: node --import ./register.js script.ts
* *
* The new approach is to run this script, which then registers the ESM loader * 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 * with Node, then immediately uses it. You can do more in this script, but
* that's bad practice. * that's bad practice.
* *
* Don't perform application setup and initialization in this module. Register * Don't perform application setup and initialization in this module. Register
* any loaders you'll need, but that's all. * any loaders you'll need, but that's all.
*/ */
@ -17,4 +17,5 @@
import { register } from "node:module"; import { register } from "node:module";
import { pathToFileURL } from "node:url"; import { pathToFileURL } from "node:url";
register("ts-node/esm", pathToFileURL("./")); // register("./es-import-trace.js", pathToFileURL("./"));
register("ts-node/esm", pathToFileURL("./"));

Loading…
Cancel
Save