refactor(headless): import coveo.analytics modules instead of vendoring them - #8169
refactor(headless): import coveo.analytics modules instead of vendoring them#8169alexprudhomme wants to merge 3 commits into
Conversation
🦋 Changeset detectedLatest commit: 7aeeb49 The changes in this PR will be included in the next version bump. This PR includes changesets to release 19 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
@coveo/atomic
@coveo/atomic-hosted-page
@coveo/atomic-legacy
@coveo/atomic-react
@coveo/auth
@coveo/bueno
@coveo/create-atomic
@coveo/create-atomic-component
@coveo/create-atomic-component-project
@coveo/create-atomic-result-component
@coveo/create-atomic-rollup-plugin
@coveo/create-ui
@coveo/headless
@coveo/headless-react
@coveo/relay
@coveo/shopify
commit: |
|
Tip All tests passed and all changes approved!🟢 UI Tests: 465 tests unchanged |
1e66ed0 to
ae9bfe9
Compare
382d4f6 to
791814a
Compare
791814a to
4aea200
Compare
ae9bfe9 to
9b8e91e
Compare
4aea200 to
6a61ff2
Compare
6a61ff2 to
f557ba9
Compare
There was a problem hiding this comment.
Pull request overview
Refactors @coveo/headless to stop vendoring several coveo.analytics internals (cookie/detector/storage/history-store) and instead consume the newly published per-module ESM output, keeping Headless’s singleton HistoryStore behavior via a thin subclass wrapper.
Changes:
- Deleted the vendored
cookie,detector, andstoragemodules (and their unit tests) from Headless. - Replaced the vendored
history-storeimplementation with a subclass ofcoveo.analytics/dist/esm/history.mjsthat preserves the shared-instance (getInstance) behavior. - Updated Headless’ esbuild aliasing to ensure deep
coveo.analytics/dist/esm/history.mjsimports are bundled (avoidingrequire()of ESM in CJS output), and added a changeset.
Reviewed changes
Copilot reviewed 10 out of 10 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/headless/src/api/analytics/coveo.analytics/storage.ts | Deleted vendored storage implementation (now sourced transitively from coveo.analytics history module). |
| packages/headless/src/api/analytics/coveo.analytics/storage.test.ts | Deleted Headless storage unit tests (coverage now expected upstream). |
| packages/headless/src/api/analytics/coveo.analytics/history-store.ts | Replaced vendored HistoryStore with a subclass wrapper around coveo.analytics’ per-module ESM HistoryStore and re-exported HistoryElement type. |
| packages/headless/src/api/analytics/coveo.analytics/history-store.test.ts | Updated tests to import constants/types from coveo.analytics/dist/esm/* while continuing to test the Headless wrapper. |
| packages/headless/src/api/analytics/coveo.analytics/detector.ts | Deleted vendored detector implementation. |
| packages/headless/src/api/analytics/coveo.analytics/detector.test.ts | Deleted Headless detector unit tests (coverage now expected upstream). |
| packages/headless/src/api/analytics/coveo.analytics/cookie.ts | Deleted vendored cookie implementation. |
| packages/headless/src/api/analytics/coveo.analytics/cookie.test.ts | Deleted Headless cookie unit tests (coverage now expected upstream). |
| packages/headless/esbuild.mjs | Added deep-path aliasing for coveo.analytics/dist/esm/history.mjs to keep it bundled and avoid require(ESM) in generated CJS output. |
| .changeset/headless-cajs-per-module-esm.md | Added changeset documenting the refactor. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
…e cajs aliases Keeps `new HistoryStore()` unreachable from outside the class, as the vendored copy did, and extracts the duplicated coveo.analytics alias map so the browser and node esbuild configs cannot drift.
KIT-5959
Problem
Headless vendors four coveo.analytics modules under
packages/headless/src/api/analytics/coveo.analytics/—cookie.ts,detector.ts,storage.ts,history-store.tsplus their tests. They were copied in #5346 because coveo.analytics shipped only bundled artifacts, so importingHistoryStorewould have pulled a whole bundle into every consumer.That copy has drifted: the
Securecookie fix landed only in Headless (ported back in #8156), and the Headless copy also gained IPv4/IPv6 host handling that coveo.analytics still lacks.#8157 publishes real per-module ESM, so the copy is no longer necessary.
Solution
Delete the vendored modules and import from
coveo.analytics/dist/esm/*.mjs.Only
history-storewas imported outside the vendored folder (12 call sites);cookie,detectorandstorageexisted purely to serve it, so they delete outright.history-store.tsshrinks from 187 lines to a subclass that keeps the shared-instance behavior Headless relies on:Subclassing keeps
HistoryStoreusable as both a value and a type, so all 12 call sites and the existinghistory-store.test.tsare untouched.Net: -1033 / +40 lines.
esbuild alias (required, not cosmetic)
The first build silently emitted this into
dist/cjs/headless.cjs:require()of an ESM file. It happens to work on Node 22.20, but throwsERR_REQUIRE_ESMon older Node, and this package declaresengines.node: ^20.9.0 || ^22.11.0 || ^24.11.0—require(ESM)only arrived in 20.19 / 22.12, so the bottom of the supported range would break.Cause:
esbuild.mjsusespackages: 'external'together with an alias that maps barecoveo.analyticsto an absolute path viaresolveEsm. Absolute paths are not externalized, so coveo.analytics is bundled today. A deep specifier is not covered by that alias, so it stayed external. Fixed by aliasing the deep path the same way. Verified afterwards: zerorequire("coveo.analytics...")across every CJS use-case bundle, andHistoryStoreis inlined again.Any future deep import into coveo.analytics needs the same treatment.
Bundle size
Measured from clean full rebuilds of
packages/headless/diston each side:esm/cjs/definitions/esm/api/analytics/cjs/headless.cjsesm/file countEssentially flat, and that is the expected result. The CJS bundle inlines the modules either way, so this is a swap rather than a removal. The value here is deleting ~1000 lines of duplicated source and the drift risk that comes with it — not bytes. Framing it as a size optimization would be wrong.
Dropped IP-host cookie branch (local dev only)
The vendored
cookie.tsomits the cookiedomainattribute for bare-IP hosts;coveo.analytics/src/cookieutils.tshas no equivalent, so this PR drops it (#8174, closed).It only matters when a page is served from a bare IPv4 host (
http://192.168.1.1:3000, not a hostname) andlocalStorageis unavailable — the only case where the vendoredCookieis reachable at all. There, actions history stops working. No deployment on a real domain is affected, andvisitorIdnever used this path.It was authored in the vendored copy in #5346 (
80d43f02c9) while adding review-requested tests, not to fix a bug — a local divergence being retired, not a fix being lost.Verification
tsc --noEmitclean.history-store.test.tsunchanged against the subclass.@coveo/headless#buildpasses; no ESMrequirein any CJS output.lint:checkclean.