Skip to content

Commit decba8f

Browse files
authored
Fix HMR cache of parcel when first entry is changed (codesandbox#616)
1 parent 0a060c5 commit decba8f

File tree

5 files changed

+35
-8
lines changed

5 files changed

+35
-8
lines changed

packages/app/src/sandbox/eval/presets/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export default class Preset {
142142
* Get transpilers from the given query, the query is webpack like:
143143
* eg. !babel-loader!./test.js
144144
*/
145-
getLoaders(module: Module, query: string = '') {
145+
getLoaders(module: Module, query: string = ''): Array<TranspilerDefinition> {
146146
const loader = this.loaders.find(t => t.test(module));
147147

148148
// Starting !, drop all transpilers

packages/app/src/sandbox/eval/presets/parcel/transpilers/html-transpiler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class HTMLTranspiler extends WorkerTranspiler {
1111
this.HMREnabled = false;
1212
}
1313

14-
doTranspilation(code: string, loaderContext: LoaderContext) {
14+
doTranspilation(code: string, loaderContext: LoaderContext): Promise<null> {
1515
return new Promise((resolve, reject) => {
1616
this.queueTask(
1717
{

packages/app/src/sandbox/eval/presets/parcel/transpilers/html-worker.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ self.addEventListener('message', async event => {
7272
self.postMessage({
7373
type: 'add-dependency',
7474
path: assetPath,
75+
isEntry: true,
7576
});
7677

7778
resources.push(assetPath);

packages/app/src/sandbox/eval/transpiled-module.js

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type { WarningStructure } from './transpilers/utils/worker-warning-handle
1919
import resolveDependency from './loaders/dependency-resolver';
2020
import evaluate from './loaders/eval';
2121

22-
import type Manager from './manager';
22+
import type { default as Manager } from './manager';
2323
import HMR from './hmr';
2424

2525
const debug = _debug('cs:compiler:transpiled-module');
@@ -97,12 +97,14 @@ export type LoaderContext = {
9797
depPath: string,
9898
options: ?{
9999
isAbsolute: boolean,
100+
isEntry: boolean,
100101
}
101102
) => void,
102103
addDependenciesInDirectory: (
103104
depPath: string,
104105
options: {
105106
isAbsolute: boolean,
107+
isEntry: boolean,
106108
}
107109
) => void,
108110
_module: TranspiledModule,
@@ -259,6 +261,7 @@ export default class TranspiledModule {
259261
if (this.compilation) {
260262
this.compilation = null;
261263
}
264+
262265
Array.from(this.initiators)
263266
.filter(t => t.compilation)
264267
.forEach(dep => {
@@ -270,6 +273,16 @@ export default class TranspiledModule {
270273
.forEach(dep => {
271274
dep.resetCompilation();
272275
});
276+
277+
// If this is an entry we want all direct entries to be reset as well.
278+
// Entries generally have side effects
279+
if (this.isEntry) {
280+
Array.from(this.dependencies)
281+
.filter(t => t.compilation && t.isEntry)
282+
.forEach(dep => {
283+
dep.resetCompilation();
284+
});
285+
}
273286
}
274287
}
275288

@@ -354,6 +367,10 @@ export default class TranspiledModule {
354367

355368
this.transpilationDependencies.add(tModule);
356369
tModule.transpilationInitiators.add(this);
370+
371+
if (options.isEntry) {
372+
tModule.setIsEntry(true);
373+
}
357374
},
358375
addDependency: (depPath: string, options = {}) => {
359376
if (
@@ -371,6 +388,10 @@ export default class TranspiledModule {
371388

372389
this.dependencies.add(tModule);
373390
tModule.initiators.add(this);
391+
392+
if (options.isEntry) {
393+
tModule.setIsEntry(true);
394+
}
374395
} catch (e) {
375396
if (e.type === 'module-not-found' && e.isDependency) {
376397
this.asyncDependencies.push(
@@ -388,7 +409,7 @@ export default class TranspiledModule {
388409
}
389410
}
390411
},
391-
addDependenciesInDirectory: (folderPath: string, options) => {
412+
addDependenciesInDirectory: (folderPath: string, options = {}) => {
392413
const tModules = manager.resolveTranspiledModulesInDirectory(
393414
folderPath,
394415
options && options.isAbsolute ? '/' : this.module.path
@@ -397,6 +418,10 @@ export default class TranspiledModule {
397418
tModules.forEach(tModule => {
398419
this.dependencies.add(tModule);
399420
tModule.initiators.add(this);
421+
422+
if (options.isEntry) {
423+
tModule.setIsEntry(true);
424+
}
400425
});
401426
},
402427
resolveTranspiledModule: (depPath: string, options = {}) =>
@@ -617,10 +642,8 @@ export default class TranspiledModule {
617642
) {
618643
return this.compilation.exports;
619644
}
620-
} else {
621-
if (this.compilation && this.compilation.exports) {
622-
return this.compilation.exports;
623-
}
645+
} else if (this.compilation && this.compilation.exports) {
646+
return this.compilation.exports;
624647
}
625648

626649
if (this.hmrConfig) {

packages/app/src/sandbox/eval/transpilers/worker-transpiler.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,12 @@ export default class WorkerTranspiler extends Transpiler {
125125
if (data.isGlob) {
126126
loaderContext.addDependenciesInDirectory(data.path, {
127127
isAbsolute: data.isAbsolute,
128+
isEntry: data.isEntry,
128129
});
129130
} else {
130131
loaderContext.addDependency(data.path, {
131132
isAbsolute: data.isAbsolute,
133+
isEntry: data.isEntry,
132134
});
133135
}
134136
return;
@@ -137,6 +139,7 @@ export default class WorkerTranspiler extends Transpiler {
137139
if (data.type === 'add-transpilation-dependency') {
138140
loaderContext.addTranspilationDependency(data.path, {
139141
isAbsolute: data.isAbsolute,
142+
isEntry: data.isEntry,
140143
});
141144
return;
142145
}

0 commit comments

Comments
 (0)