Skip to content

Commit 05b5eb1

Browse files
refactor!: remove legacy AMD require shim from expose-modules (#5185)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent 243aa45 commit 05b5eb1

10 files changed

Lines changed: 37 additions & 97 deletions

File tree

src/core/exporter.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88

99
import { removeCommentNodes, removeReSpec } from "./utils.js";
10-
import { expose } from "./expose-modules.js";
1110
import { html } from "./import-maps.js";
1211
import { pub } from "./pubsubhub.js";
1312

@@ -107,5 +106,3 @@ function prettify(cloneDoc) {
107106
el.outerHTML = `\n${el.outerHTML}`;
108107
});
109108
}
110-
111-
expose("core/exporter", { rsDocToDataURL });

src/core/expose-modules.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/core/pubsubhub.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
*/
88
export const name = "core/pubsubhub";
99

10-
import { expose } from "./expose-modules.js";
1110
import { showError } from "./utils.js";
1211

1312
const subscriptions = new EventTarget();
@@ -51,5 +50,3 @@ export function sub(topic, cb, options = { once: false }) {
5150
};
5251
subscriptions.addEventListener(topic, /** @type {any} */ (listener), options);
5352
}
54-
55-
expose(name, { sub });

src/core/respec-global.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
* readonly properties:
55
* - version: returns version of ReSpec Script.
66
* - ready: returns a promise that settles when ReSpec finishes processing.
7+
* - worker: returns a promise that resolves to the ReSpec Web Worker.
78
*
89
*/
910
import { serialize } from "../core/exporter.js";
1011
import { sub } from "./pubsubhub.js";
12+
import { workerPromise } from "./worker.js";
1113

1214
export const name = "core/respec-global";
1315

@@ -41,6 +43,10 @@ class ReSpec {
4143
return this._respecDonePromise;
4244
}
4345

46+
get worker() {
47+
return workerPromise;
48+
}
49+
4450
async toHTML() {
4551
return serialize("html", document);
4652
}

src/core/worker.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ export const name = "core/worker";
99

1010
// Opportunistically preload syntax highlighter, which is used by the worker
1111
import { createResourceHint } from "./utils.js";
12-
import { expose } from "./expose-modules.js";
1312
import { fetchBase } from "./text-loader.js";
1413

1514
// Derive the highlight URL from the ReSpec bundle location. In the IIFE bundle,
@@ -77,8 +76,3 @@ async function createWorker() {
7776
}
7877

7978
export const workerPromise = createWorker();
80-
81-
expose(
82-
name,
83-
workerPromise.then(worker => ({ worker }))
84-
);

src/type-helper.d.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ interface Window {
5050
closeModal(owner?: Element): void;
5151
freshModal(title: string, content: Node, currentOwner: Element): void;
5252
};
53-
require?: {
54-
(deps: string[], callback: (...modules: any[]) => void): void;
55-
modules: { [dep: string]: any };
56-
};
5753
axe?: {
5854
run(context: Node, options: any): Promise<{ violations: AxeViolation[] }>;
5955
};
@@ -63,6 +59,7 @@ interface Document {
6359
respec: {
6460
readonly version: string;
6561
readonly ready: Promise<void>;
62+
readonly worker: Promise<Worker>;
6663
};
6764
}
6865

tests/spec/core/exporter-spec.js

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
"use strict";
22

3-
import { flushIframes, makeRSDoc, makeStandardOps } from "../SpecHelper.js";
3+
import {
4+
flushIframes,
5+
getExportedDoc,
6+
makeRSDoc,
7+
makeStandardOps,
8+
} from "../SpecHelper.js";
49

