|
| 1 | +// Internal alias mapping for default and 3rd party modules. |
| 2 | +// Provides short require identifiers: "logger" and "node_helper". |
| 3 | +// For a future ESM migration, replace this with a public export/import surface. |
| 4 | + |
| 5 | +const path = require("node:path"); |
| 6 | +const Module = require("module"); |
| 7 | + |
| 8 | +const root = path.join(__dirname, ".."); |
| 9 | + |
| 10 | +// Keep this list minimal; do not add new aliases without architectural review. |
| 11 | +const ALIASES = { |
| 12 | + logger: "js/logger.js", |
| 13 | + node_helper: "js/node_helper.js" |
| 14 | +}; |
| 15 | + |
| 16 | +// Resolve to absolute paths now. |
| 17 | +const resolved = Object.fromEntries( |
| 18 | + Object.entries(ALIASES).map(([k, rel]) => [k, path.join(root, rel)]) |
| 19 | +); |
| 20 | + |
| 21 | +// Prevent multiple patching if this file is required more than once. |
| 22 | +if (!Module._mmAliasPatched) { |
| 23 | + const origResolveFilename = Module._resolveFilename; |
| 24 | + Module._resolveFilename = function (request, parent, isMain, options) { |
| 25 | + if (Object.prototype.hasOwnProperty.call(resolved, request)) { |
| 26 | + return resolved[request]; |
| 27 | + } |
| 28 | + return origResolveFilename.call(this, request, parent, isMain, options); |
| 29 | + }; |
| 30 | + Module._mmAliasPatched = true; // non-enumerable marker would be overkill here |
| 31 | +} |
0 commit comments