diff --git a/docs/content/docs/api-surface.mdx b/docs/content/docs/api-surface.mdx index e7f4c651..1b182e9c 100644 --- a/docs/content/docs/api-surface.mdx +++ b/docs/content/docs/api-surface.mdx @@ -81,6 +81,7 @@ WebAPI.URL WebAPI.Fetch WebAPI.UIEvents WebAPI.Observers WebAPI.Media WebAPI.WebAudio WebAPI.Storage WebAPI.Messaging WebAPI.Workers WebAPI.Crypto WebAPI.Performance WebAPI.ViewTransitions +WebAPI.History WebAPI.Location WebAPI.SafeFetch ``` @@ -106,6 +107,31 @@ helpers can select only the safe feature: The `let?` examples below require the experimental `LetUnwrap` setting with the current ReScript 13 alpha compiler. The SafeFetch API itself can also be used with ordinary `switch` expressions. +### DOM types without the Window bundle + +`WebAPI.History` independently exposes `WebAPI.History` and its existing +`WebAPI.History.Types.history` type. `WebAPI.Location` exposes `WebAPI.Location` +and depends only on the core `WebAPI.DOM` feature. `WebAPI.Window` includes both +features, so existing Window consumers keep the same API and type identities. + +For React bindings that need element, event, window, document, history, and location +types without the complete Window API, select: + +```json +{ + "dependencies": [ + { + "name": "@rescript/webapi", + "features": ["WebAPI.DOMNodes", "WebAPI.History", "WebAPI.Location"] + } + ] +} +``` + +DOMNodes already includes DOM transitively. This selection does not expose +`WebAPI.Window` or `WebAPI.Document`; add `WebAPI.Window` if you need their methods. +An unrestricted WebAPI dependency elsewhere in the dependency graph enables all features. + ## Fetch Use `WebAPI.Fetch.fetch` for string URLs and `WebAPI.Fetch.fetchWithRequest` diff --git a/rescript.json b/rescript.json index 8ac1cd9c..83f718c2 100644 --- a/rescript.json +++ b/rescript.json @@ -19,7 +19,11 @@ "DOMNodes" ], "WebAPI.HTML": ["WebAPI.DOMNodes", "WebAPI.File", "HTML"], + "WebAPI.History": ["History"], + "WebAPI.Location": ["WebAPI.DOM", "Location"], "WebAPI.Window": [ + "WebAPI.History", + "WebAPI.Location", "WebAPI.HTML", "WebAPI.Media", "WebAPI.Storage", @@ -294,6 +298,18 @@ "HTMLUnknownElement" ] }, + { + "dir": "src/history", + "subdirs": true, + "feature": "History", + "public": ["History"] + }, + { + "dir": "src/location", + "subdirs": true, + "feature": "Location", + "public": ["Location"] + }, { "dir": "src/window", "subdirs": true, @@ -301,9 +317,7 @@ "public": [ "CustomElementRegistry", "Document", - "History", "IdleDeadline", - "Location", "VisualViewport", "Window", "XPathExpression", diff --git a/scripts/check-features.mjs b/scripts/check-features.mjs index 03ddf84b..4b2940d0 100644 --- a/scripts/check-features.mjs +++ b/scripts/check-features.mjs @@ -25,6 +25,8 @@ const expectedFeatureOwners = new Map([ ["WebAPI.DOMNodes", "DOMNodes"], ["WebAPI.File", "File"], ["WebAPI.HTML", "HTML"], + ["WebAPI.History", "History"], + ["WebAPI.Location", "Location"], ["WebAPI.Window", "Window"], ["WebAPI.CSSOM", "CSSOM"], ["WebAPI.CSSFontLoading", "CSSFontLoading"], @@ -237,7 +239,7 @@ const validateCompletionAliases = (sourceEntries) => { ); }; -const validateConfig = (config) => { +export const validateConfig = (config) => { const featureEntries = Object.entries(config.features ?? {}); const sourceEntries = (config.sources ?? []).filter( (source) => source !== null && typeof source === "object" && typeof source.feature === "string", diff --git a/scripts/check-features.test.mjs b/scripts/check-features.test.mjs index 590b42c8..70353ed6 100644 --- a/scripts/check-features.test.mjs +++ b/scripts/check-features.test.mjs @@ -1,7 +1,12 @@ import assert from "node:assert/strict"; +import { readFileSync } from "node:fs"; import test from "node:test"; -import { isMissingSafeFetchDiagnostic, parsePublicTypeAlias } from "./check-features.mjs"; +import { + isMissingSafeFetchDiagnostic, + parsePublicTypeAlias, + validateConfig, +} from "./check-features.mjs"; test("parses a public type alias without type parameters", () => { assert.deepEqual(parsePublicTypeAlias("type t = DOMTree.element"), { @@ -17,6 +22,32 @@ test("parses a public type alias with type parameters", () => { }); }); +const config = JSON.parse(readFileSync(new URL("../rescript.json", import.meta.url), "utf8")); + +test("validates independent History and Location feature ownership", () => { + assert.deepEqual(validateConfig(config), []); + assert.deepEqual(config.features["WebAPI.History"], ["History"]); + assert.deepEqual(config.features["WebAPI.Location"], ["WebAPI.DOM", "Location"]); + assert.ok(config.features["WebAPI.Window"].includes("WebAPI.History")); + assert.ok(config.features["WebAPI.Window"].includes("WebAPI.Location")); +}); + +test("rejects a missing History feature", () => { + const features = Object.fromEntries( + Object.entries(config.features).filter(([name]) => name !== "WebAPI.History"), + ); + assert.ok( + validateConfig({ ...config, features }).some((error) => error.includes("Expected exactly")), + ); +}); + +test("rejects duplicate Location public ownership", () => { + const sources = config.sources.map((source) => + source.feature === "Window" ? { ...source, public: [...source.public, "Location"] } : source, + ); + assert.ok(validateConfig({ ...config, sources }).includes("Duplicate public modules: Location")); +}); + test("recognizes the unavailable SafeFetch module diagnostic across line breaks", () => { assert.equal( isMissingSafeFetchDiagnostic("The module or file WebAPI.SafeFetch\ncan't be found."), diff --git a/src/window/History.res b/src/history/History.res similarity index 100% rename from src/window/History.res rename to src/history/History.res diff --git a/src/window/HistoryTypes.res b/src/history/HistoryTypes.res similarity index 100% rename from src/window/HistoryTypes.res rename to src/history/HistoryTypes.res diff --git a/src/window/Location.res b/src/location/Location.res similarity index 100% rename from src/window/Location.res rename to src/location/Location.res