Skip to content

Commit d806701

Browse files
committed
Fixes hot reload bug
1 parent 4f29cc6 commit d806701

File tree

1 file changed

+15
-13
lines changed

1 file changed

+15
-13
lines changed

scripts/playground-server.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,10 @@ function handleGetFileChangesRequest(watcher: DirWatcher, fileServer: FileServer
235235
function makeLoaderJsHotReloadable(loaderJsCode: string, fileChangesUrl: URL): string {
236236
loaderJsCode = loaderJsCode.replace(
237237
/constructor\(env, scriptLoader, defineFunc, requireFunc, loaderAvailableTimestamp = 0\) {/,
238-
'$&globalThis.$$globalModuleManager = this;'
238+
'$&globalThis.___globalModuleManager = this;'
239239
);
240240

241-
const $$globalModuleManager: any = undefined;
241+
const ___globalModuleManager: any = undefined;
242242

243243
// This code will be appended to loader.js
244244
function $watchChanges(fileChangesUrl: string) {
@@ -266,24 +266,26 @@ function makeLoaderJsHotReloadable(loaderJsCode: string, fileChangesUrl: URL): s
266266
const data = JSON.parse(line);
267267
let handled = false;
268268
if (data.changedPath.endsWith('.css')) {
269-
console.log('css changed', data.changedPath);
270-
const styleSheet = [...document.querySelectorAll(`link[rel='stylesheet']`)].find((l: any) => new URL(l.href, document.location.href).pathname.endsWith(data.changedPath)) as any;
271-
if (styleSheet) {
272-
styleSheet.href = styleSheet.href.replace(/\?.*/, '') + '?' + Date.now();
269+
if (typeof document !== 'undefined') {
270+
console.log('css changed', data.changedPath);
271+
const styleSheet = [...document.querySelectorAll(`link[rel='stylesheet']`)].find((l: any) => new URL(l.href, document.location.href).pathname.endsWith(data.changedPath)) as any;
272+
if (styleSheet) {
273+
styleSheet.href = styleSheet.href.replace(/\?.*/, '') + '?' + Date.now();
274+
}
273275
}
274276
handled = true;
275277
} else if (data.changedPath.endsWith('.js') && data.moduleId) {
276278
console.log('js changed', data.changedPath);
277-
const moduleId = $$globalModuleManager._moduleIdProvider.getModuleId(data.moduleId);
278-
if ($$globalModuleManager._modules2[moduleId]) {
279-
const srcUrl = $$globalModuleManager._config.moduleIdToPaths(data.moduleId);
279+
const moduleId = ___globalModuleManager._moduleIdProvider.getModuleId(data.moduleId);
280+
if (___globalModuleManager._modules2[moduleId]) {
281+
const srcUrl = ___globalModuleManager._config.moduleIdToPaths(data.moduleId);
280282
const newSrc = await (await fetch(srcUrl)).text();
281283
(new Function('define', newSrc))(function (deps, callback) {
282-
const oldModule = $$globalModuleManager._modules2[moduleId];
283-
delete $$globalModuleManager._modules2[moduleId];
284+
const oldModule = ___globalModuleManager._modules2[moduleId];
285+
delete ___globalModuleManager._modules2[moduleId];
284286

285-
$$globalModuleManager.defineModule(data.moduleId, deps, callback);
286-
const newModule = $$globalModuleManager._modules2[moduleId];
287+
___globalModuleManager.defineModule(data.moduleId, deps, callback);
288+
const newModule = ___globalModuleManager._modules2[moduleId];
287289
const oldExports = { ...oldModule.exports };
288290

289291
Object.assign(oldModule.exports, newModule.exports);

0 commit comments

Comments
 (0)