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
Destructing CommonJS export with import #1483
Comments
We're discussing making this work at the moment actually in babel/babel#95. |
In which environment are you running this? Are you compiling to commonjs/AMD or using |
@UltCombo I'm using |
Trying to compile your ES6 sample to CJS also fails with the error |
I do not think this makes sense. Remember that |
Oh yes, @domenic is right, the CJS |
Well this is an important thing to discuss! We can do it, by making the module for a CommonJS module effectively: function createES6Module(cjsModule) {
var es6Module = {};
// exports copied from CommonJS object
Object.keys(cjsModule).forEach(function(export) {
es6Module[export] = cjsModule[export];
});
// default export is entire CommonJS object
es6Module.default = cjsModule;
} So the question is whether we want this decorated CommonJS ES6 form supported or not (to allow |
@guybedford as a side effect, wouldn't that allow both |
IMO we should not go to that trouble. (Because then you have to worry about when IMO our story should be:
In this way the interop story is a simple band-aid for people who hate to see the word |
Yes I've advocated for the simple interop as you've mentioned above, but was under the impression others were keen to see exports for CommonJS imports (@caridy). Note that the |
It's not about being a person who hates seeing |
@guybedford I'm supportive of @domenic suggestions, I think that is good enough to facilitate the transition, and I have been pushing back on the On the flip side of that coin, to require a ES6 module from a CommonJS module, we are advocating for a very simple shim, created manually by the pkg author, to simply define the |
correct, e.g.:
|
@guybedford From my point of view, transpilers should follow spec'd behavior. AFAIK, Node.js has no solid plans to support ES6 modules yet. It looks like you're trying to p(r)olyfill ES6 modules support into Node, but instead your logic simply transpiles ES6 syntax into CJS syntax. IMO this is bad because:
|
@UltCombo agreed, but this is not different from using cjs modules in a browser (thru browserify or webpack), where you need a build process to produce code for a different runtime. In most cases, we end up authoring modules in one format, and compiling it into distros for the different runtimes, and supporting interoperability thru all those different formats. In our case, the whole FormatJS project is written in ES6, each library is transpiled into two other formats: bundle format (browser distro) and cjs (nodejs distro), but still you can create a new library that depends on some of those libraries using the ES6 semantics by depending on the ES6 source code directly by using the It works very well, but, yes, it is a lot of extra work and boilerplate to achieve it. And obviously, this is only needed for a library, apps are a completely different story. |
... and one more note, when we transpile, the intention is always to output a module that works with the semantics of the output format. Doing it the other way around to try to bring alien semantics to one of those runtimes will be a mistake. |
@caridy thanks for the insight, it makes a lot of sense. I wonder if it would at all be possible to simplify the overall process. My initial thought was to use |
How come this doesn't work?
The text was updated successfully, but these errors were encountered: