esm-to-cjs
esm-to-cjs copied to clipboard
wrong result when object literal is exported as default
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 :)
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, {})