fix: load CLI configs with top-level await - #4100
pengboyu-dev wants to merge 1 commit into
Conversation
|
@pengboyu-dev is attempting to deploy a commit to the MarkedJS Team on Vercel. A member of the Team first needs to authorize it. |
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| @@ -180,7 +180,7 @@ export async function main(nodeProcess) { | |||
| // try require for json | |||
| markedConfig = require(configFile); | |||
There was a problem hiding this comment.
I think we can remove require(configFile) now since we can always run await import(pathToFileURL(configFile).href) with any LTS version of Node.js, and it works with ESM and CJS
There was a problem hiding this comment.
Thanks! Agreed that dynamic import() can handle ordinary ESM and CJS configs. I checked the existing loader behavior and found two compatibility details we'd need to preserve:
- The CLI documents and automatically loads
~/.marked.json. A plainimport()fails for JSON unless we handle it explicitly or supply the appropriate import attribute. - CJS configs such as
exports.default = { breaks: true }currently work. Withimport(), the additional namespace wrapper means the existing single.defaultunwrap no longer reaches the config; the CLI exits successfully but the option is not applied.
I verified the current patch across Node 20, 22, 24, and 26: it fixes configs with top-level await while preserving those existing cases. My preference would be to keep this PR as the targeted fix and handle the loader simplification separately with compatibility tests. Does that sound reasonable?
The CLI cannot load an ESM config containing top-level
awaiton Node.js versions that support synchronousrequire(esm). For example:marked --config config.mjs --string textexits withERR_REQUIRE_ASYNC_MODULEon Node 24.19.0. The config loader only falls back toimport()forERR_REQUIRE_ESM, so it never reaches the existing asynchronous import path.Treat
ERR_REQUIRE_ASYNC_MODULEas another reason to use that path. Other loading errors continue to propagate, and the existing JSON/CommonJS loading path is unchanged.Validation:
await; it fails before the fix and passes after it.npm testpasses on Node 24.19.0, including specs, unit tests, UMD/CJS, types, and lint.Contributor