feat: make loadConfig synchronous#22
Merged
Merged
Conversation
Contributor
Preview releaseLatest commit: Some packages have been released:
Note Use the PR number as tag to install any package. For instance: |
d1e41de to
8620276
Compare
bbc6590 to
00d51b7
Compare
`@vlandoss/env/fs`'s `loadConfig` now loads the config with `require()` and
returns `Config<S>` directly instead of a `Promise`. One function, no `await` —
so it works in app code and in config files that tooling loads via `require()`
or bundles to CJS, where a top-level `await` is rejected.
BREAKING: `loadConfig` is synchronous. Drop the `await`:
- const config = await loadConfig(Env)
+ const config = loadConfig(Env)
(Awaiting a non-promise still returns the value at runtime, but the `await`
keeps the module async so a config file can't be require()'d — remove it.)
- fs: sync `loadConfig` over a shared pure core; loads via `require()`
- unwrapDefault: discriminates ESM namespace by `Symbol.toStringTag === "Module"`
(verified consistent across Node 22.22.3, Bun 1.2.4, Deno 2) — a CJS exports
object that owns a `default` property is no longer silently stripped; sibling
keys survive
- auto-discovery: now also covers `.cjs` / `.cts` (was just .ts/.mts/.js/.mjs/.json)
- options: `loadConfig({ schema, pattern?, cwd? })` — `cwd` overrides
`process.cwd()` for orchestrators / SSR workers / monorepo runners
- tests: fs.test.ts now covers CJS auto-discovery, the `default`-key
preservation invariant (regression guard), and the `cwd` option
- examples: backend-{node,bun,deno} drop the await; backend-{node,bun,deno}-cjs
boot the server from a CommonJS require() entry, verified with Playwright e2e
(validation.spec spawns now pin `cwd` for cwd-independence)
- engines: bump to >=22.12 (the `require(esm)` baseline). Per-extension
requirements documented: .ts/.mts/.cts need Node ≥22.18 (native TS stripping);
.mjs/.js/.cjs need ≥22.12; .json works on any supported Node
- docs: loadConfig guide, fs reference, quickstart, ssr, landing, examples tables
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
00d51b7 to
e0021bb
Compare
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
loadConfigwas async only because dynamicimport()is — the async-ness was incidental, not a feature. Pre-v1, collapsing it to a single synchronous function is simpler and strictly more capable: it works in app code and in config files that tooling loads viarequire()or bundles to CJS (e.g. database/ORM tool configs), where a top-levelawaitis rejected (ERR_REQUIRE_ASYNC_MODULE).Verified across the matrix:
require(esm)+ native TS stripping load.ts/.mjs/.jsonconfigs synchronously on Node ≥22.18, Bun, and Deno (withcreateRequirebased on the config path, so it stays clean even when a config is bundled to CJS).What
@vlandoss/env/fsexports a single, synchronousloadConfig— same auto-discovery and{env}template shapes, but it loads withrequire()and returnsConfig<S>directly:BREAKING
loadConfigis now synchronous — drop theawait:await loadConfig(...)still returns the right value at runtime (awaiting a non-promise yields the value), but theawaitkeeps the module asynchronous — a config file that keeps it still can't berequire()'d. Remove it. (Minor bump: pre-v1.)Changes
loadConfigis sync, over a shared pure core (discovery,{env}template, default-export unwrap, normalization); loads viarequire().fs.test.tscovers the sync loader — auto-discovery,.tsvia require, template, throws, composition.backend-{node,bun,deno}drop theawait; newbackend-{node,bun,deno}-cjsboot the server from a CommonJSrequire()entry (server.cjs) and are e2e-tested with Playwright — they only boot becauseloadConfigis sync.require(esm)+ native TS stripping).loadConfigguide, fs reference, quickstart, SSR guide, landing snippet, examples tables — all reflect the sync API.Verification
backend-{node,bun,deno}+backend-{node,bun,deno}-cjs(6 each) andssr-react-router(4) pass.rr check(package + docsite): pass.🤖 Generated with Claude Code