Skip to content
This repository was archived by the owner on Aug 7, 2021. It is now read-only.

Commit 50e1ad7

Browse files
authored
refactor: HMR improvements (#667)
* Add HMR prefix to logs from HMR to be filterable * add global.__hmrRefresh function so that it can be easily used by users * Remove timeout as it is handled in the latest modules * Improve file filtering of emitted hmr files
1 parent 08fefb9 commit 50e1ad7

File tree

4 files changed

+38
-22
lines changed

4 files changed

+38
-22
lines changed

bundle-config-loader.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,20 @@ module.exports = function (source) {
1515
1616
global.__hmrLivesyncBackup = global.__onLiveSync;
1717
global.__onLiveSync = function () {
18-
console.log("HMR Sync...");
18+
console.log("HMR: Sync...");
1919
require("nativescript-dev-webpack/hot")(__webpack_require__.h(), (fileName) => applicationFiles.getFile(fileName));
2020
};
21+
22+
global.__hmrRefresh = function(type) {
23+
global.__hmrNeedReload = true;
24+
setTimeout(() => {
25+
if(global.__hmrNeedReload) {
26+
global.__hmrNeedReload = false;
27+
global.__hmrLivesyncBackup();
28+
}
29+
});
30+
}
31+
2132
global.__hmrInitialSync = true; // needed to determine if we are performing initial sync
2233
global.__onLiveSync();
2334
}

hot-loader-helper.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@ module.exports.reload = function(type) { return `
22
if (module.hot) {
33
module.hot.accept();
44
module.hot.dispose(() => {
5-
global.__hmrNeedReload = true;
6-
setTimeout(() => {
7-
if(global.__hmrNeedReload) {
8-
global.__hmrNeedReload = false;
9-
global.__hmrLivesyncBackup();
10-
}
11-
}, ${type === 'style' ? "global.__hmrInitialSync ? 1000 : 0" : 0});
5+
global.__hmrRefresh('${type}');
126
})
137
}
148
`};
15-
// we need to add a timeout of 1000 if we have a css change, otherwise the app crashes on initial hmr sync
9+

hot.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
const log = console;
1+
const hmrPrefix = 'HMR:';
2+
const log = {
3+
info: (message) => console.info(`${hmrPrefix} ${message}`),
4+
warn: (message) => console.warn(`${hmrPrefix} ${message}`),
5+
error: (message) => console.error(`${hmrPrefix} ${message}`),
6+
};
27
const refresh = 'Application needs to be restarted in order to apply the changes.';
38
const hotOptions = {
49
ignoreUnaccepted: true,
@@ -45,19 +50,19 @@ function result(modules, appliedModules) {
4550
}
4651

4752
if (!(appliedModules || []).length) {
48-
console.info('No Modules Updated.');
53+
log.info('No Modules Updated.');
4954
} else {
50-
console.info('The following modules were updated:');
55+
log.info('The following modules were updated:');
5156

5257
for (const moduleId of appliedModules) {
53-
console.info(` ↻ ${moduleId}`);
58+
log.info(` ↻ ${moduleId}`);
5459
}
5560

5661
const numberIds = appliedModules.every(
5762
(moduleId) => typeof moduleId === 'number'
5863
);
5964
if (numberIds) {
60-
console.info(
65+
log.info(
6166
'Please consider using the NamedModulesPlugin for module names.'
6267
);
6368
}
@@ -85,7 +90,7 @@ function check(options) {
8590
result(modules, appliedModules);
8691

8792
if (upToDate()) {
88-
console.info('App is up to date.');
93+
log.info('App is up to date.');
8994
}
9095
})
9196
.catch((err) => {
@@ -97,7 +102,7 @@ function check(options) {
97102
window.location.reload();
98103
}
99104
} else {
100-
log.warn(`Update failed: ${err.stack}` || err.message);
105+
log.warn(`Update failed: ${err.stack || err.message}`);
101106
}
102107
});
103108
})
@@ -107,15 +112,15 @@ function check(options) {
107112
log.warn(`Cannot check for update. ${refresh}`);
108113
log.warn(err.stack || err.message);
109114
} else {
110-
log.warn(`Update check failed: ${err.stack}` || err.message);
115+
log.warn(`Update check failed: ${err.stack|| err.message}`);
111116
}
112117
});
113118
}
114119

115120
if (module.hot) {
116-
console.info('Hot Module Replacement Enabled. Waiting for signal.');
121+
log.info('Hot Module Replacement Enabled. Waiting for signal.');
117122
} else {
118-
console.error('Hot Module Replacement is disabled.');
123+
log.error('Hot Module Replacement is disabled.');
119124
}
120125

121126
function update(currentHash, options) {
@@ -124,7 +129,7 @@ function update(currentHash, options) {
124129
const status = module.hot.status();
125130

126131
if (status === 'idle') {
127-
console.info('Checking for updates to the bundle.');
132+
log.info('Checking for updates to the bundle.');
128133
check(options);
129134
} else if (['abort', 'fail'].indexOf(status) >= 0) {
130135
log.warn(

plugins/WatchStateLoggerPlugin.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,15 @@ export class WatchStateLoggerPlugin {
9898
* but only the update chunks
9999
*/
100100
static getUpdatedEmittedFiles(emittedFiles) {
101-
// TODO: Once we are able to determine whether this is a vue project do not always include .css and .scss files, but only for vue projects
102101
if(emittedFiles.some(x => x.endsWith('.hot-update.json'))) {
103-
return emittedFiles.filter(x => x.indexOf('.hot-update.') > 0 || x.endsWith('css'));
102+
let result = emittedFiles.slice();
103+
const hotUpdateScripts = emittedFiles.filter(x => x.endsWith('.hot-update.js'));
104+
hotUpdateScripts.forEach(hotUpdateScript => {
105+
const { name } = this.parseHotUpdateChunkName(hotUpdateScript);
106+
// remove bundle/vendor.js files if there's a bundle.XXX.hot-update.js or vendor.XXX.hot-update.js
107+
result = result.filter(file => file !== `${name}.js`);
108+
});
109+
return result;
104110
} else {
105111
return emittedFiles;
106112
}

0 commit comments

Comments
 (0)