Skip to content

Commit b04c358

Browse files
authored
refactor(webapi): isolate DOMTree and core CSS features [Codex] (#415)
Stacked on #413. This replaces the earlier six-module consolidation with selective feature boundaries. Keep basic `DOM.res` and seven CSS declaration modules byte-for-byte unchanged from the History/Location baseline. Add `WebAPI.DOMTree` for the existing rich tree records and `WebAPI.CSSOMCore` for the CSS modules those records need. DOMNodes and CSSOM continue to include their required features, preserving their existing public APIs. Move DOMStringMap alongside DOMTree and move only the small generic HTMLCollection representation into DOMTree, retaining the public collection alias and methods. CSS modules keep their original records and operations together rather than introducing a large shared-record layer. The existing fields, generic parameters, privacy, constructors, and runtime behavior are preserved; all 338 existing modules emit identical JavaScript to baseline. React can select `WebAPI.DOMTree`, `WebAPI.History`, and `WebAPI.Location`: 15 WebAPI source modules rather than 59. The controlled comparison recorded a 122.27 ms median clean React build, effectively tied with the earlier six-module version's 121.48 ms, without enlarging basic DOM. The original 59-module selection measured 196.98 ms in that sample. Timings have substantial variance and are not a universal performance guarantee. The seven CSS modules' operations are intentionally included in CSSOMCore. Element, Node, HTMLCollection, Event, geometry, Animation, Window, and Document operations still require their respective features. CSSOM does not select DOMTree. The dependent React migration, full raw measurements, consumer costs, and reproduction details are in jderochervlk/rescript-react#1. Its separate JsxDOM removal is jderochervlk/rescript-react#2. Style-input and synthetic-event ownership changes remain outside this PR.
1 parent 994d4e4 commit b04c358

65 files changed

Lines changed: 871 additions & 274 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎docs/content/docs/api-surface.mdx‎

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ WebAPI.URL WebAPI.Fetch WebAPI.UIEvents
8181
WebAPI.Observers WebAPI.Media WebAPI.WebAudio
8282
WebAPI.Storage WebAPI.Messaging WebAPI.Workers
8383
WebAPI.Crypto WebAPI.Performance WebAPI.ViewTransitions
84+
WebAPI.DOMTree WebAPI.CSSOMCore
85+
WebAPI.Touch WebAPI.DataTransfer WebAPI.FileEntries
86+
WebAPI.FormData
8487
WebAPI.History WebAPI.Location
8588
WebAPI.SafeFetch
8689
```
@@ -115,23 +118,73 @@ and depends only on the core `WebAPI.DOM` feature. `WebAPI.Window` includes both
115118
features, so existing Window consumers keep the same API and type identities.
116119

117120
For React bindings that need element, event, window, document, history, and location
118-
types without the complete Window API, select:
121+
types without the DOM operation modules, select:
119122

120123
```json
121124
{
122125
"dependencies": [
123126
{
124127
"name": "@rescript/webapi",
125-
"features": ["WebAPI.DOMNodes", "WebAPI.History", "WebAPI.Location"]
128+
"features": ["WebAPI.DOMTree", "WebAPI.History", "WebAPI.Location"]
126129
}
127130
]
128131
}
129132
```
130133

131-
DOMNodes already includes DOM transitively. This selection does not expose
132-
`WebAPI.Window` or `WebAPI.Document`; add `WebAPI.Window` if you need their methods.
134+
DOMTree includes the lightweight DOM types and the shared CSSOMCore records.
135+
It does not enable Element, Node, or HTMLCollection operation modules. The small
136+
CSS declaration modules are kept intact, including their own operations. Select
137+
DOMNodes for element/node operations and CSSOM for the complete CSS API,
138+
and Window for Window or Document methods. Existing operation features include
139+
their required type features automatically.
133140
An unrestricted WebAPI dependency elsewhere in the dependency graph enables all features.
134141

142+
## Narrow touch, clipboard and form-data features
143+
144+
Select `WebAPI.Touch` for Touch/TouchList and `WebAPI.FormData` for FormData and
145+
its unboxed string/file entry values. Each depends only on basic DOM and compiles
146+
five source modules including that dependency.
147+
148+
`WebAPI.DataTransfer` includes its item/list APIs, DOMTree for `setDragImage`,
149+
and `WebAPI.FileEntries` for the existing `webkitGetAsEntry` operation.
150+
FileEntries is the five-module legacy entry family, not the modern filesystem
151+
handle or stream APIs. UIEvents, Fetch, and File continue to include all their
152+
previous modules through these smaller features.
153+
154+
For React bindings selecting DOMTree, History, and Location, adding Touch,
155+
DataTransfer, and FormData selects **27 WebAPI source modules**, rather than
156+
102 with broad UIEvents and Fetch. These are source counts, not speed claims.
157+
Basic DOM is unchanged. FileList operations still require WebAPI.File; a
158+
DataTransfer's `files` field exposes the core file-list type and its length.
159+
160+
```json
161+
{
162+
"dependencies": [
163+
{
164+
"name": "@rescript/webapi",
165+
"features": [
166+
"WebAPI.DOMTree",
167+
"WebAPI.History",
168+
"WebAPI.Location",
169+
"WebAPI.Touch",
170+
"WebAPI.DataTransfer",
171+
"WebAPI.FormData"
172+
]
173+
}
174+
]
175+
}
176+
```
177+
178+
Use `Touch.init` for constructor options and `Touch.Direct` / `Touch.Stylus`
179+
for its touch-type variant. Existing broad event and fetch APIs accept these
180+
same types without casts. FormDataEntryValue remains an **unboxed** union;
181+
it is not interchangeable with a separately defined boxed React variant.
182+
183+
The declared private empty FormData representation is unchanged. ReScript
184+
13.0.0-alpha.7 still accepts an empty-record literal for this type, as it did
185+
before this split; use the FormData constructor rather than constructing records.
186+
The split does not claim to fix that existing compiler limitation.
187+
135188
## Fetch
136189

137190
Use `WebAPI.Fetch.fetch` for string URLs and `WebAPI.Fetch.fetchWithRequest`
@@ -434,6 +487,17 @@ let redirect = WebAPI.Response.redirect(~url="/login", ~status=302)
434487

435488
## DOM
436489

490+
`WebAPI.DOM` stays lightweight: it supplies basic types such as window, document,
491+
and event without pulling in the rich element or CSS declaration records.
492+
`WebAPI.DOMTree` is the independently selectable rich DOM type surface, including
493+
recursive element fields and HTML element styles. `WebAPI.CSSOMCore` supplies
494+
the required CSS declaration modules without selecting Event or DOMTree.
495+
496+
```ReScript
497+
let parentNode = (element: WebAPI.DOMTree.element) => element.parentNode
498+
let style = (element: WebAPI.DOMTree.htmlElement) => element.style
499+
```
500+
437501
DOM values are operated on through public interface modules.
438502

439503
```ReScript

‎package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@
4444
"check:consumer": "node scripts/check-consumer.mjs",
4545
"check:release": "node --test scripts/validate-release-artifact.test.mjs scripts/check-release.test.mjs && node scripts/check-release.mjs",
4646
"check:docs-links": "node --test scripts/check-doc-links.test.mjs scripts/check-doc-type-links.test.mjs && node scripts/check-doc-links.mjs",
47-
"format": "rescript format && oxfmt ./tests/index.js ./tests/fixtures.json ./tests/browser/*.mjs ./scripts/negative-fixtures.json ./scripts/validate-release-artifact.mjs ./scripts/validate-release-artifact.test.mjs ./playwright.config.mjs ./astro.config.mjs ./scripts/check-consumer.mjs ./scripts/check-release.mjs ./scripts/check-release.test.mjs ./scripts/check-features.mjs ./scripts/check-features.test.mjs ./scripts/check-doc-links.mjs ./scripts/check-doc-links.test.mjs ./scripts/check-doc-type-links.test.mjs ./package.json ./README.md ./CHANGELOG.md ./docs && prettier --write .github/workflows/*.yml ./docs/pages",
48-
"format:check": "rescript format --check && oxfmt ./tests/index.js ./tests/fixtures.json ./tests/browser/*.mjs ./scripts/negative-fixtures.json ./scripts/validate-release-artifact.mjs ./scripts/validate-release-artifact.test.mjs ./playwright.config.mjs ./astro.config.mjs ./scripts/check-consumer.mjs ./scripts/check-release.mjs ./scripts/check-release.test.mjs ./scripts/check-features.mjs ./scripts/check-features.test.mjs ./scripts/check-doc-links.mjs ./scripts/check-doc-links.test.mjs ./scripts/check-doc-type-links.test.mjs ./package.json ./README.md ./CHANGELOG.md ./docs --check && prettier --check .github/workflows/*.yml ./docs/pages",
47+
"format": "rescript format && oxfmt ./tests/index.js ./tests/fixtures.json ./tests/browser/*.mjs ./scripts/negative-fixtures.json ./scripts/validate-release-artifact.mjs ./scripts/validate-release-artifact.test.mjs ./playwright.config.mjs ./astro.config.mjs ./scripts/check-consumer.mjs ./scripts/check-release.mjs ./scripts/check-release.test.mjs ./scripts/check-features.mjs ./scripts/check-features.test.mjs ./scripts/data-feature-consumers.mjs ./scripts/check-doc-links.mjs ./scripts/check-doc-links.test.mjs ./scripts/check-doc-type-links.test.mjs ./package.json ./README.md ./CHANGELOG.md ./docs && prettier --write .github/workflows/*.yml ./docs/pages",
48+
"format:check": "rescript format --check && oxfmt ./tests/index.js ./tests/fixtures.json ./tests/browser/*.mjs ./scripts/negative-fixtures.json ./scripts/validate-release-artifact.mjs ./scripts/validate-release-artifact.test.mjs ./playwright.config.mjs ./astro.config.mjs ./scripts/check-consumer.mjs ./scripts/check-release.mjs ./scripts/check-release.test.mjs ./scripts/check-features.mjs ./scripts/check-features.test.mjs ./scripts/data-feature-consumers.mjs ./scripts/check-doc-links.mjs ./scripts/check-doc-links.test.mjs ./scripts/check-doc-type-links.test.mjs ./package.json ./README.md ./CHANGELOG.md ./docs --check && prettier --check .github/workflows/*.yml ./docs/pages",
4949
"docs": "astro dev",
5050
"prebuild:docs": "node docs/llm.js",
5151
"build:docs": "astro build"

‎rescript.json‎

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
"WebAPI.DOMPlatform": ["WebAPI.Event", "DOMPlatform"],
1111
"WebAPI.Animation": ["WebAPI.Event", "Animation"],
1212
"WebAPI.Geometry": ["WebAPI.DOM", "Geometry"],
13-
"WebAPI.CSSOM": ["WebAPI.Event", "CSSOM"],
13+
"WebAPI.CSSOMCore": ["CSSOMCore"],
14+
"WebAPI.CSSOM": ["WebAPI.Event", "WebAPI.CSSOMCore", "CSSOM"],
15+
"WebAPI.DOMTree": ["WebAPI.DOM", "WebAPI.CSSOMCore", "DOMTree"],
1416
"WebAPI.DOMNodes": [
17+
"WebAPI.DOMTree",
1518
"WebAPI.DOMPlatform",
1619
"WebAPI.Animation",
1720
"WebAPI.CSSOM",
@@ -41,7 +44,9 @@
4144
"WebAPI.Geometry",
4245
"Observers"
4346
],
44-
"WebAPI.UIEvents": ["WebAPI.DOMNodes", "WebAPI.File", "UIEvents"],
47+
"WebAPI.Touch": ["WebAPI.DOM", "Touch"],
48+
"WebAPI.DataTransfer": ["WebAPI.DOMTree", "WebAPI.FileEntries", "DataTransfer"],
49+
"WebAPI.UIEvents": ["WebAPI.DOMNodes", "WebAPI.File", "WebAPI.Touch", "WebAPI.DataTransfer", "UIEvents"],
4550
"WebAPI.Canvas": [
4651
"WebAPI.Geometry",
4752
"WebAPI.HTML",
@@ -59,9 +64,11 @@
5964
"WebAPI.Workers",
6065
"Navigator"
6166
],
62-
"WebAPI.File": ["WebAPI.Event", "File"],
67+
"WebAPI.FileEntries": ["WebAPI.DOM", "FileEntries"],
68+
"WebAPI.File": ["WebAPI.Event", "WebAPI.FileEntries", "File"],
6369
"WebAPI.URL": ["WebAPI.DOM", "URL"],
64-
"WebAPI.Fetch": ["WebAPI.Event", "WebAPI.File", "WebAPI.URL", "Fetch"],
70+
"WebAPI.FormData": ["WebAPI.DOM", "FormData"],
71+
"WebAPI.Fetch": ["WebAPI.Event", "WebAPI.File", "WebAPI.URL", "WebAPI.FormData", "Fetch"],
6572
"WebAPI.SafeFetch": ["WebAPI.Fetch", "SafeFetch"],
6673
"WebAPI.CSSFontLoading": ["WebAPI.Event", "CSSFontLoading"],
6774
"WebAPI.Device": ["WebAPI.Event", "Device"],
@@ -90,6 +97,18 @@
9097
"DOMTokenList"
9198
]
9299
},
100+
{
101+
"dir": "src/cssom-core",
102+
"subdirs": true,
103+
"feature": "CSSOMCore",
104+
"public": ["CSSStyleDeclaration", "CSSStyleValue", "MediaList", "StylePropertyMap", "StylePropertyMapReadOnly", "StyleSheet", "StyleSheetList"]
105+
},
106+
{
107+
"dir": "src/dom-tree",
108+
"subdirs": true,
109+
"feature": "DOMTree",
110+
"public": ["DOMTree"]
111+
},
93112
{
94113
"dir": "src/dom-nodes",
95114
"subdirs": true,
@@ -102,7 +121,6 @@
102121
"CharacterData",
103122
"Comment",
104123
"DOMImplementation",
105-
"DOMTree",
106124
"DocumentFragment",
107125
"Element",
108126
"ElementInternals",
@@ -138,14 +156,7 @@
138156
"feature": "CSSOM",
139157
"public": [
140158
"CSSRuleList",
141-
"CSSStyleDeclaration",
142-
"CSSStyleValue",
143-
"MediaList",
144-
"MediaQueryList",
145-
"StylePropertyMap",
146-
"StylePropertyMapReadOnly",
147-
"StyleSheet",
148-
"StyleSheetList"
159+
"MediaQueryList"
149160
]
150161
},
151162
{
@@ -343,8 +354,6 @@
343354
"public": [
344355
"BodyInit",
345356
"Fetch",
346-
"FormData",
347-
"FormDataEntryValue",
348357
"Headers",
349358
"HeadersInit",
350359
"Request",
@@ -365,10 +374,7 @@
365374
"Blob",
366375
"File",
367376
"FileList",
368-
"FileSystemDirectoryEntry",
369377
"FileSystemDirectoryHandle",
370-
"FileSystemDirectoryReader",
371-
"FileSystemEntry",
372378
"FileSystemFileHandle",
373379
"FileSystemHandle",
374380
"FileSystemWritableFileStream",
@@ -481,23 +487,42 @@
481487
"WorkerLocation"
482488
]
483489
},
490+
{
491+
"dir": "src/touch",
492+
"subdirs": true,
493+
"feature": "Touch",
494+
"public": ["Touch", "TouchList"]
495+
},
496+
{
497+
"dir": "src/form-data",
498+
"subdirs": true,
499+
"feature": "FormData",
500+
"public": ["FormData", "FormDataEntryValue"]
501+
},
502+
{
503+
"dir": "src/file-entries",
504+
"subdirs": true,
505+
"feature": "FileEntries",
506+
"public": ["FileSystemEntry", "FileSystemDirectoryEntry", "FileSystemDirectoryReader"]
507+
},
508+
{
509+
"dir": "src/data-transfer",
510+
"subdirs": true,
511+
"feature": "DataTransfer",
512+
"public": ["DataTransfer", "DataTransferItem", "DataTransferItemList"]
513+
},
484514
{
485515
"dir": "src/ui-events",
486516
"subdirs": true,
487517
"feature": "UIEvents",
488518
"public": [
489519
"CompositionEvent",
490-
"DataTransfer",
491-
"DataTransferItem",
492-
"DataTransferItemList",
493520
"FocusEvent",
494521
"InputEvent",
495522
"KeyboardEvent",
496523
"MouseEvent",
497524
"PointerEvent",
498-
"Touch",
499525
"TouchEvent",
500-
"TouchList",
501526
"UIEvent",
502527
"WheelEvent"
503528
]

‎scripts/check-consumer.mjs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import os from "node:os";
44
import path from "node:path";
55
import { fileURLToPath, pathToFileURL } from "node:url";
66
import { validateReleaseArtifact } from "./validate-release-artifact.mjs";
7+
import { dataFeatureConsumers } from "./data-feature-consumers.mjs";
78

89
const repoRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "..");
910
const packageJson = JSON.parse(readFileSync(path.join(repoRoot, "package.json"), "utf8"));
@@ -193,6 +194,14 @@ const checkConsumers = async () => {
193194

194195
if (!negativeOnly) {
195196
const scenarios = [
197+
...dataFeatureConsumers.map((consumer) => ({
198+
...consumer,
199+
fixture: `scripts/${consumer.fixture}`,
200+
expectedFailure:
201+
consumer.missingModule === undefined
202+
? null
203+
: `The module or file WebAPI.${consumer.missingModule} can\'t be found.`,
204+
})),
196205
{ name: "default", fixture: "scripts/consumer-default.res", features: null },
197206
{
198207
name: "concrete public types",
@@ -240,6 +249,18 @@ const checkConsumers = async () => {
240249
projectDirectory,
241250
);
242251
console.log(`[ok] packed consumer runtime using ReScript ${compilerVersion}`);
252+
writeProject(projectDirectory, "scripts/consumer-data-form-runtime.res", ["WebAPI.FormData"]);
253+
compile(projectDirectory);
254+
runCommand(
255+
process.execPath,
256+
[
257+
"--input-type=module",
258+
"--eval",
259+
`await import(${JSON.stringify(pathToFileURL(path.join(projectDirectory, "src/Consumer.js")).href)} + "?formdata")`,
260+
],
261+
projectDirectory,
262+
);
263+
console.log("[ok] packed narrow FormData runtime");
243264
}
244265

245266
const negativeCases = JSON.parse(

0 commit comments

Comments
 (0)