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

Commit f70297c

Browse files
vtrifonovsis0k0
authored andcommitted
Sync only hmr updates without bundle files (#650)
* Sync only hmr updates without bundle files * Messaging updates
1 parent fe43c90 commit f70297c

7 files changed

+55
-15
lines changed

bundle-config-loader.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,16 @@ module.exports = function (source) {
1010
if (!angular && registerModules) {
1111
const hmr = `
1212
if (module.hot) {
13+
const fileSystemModule = require("tns-core-modules/file-system");
14+
const applicationFiles = fileSystemModule.knownFolders.currentApp();
15+
1316
global.__hmrLivesyncBackup = global.__onLiveSync;
1417
global.__onLiveSync = function () {
15-
console.log("LiveSyncing...");
16-
require("nativescript-dev-webpack/hot")("", {});
18+
console.log("HMR Sync...");
19+
require("nativescript-dev-webpack/hot")(__webpack_require__.h(), (fileName) => applicationFiles.getFile(fileName));
1720
};
21+
global.__hmrInitialSync = true; // needed to determine if we are performing initial sync
22+
global.__onLiveSync();
1823
}
1924
`;
2025

hot-loader-helper.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
module.exports.reload = `
1+
module.exports.reload = function(type) { return `
22
if (module.hot) {
33
module.hot.accept();
44
module.hot.dispose(() => {
55
setTimeout(() => {
66
global.__hmrLivesyncBackup();
7-
});
7+
}, ${type === 'style' ? "global.__hmrInitialSync ? 1000 : 0" : 0});
88
})
99
}
10-
`;
11-
10+
`};
11+
// we need to add a timeout of 1000 if we have a css change, otherwise the app crashes on initial hmr sync

hot.js

+25-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const log = console;
2-
const refresh = 'Please refresh the page.';
2+
const refresh = 'Please restart the application.';
33
const hotOptions = {
44
ignoreUnaccepted: true,
55
ignoreDeclined: true,
@@ -73,7 +73,7 @@ function check(options) {
7373
.then((modules) => {
7474
if (!modules) {
7575
log.warn(
76-
`Cannot find update. The server may have been restarted. ${refresh}`
76+
`Cannot find update. ${refresh}`
7777
);
7878
return null;
7979
}
@@ -82,8 +82,7 @@ function check(options) {
8282
.apply(hotOptions)
8383
.then((appliedModules) => {
8484
if (!upToDate()) {
85-
log.warn("Hashes don't match. Ignoring second update...");
86-
// check(options);
85+
check(options);
8786
}
8887

8988
result(modules, appliedModules);
@@ -122,7 +121,7 @@ if (module.hot) {
122121
console.error('Hot Module Replacement is disabled.');
123122
}
124123

125-
module.exports = function update(currentHash, options) {
124+
function update(currentHash, options) {
126125
lastHash = currentHash;
127126
if (!upToDate()) {
128127
const status = module.hot.status();
@@ -138,3 +137,24 @@ module.exports = function update(currentHash, options) {
138137
}
139138
};
140139

140+
function getCurrentHash(currentHash, getFileContent) {
141+
const file = getFileContent(`${currentHash}.hot-update.json`);
142+
return file.readText().then(hotUpdateContent => {
143+
if(hotUpdateContent) {
144+
const manifest = JSON.parse(hotUpdateContent);
145+
const newHash = manifest.h;
146+
return getCurrentHash(newHash, getFileContent);
147+
} else {
148+
return Promise.resolve(currentHash);
149+
}
150+
}).catch(error => Promise.reject(error));
151+
}
152+
153+
module.exports = function checkState(initialHash, getFileContent) {
154+
getCurrentHash(initialHash, getFileContent).then(currentHash => {
155+
if(currentHash != initialHash) {
156+
update(currentHash, {});
157+
}
158+
})
159+
}
160+

markup-hot-loader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { reload } = require("./hot-loader-helper");
22

33
module.exports = function (source) {
4-
return `${source};${reload}`;
4+
return `${source};${reload('markup')}`;
55
};

page-hot-loader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { reload } = require("./hot-loader-helper");
22

33
module.exports = function (source) {
4-
return `${source};${reload}`;
4+
return `${source};${reload('page')}`;
55
};

plugins/WatchStateLoggerPlugin.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,16 @@ export class WatchStateLoggerPlugin {
3232
console.log(messages.compilationComplete);
3333
}
3434

35-
const emittedFiles = Object
35+
let emittedFiles = Object
3636
.keys(compilation.assets)
3737
.filter(assetKey => compilation.assets[assetKey].emitted);
3838

3939
if (compilation.errors.length > 0) {
4040
WatchStateLoggerPlugin.rewriteHotUpdateChunk(compiler, compilation, emittedFiles);
4141
}
4242

43+
emittedFiles = WatchStateLoggerPlugin.getUpdatedEmittedFiles(emittedFiles);
44+
4345
// provide fake paths to the {N} CLI - relative to the 'app' folder
4446
// in order to trigger the livesync process
4547
const emittedFilesFakePaths = emittedFiles
@@ -90,6 +92,19 @@ export class WatchStateLoggerPlugin {
9092
return content.substring(startIndex, endIndex);
9193
}
9294

95+
/**
96+
* Checks if there's a file in the following pattern 5e0326f3bb50f9f26cf0.hot-update.json
97+
* if yes this is a HMR update and remove all bundle files as we don't need them to be synced,
98+
* but only the update chunks
99+
*/
100+
static getUpdatedEmittedFiles(emittedFiles) {
101+
if(emittedFiles.some(x => x.endsWith('.hot-update.json'))) {
102+
return emittedFiles.filter(x => x.indexOf('.hot-update.') > 0);
103+
} else {
104+
return emittedFiles;
105+
}
106+
}
107+
93108
/**
94109
* Gets the webpackHotUpdate call with updated modules not to include the ones with errors
95110
*/

style-hot-loader.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { reload } = require("./hot-loader-helper");
22

33
module.exports = function (source) {
4-
return `${source};${reload}`;
4+
return `${source};${reload('style')}`;
55
};

0 commit comments

Comments
 (0)