Skip to content

Commit e77439c

Browse files
Fix(loader): Guard requirejs usage to support Jest tests
1 parent ce00138 commit e77439c

File tree

1 file changed

+100
-98
lines changed

1 file changed

+100
-98
lines changed

js/loader.js

Lines changed: 100 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -11,120 +11,122 @@
1111

1212
/* global requirejs */
1313

14-
requirejs.config({
15-
baseUrl: "lib",
16-
shim: {
17-
easel: {
18-
exports: "createjs"
19-
}
20-
},
21-
paths: {
22-
utils: "../js/utils",
23-
widgets: "../js/widgets",
24-
activity: "../js",
25-
easel: "../lib/easeljs",
26-
twewn: "../lib/tweenjs",
27-
prefixfree: "../bower_components/prefixfree/prefixfree.min",
28-
samples: "../sounds/samples",
29-
planet: "../js/planet",
30-
tonejsMidi: "../node_modules/@tonejs/midi/dist/Midi",
31-
i18next: [
32-
"../lib/i18next.min",
33-
"https://cdn.jsdelivr.net/npm/i18next@23.11.5/dist/umd/i18next.min"
34-
],
35-
i18nextHttpBackend: [
36-
"../lib/i18nextHttpBackend.min",
37-
"https://cdn.jsdelivr.net/npm/i18next-http-backend@2.5.1/i18nextHttpBackend.min"
38-
]
39-
},
40-
packages: []
41-
});
14+
if (typeof requirejs !== "undefined") {
15+
requirejs.config({
16+
baseUrl: "lib",
17+
shim: {
18+
easel: {
19+
exports: "createjs"
20+
}
21+
},
22+
paths: {
23+
utils: "../js/utils",
24+
widgets: "../js/widgets",
25+
activity: "../js",
26+
easel: "../lib/easeljs",
27+
twewn: "../lib/tweenjs",
28+
prefixfree: "../bower_components/prefixfree/prefixfree.min",
29+
samples: "../sounds/samples",
30+
planet: "../js/planet",
31+
tonejsMidi: "../node_modules/@tonejs/midi/dist/Midi",
32+
i18next: [
33+
"../lib/i18next.min",
34+
"https://cdn.jsdelivr.net/npm/i18next@23.11.5/dist/umd/i18next.min"
35+
],
36+
i18nextHttpBackend: [
37+
"../lib/i18nextHttpBackend.min",
38+
"https://cdn.jsdelivr.net/npm/i18next-http-backend@2.5.1/i18nextHttpBackend.min"
39+
]
40+
},
41+
packages: []
42+
});
4243

43-
requirejs(["i18next", "i18nextHttpBackend"], function (i18next, i18nextHttpBackend) {
44+
requirejs(["i18next", "i18nextHttpBackend"], function (i18next, i18nextHttpBackend) {
4445

45-
function getLanguage() {
46-
let lang = navigator.language;
47-
if (localStorage.languagePreference) {
48-
lang = localStorage.languagePreference;
46+
function getLanguage() {
47+
let lang = navigator.language;
48+
if (localStorage.languagePreference) {
49+
lang = localStorage.languagePreference;
50+
}
51+
return lang || "enUS";
4952
}
50-
return lang || "enUS";
51-
}
5253

53-
function updateContent() {
54-
console.log("updateContent() called");
55-
const elements = document.querySelectorAll("[data-i18n]");
54+
function updateContent() {
55+
console.log("updateContent() called");
56+
const elements = document.querySelectorAll("[data-i18n]");
5657

57-
elements.forEach(element => {
58-
const key = element.getAttribute("data-i18n");
59-
const translation = i18next.t(key);
60-
element.textContent = translation;
61-
});
62-
}
58+
elements.forEach(element => {
59+
const key = element.getAttribute("data-i18n");
60+
const translation = i18next.t(key);
61+
element.textContent = translation;
62+
});
63+
}
6364

64-
async function initializeI18next() {
65-
return new Promise((resolve, reject) => {
66-
i18next.use(i18nextHttpBackend).init(
67-
{
68-
lng: getLanguage(),
69-
fallbackLng: "en",
70-
keySeparator: false,
71-
nsSeparator: false,
72-
interpolation: {
73-
escapeValue: false
65+
async function initializeI18next() {
66+
return new Promise((resolve, reject) => {
67+
i18next.use(i18nextHttpBackend).init(
68+
{
69+
lng: getLanguage(),
70+
fallbackLng: "en",
71+
keySeparator: false,
72+
nsSeparator: false,
73+
interpolation: {
74+
escapeValue: false
75+
},
76+
backend: {
77+
// REMOVED: Date.now() cache buster.
78+
// Using a static version or relying on browser cache is better for performance.
79+
loadPath: "locales/{{lng}}.json"
80+
}
7481
},
75-
backend: {
76-
// REMOVED: Date.now() cache buster.
77-
// Using a static version or relying on browser cache is better for performance.
78-
loadPath: "locales/{{lng}}.json"
82+
function (err, t) {
83+
if (err) {
84+
console.error("i18next init failed:", err);
85+
reject(err);
86+
} else {
87+
console.log("i18next initialized");
88+
window.i18next = i18next;
89+
resolve(i18next);
90+
}
7991
}
80-
},
81-
function (err, t) {
82-
if (err) {
83-
console.error("i18next init failed:", err);
84-
reject(err);
85-
} else {
86-
console.log("i18next initialized");
87-
window.i18next = i18next;
88-
resolve(i18next);
89-
}
90-
}
91-
);
92+
);
9293

93-
i18next.on("initialized", function () {
94-
console.log("i18next initialized");
95-
});
94+
i18next.on("initialized", function () {
95+
console.log("i18next initialized");
96+
});
9697

97-
i18next.on("loaded", function (loaded) {
98-
console.log("i18next loaded:", loaded);
98+
i18next.on("loaded", function (loaded) {
99+
console.log("i18next loaded:", loaded);
100+
});
99101
});
100-
});
101-
}
102+
}
102103

103-
async function main() {
104-
try {
105-
await initializeI18next();
104+
async function main() {
105+
try {
106+
await initializeI18next();
106107

107-
// Setup language change listener
108-
i18next.on("languageChanged", function () {
109-
updateContent();
110-
});
111-
112-
if (document.readyState === "loading") {
113-
document.addEventListener("DOMContentLoaded", function () {
108+
// Setup language change listener
109+
i18next.on("languageChanged", function () {
114110
updateContent();
115111
});
116-
} else {
117-
console.log("DOM already loaded, updating content immediately");
118-
updateContent();
119-
}
120112

121-
// Load application logic after i18n is ready
122-
requirejs(["utils/utils", "activity/activity"]);
113+
if (document.readyState === "loading") {
114+
document.addEventListener("DOMContentLoaded", function () {
115+
updateContent();
116+
});
117+
} else {
118+
console.log("DOM already loaded, updating content immediately");
119+
updateContent();
120+
}
123121

124-
} catch (error) {
125-
console.error("Error initializing app:", error);
122+
// Load application logic after i18n is ready
123+
requirejs(["utils/utils", "activity/activity"]);
124+
125+
} catch (error) {
126+
console.error("Error initializing app:", error);
127+
}
126128
}
127-
}
128129

129-
main();
130-
});
130+
main();
131+
});
132+
}

0 commit comments

Comments
 (0)