Skip to content

Commit

Permalink
HMR: Don't wait for packaging (#8582)
Browse files Browse the repository at this point in the history
  • Loading branch information
mischnic committed Nov 3, 2022
1 parent 225face commit eeb10f2
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 12 deletions.
8 changes: 6 additions & 2 deletions packages/core/core/src/ReporterRunner.js
Expand Up @@ -28,6 +28,8 @@ type Opts = {|
workerFarm: WorkerFarm,
|};

const instances: Set<ReporterRunner> = new Set();

export default class ReporterRunner {
workerFarm: WorkerFarm;
config: ParcelConfig;
Expand All @@ -44,6 +46,7 @@ export default class ReporterRunner {
logger.onLog(event => this.report(event));

bus.on('reporterEvent', this.eventHandler);
instances.add(this);

if (this.options.shouldPatchConsole) {
patchConsole();
Expand Down Expand Up @@ -107,6 +110,7 @@ export default class ReporterRunner {
dispose() {
bus.off('reporterEvent', this.eventHandler);
instances.delete(this);
}
}
Expand All @@ -130,6 +134,6 @@ export function reportWorker(workerApi: WorkerApi, event: ReporterEvent) {
bus.emit('reporterEvent', event);
}

export function report(event: ReporterEvent) {
bus.emit('reporterEvent', event);
export async function report(event: ReporterEvent): Promise<void> {
await Promise.all([...instances].map(instance => instance.report(event)));
}
21 changes: 21 additions & 0 deletions packages/core/core/src/requests/ParcelBuildRequest.js
Expand Up @@ -14,6 +14,10 @@ import createWriteBundlesRequest from './WriteBundlesRequest';
import {assertSignalNotAborted} from '../utils';
import dumpGraphToGraphViz from '../dumpGraphToGraphViz';
import {bundleGraphEdgeTypes} from '../BundleGraph';
import {report} from '../ReporterRunner';
import IBundleGraph from '../public/BundleGraph';
import {NamedBundle} from '../public/Bundle';
import {assetFromValue} from '../public/Asset';

type ParcelBuildRequestInput = {|
optionsRef: SharedReference,
Expand Down Expand Up @@ -68,6 +72,23 @@ async function run({input, api, options}: RunInput) {
// $FlowFixMe Added in Flow 0.121.0 upgrade in #4381 (Windows only)
dumpGraphToGraphViz(bundleGraph._graph, 'BundleGraph', bundleGraphEdgeTypes);

await report({
type: 'buildProgress',
phase: 'bundled',
bundleGraph: new IBundleGraph(
bundleGraph,
(bundle, bundleGraph, options) =>
NamedBundle.get(bundle, bundleGraph, options),
options,
),
changedAssets: new Map(
Array.from(changedAssets).map(([id, asset]) => [
id,
assetFromValue(asset, options),
]),
),
});

let writeBundlesRequest = createWriteBundlesRequest({
bundleGraph,
optionsRef,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/core/src/types.js
Expand Up @@ -529,4 +529,4 @@ export type ValidationOpts = {|
configCachePath: string,
|};

export type ReportFn = (event: ReporterEvent) => void;
export type ReportFn = (event: ReporterEvent) => void | Promise<void>;
8 changes: 8 additions & 0 deletions packages/core/types/index.js
Expand Up @@ -1765,6 +1765,13 @@ export type BundlingProgressEvent = {|
+phase: 'bundling',
|};

export type BundledProgressEvent = {|
+type: 'buildProgress',
+phase: 'bundled',
+bundleGraph: BundleGraph<NamedBundle>,
+changedAssets: Map<string, Asset>,
|};

/**
* A new Bundle is being packaged.
* @section reporter
Expand Down Expand Up @@ -1792,6 +1799,7 @@ export type BuildProgressEvent =
| ResolvingProgressEvent
| TransformingProgressEvent
| BundlingProgressEvent
| BundledProgressEvent
| PackagingProgressEvent
| OptimizingProgressEvent;

Expand Down
17 changes: 11 additions & 6 deletions packages/reporters/dev-server/src/HMRServer.js
@@ -1,12 +1,12 @@
// @flow

import type {
BuildSuccessEvent,
Dependency,
PluginOptions,
Asset,
BundleGraph,
Dependency,
NamedBundle,
PackagedBundle,
Asset,
PluginOptions,
} from '@parcel/types';
import type {Diagnostic} from '@parcel/diagnostic';
import type {AnsiDiagnosticResult} from '@parcel/utils';
Expand Down Expand Up @@ -60,7 +60,8 @@ export default class HMRServer {
wss: WebSocket.Server;
unresolvedError: HMRMessage | null = null;
options: HMRServerOptions;
bundleGraph: BundleGraph<PackagedBundle> | null = null;
bundleGraph: BundleGraph<PackagedBundle> | BundleGraph<NamedBundle> | null =
null;
stopServer: ?() => Promise<void>;

constructor(options: HMRServerOptions) {
Expand Down Expand Up @@ -149,7 +150,11 @@ export default class HMRServer {
this.broadcast(this.unresolvedError);
}

async emitUpdate(event: BuildSuccessEvent) {
async emitUpdate(event: {
+bundleGraph: BundleGraph<PackagedBundle> | BundleGraph<NamedBundle>,
+changedAssets: Map<string, Asset>,
...
}) {
this.unresolvedError = null;
this.bundleGraph = event.bundleGraph;

Expand Down
8 changes: 5 additions & 3 deletions packages/reporters/dev-server/src/ServerReporter.js
Expand Up @@ -89,6 +89,11 @@ export default (new Reporter({
server.buildStart();
}
break;
case 'buildProgress':
if (event.phase === 'bundled' && hmrServer) {
await hmrServer.emitUpdate(event);
}
break;
case 'buildSuccess':
if (serveOptions) {
if (!server) {
Expand All @@ -100,9 +105,6 @@ export default (new Reporter({

server.buildSuccess(event.bundleGraph, event.requestBundle);
}
if (hmrServer) {
hmrServer.emitUpdate(event);
}
break;
case 'buildFailure':
// On buildFailure watchStart sometimes has not been called yet
Expand Down

0 comments on commit eeb10f2

Please sign in to comment.