Skip to content

Commit 25712b4

Browse files
author
Dimitar Tachev
authored
Merge pull request #4747 from NativeScript/tachev/png-hmr
fix: try HMR refresh only when all emitted files are hot updates
2 parents f8647c2 + 5f32463 commit 25712b4

File tree

5 files changed

+40
-26
lines changed

5 files changed

+40
-26
lines changed

lib/controllers/prepare-controller.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ export class PrepareController extends EventEmitter {
9393
this.isInitialPrepareReady = true;
9494

9595
if (this.persistedData && this.persistedData.length) {
96-
this.emitPrepareEvent({ files: [], hasNativeChanges: result.hasNativeChanges, hmrData: null, platform: platformData.platformNameLowerCase });
96+
this.emitPrepareEvent({ files: [], hasOnlyHotUpdateFiles: false, hasNativeChanges: result.hasNativeChanges, hmrData: null, platform: platformData.platformNameLowerCase });
9797
}
9898

9999
return result;
@@ -130,7 +130,7 @@ export class PrepareController extends EventEmitter {
130130
.on("all", async (event: string, filePath: string) => {
131131
filePath = path.join(projectData.projectDir, filePath);
132132
this.$logger.trace(`Chokidar raised event ${event} for ${filePath}.`);
133-
this.emitPrepareEvent({ files: [], hmrData: null, hasNativeChanges: true, platform: platformData.platformNameLowerCase });
133+
this.emitPrepareEvent({ files: [], hasOnlyHotUpdateFiles: false, hmrData: null, hasNativeChanges: true, platform: platformData.platformNameLowerCase });
134134
});
135135

136136
this.watchersData[projectData.projectDir][platformData.platformNameLowerCase].nativeFilesWatcher = watcher;

lib/controllers/run-controller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export class RunController extends EventEmitter implements IRunController {
152152
const platformLiveSyncService = this.$liveSyncServiceResolver.resolveLiveSyncService(platform);
153153

154154
try {
155-
let shouldRestart = filesChangeEventData && filesChangeEventData.hasNativeChanges;
155+
let shouldRestart = filesChangeEventData && (filesChangeEventData.hasNativeChanges || !filesChangeEventData.hasOnlyHotUpdateFiles);
156156
if (!shouldRestart) {
157157
shouldRestart = await platformLiveSyncService.shouldRestart(projectData, liveSyncResultInfo);
158158
}

lib/services/webpack/webpack-compiler-service.ts

+29-22
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,26 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
2828
prepareData.watch = true;
2929
const childProcess = await this.startWebpackProcess(platformData, projectData, prepareData);
3030

31-
childProcess.on("message", (message: any) => {
31+
childProcess.on("message", (message: string | IWebpackEmitMessage) => {
3232
if (message === "Webpack compilation complete.") {
3333
this.$logger.info("Webpack build done!");
3434
resolve(childProcess);
3535
}
3636

37+
message = message as IWebpackEmitMessage;
3738
if (message.emittedFiles) {
3839
if (isFirstWebpackWatchCompilation) {
3940
isFirstWebpackWatchCompilation = false;
4041
return;
4142
}
4243

43-
const result = this.getUpdatedEmittedFiles(message.emittedFiles, message.webpackRuntimeFiles, message.entryPointFiles);
44+
let result;
45+
46+
if (prepareData.hmr) {
47+
result = this.getUpdatedEmittedFiles(message.emittedFiles, message.webpackRuntimeFiles, message.entryPointFiles);
48+
} else {
49+
result = { emittedFiles: message.emittedFiles, fallbackFiles: <string[]>[], hash: "" };
50+
}
4451

4552
const files = result.emittedFiles
4653
.map((file: string) => path.join(platformData.appDestinationDirectoryPath, "app", file));
@@ -49,13 +56,16 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
4956

5057
const data = {
5158
files,
59+
hasOnlyHotUpdateFiles: files.every(f => f.indexOf("hot-update") > -1),
5260
hmrData: {
5361
hash: result.hash,
5462
fallbackFiles
5563
}
5664
};
5765

58-
this.emit(WEBPACK_COMPILATION_COMPLETE, data);
66+
if (data.files.length) {
67+
this.emit(WEBPACK_COMPILATION_COMPLETE, data);
68+
}
5969
}
6070
});
6171

@@ -193,27 +203,24 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
193203
private getUpdatedEmittedFiles(emittedFiles: string[], webpackRuntimeFiles: string[], entryPointFiles: string[]) {
194204
let fallbackFiles: string[] = [];
195205
let hotHash;
196-
if (emittedFiles.some(x => x.endsWith('.hot-update.json'))) {
197-
let result = emittedFiles.slice();
198-
const hotUpdateScripts = emittedFiles.filter(x => x.endsWith('.hot-update.js'));
199-
if (webpackRuntimeFiles && webpackRuntimeFiles.length) {
200-
result = result.filter(file => webpackRuntimeFiles.indexOf(file) === -1);
201-
}
202-
if (entryPointFiles && entryPointFiles.length) {
203-
result = result.filter(file => entryPointFiles.indexOf(file) === -1);
204-
}
205-
hotUpdateScripts.forEach(hotUpdateScript => {
206-
const { name, hash } = this.parseHotUpdateChunkName(hotUpdateScript);
207-
hotHash = hash;
208-
// remove bundle/vendor.js files if there's a bundle.XXX.hot-update.js or vendor.XXX.hot-update.js
209-
result = result.filter(file => file !== `${name}.js`);
210-
});
211-
// if applying of hot update fails, we must fallback to the full files
212-
fallbackFiles = emittedFiles.filter(file => result.indexOf(file) === -1);
213-
return { emittedFiles: result, fallbackFiles, hash: hotHash };
206+
let result = emittedFiles.slice();
207+
const hotUpdateScripts = emittedFiles.filter(x => x.endsWith('.hot-update.js'));
208+
if (webpackRuntimeFiles && webpackRuntimeFiles.length) {
209+
result = result.filter(file => webpackRuntimeFiles.indexOf(file) === -1);
214210
}
211+
if (entryPointFiles && entryPointFiles.length) {
212+
result = result.filter(file => entryPointFiles.indexOf(file) === -1);
213+
}
214+
hotUpdateScripts.forEach(hotUpdateScript => {
215+
const { name, hash } = this.parseHotUpdateChunkName(hotUpdateScript);
216+
hotHash = hash;
217+
// remove bundle/vendor.js files if there's a bundle.XXX.hot-update.js or vendor.XXX.hot-update.js
218+
result = result.filter(file => file !== `${name}.js`);
219+
});
220+
// if applying of hot update fails, we must fallback to the full files
221+
fallbackFiles = emittedFiles.filter(file => hotUpdateScripts.indexOf(file) === -1);
215222

216-
return { emittedFiles, fallbackFiles };
223+
return { emittedFiles: result, fallbackFiles, hash: hotHash };
217224
}
218225

219226
private parseHotUpdateChunkName(name: string) {

lib/services/webpack/webpack.d.ts

+7
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,16 @@ declare global {
2828
platform: string;
2929
files: string[];
3030
hmrData: IPlatformHmrData;
31+
hasOnlyHotUpdateFiles: boolean;
3132
hasNativeChanges: boolean;
3233
}
3334

35+
interface IWebpackEmitMessage {
36+
emittedFiles: string[];
37+
webpackRuntimeFiles: string[];
38+
entryPointFiles: string[];
39+
}
40+
3441
interface IPlatformProjectService extends NodeJS.EventEmitter, IPlatformProjectServiceBase {
3542
getPlatformData(projectData: IProjectData): IPlatformData;
3643
validate(projectData: IProjectData, options: IOptions, notConfiguredEnvOptions?: INotConfiguredEnvOptions): Promise<IValidatePlatformOutput>;

test/controllers/prepare-controller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ describe("prepareController", () => {
103103
assert.lengthOf(emittedEventNames, 1);
104104
assert.lengthOf(emittedEventData, 1);
105105
assert.deepEqual(emittedEventNames[0], PREPARE_READY_EVENT_NAME);
106-
assert.deepEqual(emittedEventData[0], { files: [], hasNativeChanges: true, hmrData: null, platform: platform.toLowerCase() });
106+
assert.deepEqual(emittedEventData[0], { files: [], hasNativeChanges: true, hasOnlyHotUpdateFiles: false, hmrData: null, platform: platform.toLowerCase() });
107107
});
108108
});
109109
});

0 commit comments

Comments
 (0)