New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
@babel/plugin-transform-modules-amd doesn't behave as expected #9793
Comments
Hey @jslegers! We really appreciate you taking the time to report an issue. The collaborators If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack |
There isn't: a default export is just a named export whose name is export default { foo: 2 };
export const foo = 3; |
Why not? What I would expect as output in that case, is for only the default export to be exported and for the named export to be ignored. AMD doesn't know the concept of named & default exports, really, and trying to include both when you convert from ES6 to AMD therefore doesn't really make much sense... especially considering it doesn't allow converting from legacy AMD to ES6 and back. Our use caseAt Luciad, we are currently in the process of converting an AMD library to ES6, but we need to maintain backwards compatibility for all of our older customers who still use AMD, by converting our ES6 modules back to AMD and shipping it in our release, along with our ES6 version. At the moment I'm using jonbretman's I'm using ecomfe's For most part, this two-way conversion seems to work as expected. However, the latter only supports either default or named exports. Instead of ignoring one of them or including both, it just throws a For maintainability sake I would prefer to use an official babel plugin, but with your current implementation this is not possible for us, as it offers no way to get the expected output. |
That's not how es modules work. AMD and CommonJS aren't compatible with ES Modules, and the Another example that doesn't work without the // module.js
export default function foo() { return 2 }
foo.update = function () {
foo = 3;
} // main.js
import { default as x } from "./module.js"
console.log(x); // a function
x.update();
console.log(x); // 3 If you want to use
Can't you just add it to your normal Babel config?
Also not setting the default export using the
It is better to throw a TypeError at compile time, rather than it being ignored and then having bugs caused by missing exports (
). Everything that can be reported at compile-time rather than at runtime should be reported, and that plugin is doing the correct thing. |
@nicolo-ribaudo is it possible to make it so that plugin accepts an argument that allows for what @jslegers is requesting? Our business is in a similar position and the ability to not have to refactor legacy code to make everything use |
Bug Report
I have the following
package.json
:I have the following
.babelrc
:I have the following es6 module in my
es6/ria
folder :If I run
npm run build
This produces an AMD module with the following implementation :When I try to load the
Evented
module with eg. Require.Js, I expect theEvented
"class".Instead I get an object with a
default
property, which contains theEvented
"class".Is this a bug? Or is it designed this way?
And it it isn't a bug, is there a way to achieve the desired effect?
Is there a way I can convert this ES6 to AMD with the @babel/plugin-transform-modules-amd plugin, so it returns the
Evented
"class" as expected?The text was updated successfully, but these errors were encountered: