Skip to content

feat: add genExportDefault for ESM export default support - #148

Open
MFA-G wants to merge 1 commit into
unjs:mainfrom
MFA-G:feat/gen-export-default
Open

MFA-G wants to merge 1 commit into
unjs:mainfrom
MFA-G:feat/gen-export-default

Conversation

@MFA-G

@MFA-G MFA-G commented Jun 26, 2026

Copy link
Copy Markdown

Closes #77

Problem

knitwork can generate import, export, and dynamic import() statements, but there is no helper for generating export default <expression>.

Changes

Adds genExportDefault(expression) which returns an export default <expression>; statement.

The expression is used as-is (raw, not escaped or quoted), consistent with knitwork's existing raw-expression helpers such as genObjectFromRaw. Serialization of runtime values is left to the caller, e.g. genObjectFromValues(...) or JSON.stringify(...). This keeps the API unambiguous and composable rather than guessing whether a string is a value or an expression.

genExportDefault("foo");
// ~> export default foo;

genExportDefault("() => {}");
// ~> export default () => {};

genExportDefault(genObjectFromValues({ foo: "bar" }));
// ~> export default { foo: "bar" };
  • src/esm.ts: implement and document genExportDefault
  • test/esm.test.ts: add cases for identifiers, objects, arrow functions, strings, and numbers
  • README.md: regenerated via automd from the new JSDoc

Validation

  • pnpm vitest run — 64 tests passing (5 new)
  • pnpm lint — eslint + prettier clean
  • pnpm build — unbuild succeeds, genExportDefault exported in both ESM and CJS

Summary by CodeRabbit

  • New Features

    • Added a new helper to generate ESM export default statements from a provided expression.
    • The helper supports a range of valid JavaScript expressions, including objects, functions, strings, and numbers.
  • Documentation

    • Expanded the README with clearer usage guidance and examples showing the generated output format.
    • Added a note explaining that expressions are inserted directly, along with a recommendation to use serialization helpers when needed.
  • Tests

    • Added coverage for multiple expression types to verify the generated output matches expectations.

Closes unjs#77

Adds genExportDefault(expression) which generates an
`export default <expression>;` statement. The expression is used
as-is (raw), consistent with knitwork's existing raw-expression
helpers such as genObjectFromRaw, leaving serialization of runtime
values to the caller (e.g. genObjectFromValues or JSON.stringify).

- src/esm.ts: implement and document genExportDefault
- test/esm.test.ts: cover identifiers, objects, arrow fns, strings, numbers
- README.md: regenerated via automd from JSDoc
@coderabbitai

coderabbitai Bot commented Jun 26, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 50db1ab0-11f5-4385-8e2a-14c2a76ddc7b

📥 Commits

Reviewing files that changed from the base of the PR and between a95f4d4 and b45dcb4.

📒 Files selected for processing (3)
  • README.md
  • src/esm.ts
  • test/esm.test.ts

📝 Walkthrough

Walkthrough

Adds genExportDefault(expression) to generate export default ...; strings, documents its direct expression interpolation, and adds tests covering several expression forms.

Changes

Export default helper

Layer / File(s) Summary
Helper and docs
src/esm.ts, README.md
genExportDefault(expression) is added to src/esm.ts, and README.md documents the direct interpolation behavior with example outputs.
Helper tests
test/esm.test.ts
genExportDefault is imported and tested against identifier, object literal, arrow function, string literal, and numeric literal inputs.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I’m a bunny with a fluffy grin,
I hop to the code and let exports in.
default now dances, neat and bright,
With tests that hop along just right.
🐰✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the main change: adding genExportDefault for ESM default exports.
Linked Issues check ✅ Passed The PR implements support for generating export default <exports>; via a new helper and tests it, matching issue #77.
Out of Scope Changes check ✅ Passed The changes are limited to the new helper, its tests, and related README docs, with no obvious unrelated additions.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support export default

1 participant