Skip to content

Commit

Permalink
chore: remove hot document (#6271)
Browse files Browse the repository at this point in the history
  • Loading branch information
LingyuCoder authored Apr 17, 2024
1 parent 55ca885 commit 0a88b52
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 218 deletions.
43 changes: 38 additions & 5 deletions packages/rspack-test-tools/src/helper/legacy/FakeDocument.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ const getPropertyValue = function (property) {
return this[property];
};

module.exports = class FakeDocument {
constructor(basePath) {
export default class FakeDocument {
constructor(basePath, options = {}) {
this.head = this.createElement("head");
this.body = this.createElement("body");
this.baseURI = "https://test.cases/path/index.html";
Expand All @@ -17,6 +17,7 @@ module.exports = class FakeDocument {
]);
this._basePath = basePath;
this.currentScript = undefined;
this._options = options || {};
}

createElement(type) {
Expand Down Expand Up @@ -56,20 +57,32 @@ module.exports = class FakeDocument {
}
return style;
}
};
}

class FakeElement {
export class FakeElement {
constructor(document, type, basePath) {
this._document = document;
this._type = type;
this._children = [];
this._attributes = Object.create(null);
this._src = undefined;
this._href = undefined;
this.rel = undefined;
this.parentNode = undefined;
this.sheet = type === "link" ? new FakeSheet(this, basePath) : undefined;
}

insertBefore(node, before) {
this._document._onElementAttached(node);
node.parentNode = this;
this._children.unshift(node);
Promise.resolve().then(() => {
if (node.onload) {
node.onload({ type: "load", target: node });
}
});
}

appendChild(node) {
this._document._onElementAttached(node);
this._children.push(node);
Expand All @@ -78,6 +91,22 @@ class FakeElement {
setTimeout(() => {
if (node.onload) node.onload({ type: "load", target: node });
}, 100);
} else if (node._type === "script") {
Promise.resolve().then(() => {
if (typeof this._document._options.onScript === "function") {
this._document._options.onScript(node);
}
if (node.onload) {
node.onload({
type: "load",
target: node
});
}
});
} else {
if (node.onload) {
node.onload({ type: "load", target: node });
}
}
}

Expand Down Expand Up @@ -132,6 +161,10 @@ class FakeElement {
}
}

get children() {
return this._children;
}

get src() {
return this._src;
}
Expand All @@ -147,7 +180,7 @@ class FakeElement {
}
}

class FakeSheet {
export class FakeSheet {
constructor(element, basePath) {
this._element = element;
this._basePath = basePath;
Expand Down
78 changes: 0 additions & 78 deletions packages/rspack-test-tools/src/helper/legacy/createHotDocument.js

This file was deleted.

6 changes: 4 additions & 2 deletions packages/rspack-test-tools/src/runner/runner/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,13 @@ export class BasicRunner<T extends ECompilerType = ECompilerType.Rspack>

protected createBaseModuleScope(): IBasicModuleScope {
const baseModuleScope: IBasicModuleScope = {
console: console,
console: this.globalContext!.console,
expect: this.globalContext!.expect,
setTimeout: this.globalContext!.setTimeout,
clearTimeout: this.globalContext!.clearTimeout,
it: this._options.env.it,
beforeEach: this._options.env.beforeEach,
afterEach: this._options.env.afterEach,
expect,
jest,
nsObj: (m: Object) => {
Object.defineProperty(m, Symbol.toStringTag, {
Expand Down
120 changes: 2 additions & 118 deletions packages/rspack-test-tools/src/runner/runner/hot.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
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 } from "../../type";
import { BasicRunner } from "./basic";
import {
IBasicModuleScope,
IBasicRunnerOptions,
TBasicRunnerFile,
TRunnerRequirer
} from "../type";
import fs from "fs";
import path from "path";
import { StatsCompilation } from "@rspack/core";
import { WebRunner } from "./web";

interface IHotRunnerOptionsr<T extends ECompilerType = ECompilerType.Rspack>
extends IBasicRunnerOptions<T> {
Expand All @@ -23,73 +17,9 @@ interface IHotRunnerOptionsr<T extends ECompilerType = ECompilerType.Rspack>

export class HotRunner<
T extends ECompilerType = ECompilerType.Rspack
> extends BasicRunner<T> {
private document: any;
> extends WebRunner<T> {
constructor(protected _options: IHotRunnerOptionsr<T>) {
super(_options);
this.document = createHotDocument(
_options.dist,
this.getRequire.bind(this)
);
}

run(file: string) {
if (!file.endsWith(".js")) {
const cssElement = this.document.createElement("link");
cssElement.href = file;
cssElement.rel = "stylesheet";
this.document.head.appendChild(cssElement);
return Promise.resolve();
}
return super.run(file);
}

protected createGlobalContext() {
const globalContext = super.createGlobalContext();
const urlToPath = (url: string) => {
if (url.startsWith("https://test.cases/path/")) url = url.slice(24);
return path.resolve(this._options.dist, `./${url}`);
};

globalContext["fetch"] = async (url: string) => {
try {
const buffer: Buffer = await new Promise((resolve, reject) =>
fs.readFile(urlToPath(url), (err, b) =>
err ? reject(err) : resolve(b)
)
);
return {
status: 200,
ok: true,
json: async () => JSON.parse(buffer.toString("utf-8"))
};
} catch (err) {
if ((err as { code: string }).code === "ENOENT") {
return {
status: 404,
ok: false
};
}
throw err;
}
};
globalContext["importScripts"] = (url: string) => {
expect(url).toMatch(/^https:\/\/test\.cases\/path\//);
this.requirers.get("entry")!(this._options.dist, urlToRelativePath(url));
};
globalContext["document"] = this.document;
globalContext["Worker"] = createFakeWorker({
outputDirectory: this._options.dist
});
globalContext["EventSource"] = EventSource;
globalContext["location"] = {
href: "https://test.cases/path/index.html",
origin: "https://test.cases",
toString() {
return "https://test.cases/path/index.html";
}
};
return globalContext;
}

protected createModuleScope(
Expand All @@ -98,53 +28,7 @@ export class HotRunner<
file: TBasicRunnerFile
): IBasicModuleScope {
const moduleScope = super.createModuleScope(requireFn, m, file);
moduleScope["__dirname"] = this._options.dist;
moduleScope["window"] = this.globalContext;
moduleScope["self"] = this.globalContext;
moduleScope["fetch"] = this.globalContext!["fetch"];
moduleScope["document"] = this.globalContext!["document"];
moduleScope["importScripts"] = this.globalContext!["importScripts"];
moduleScope["Worker"] = this.globalContext!["Worker"];
moduleScope["EventSource"] = this.globalContext!["EventSource"];
moduleScope["NEXT"] = this._options.next;
return moduleScope;
}

protected createJsonRequirer(): TRunnerRequirer {
return (_, modulePath, context = {}) => {
if (Array.isArray(modulePath)) {
throw new Error("Array module path is not supported in hot cases");
}
return JSON.parse(
fs.readFileSync(path.join(this._options.dist, modulePath), "utf-8")
);
};
}

protected createRunner() {
super.createRunner();
this.requirers.set("cjs", this.getRequire());
this.requirers.set("json", this.createJsonRequirer());
this.requirers.set("entry", (_, modulePath, context) => {
if (Array.isArray(modulePath)) {
throw new Error("Array module path is not supported in hot cases");
}
if (!modulePath.startsWith("./")) {
return require(modulePath);
}
if (modulePath.endsWith(".json")) {
return this.requirers.get("json")!(
this._options.dist,
modulePath,
context
);
} else {
return this.requirers.get("cjs")!(
this._options.dist,
modulePath,
context
);
}
});
}
}
1 change: 0 additions & 1 deletion packages/rspack-test-tools/src/runner/runner/normal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export class NormalRunner<
global,
URL,
Buffer,
setTimeout,
setImmediate
});
return baseModuleScope;
Expand Down
Loading

2 comments on commit 0a88b52

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Benchmark detail: Open

Name Base (2024-04-17 a6841aa) Current Change
10000_development-mode + exec 2.68 s ± 18 ms 2.71 s ± 54 ms +1.40 %
10000_development-mode_hmr + exec 685 ms ± 6.1 ms 689 ms ± 8.1 ms +0.69 %
10000_production-mode + exec 2.48 s ± 22 ms 2.52 s ± 22 ms +1.69 %
arco-pro_development-mode + exec 2.54 s ± 66 ms 2.57 s ± 84 ms +1.24 %
arco-pro_development-mode_hmr + exec 430 ms ± 1.3 ms 430 ms ± 1.2 ms -0.07 %
arco-pro_development-mode_hmr_intercept-plugin + exec 441 ms ± 2.7 ms 441 ms ± 2.6 ms +0.19 %
arco-pro_development-mode_intercept-plugin + exec 3.3 s ± 69 ms 3.27 s ± 55 ms -0.96 %
arco-pro_production-mode + exec 4.02 s ± 48 ms 4.01 s ± 74 ms -0.06 %
arco-pro_production-mode_intercept-plugin + exec 4.75 s ± 97 ms 4.78 s ± 78 ms +0.63 %
threejs_development-mode_10x + exec 2.05 s ± 27 ms 2.05 s ± 12 ms +0.02 %
threejs_development-mode_10x_hmr + exec 757 ms ± 13 ms 761 ms ± 12 ms +0.53 %
threejs_production-mode_10x + exec 5.14 s ± 40 ms 5.2 s ± 58 ms +1.04 %

@rspack-bot
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📝 Ran ecosystem CI: Open

suite result
modernjs, self-hosted, Linux, ci ✅ success
_selftest, ubuntu-latest ✅ success
nx, ubuntu-latest ✅ success
rspress, ubuntu-latest ✅ success
rsbuild, ubuntu-latest ✅ success
compat, ubuntu-latest ✅ success
examples, ubuntu-latest ✅ success

Please sign in to comment.