Skip to content

Commit 787cc6b

Browse files
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! 🙇 😃
1 parent b1a189b commit 787cc6b

File tree

9 files changed

+49
-24
lines changed

9 files changed

+49
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ planned for 2026-01-01
1515

1616
### Changed
1717

18+
- [core] refactor: replace `module-alias` dependency with internal alias resolver (#3893)
19+
1820
### Fixed
1921

2022
### Updated

jest.config.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
const aliasMapper = {
2+
logger: "<rootDir>/js/logger.js"
3+
};
4+
15
const config = {
26
verbose: true,
37
testTimeout: 20000,
@@ -6,21 +10,21 @@ const config = {
610
{
711
displayName: "unit",
812
globalSetup: "<rootDir>/tests/unit/helpers/global-setup.js",
9-
moduleNameMapper: {
10-
logger: "<rootDir>/js/logger.js"
11-
},
13+
moduleNameMapper: aliasMapper,
1214
testMatch: ["**/tests/unit/**/*.[jt]s?(x)"],
1315
testPathIgnorePatterns: ["<rootDir>/tests/unit/mocks", "<rootDir>/tests/unit/helpers"]
1416
},
1517
{
1618
displayName: "electron",
1719
testMatch: ["**/tests/electron/**/*.[jt]s?(x)"],
20+
moduleNameMapper: aliasMapper,
1821
testPathIgnorePatterns: ["<rootDir>/tests/electron/helpers"]
1922
},
2023
{
2124
displayName: "e2e",
2225
testMatch: ["**/tests/e2e/**/*.[jt]s?(x)"],
2326
modulePaths: ["<rootDir>/js/"],
27+
moduleNameMapper: aliasMapper,
2428
testPathIgnorePatterns: ["<rootDir>/tests/e2e/helpers", "<rootDir>/tests/e2e/mocks"]
2529
}
2630
],

js/alias-resolver.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
}

js/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
// Alias modules mentioned in package.js under _moduleAliases.
2-
require("module-alias/register");
1+
// Load lightweight internal alias resolver
2+
require("./alias-resolver");
33

44
const fs = require("node:fs");
55
const path = require("node:path");

js/check_config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
// Ensure internal require aliases (e.g., "logger") resolve when this file is run as a standalone script
2+
require("./alias-resolver");
3+
14
const path = require("node:path");
25
const fs = require("node:fs");
36
const { styleText } = require("node:util");
47
const Ajv = require("ajv");
58
const globals = require("globals");
69
const { Linter } = require("eslint");
10+
const Log = require("logger");
711

812
const rootPath = path.resolve(`${__dirname}/../`);
9-
const Log = require(`${rootPath}/js/logger.js`);
1013
const Utils = require(`${rootPath}/js/utils.js`);
1114

1215
const linter = new Linter({ configType: "flat" });

js/utils.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
const path = require("node:path");
2-
3-
const rootPath = path.resolve(`${__dirname}/../`);
4-
const Log = require(`${rootPath}/js/logger.js`);
51
const os = require("node:os");
62
const fs = require("node:fs");
73
const si = require("systeminformation");
4+
const Log = require("logger");
85

96
const modulePositions = []; // will get list from index.html
107
const regionRegEx = /"region ([^"]*)/i;

modules/default/calendar/debug.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* use this script with `node debug.js` to test the fetcher without the need
44
* of starting the MagicMirror² core. Adjust the values below to your desire.
55
*/
6-
// Alias modules mentioned in package.js under _moduleAliases.
7-
require("module-alias/register");
6+
// Load internal alias resolver
7+
require("../../../js/alias-resolver");
88
const Log = require("logger");
99

1010
const CalendarFetcher = require("./calendarfetcher");

package-lock.json

Lines changed: 0 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484
"helmet": "^8.1.0",
8585
"html-to-text": "^9.0.5",
8686
"iconv-lite": "^0.7.0",
87-
"module-alias": "^2.2.3",
8887
"moment": "^2.30.1",
8988
"moment-timezone": "^0.6.0",
9089
"node-ical": "^0.21.0",
@@ -121,9 +120,5 @@
121120
},
122121
"engines": {
123122
"node": ">=22.18.0"
124-
},
125-
"_moduleAliases": {
126-
"node_helper": "js/node_helper.js",
127-
"logger": "js/logger.js"
128123
}
129124
}

0 commit comments

Comments
 (0)