Skip to content
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

Revised ES6 modules #2460

Merged
merged 30 commits into from
Mar 25, 2015
Merged

Revised ES6 modules #2460

merged 30 commits into from
Mar 25, 2015

Conversation

ahejlsberg
Copy link
Member

This PR addresses feedback on our design for ES6 module support (see #2242). The primary change is that export default and export = become separate constructs with different semantics:

  • An export default declaration always declares an exported member named default and is always emitted as an assignment to exports.default. In other words, export default consistently has ES module semantics.
  • An export = declaration, which substitutes a different entity to be exported in place of the module itself, is always emitted as an assignment to module.exports. It is an error to have other exports in a module that uses export =. This is the existing TypeScript behavior.
  • A module that uses export = to export another module or a "module like" entity can be imported using the new ES6 constructs. In particular, the convenient destructuring imports can be used with such modules. The pattern of using export = to export another module is common in .d.ts files that provide a CommonJS/AMD view of an internal module (e.g. angular.d.ts).
  • A module that uses export = to export a non-module entity in place of the module itself must be imported using the existing import x = require("foo") syntax as is the case today.

This approach has several advantages:

  • It reduces the amount of magic used in mapping ES6 modules onto CommonJS/AMD.
  • It gives us a better story for interop with existing declaration files.
  • It allows circularly dependent modules to work in a consistent manner (including circularly dependent default exports). As has been pointed out here, the ability to have circular module references is actually one of the major advantages of the ES6 module design and it is important for us to preserve this attribute in down-level scenarios.

@ahejlsberg
Copy link
Member Author

This PR also fixes #2451.

@ahejlsberg
Copy link
Member Author

Also fixes #2383.

@@ -123,7 +123,7 @@ module ts {
case SyntaxKind.ExportDeclaration:
return "__export";
case SyntaxKind.ExportAssignment:
return "default";
return (<ExportAssignment>node).isExportEquals ? "export=" : "default";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is a fairly odd name to give this entity. Is it used elsewhere? if so, we should put it in a common location so we don't have magic strings like this floating around.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs a name that can meaningfully be shown in error messages (such as duplicate symbol 'export=' when there is more than one). It is used in a few other places, but so are all the other ones here.

ahejlsberg and others added 13 commits March 23, 2015 11:05
Conflicts:
	src/compiler/checker.ts
	src/compiler/diagnosticInformationMap.generated.ts
	src/compiler/emitter.ts
	tests/baselines/reference/es6ImportDefaultBinding.errors.txt
	tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImport.errors.txt
	tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamedImportInEs5.errors.txt
	tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBinding.errors.txt
	tests/baselines/reference/es6ImportDefaultBindingFollowedWithNamespaceBindingInEs5.errors.txt
	tests/baselines/reference/es6ImportDefaultBindingInEs5.errors.txt
mhegazy and others added 6 commits March 24, 2015 21:43
Conflicts:
	src/compiler/checker.ts
	src/compiler/emitter.ts
	src/compiler/types.ts
	tests/baselines/reference/APISample_compile.js
	tests/baselines/reference/APISample_linter.js
	tests/baselines/reference/APISample_transform.js
	tests/baselines/reference/APISample_watcher.js
	tests/baselines/reference/es5ExportDefaultClassDeclaration2.js
	tests/baselines/reference/es5ExportDefaultFunctionDeclaration2.js
	tests/baselines/reference/es6ExportAllInEs5.js
	tests/baselines/reference/es6ExportClauseWithoutModuleSpecifierInEs5.js
ahejlsberg added a commit that referenced this pull request Mar 25, 2015
@ahejlsberg ahejlsberg merged commit e925aa7 into master Mar 25, 2015
@mhegazy mhegazy deleted the exportEquals branch March 25, 2015 19:25
@ahejlsberg ahejlsberg mentioned this pull request Apr 5, 2015
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants