mirror of
https://github.com/MichMich/MagicMirror.git
synced 2025-10-20 19:52:00 +00:00
refactor: replace module-alias
dependency with internal alias resolver (#3893)
- removes the external unmaintained `module-alias` dependency -> reducing complexity and risk - introduces a small internal alias mechanism for `logger` and `node_helper` - preserves backward compatibility for existing 3rd‑party modules - should simplify a future ESM migration of MagicMirror I'm confident that it shouldn't cause any problems, but we could also consider including it in the release after next. What do you think? This PR is inspired by PR #2934 - so thanks to @thesebas! 🙇 😃
This commit is contained in:
committed by
GitHub
parent
b1a189b238
commit
787cc6bd1f
31
js/alias-resolver.js
Normal file
31
js/alias-resolver.js
Normal file
@@ -0,0 +1,31 @@
|
||||
// Internal alias mapping for default and 3rd party modules.
|
||||
// Provides short require identifiers: "logger" and "node_helper".
|
||||
// For a future ESM migration, replace this with a public export/import surface.
|
||||
|
||||
const path = require("node:path");
|
||||
const Module = require("module");
|
||||
|
||||
const root = path.join(__dirname, "..");
|
||||
|
||||
// Keep this list minimal; do not add new aliases without architectural review.
|
||||
const ALIASES = {
|
||||
logger: "js/logger.js",
|
||||
node_helper: "js/node_helper.js"
|
||||
};
|
||||
|
||||
// Resolve to absolute paths now.
|
||||
const resolved = Object.fromEntries(
|
||||
Object.entries(ALIASES).map(([k, rel]) => [k, path.join(root, rel)])
|
||||
);
|
||||
|
||||
// Prevent multiple patching if this file is required more than once.
|
||||
if (!Module._mmAliasPatched) {
|
||||
const origResolveFilename = Module._resolveFilename;
|
||||
Module._resolveFilename = function (request, parent, isMain, options) {
|
||||
if (Object.prototype.hasOwnProperty.call(resolved, request)) {
|
||||
return resolved[request];
|
||||
}
|
||||
return origResolveFilename.call(this, request, parent, isMain, options);
|
||||
};
|
||||
Module._mmAliasPatched = true; // non-enumerable marker would be overkill here
|
||||
}
|
Reference in New Issue
Block a user