esnext / es6-module-transpiler

Tomorrow’s JavaScript module syntax today
http://esnext.github.io/es6-module-transpiler/
Other
1.21k stars 84 forks source link

Default exports #143

Closed matthewrobb closed 10 years ago

matthewrobb commented 10 years ago

I may be missing something but I cannot get default exports to work with the current or previous few versions of es6-module-transpiler.

Error:

...\node_modules\es6-module-transpiler\lib\exports.js:54
  } else if (n.ExportBatchSpecifier.check(node.specifiers[0])) {
                                                         ^
TypeError: Cannot read property '0' of null
    at ExportDeclarationList.declarationForNode (...\node_modules\es6-module-transpiler\lib\exports.js:54:58)
    at ...\node_modules\es6-module-transpiler\lib\module_binding_list.js:213:17
    at Array.map (native)
    at ExportDeclarationList.module.exports (...\node_modules\es6-module-transpiler\lib\module_binding_list.js:212:22)
    at ExportDeclarationList.Object.defineProperty.get (...\node_modules\es6-module-transpiler\lib\utils.js:11:31)
    at ...\node_modules\es6-module-transpiler\lib\rewriter.js:60:19
    at Array.forEach (native)
    at ...\node_modules\es6-module-transpiler\lib\rewriter.js:59:32
    at Array.forEach (native)
    at Rewriter.rewrite (...\node_modules\es6-module-transpiler\lib\rewriter.js:58:11)

When looking a little deeper it appears that the ast node used in exports.js has the default property undefined no matter what:

/**
 * Gets an export declaration for the given `node`.
 *
 * @private
 * @param {ast-types.ExportDeclaration} node
 * @return {Import}
 */
ExportDeclarationList.prototype.declarationForNode = function(node) {
  if (node.default) {
    return new DefaultExportDeclaration(this.module, node);
  } else if (n.VariableDeclaration.check(node.declaration)) {
    return new VariableExportDeclaration(this.module, node);
  } else if (n.FunctionDeclaration.check(node.declaration)) {
    return new FunctionExportDeclaration(this.module, node);
  } else if (n.ExportBatchSpecifier.check(node.specifiers[0])) {
    throw new Error(
      '`export *` found at ' + sourcePosition(this.module, node) +
      ' is not supported, please use `export { … }` instead'
    );
  } else {
    return new NamedExportDeclaration(this.module, node);
  }
};
caridy commented 10 years ago

this normally happen when you have a linked pkg that contains recast as a dep, while you have another recast dep that does the parsing. Even if the version of the recast pkgs are the same, it seems to be a problem with the way recast compare references. Nothing to do with the transpiler IMO.

matthewrobb commented 10 years ago

@caridy I did a bit more digging based on what you said and I found that the problem stems from the fact that recast depends on a different esprima than the version es6-module-transpiler depends on. If I adjust recast to have the same dependency then it works, otherwise not.

matthewrobb commented 10 years ago

It might be valuable to put some documentation up somewhere that in order to use es6-module-transpiler with other recast transformers you need to pass esprima into recast.parse() and it has to be git://github.com/thomasboyt/esprima#4be906f1abcbb

Alternatively, perhaps exporting esprima from es6-module-transpiler would be a good choice. Allowing people to do recast.parse(..., { esprima: require("es6-module-transpiler").esprima })

gigr commented 10 years ago

@matthewrobb Care to share some more details as to how you resolved this issue?

eventualbuddha commented 10 years ago

I believe the current plan is to standardize on using esprima-fb instead of esprima#harmony. That way npm can properly dedupe and everyone will be happy :wink:

caridy commented 10 years ago

I think we can close this one now.

ericf commented 10 years ago

Maybe this should be reopened. With v0.9.0 when using:

export * from './foo';

I'm seeing this error:

Error writing bundle in "bundle.js": Error: `export *` found at foo.js:1:1 is not supported, please use `export { … }` instead

This export-from syntax is suppose to be supported, correct?

caridy commented 10 years ago

@ericf this is tracked here: https://github.com/esnext/es6-module-transpiler/issues/29