Skip to content

Commit

Permalink
refactor: watch test runner should extends fake web runner
Browse files Browse the repository at this point in the history
  • Loading branch information
JSerFeng committed Jul 29, 2024
1 parent e338fc5 commit 41b2bd4
Show file tree
Hide file tree
Showing 20 changed files with 459 additions and 35 deletions.
13 changes: 7 additions & 6 deletions packages/rspack-test-tools/etc/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ import type { Compiler } from '@rspack/core';
import type { Compiler as Compiler_2 } from 'webpack';
import type { Configuration } from 'webpack';
import type EventEmitter from 'node:events';
import { IBasicGlobalContext as IBasicGlobalContext_2 } from '../type';
import { IBasicGlobalContext as IBasicGlobalContext_3 } from '../../type';
import { IBasicGlobalContext as IBasicGlobalContext_2 } from '../../type';
import { IBasicModuleScope as IBasicModuleScope_2 } from '../../type';
import { ITestCompilerManager as ITestCompilerManager_2 } from '../type';
import type { RspackOptions } from '@rspack/core';
Expand Down Expand Up @@ -377,7 +376,7 @@ export class FakeDocumentWebRunner<T extends ECompilerType = ECompilerType.Rspac
// (undocumented)
protected createBaseModuleScope(): IBasicModuleScope_2;
// (undocumented)
protected createGlobalContext(): IBasicGlobalContext_3;
protected createGlobalContext(): IBasicGlobalContext_2;
// (undocumented)
protected createJsonRequirer(): TRunnerRequirer;
// (undocumented)
Expand Down Expand Up @@ -1007,6 +1006,8 @@ export interface IWatchProcessorOptions<T extends ECompilerType> extends IMultiT

// @public (undocumented)
interface IWatchRunnerOptions<T extends ECompilerType = ECompilerType.Rspack> extends IBasicRunnerOptions<T> {
// (undocumented)
isWeb: boolean;
// (undocumented)
stepName: string;
}
Expand Down Expand Up @@ -1478,13 +1479,13 @@ export class WatchProcessor<T extends ECompilerType> extends MultiTaskProcessor<
}

// @public (undocumented)
export class WatchRunner<T extends ECompilerType = ECompilerType.Rspack> extends CommonJsRunner<T> {
export class WatchRunner<T extends ECompilerType = ECompilerType.Rspack> extends FakeDocumentWebRunner<T> {
constructor(_watchOptions: IWatchRunnerOptions<T>);
// (undocumented)
protected createGlobalContext(): IBasicGlobalContext_2;
// (undocumented)
protected createModuleScope(requireFn: TRunnerRequirer, m: any, file: TBasicRunnerFile): IBasicModuleScope;
// (undocumented)
run(file: string): Promise<unknown>;
// (undocumented)
protected _watchOptions: IWatchRunnerOptions<T>;
}

Expand Down
3 changes: 2 additions & 1 deletion packages/rspack-test-tools/src/processor/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import type {
ITestEnv,
TCompilerOptions
} from "../type";
import { ConfigProcessor } from "./config";
import { type IMultiTaskProcessorOptions, MultiTaskProcessor } from "./multi";

// This file is used to port step number to rspack.config.js/webpack.config.js
Expand Down Expand Up @@ -37,7 +38,7 @@ export class WatchProcessor<
constructor(protected _watchOptions: IWatchProcessorOptions<T>) {
super({
overrideOptions: WatchProcessor.overrideOptions<T>(_watchOptions),
findBundle: () => "bundle.js",
findBundle: ConfigProcessor.findBundle<T>,
..._watchOptions
});
}
Expand Down
6 changes: 4 additions & 2 deletions packages/rspack-test-tools/src/runner/runner/cjs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ export class CommonJsRunner<
);

if (this._options.testConfig.moduleScope) {
this._options.testConfig.moduleScope(currentModuleScope, this._options.stats);
this._options.testConfig.moduleScope(
currentModuleScope,
this._options.stats
);
}

if (!this._options.runInNewContext) {
Expand All @@ -133,7 +136,6 @@ export class CommonJsRunner<
const fn = this._options.runInNewContext
? vm.runInNewContext(code, this.globalContext!, file.path)
: vm.runInThisContext(code, file.path);

fn.call(
this._options.testConfig.nonEsmThis
? this._options.testConfig.nonEsmThis(modulePath)
Expand Down
18 changes: 7 additions & 11 deletions packages/rspack-test-tools/src/runner/runner/watch.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,26 @@
import path from "node:path";

import FakeDocument from "../../helper/legacy/FakeDocument";
import type { ECompilerType } from "../../type";
import type {
IBasicModuleScope,
TBasicRunnerFile,
TRunnerRequirer
} from "../type";
import type { IBasicRunnerOptions } from "./basic";
import { CommonJsRunner } from "./cjs";
import { FakeDocumentWebRunner } from "./web/fake";

interface IWatchRunnerOptions<T extends ECompilerType = ECompilerType.Rspack>
extends IBasicRunnerOptions<T> {
stepName: string;
isWeb: boolean;
}

export class WatchRunner<
T extends ECompilerType = ECompilerType.Rspack
> extends CommonJsRunner<T> {
private document: any;
> extends FakeDocumentWebRunner<T> {
private state: Record<string, any> = {};
constructor(protected _watchOptions: IWatchRunnerOptions<T>) {
super(_watchOptions);
this.document = new FakeDocument(_watchOptions.dist);
}

protected createGlobalContext() {
const globalContext = super.createGlobalContext();
globalContext.document = this.document;
return globalContext;
}

protected createModuleScope(
Expand All @@ -43,4 +35,8 @@ export class WatchRunner<
moduleScope.WATCH_STEP = this._watchOptions.stepName;
return moduleScope;
}

run(file: string) {
return super.run(file);
}
}
13 changes: 10 additions & 3 deletions packages/rspack-test-tools/src/runner/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,21 @@ export class WatchRunnerFactory<
if (!stepName) {
throw new Error("Can not get watch step name from context");
}

const isWeb = Array.isArray(compilerOptions)
? compilerOptions.some(option => {
return option.target === "web" || option.target === "webworker";
})
: compilerOptions.target === "web" ||
compilerOptions.target === "webworker";

return new WatchRunner({
env,
stats,
name: this.name,
stepName,
runInNewContext:
compilerOptions.target === "web" ||
compilerOptions.target === "webworker",
runInNewContext: isWeb,
isWeb,
testConfig: this.context.getTestConfig(),
source: this.context.getSource(),
dist: this.context.getDist(),
Expand Down
Loading

0 comments on commit 41b2bd4

Please sign in to comment.