From 10b96053c3ab2e77464eb59b94f9adcfa50e8a28 Mon Sep 17 00:00:00 2001 From: harpsealjs Date: Mon, 8 Apr 2024 19:44:12 +0800 Subject: [PATCH] chore: move hot next function from runner to runner factory (#6140) --- packages/rspack-test-tools/src/runner/hot.ts | 55 +++++++++++++++++-- .../src/runner/runner/hot.ts | 51 ++--------------- 2 files changed, 55 insertions(+), 51 deletions(-) diff --git a/packages/rspack-test-tools/src/runner/hot.ts b/packages/rspack-test-tools/src/runner/hot.ts index ff3cc7f4ce7..8cda2b7660a 100644 --- a/packages/rspack-test-tools/src/runner/hot.ts +++ b/packages/rspack-test-tools/src/runner/hot.ts @@ -1,3 +1,5 @@ +import { StatsCompilation } from "@rspack/core"; +import checkArrayExpectation from "../helper/legacy/checkArrayExpectation"; import { ECompilerType, ITestEnv, @@ -18,21 +20,64 @@ export class HotRunnerFactory< const compiler = this.context.getCompiler(this.name); const testConfig = this.context.getTestConfig(); const stats = compiler.getStats(); + const source = this.context.getSource(); + const dist = this.context.getDist(); const hotUpdateContext = this.context.getValue( this.name, "hotUpdateContext" ) as { updateIndex: number }; + + const next = ( + callback: (error: Error | null, stats?: StatsCompilation) => void + ) => { + hotUpdateContext.updateIndex++; + compiler + .build() + .then(stats => { + if (!stats) + return callback(new Error("Should generate stats during build")); + const jsonStats = stats.toJson({ + // errorDetails: true + }); + if ( + checkArrayExpectation( + source, + jsonStats, + "error", + "errors" + hotUpdateContext.updateIndex, + "Error", + callback + ) + ) { + return; + } + if ( + checkArrayExpectation( + source, + jsonStats, + "warning", + "warnings" + hotUpdateContext.updateIndex, + "Warning", + callback + ) + ) { + return; + } + callback(null, jsonStats as StatsCompilation); + }) + .catch(callback); + }; + return new HotRunner({ env, stats: stats!, name: this.name, runInNewContext: false, testConfig, - source: this.context.getSource(), - dist: this.context.getDist(), - compilerOptions, - compiler, - hotUpdateContext + source, + dist, + next, + compilerOptions }); } } diff --git a/packages/rspack-test-tools/src/runner/runner/hot.ts b/packages/rspack-test-tools/src/runner/runner/hot.ts index 6b656e51415..7c7c5047cd4 100644 --- a/packages/rspack-test-tools/src/runner/runner/hot.ts +++ b/packages/rspack-test-tools/src/runner/runner/hot.ts @@ -2,7 +2,7 @@ import createHotDocument from "../../helper/legacy/createHotDocument"; import urlToRelativePath from "../../helper/legacy/urlToRelativePath"; import createFakeWorker from "../../helper/legacy/createFakeWorker"; import EventSource from "../../helper/legacy/EventSourceForNode"; -import { ECompilerType, ITestCompilerManager } from "../../type"; +import { ECompilerType } from "../../type"; import { BasicRunner } from "./basic"; import { IBasicModuleScope, @@ -13,14 +13,12 @@ import { import fs from "fs"; import path from "path"; import { StatsCompilation } from "@rspack/core"; -import checkArrayExpectation from "../../helper/legacy/checkArrayExpectation"; interface IHotRunnerOptionsr extends IBasicRunnerOptions { - hotUpdateContext: { - updateIndex: number; - }; - compiler: ITestCompilerManager; + next: ( + callback: (error: Error | null, stats?: StatsCompilation) => void + ) => void; } export class HotRunner< @@ -109,46 +107,7 @@ export class HotRunner< moduleScope["Worker"] = this.globalContext!["Worker"]; moduleScope["EventSource"] = this.globalContext!["EventSource"]; moduleScope["STATS"] = moduleScope.__STATS__; - moduleScope["NEXT"] = ( - callback: (error: Error | null, stats?: StatsCompilation) => void - ) => { - this._options.hotUpdateContext.updateIndex++; - this._options.compiler - .build() - .then(stats => { - if (!stats) - return callback(new Error("Should generate stats during build")); - const jsonStats = stats.toJson({ - // errorDetails: true - }); - if ( - checkArrayExpectation( - this._options.source, - jsonStats, - "error", - "errors" + this._options.hotUpdateContext.updateIndex, - "Error", - callback - ) - ) { - return; - } - if ( - checkArrayExpectation( - this._options.source, - jsonStats, - "warning", - "warnings" + this._options.hotUpdateContext.updateIndex, - "Warning", - callback - ) - ) { - return; - } - callback(null, jsonStats as StatsCompilation); - }) - .catch(callback); - }; + moduleScope["NEXT"] = this._options.next; return moduleScope; }