unjs / mlly

🤝 Common ECMAScript module utils
MIT License
471 stars 36 forks source link

interopDefault breaks when combining a default string export with named exports #194

Closed hayes closed 10 months ago

hayes commented 1 year ago

Environment

node 18

Reproduction

require('mlly').interopDefault({ default: 'test', x: 123 })

Describe the bug

I would expect in this case that the original module would be returned rather the just the default export test

Additional context

Checking if newModule is an object after it is assigned here: https://github.com/unjs/mlly/blob/main/src/cjs.ts#L38C3-L38C3 and returning the sourceModule instead if its not an object or function should work

Logs

No response

pi0 commented 1 year ago

By default interopDefault returns a proxified object that allows mixed syntax usage. We might support interopDefault({ default: 'test', x: 123 }, { strict: true }) to always return default key.

pi0 commented 10 months ago

preferNamespace added as an opt-in (and non behavior changing) feature see https://github.com/unjs/mlly/commit/5d23c98a46fefd6e812fe837cbd849164eec6b50 and https://github.com/unjs/mlly/commit/14eb72d0f571e06dcd127c0d5fec7b4fa49609f9

I hope it can help in your case but if you have some usecases that belive this behavior being default could be beneficial please mention.