Skip to content

Commit 8014fc4

Browse files
committed
fix: try HMR refresh only when all emitted files are hot updates
1 parent ac38f16 commit 8014fc4

File tree

4 files changed

+38
-25
lines changed

4 files changed

+38
-25
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

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

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

35+
message = message as IWebpackEmitMessage;
3536
if (message.emittedFiles) {
3637
if (isFirstWebpackWatchCompilation) {
3738
isFirstWebpackWatchCompilation = false;
3839
return;
3940
}
4041

41-
const result = this.getUpdatedEmittedFiles(message.emittedFiles, message.webpackRuntimeFiles, message.entryPointFiles);
42+
let result;
43+
if (prepareData.hmr) {
44+
result = this.getUpdatedEmittedFiles(message.emittedFiles, message.webpackRuntimeFiles, message.entryPointFiles);
45+
} else {
46+
result = { emittedFiles: message.emittedFiles, fallbackFiles: [], hash: "" };
47+
}
4248

4349
const files = result.emittedFiles
4450
.map((file: string) => path.join(platformData.appDestinationDirectoryPath, "app", file));
4551

4652
const data = {
4753
files,
54+
hasOnlyHotUpdateFiles: files.every(f => f.indexOf("hot-update") > -1),
4855
hmrData: {
4956
hash: result.hash,
5057
fallbackFiles: result.fallbackFiles
5158
}
5259
};
5360

54-
this.emit(WEBPACK_COMPILATION_COMPLETE, data);
61+
if (data.files.length) {
62+
this.emit(WEBPACK_COMPILATION_COMPLETE, data);
63+
}
5564
}
5665
});
5766

@@ -184,27 +193,24 @@ export class WebpackCompilerService extends EventEmitter implements IWebpackComp
184193
private getUpdatedEmittedFiles(emittedFiles: string[], webpackRuntimeFiles: string[], entryPointFiles: string[]) {
185194
let fallbackFiles: string[] = [];
186195
let hotHash;
187-
if (emittedFiles.some(x => x.endsWith('.hot-update.json'))) {
188-
let result = emittedFiles.slice();
189-
const hotUpdateScripts = emittedFiles.filter(x => x.endsWith('.hot-update.js'));
190-
if (webpackRuntimeFiles && webpackRuntimeFiles.length) {
191-
result = result.filter(file => webpackRuntimeFiles.indexOf(file) === -1);
192-
}
193-
if (entryPointFiles && entryPointFiles.length) {
194-
result = result.filter(file => entryPointFiles.indexOf(file) === -1);
195-
}
196-
hotUpdateScripts.forEach(hotUpdateScript => {
197-
const { name, hash } = this.parseHotUpdateChunkName(hotUpdateScript);
198-
hotHash = hash;
199-
// remove bundle/vendor.js files if there's a bundle.XXX.hot-update.js or vendor.XXX.hot-update.js
200-
result = result.filter(file => file !== `${name}.js`);
201-
});
202-
// if applying of hot update fails, we must fallback to the full files
203-
fallbackFiles = emittedFiles.filter(file => result.indexOf(file) === -1);
204-
return { emittedFiles: result, fallbackFiles, hash: hotHash };
196+
let result = emittedFiles.slice();
197+
const hotUpdateScripts = emittedFiles.filter(x => x.endsWith('.hot-update.js'));
198+
if (webpackRuntimeFiles && webpackRuntimeFiles.length) {
199+
result = result.filter(file => webpackRuntimeFiles.indexOf(file) === -1);
200+
}
201+
if (entryPointFiles && entryPointFiles.length) {
202+
result = result.filter(file => entryPointFiles.indexOf(file) === -1);
205203
}
204+
hotUpdateScripts.forEach(hotUpdateScript => {
205+
const { name, hash } = this.parseHotUpdateChunkName(hotUpdateScript);
206+
hotHash = hash;
207+
// remove bundle/vendor.js files if there's a bundle.XXX.hot-update.js or vendor.XXX.hot-update.js
208+
result = result.filter(file => file !== `${name}.js`);
209+
});
210+
// if applying of hot update fails, we must fallback to the full files
211+
fallbackFiles = emittedFiles.filter(file => hotUpdateScripts.indexOf(file) === -1);
206212

207-
return { emittedFiles, fallbackFiles };
213+
return { emittedFiles: result, fallbackFiles, hash: hotHash };
208214
}
209215

210216
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>;

0 commit comments

Comments
 (0)