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 30, 2024
1 parent 096b9af commit 6377d98
Show file tree
Hide file tree
Showing 45 changed files with 665 additions and 98 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@
"threejs",
"tokio",
"ukey",
"Ukey"
"Ukey",
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ fn lzw_encode(input: &str) -> String {
}
let mut map: HashMap<String, char> = HashMap::default();
let mut encoded = String::new();
let mut phrase = input.chars().next().unwrap().to_string();
let mut phrase = input.chars().next().expect("should have value").to_string();
let mut code = 256u16;
let max_code = 0xFFFF;

Expand All @@ -268,12 +268,15 @@ fn lzw_encode(input: &str) -> String {
phrase = next_phrase;
} else {
if phrase.len() > 1 {
encoded.push(*map.get(&phrase).unwrap());
encoded.push(*map.get(&phrase).expect("should convert to u32 correctly"));
} else {
encoded += &phrase;
}
if code <= max_code {
map.insert(next_phrase, std::char::from_u32(code as u32).unwrap());
map.insert(
next_phrase,
std::char::from_u32(code as u32).expect("should convert to u32 correctly"),
);
code += 1;
}
if code > max_code {
Expand All @@ -285,7 +288,7 @@ fn lzw_encode(input: &str) -> String {
}

if phrase.len() > 1 {
encoded.push(*map.get(&phrase).unwrap());
encoded.push(*map.get(&phrase).expect("should have phrase"));
} else {
encoded += &phrase;
}
Expand Down
13 changes: 8 additions & 5 deletions crates/rspack_plugin_css/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ for(i = 0; cc; i++) {{
{}installedChunks[chunkId] = 0;
{}
"#,
with_hmr.then(|| "moduleIds = [], ").unwrap_or_default(),
with_hmr.then_some("moduleIds = [], ").unwrap_or_default(),
if with_compression {
r#"var map = {}, char = data[0], oldPhrase = char, decoded = char, code = 256, maxCode = "\uffff".charCodeAt(0), phrase;
for (i = 1; i < data.length; i++) {
Expand All @@ -156,12 +156,15 @@ for(i = 0; cc; i++) {{
"handleCssComposes(exports, composes)\nmodule.exports = exports;"
),
with_hmr
.then(|| "moduleIds.push(token); ")
.then_some("moduleIds.push(token); ")
.unwrap_or_default(),
with_hmr
.then(|| format!("if(target == {})", RuntimeGlobals::MODULE_FACTORIES))
.then_some(format!(
"if(target == {})",
RuntimeGlobals::MODULE_FACTORIES
))
.unwrap_or_default(),
with_hmr.then(|| "return moduleIds;").unwrap_or_default()
with_hmr.then_some("return moduleIds;").unwrap_or_default()
),
);
let load_initial_chunk_data = if initial_chunk_ids_with_css.len() > 2 {
Expand All @@ -174,7 +177,7 @@ for(i = 0; cc; i++) {{
.join(","),
RuntimeGlobals::MODULE_FACTORIES
))
} else if initial_chunk_ids_with_css.len() > 0 {
} else if !initial_chunk_ids_with_css.is_empty() {
Cow::Owned(
initial_chunk_ids_with_css
.iter()
Expand Down
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 @@ -1009,6 +1008,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 @@ -1481,13 +1482,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
6 changes: 3 additions & 3 deletions packages/rspack-test-tools/src/case/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ export type TConfigCaseConfig = Omit<
const creator = new BasicCaseCreator({
clean: true,
describe: false,
testConfig: (testConfig) => {
let oldModuleScope = testConfig.moduleScope;
testConfig: testConfig => {
const oldModuleScope = testConfig.moduleScope;
testConfig.moduleScope = (ms, stats) => {
let res = ms;
// TODO: modify runner module scope based on stats here
if (typeof oldModuleScope === 'function') {
if (typeof oldModuleScope === "function") {
res = oldModuleScope(ms, stats);
}
return res;
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
8 changes: 4 additions & 4 deletions packages/rspack-test-tools/src/test/creator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface IBasicCaseCreatorOptions<T extends ECompilerType> {
temp: string | void;
}
) => ITestProcessor[];
testConfig?: (testConfig: TTestConfig<T>) => void,
testConfig?: (testConfig: TTestConfig<T>) => void;
description?: (name: string, step: number) => string;
runner?: new (
name: string,
Expand All @@ -37,10 +37,10 @@ export interface IBasicCaseCreatorOptions<T extends ECompilerType> {
}

export class BasicCaseCreator<T extends ECompilerType> {
constructor(protected _options: IBasicCaseCreatorOptions<T>) { }
constructor(protected _options: IBasicCaseCreatorOptions<T>) {}

create(name: string, src: string, dist: string, temp?: string) {
let testConfig = this.readTestConfig(src);
const testConfig = this.readTestConfig(src);
if (typeof this._options.testConfig === "function") {
this._options.testConfig(testConfig);
}
Expand Down Expand Up @@ -134,7 +134,7 @@ export class BasicCaseCreator<T extends ECompilerType> {
describe.skip(name, () => {
it(
typeof reason === "string" ? `filtered by ${reason}` : "filtered",
() => { }
() => {}
);
});
}
Expand Down
5 changes: 4 additions & 1 deletion packages/rspack-test-tools/src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,10 @@ export type TTestConfig<T extends ECompilerType> = {
noTest?: boolean;
beforeExecute?: () => void;
afterExecute?: () => void;
moduleScope?: (ms: IBasicModuleScope, stats?: TCompilerStatsCompilation<T>) => IBasicModuleScope;
moduleScope?: (
ms: IBasicModuleScope,
stats?: TCompilerStatsCompilation<T>
) => IBasicModuleScope;
findBundle?: (
index: number,
options: TCompilerOptions<T>
Expand Down
Loading

0 comments on commit 6377d98

Please sign in to comment.