Skip to content

Commit 3e223b7

Browse files
refactor: replace Loader object with named exports (#4195)
This PR removes the Loader wrapper object and switches to direct named exports (`loadModules`, `loadFileForModule`). It keeps behavior the same, but makes the API simpler and more idiomatic for ESM. This was reported in a PR comment by **github-code-quality** here #4194 (review).
1 parent 503c171 commit 3e223b7

3 files changed

Lines changed: 50 additions & 53 deletions

File tree

js/loader.js

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -249,60 +249,57 @@ function loadFile (fileName) {
249249

250250
/* Public Methods */
251251

252-
export const Loader = {
253-
254-
/**
255-
* Load all modules as defined in the config.
256-
*/
257-
async loadModules () {
258-
const moduleData = await getModuleData();
259-
const envVars = await getEnvVars();
260-
const customCss = envVars.customCss;
261-
262-
// Load all modules
263-
for (const module of moduleData) {
264-
await loadModule(module);
265-
}
252+
/**
253+
* Load all modules as defined in the config.
254+
*/
255+
export async function loadModules () {
256+
const moduleData = await getModuleData();
257+
const envVars = await getEnvVars();
258+
const customCss = envVars.customCss;
266259

267-
// Load custom.css
268-
// Since this happens after loading the modules,
269-
// it overwrites the default styles.
270-
await loadFile(customCss);
260+
// Load all modules
261+
for (const module of moduleData) {
262+
await loadModule(module);
263+
}
271264

272-
// Start all modules.
273-
await startModules();
274-
},
265+
// Load custom.css
266+
// Since this happens after loading the modules,
267+
// it overwrites the default styles.
268+
await loadFile(customCss);
275269

276-
/**
277-
* Load a file (script or stylesheet).
278-
* Prevent double loading and search for files defined in js/vendor.js.
279-
* @param {string} fileName Path of the file we want to load.
280-
* @param {Module} module The module that calls the loadFile function.
281-
* @returns {Promise} resolved when the file is loaded
282-
*/
283-
loadFileForModule (fileName, module) {
284-
if (loadedFiles.indexOf(fileName.toLowerCase()) !== -1) {
285-
Log.log(`File already loaded: ${fileName}`);
286-
return Promise.resolve();
287-
}
270+
// Start all modules.
271+
await startModules();
272+
}
288273

289-
if (fileName.indexOf("http://") === 0 || fileName.indexOf("https://") === 0 || fileName.indexOf("/") !== -1) {
290-
// This is an absolute or relative path.
291-
// Load it and then return.
292-
loadedFiles.push(fileName.toLowerCase());
293-
return loadFile(fileName);
294-
}
274+
/**
275+
* Load a file (script or stylesheet).
276+
* Prevent double loading and search for files defined in js/vendor.js.
277+
* @param {string} fileName Path of the file we want to load.
278+
* @param {Module} module The module that calls the loadFile function.
279+
* @returns {Promise} resolved when the file is loaded
280+
*/
281+
export function loadFileForModule (fileName, module) {
282+
if (loadedFiles.indexOf(fileName.toLowerCase()) !== -1) {
283+
Log.log(`File already loaded: ${fileName}`);
284+
return Promise.resolve();
285+
}
295286

296-
if (vendor[fileName] !== undefined) {
297-
// This file is defined in js/vendor.js.
298-
// Load it from its location.
299-
loadedFiles.push(fileName.toLowerCase());
300-
return loadFile(`${vendor[fileName]}`);
301-
}
287+
if (fileName.indexOf("http://") === 0 || fileName.indexOf("https://") === 0 || fileName.indexOf("/") !== -1) {
288+
// This is an absolute or relative path.
289+
// Load it and then return.
290+
loadedFiles.push(fileName.toLowerCase());
291+
return loadFile(fileName);
292+
}
302293

303-
// File not loaded yet.
304-
// Load it based on the module path.
294+
if (vendor[fileName] !== undefined) {
295+
// This file is defined in js/vendor.js.
296+
// Load it from its location.
305297
loadedFiles.push(fileName.toLowerCase());
306-
return loadFile(module.file(fileName));
298+
return loadFile(`${vendor[fileName]}`);
307299
}
308-
};
300+
301+
// File not loaded yet.
302+
// Load it based on the module path.
303+
loadedFiles.push(fileName.toLowerCase());
304+
return loadFile(module.file(fileName));
305+
}

js/main.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global addAnimateCSS, removeAnimateCSS, AnimateCSSIn, AnimateCSSOut, modulePositions, io */
22

33
// eslint-disable-next-line import-x/extensions
4-
import { Loader } from "./loader.js";
4+
import { loadModules } from "./loader.js";
55

66
let modules = [];
77

@@ -573,7 +573,7 @@ export const MM = {
573573
Log.setLogLevel(config.logLevel);
574574

575575
await globalThis.Translator.loadCoreTranslations(config.language);
576-
await Loader.loadModules();
576+
await loadModules();
577577
},
578578

579579
/**

js/module.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* global nunjucks */
22

33
// eslint-disable-next-line import-x/extensions
4-
import { Loader } from "./loader.js";
4+
import { loadFileForModule } from "./loader.js";
55
// eslint-disable-next-line import-x/extensions
66
import { MMSocket } from "./socketclient.js";
77

@@ -282,7 +282,7 @@ export class Module {
282282
const loadNextDependency = async () => {
283283
if (dependencies.length > 0) {
284284
const nextDependency = dependencies[0];
285-
await Loader.loadFileForModule(nextDependency, this);
285+
await loadFileForModule(nextDependency, this);
286286
dependencies = dependencies.slice(1);
287287
await loadNextDependency();
288288
} else {

0 commit comments

Comments
 (0)