510
describe("Core - exporter", () => {
611
afterAll(flushIframes);
712

8-
async function getExportedDoc(ops) {
9-
const doc = await makeRSDoc(ops);
10-
const dataURL = await new Promise(resolve => {
11-
doc.defaultView.require(["core/exporter"], ({ rsDocToDataURL }) =>
12-
resolve(rsDocToDataURL("text/html", doc))
13-
);
14-
});
15-
const docString = decodeURIComponent(dataURL).replace(
16-
"data:text/html;charset=utf-8,",
17-
""
18-
);
19-
return new DOMParser().parseFromString(docString, "text/html");
20-
}
21-
2213
it("removes .removeOnSave elements", async () => {
2314
const ops = makeStandardOps();
2415
ops.body = `<div class="removeOnSave" id="this-should-be-removed">this should be removed</div>`;
25-
const doc = await getExportedDoc(ops);
16+
const doc = await getExportedDoc(await makeRSDoc(ops));
2617

2718
expect(doc.getElementById("this-should-be-removed")).toBeFalsy();
2819
expect(doc.querySelectorAll(".removeOnSave")).toHaveSize(0);
@@ -31,7 +22,7 @@ describe("Core - exporter", () => {
3122
it("removes all comments", async () => {
3223
const ops = makeStandardOps();
3324
ops.body = `<div><!-- remove -->PASS <span><!-- remove --></span></div>`;
34-
const doc = await getExportedDoc(ops);
25+
const doc = await getExportedDoc(await makeRSDoc(ops));
3526

3627
const walker = document.createTreeWalker(doc.body, NodeFilter.SHOW_COMMENT);
3728
const comments = [];
@@ -59,7 +50,7 @@ describe("Core - exporter", () => {
5950
>
6051
`;
6152
const ops = makeStandardOps(null, body);
62-
const doc = await getExportedDoc(ops);
53+
const doc = await getExportedDoc(await makeRSDoc(ops));
6354

6455
const anchor = doc.getElementById("ANCHOR");
6556
expect(anchor.hasAttribute("data-cite")).toBeFalse();
@@ -86,7 +77,7 @@ describe("Core - exporter", () => {
8677
<pre class="example js">
8778
function Foo(){};
8879
</pre>`;
89-
const doc = await getExportedDoc(ops);
80+
const doc = await getExportedDoc(await makeRSDoc(ops));
9081
const { lastElementChild } = doc.head;
9182
expect(lastElementChild.href).toBe(
9283
"https://www.w3.org/StyleSheets/TR/2021/W3C-ED"

tests/spec/core/highlight.html

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
<title>Highlight custom language test</title>
66
<script class="remove">
77
async function loadTestLang() {
8-
const worker = await new Promise(resolve => {
9-
require(["core/worker"], ({ worker }) => resolve(worker));
10-
});
8+
const worker = await document.respec.worker;
119
const action = "highlight-load-lang";
1210
const langURL = new URL(
1311
"../../support-files/hljs-testlang.js",

tests/spec/core/respec-global-spec.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ describe("Core — Respec Global - document.respec", () => {
4747
expect(doc.respec.errors).toHaveSize(0);
4848
});
4949

50+
it("exposes a worker promise that resolves to a Worker", async () => {
51+
const doc = await makeRSDoc(makeStandardOps());
52+
const { worker } = doc.respec;
53+
expect(worker).toBeDefined();
54+
expect(typeof worker.then).toBe("function");
55+
const resolved = await worker;
56+
expect(resolved).toBeTruthy();
57+
expect(typeof resolved.postMessage).toBe("function");
58+
});
59+
5060
it("returns exported html with toHTML()", async () => {
5161
const ops = makeStandardOps();
5262
const doc = await makeRSDoc(ops);

tools/respecDocWriter.js

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ async function checkIfReSpec(page) {
225225
*/
226226
async function generateHTML(page, timer, version, url) {
227227
try {
228-
return await page.evaluate(evaluateHTML, version, timer);
228+
return await page.evaluate(evaluateHTML, timer);
229229
} catch (err) {
230230
const msg = `\n😭 Sorry, there was an error generating the HTML. Please report this issue!\n${`${
231231
`Specification: ${url}\n` +
@@ -237,43 +237,24 @@ async function generateHTML(page, timer, version, url) {
237237
}
238238

239239
/**
240-
* @param {ReSpecVersion} version
241240
* @param {ReturnType<typeof createTimer>} timer
242241
*/
243-
async function evaluateHTML(version, timer) {
242+
async function evaluateHTML(timer) {
244243
await timeout(
245244
document.respec ? document.respec.ready : document.respecIsReady,
246245
timer.remaining
247246
);
248247

249-
const [major, minor] = version;
250-
if (major < 20 || (major === 20 && minor < 10)) {
251-
console.warn(
252-
"👴🏽 Ye Olde ReSpec version detected! Please update to 20.10.0 or above. " +
253-
`Your version: ${window.respecVersion}.`
248+
if (!document.respec?.toHTML) {
249+
throw new Error(
250+
"document.respec.toHTML is not available. " +
251+
"Please upgrade to a newer version of ReSpec, " +
252+
"or use an older version of the ReSpec CLI."
254253
);
255-
// Document references an older version of ReSpec that does not yet
256-
// have the "core/exporter" module. Try with the old "ui/save-html"
257-
// module.
258-
const { exportDocument } = await new Promise((resolve, reject) => {
259-
require(["ui/save-html"], resolve, err => {
260-
reject(new Error(err.message));
261-
});
262-
});
263-
return exportDocument("html", "text/html");
264-
} else if (!document.respec || !document.respec.toHTML) {
265-
const { rsDocToDataURL } = await new Promise((resolve, reject) => {
266-
require(["core/exporter"], resolve, err => {
267-
reject(new Error(err.message));
268-
});
269-
});
270-
const dataURL = rsDocToDataURL("text/html");
271-
const encodedString = dataURL.replace(/^data:\w+\/\w+;charset=utf-8,/, "");
272-
return decodeURIComponent(encodedString);
273-
} else {
274-
return await document.respec.toHTML();
275254
}
276255

256+
return await document.respec.toHTML();
257+
277258
function timeout(promise, ms) {
278259
return new Promise((resolve, reject) => {
279260
promise.then(resolve, reject);

0 commit comments

Comments
 (0)