esm-to-cjs icon indicating copy to clipboard operation
esm-to-cjs copied to clipboard

wrong result when object literal is exported as default

Open iambumblehead opened this issue 9 months ago • 1 comments

import esmToCJS from 'esm-to-cjs'

console.log(esmToCJS.runTransform(`
const prop1 = 'val1'
const prop2 = 'val2'

export default {
  prop1
  prop2
}
`, {}))
// const prop1 = 'val1'
// const prop2 = 'val2'
//
// {
//   prop1
//   prop2
// }
//
// module.exports = {
//   default
// }

Hello, this package is an awesome idea. It seems esm is designed purposefully to prevent transformations that would empower possible interoperability with cjs, umd or other approaches. The above transformation example shows a wrong-transformation. thanks :)

iambumblehead avatar May 06 '24 23:05 iambumblehead

Not sure if this is a safe transformation generally, but this appears to be okay for limited usages tested here so far,

import esmToCJS from 'esm-to-cjs'

str = `
const prop1 = 'val1'
const prop2 = 'val2'

export default {
  prop1
  prop2
}
`
str = str.replace(/export default/g, 'module.exports =')
str = esmToCJS.runTransform(str, {})

iambumblehead avatar May 07 '24 00:05 iambumblehead