Skip to content

fix: restore generate-sections/loaders, drop broken ESM bundles - #167

Merged
tlgimenes merged 1 commit into
mainfrom
tlgimenes/fix-5.1.0-scripts
May 11, 2026
Merged

fix: restore generate-sections/loaders, drop broken ESM bundles#167
tlgimenes merged 1 commit into
mainfrom
tlgimenes/fix-5.1.0-scripts

Conversation

@tlgimenes

@tlgimenes tlgimenes commented May 11, 2026

Copy link
Copy Markdown
Contributor

Summary

Patches three regressions in 5.1.0's build-time scripts surface:

  • generate-sections / generate-loaders are back. Sources still existed in scripts/ but had been dropped from the tsup entries in PR Framework-agnostic entrypoints: /core, /tanstack, /next, /node #164; files: ["dist"] then hid them from consumers.
  • CLI scripts are now CJS-only. The 5.1.0 ESM bundles failed immediately with Dynamic require of "fs" is not supported because tsup used platform: "neutral" — switching the scripts bundle to format: ["cjs"] + platform: "node" fixes it and stops shipping a broken .js alongside each .cjs.
  • generate-invoke resolution + error. Now walks up node_modules from CWD and distinguishes "@decocms/apps not installed" from "installed but vtex/invoke.ts not shipped" (the published tarball omits the source file the script parses) with an actionable error pointing at --apps-dir.

CHANGELOG entry added as 5.1.1 since 5.1.0 was published as a minor but acted as a breaking change for any site running the generators in CI.

Test plan

  • bun run build clean (all 10 scripts emit, no .js files)
  • bun run typecheck passes
  • bun run test — 45 files, 541 tests passing
  • node dist/scripts/generate-schema.cjs no longer throws the dynamic-require error
  • node dist/scripts/generate-invoke.cjs against a site with @decocms/apps@1.13.0 installed gives the new actionable error
  • node dist/scripts/generate-sections.cjs and generate-loaders.cjs run end-to-end in a scratch dir

🤖 Generated with Claude Code


Summary by cubic

Restores generate-sections and generate-loaders, makes all CLI scripts CJS-only to stop ESM crashes, and improves generate-invoke resolution and error messaging. This fixes 5.1.0 regressions that broke build-time generators.

  • Bug Fixes

    • Restored scripts/generate-sections and scripts/generate-loaders to the tsup build and package.json exports; both now ship as .cjs.
    • Switched script builds to CJS-only (format: ["cjs"], platform: "node"); drops broken ESM .js and fixes “Dynamic require of 'fs' is not supported”.
    • generate-invoke now walks up node_modules to find @decocms/apps and shows a clear error when vtex/invoke.ts is missing, with a hint to use --apps-dir.
  • Migration

    • Call the compiled .cjs scripts, e.g. node node_modules/@decocms/start/dist/scripts/generate-schema.cjs.
    • For generate-invoke, point --apps-dir to a local checkout that includes the source (e.g. ../apps-start), or skip regeneration if invoke.gen.ts is already committed.

Written for commit 266a48f. Summary will update on new commits.

Patches three 5.1.0 regressions in the build-time scripts surface:

- generate-sections and generate-loaders are rebuilt and re-exported.
  Sources were still in scripts/ but had been dropped from the tsup
  entries (PR #164 oversight); files: ["dist"] then hid them.
- Scripts bundle switches to format: ["cjs"] + platform: "node". The
  prior platform: "neutral" ESM emit wrapped externalized require() in
  a __require shim that threw "Dynamic require of fs" as soon as
  ts-morph's bundled typescript loaded. CLI scripts are CJS-only now.
- generate-invoke resolveAppsDir() walks up node_modules from CWD and
  distinguishes "package missing" from "vtex/invoke.ts not shipped"
  (the common case — the published @decocms/apps tarball omits the
  source file the script parses).

CHANGELOG documents this as 5.1.1.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@tlgimenes tlgimenes changed the title fix(scripts): restore generate-sections/loaders, drop broken ESM bundles fix: restore generate-sections/loaders, drop broken ESM bundles May 11, 2026
@tlgimenes
tlgimenes merged commit 154059c into main May 11, 2026
2 checks passed
@github-actions

Copy link
Copy Markdown

🎉 This PR is included in version 5.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

vibe-dex added a commit that referenced this pull request May 19, 2026
Reverts the full chain of commits introduced in PRs #164, #165, #166,
#167, #170, #174, #175, #176, #177 plus the two semantic-release auto
commits (5.1.0, 5.1.1).

Root cause:
PR #164 (Framework-agnostic entrypoints: /core, /tanstack, /next,
/node) repointed package.json exports from src/ to dist/ and split
the framework into multiple bundle chunks via tsup. This is correct
in principle, but the tsup output produces a separate copy of every
module per entry tier. Files with module-level mutable state
(let foo = ..., new Map(), new Set() at top level) end up with one
copy per dist entry, so writes through one export resolve a
different copy than reads through another export.

Symptoms observed on @decocms/start@5.1.0+ and 5.3.0-rc.0/rc.1:

  - GET/POST /deco/invoke/site/loaders/<any-site-loader>
    → 404 "Unknown handler: site/loaders/<name>"
    despite the loader being correctly emitted into
    site/server/cms/loaders.gen.ts and registered via
    setInvokeLoaders() in setup.ts. The site's setup.ts mutates
    the getRegisteredLoaders closure inside dist/core/admin/index.js,
    while handleInvoke (mounted on the catch-all route) reads
    getRegisteredLoaders from a different copy of invoke.ts bundled
    into dist/tanstack/routes/index.js — which still holds the
    default () => ({}).

  - OTel direct-POST channels silently dropped (metrics + error
    logs) — same pattern in src/core/sdk/observability.ts and
    src/core/sdk/logger.ts. Partially fixed on the
    fix/o11y-shared-state-singleton branch (the rc.1 work) but the
    fix never covered invoke.ts handler registry.

PR #167 patched two other #164 regressions (CLI scripts dropped from
the tsup entries, ESM bundle wrapping require() in a broken __require
shim) and PR #166 finalized the publish surface — both strictly
downstream of #164. #174 (router preload options) and #170/#175/
#176/#177 (CI / release machinery) build on the same foundation.

Path forward:
A future re-attempt at framework-agnostic entrypoints must (a) emit a
single shared bundle for the admin/invoke handler registry, the
observability state, and any other module-level mutable state, or
(b) hoist that state onto a globalThis-keyed singleton from the
start, with a CI guard that fails any new module-level mutable state
inside src/core/. This revert intentionally takes us back to the
single-bundle src/-exports world where the bug is latent but inert.

Verification:
  - npm run build      → succeeds against the pre-#164 source layout
  - On a casaevideo build pinned to a snapshot from this revert,
    POST /deco/invoke/site/loaders/adressByCep returns 500 ("Cannot
    read properties of undefined (reading 'replace')" from the loader
    body, not a 404) — i.e. the registry contains the handler again,
    matching v5.0.0 behaviour.

This is the working-baseline state. Re-landing #174's preload options
on top of the restored src/ layout is a follow-up.

Co-authored-by: Cursor <cursoragent@cursor.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant