Skip to content

Adopt the CLDMV library layout: fix the broken dev-mode require, rename json-dev → jsonv-dev, CJS wrappers beside their ESM files #110

Closed

Description

@Shinrai

Problem

  • Dev-mode require is broken. The json-dev condition on "." maps require to ./src/cjs/index.cjs, which doesn't exist on next (src/ has no .cjs files). So require("@cldmv/jsonv") with --conditions=json-dev fails to resolve.
  • The condition has the wrong name. It's json-dev, not jsonv-dev. The fleet namespaces the condition by package name so it can't collide with another package's condition.
  • The CJS wrappers live in a cjs/ folder. scripts/build-cjs.mjs writes thin require(esm) wrappers into dist/cjs/ (dist/cjs/index.cjs, dist/cjs/years/*.cjs) instead of beside their ESM files. That gives each entry two parallel trees, and the wrappers can't be tested from source.
  • The build is a chain of custom scripts: tsc (build:ts), tsc again for types, scripts/generate-years.mjs, scripts/build-cjs.mjs.

Target layout

The convention is in CLDMV/llm-translator (PR #1): a hand-written src/index.cjs beside the ESM entry, copied verbatim beside its built ESM file.

package.json   "." → { "jsonv-dev": { types: ./src/index.mts, import: ./src/index.mts },
                       types: ./dist/types/index.d.mts, import: ./dist/index.mjs, require: ./dist/index.cjs }
               "./parser", "./*" (years) → same pattern, jsonv-dev → src/
src/
  index.mts    the real exports
  index.cjs    require_module guard + require("./index.mjs") — copied to dist/index.cjs
dist/
  index.mjs, index.cjs           (no dist/cjs/)
  years/<year>.mjs, years/<year>.cjs

Tasks

  • Rename the condition json-dev → jsonv-dev everywhere: package.json exports, the vitest config, and test/CJS scripts.

  • Fix dev-mode require. The source is TypeScript (.mts), so a src/index.cjs doing require("./index.mjs") has no sibling to load in src/. Pick one, and document it in the README's development section:

    • (a) leave require off the jsonv-dev condition, so CJS consumers in a checkout get dist/ (tested by tests/cjs/entry.test.cjs); or
    • (b) have the CJS wrapper load the .mts through Node's type stripping (Node ≥ 22.18 / 23.6).

    Either way, remove the pointer to the missing ./src/cjs/index.cjs.

  • Move the wrappers next to their ESM files: dist/index.cjs beside dist/index.mjs, dist/years/<year>.cjs beside dist/years/<year>.mjs. Drop dist/cjs/ and dist/types/cjs/, and update main, exports.require and the wrappers' require paths. Write src/index.cjs by hand, as in llm-translator. The per-year wrappers can stay generated, but are written beside each year's .mjs.

  • Build: consider replacing the tsc + script chain with tsup, which handles .mts; keep generate:years and build:types.

  • package.json:

    • engines.node: ^20.19.0 || >=22.13.0, because the CJS entry needs require(esm) and Node 22.12.x warns when require() loads ESM (today >=18.0.0)
  • Tests: pass --conditions=jsonv-dev to vitest (resolve.conditions, ssr.resolve.conditions, test.nodeOptions) and to node --test. tests/cjs/entry.test.cjs covers dist/index.cjs and a sample dist/years/<year>.cjs.

  • ci.yml: coverage_build_command: "npm run build:ci". bundle-size.yml: dist_paths = the real dist/ output.

Compatibility

  • json-dev rename: only affects contributors running from a checkout; published consumers never see it.
  • dist/cjs/ → dist/*.cjs move: invisible to consumers going through exports.
  • Deep requires of @cldmv/jsonv/dist/cjs/...: exports doesn't expose these paths, so no consumer can be relying on them.

Activity

  1. added
    type: enhancement
    priority: medium
    area: core
    status: not started
    on Oct 25, 2026
  2. Shinrai commented on Oct 31, 2026

    @Shinrai
    ContributorAuthor

    Before this gets picked up: don't use the fire-and-forget async devcheck block from the task list. It doesn't gate anything. Code that imports the package and calls it straight away runs before devcheck exits, from ESM and from CJS. Load it synchronously at the top of src/index.mts instead:

    import { createRequire } from "node:module";
    
    try {
    	createRequire(import.meta.url)("../devcheck.mjs");
    } catch (error) {
    	if (error?.code !== "MODULE_NOT_FOUND") throw error; // devcheck.mjs is not published
    }

    Still no top-level await, so the CJS wrappers' require(esm) keeps working. Keep the ../devcheck.mjs specifier unchanged in the build output (external in tsup, or as-is from tsc). The dist test should look for the "../devcheck.mjs" specifier in dist/index.mjs, not for import(...).

    Working copy is fix-headers' src/index.mjs in CLDMV/fix-headers#165 (ships in 2.2.4).

  3. Shinrai commented on Oct 31, 2026

    @Shinrai
    ContributorAuthor

    Correction to my comment above: don't use that block either. The static import { createRequire } from "node:module" breaks browser bundles (esbuild and webpack fail the build, Vite's bundle throws at load), and jsonv runs in browsers. Use this instead:

    const nodeModule = typeof process === "undefined" ? undefined : process.getBuiltinModule?.("node:module");
    if (nodeModule) {
    	try {
    		nodeModule.createRequire(import.meta.url)("../devcheck.mjs");
    	} catch (error: any) {
    		if (error?.code !== "MODULE_NOT_FOUND") throw error; // devcheck.mjs is not published
    	}
    }

    Same synchronous gate, nothing for a bundler to resolve, and it skips itself in a browser. Checked against esbuild/webpack/Vite (browser and node), headless Chrome loading src/ directly, Node 20.19 / 22.12 / 26, ESM and CJS, and from a .mts entry under Node's type stripping. Working copy is still CLDMV/fix-headers#165.

  4. Shinrai commented on Nov 1, 2026

    @Shinrai
    ContributorAuthor

    One more thing with the sync devcheck load: on Node 22.12.x, require() of devcheck.mjs prints an ExperimentalWarning (require of an ES module) on every source-checkout run. 20.19 and 22.13+ don't warn. Consumers of the published package never hit it since devcheck.mjs isn't shipped.

    When you adopt it, set engines.node to ^20.19.0 || >=22.13.0 (the CJS shim needs require(esm) anyway, so anything below 20.19 is already wrong) and keep the CI min_node_version off 22.12. fix-headers went to >=22.13.0 in CLDMV/fix-headers#165.

  5. changed the title [-]Adopt the CLDMV library layout: fix the broken dev-mode require, rename json-dev → jsonv-dev, CJS wrappers beside their ESM files, devcheck[/-] [+]Adopt the CLDMV library layout: fix the broken dev-mode require, rename json-dev → jsonv-dev, CJS wrappers beside their ESM files[/+] on Nov 1, 2026
  6. Shinrai commented on Nov 1, 2026

    @Shinrai
    ContributorAuthor

    Dropped the devcheck part, it's being removed from all the packages instead.

  7. added
    status: in progress
    and removed
    status: not started
    on Nov 1, 2026
  8. added
    status: implemented
    and removed
    status: in progress
    on Nov 1, 2026
  9. cldmv-bot commented on Nov 1, 2026

    @cldmv-bot
    Contributor

    Marked status: implemented — resolved by #112, merged into next. This issue closes automatically when that work ships to the default branch.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corepriority: mediumstatus: implementedtype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions