{"action":"opened","number":92,"pull_request":{"url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/pulls/92","id":83328815,"html_url":"https://github.com/rollup/rollup-plugin-commonjs/pull/92","diff_url":"https://github.com/rollup/rollup-plugin-commonjs/pull/92.diff","patch_url":"https://github.com/rollup/rollup-plugin-commonjs/pull/92.patch","issue_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/issues/92","number":92,"state":"open","locked":false,"title":"BREAKING: interop, take 2","user":{"login":"Rich-Harris","id":1162160,"avatar_url":"https://avatars.githubusercontent.com/u/1162160?v=3","gravatar_id":"","url":"https://api.github.com/users/Rich-Harris","html_url":"https://github.com/Rich-Harris","followers_url":"https://api.github.com/users/Rich-Harris/followers","following_url":"https://api.github.com/users/Rich-Harris/following{/other_user}","gists_url":"https://api.github.com/users/Rich-Harris/gists{/gist_id}","starred_url":"https://api.github.com/users/Rich-Harris/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Rich-Harris/subscriptions","organizations_url":"https://api.github.com/users/Rich-Harris/orgs","repos_url":"https://api.github.com/users/Rich-Harris/repos","events_url":"https://api.github.com/users/Rich-Harris/events{/privacy}","received_events_url":"https://api.github.com/users/Rich-Harris/received_events","type":"User","site_admin":false},"body":"This supersedes #91. Summary:\r\n\r\n* Sourcemaps are no longer generated for CommonJS modules (explanation below)\r\n* Transformed modules are smaller, largely because namespaces are no longer needlessly generated\r\n* The default export of a transformed CommonJS module is `module.exports`, or, if `exports.__esModule === true`, `exports.default` (previously it would always use `.default` if it was present)\r\n* If a CommonJS module imports another CommonJS module, `.default` interop *no longer takes place* – this fixes bugs like rollup/rollup#866\r\n\r\nI won't lie: the approach here feels a little bit crazy. I think that's just the price of having good (and efficient) interop.\r\n\r\nBefore the explanation, a couple of comparisons:\r\n\r\n## Basic CommonJS importing\r\n\r\n### Source\r\n\r\n```js\r\n// main.js\r\nvar foo = require( './foo' );\r\nmodule.exports = foo * 2;\r\n\r\n// foo.js\r\nmodule.exports = 21;\r\n```\r\n\r\n### Before\r\n\r\n```js\r\n'use strict';\r\n\r\nfunction interopDefault(ex) {\r\n  return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex;\r\n}\r\n\r\nfunction createCommonjsModule(fn, module) {\r\n  return module = { exports: {} }, fn(module, module.exports), module.exports;\r\n}\r\n\r\nvar foo = createCommonjsModule(function (module) {\r\nmodule.exports = 21;\r\n});\r\n\r\nvar foo$1 = interopDefault(foo);\r\n\r\n\r\nvar require$$0 = Object.freeze({\r\n  default: foo$1\r\n});\r\n\r\nvar main = createCommonjsModule(function (module) {\r\nvar foo = interopDefault(require$$0);\r\nmodule.exports = foo * 2;\r\n});\r\n\r\nvar main$1 = interopDefault(main);\r\n\r\nmodule.exports = main$1;\r\n```\r\n\r\n### After\r\n\r\n```js\r\n'use strict';\r\n\r\nfunction createCommonjsModule(fn, module) {\r\n  return module = { exports: {} }, fn(module, module.exports), module.exports;\r\n}\r\n\r\nvar require$$0 = createCommonjsModule(function (module) {\r\nmodule.exports = 21;\r\n});\r\n\r\nvar main = createCommonjsModule(function (module) {\r\nvar foo = require$$0;\r\nmodule.exports = foo * 2;\r\n});\r\n\r\nmodule.exports = main;\r\n```\r\n\r\n## Inline require statements\r\n\r\n\r\n### Source\r\n\r\n```js\r\n// main.js\r\nmodule.exports = function () {\r\n  return require( './multiply' )( 2, require( './foo' ) );\r\n};\r\n\r\n// multiply.js\r\nmodule.exports = function ( a, b ) {\r\n  return a * b;\r\n};\r\n\r\n// foo.js\r\nmodule.exports = 1;\r\n```\r\n\r\n### Before\r\n\r\n```js\r\n'use strict';\r\n\r\nfunction interopDefault(ex) {\r\n  return ex && typeof ex === 'object' && 'default' in ex ? ex['default'] : ex;\r\n}\r\n\r\nfunction createCommonjsModule(fn, module) {\r\n  return module = { exports: {} }, fn(module, module.exports), module.exports;\r\n}\r\n\r\nvar multiply = createCommonjsModule(function (module) {\r\nmodule.exports = function ( a, b ) {\r\n  return a * b;\r\n};\r\n});\r\n\r\nvar multiply$1 = interopDefault(multiply);\r\n\r\n\r\nvar require$$1 = Object.freeze({\r\n  default: multiply$1\r\n});\r\n\r\nvar foo = createCommonjsModule(function (module) {\r\nmodule.exports = 1;\r\n});\r\n\r\nvar foo$1 = interopDefault(foo);\r\n\r\n\r\nvar require$$0 = Object.freeze({\r\n  default: foo$1\r\n});\r\n\r\nvar main = createCommonjsModule(function (module) {\r\nmodule.exports = function () {\r\n  return interopDefault(require$$1)( 2, interopDefault(require$$0) );\r\n};\r\n});\r\n\r\nvar main$1 = interopDefault(main);\r\n\r\nmodule.exports = main$1;\r\n```\r\n\r\n### After\r\n\r\n```js\r\n'use strict';\r\n\r\nfunction createCommonjsModule(fn, module) {\r\n  return module = { exports: {} }, fn(module, module.exports), module.exports;\r\n}\r\n\r\nvar require$$1 = createCommonjsModule(function (module) {\r\nmodule.exports = function ( a, b ) {\r\n  return a * b;\r\n};\r\n});\r\n\r\nvar require$$0 = createCommonjsModule(function (module) {\r\nmodule.exports = 1;\r\n});\r\n\r\nvar main = createCommonjsModule(function (module) {\r\nmodule.exports = function () {\r\n  return require$$1( 2, require$$0 );\r\n};\r\n});\r\n\r\nmodule.exports = main;\r\n```\r\n\r\n## How it works\r\n\r\nBasically, when you import a CommonJS module from an ES module, you're no longer importing the module itself but a proxy, which imports the *actual* CommonJS module but with a prefixed ID (`\\0commonjs-required:/path/to/cjs-module.js`). The proxy handles the ES <-> CJS interop – deciding whether `module.exports` or `exports.default` should be the default export, and adding named exports – while the module itself (with the prefixed ID) *always* just has a single default export which is `module.exports`. Its dependencies are re-declared as prefixed imports.\r\n\r\nMeanwhile, if a module with a prefixed ID (i.e., imported by a proxy or a CommonJS module) *isn't* a CommonJS module, we need a different kind of interop layer, which imports the underlying ES module and exports-as-default *either* its default export (if there is one) *or* the entire namespace. That way, we don't need to reify namespaces for everything that a CommonJS module imports, only ES modules.\r\n\r\nAn unfortunate side-effect of this is that sourcemaps no longer work for CommonJS modules, because the source code lives in virtual modules, which get excluded. I haven't been able to think of a good solution to this. Honestly, I think it's a small trade-off for more reliable interop and more efficient code, given that sourcemaps are mostly useful when you're debugging your own code.\r\n\r\nI think this covers all the bases. @rollup/collaborators and others – would welcome any feedback on this before we commit to this road! Sorry for the rambly and complex explanation.\r\n","created_at":"2016-08-30T19:08:01Z","updated_at":"2016-08-30T19:08:01Z","closed_at":null,"merged_at":null,"merge_commit_sha":"ed43198a92ef67678c3a89f18328eb8ac46b9b4f","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/pulls/92/commits","review_comments_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/pulls/92/comments","review_comment_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/issues/92/comments","statuses_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/statuses/f2b6084f3f7687116c818657a5bc791f5cee0b2c","head":{"label":"rollup:interop-take-2","ref":"interop-take-2","sha":"f2b6084f3f7687116c818657a5bc791f5cee0b2c","user":{"login":"rollup","id":12554859,"avatar_url":"https://avatars.githubusercontent.com/u/12554859?v=3","gravatar_id":"","url":"https://api.github.com/users/rollup","html_url":"https://github.com/rollup","followers_url":"https://api.github.com/users/rollup/followers","following_url":"https://api.github.com/users/rollup/following{/other_user}","gists_url":"https://api.github.com/users/rollup/gists{/gist_id}","starred_url":"https://api.github.com/users/rollup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rollup/subscriptions","organizations_url":"https://api.github.com/users/rollup/orgs","repos_url":"https://api.github.com/users/rollup/repos","events_url":"https://api.github.com/users/rollup/events{/privacy}","received_events_url":"https://api.github.com/users/rollup/received_events","type":"Organization","site_admin":false},"repo":{"id":44700172,"name":"rollup-plugin-commonjs","full_name":"rollup/rollup-plugin-commonjs","owner":{"login":"rollup","id":12554859,"avatar_url":"https://avatars.githubusercontent.com/u/12554859?v=3","gravatar_id":"","url":"https://api.github.com/users/rollup","html_url":"https://github.com/rollup","followers_url":"https://api.github.com/users/rollup/followers","following_url":"https://api.github.com/users/rollup/following{/other_user}","gists_url":"https://api.github.com/users/rollup/gists{/gist_id}","starred_url":"https://api.github.com/users/rollup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rollup/subscriptions","organizations_url":"https://api.github.com/users/rollup/orgs","repos_url":"https://api.github.com/users/rollup/repos","events_url":"https://api.github.com/users/rollup/events{/privacy}","received_events_url":"https://api.github.com/users/rollup/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rollup/rollup-plugin-commonjs","description":"Convert CommonJS modules to ES2015","fork":false,"url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs","forks_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/forks","keys_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/teams","hooks_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/hooks","issue_events_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/issues/events{/number}","events_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/events","assignees_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/assignees{/user}","branches_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/branches{/branch}","tags_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/tags","blobs_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/git/refs{/sha}","trees_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/statuses/{sha}","languages_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/languages","stargazers_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/stargazers","contributors_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/contributors","subscribers_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/subscribers","subscription_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/subscription","commits_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/commits{/sha}","git_commits_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/git/commits{/sha}","comments_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/comments{/number}","issue_comment_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/issues/comments{/number}","contents_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/contents/{+path}","compare_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/merges","archive_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/downloads","issues_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/issues{/number}","pulls_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/pulls{/number}","milestones_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/milestones{/number}","notifications_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/labels{/name}","releases_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/releases{/id}","deployments_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/deployments","created_at":"2015-10-21T19:47:04Z","updated_at":"2016-08-27T08:01:20Z","pushed_at":"2016-08-30T19:08:01Z","git_url":"git://github.com/rollup/rollup-plugin-commonjs.git","ssh_url":"git@github.com:rollup/rollup-plugin-commonjs.git","clone_url":"https://github.com/rollup/rollup-plugin-commonjs.git","svn_url":"https://github.com/rollup/rollup-plugin-commonjs","homepage":null,"size":122,"stargazers_count":43,"watchers_count":43,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":16,"mirror_url":null,"open_issues_count":19,"forks":16,"open_issues":19,"watchers":43,"default_branch":"master"}},"base":{"label":"rollup:master","ref":"master","sha":"715f9cd3f321273ccfc095bc99612e5837060534","user":{"login":"rollup","id":12554859,"avatar_url":"https://avatars.githubusercontent.com/u/12554859?v=3","gravatar_id":"","url":"https://api.github.com/users/rollup","html_url":"https://github.com/rollup","followers_url":"https://api.github.com/users/rollup/followers","following_url":"https://api.github.com/users/rollup/following{/other_user}","gists_url":"https://api.github.com/users/rollup/gists{/gist_id}","starred_url":"https://api.github.com/users/rollup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rollup/subscriptions","organizations_url":"https://api.github.com/users/rollup/orgs","repos_url":"https://api.github.com/users/rollup/repos","events_url":"https://api.github.com/users/rollup/events{/privacy}","received_events_url":"https://api.github.com/users/rollup/received_events","type":"Organization","site_admin":false},"repo":{"id":44700172,"name":"rollup-plugin-commonjs","full_name":"rollup/rollup-plugin-commonjs","owner":{"login":"rollup","id":12554859,"avatar_url":"https://avatars.githubusercontent.com/u/12554859?v=3","gravatar_id":"","url":"https://api.github.com/users/rollup","html_url":"https://github.com/rollup","followers_url":"https://api.github.com/users/rollup/followers","following_url":"https://api.github.com/users/rollup/following{/other_user}","gists_url":"https://api.github.com/users/rollup/gists{/gist_id}","starred_url":"https://api.github.com/users/rollup/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rollup/subscriptions","organizations_url":"https://api.github.com/users/rollup/orgs","repos_url":"https://api.github.com/users/rollup/repos","events_url":"https://api.github.com/users/rollup/events{/privacy}","received_events_url":"https://api.github.com/users/rollup/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rollup/rollup-plugin-commonjs","description":"Convert CommonJS modules to ES2015","fork":false,"url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs","forks_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/forks","keys_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/teams","hooks_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/hooks","issue_events_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/issues/events{/number}","events_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/events","assignees_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/assignees{/user}","branches_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/branches{/branch}","tags_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/tags","blobs_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/git/refs{/sha}","trees_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/statuses/{sha}","languages_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/languages","stargazers_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/stargazers","contributors_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/contributors","subscribers_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/subscribers","subscription_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/subscription","commits_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/commits{/sha}","git_commits_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/git/commits{/sha}","comments_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/comments{/number}","issue_comment_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/issues/comments{/number}","contents_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/contents/{+path}","compare_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/merges","archive_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/downloads","issues_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/issues{/number}","pulls_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/pulls{/number}","milestones_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/milestones{/number}","notifications_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/labels{/name}","releases_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/releases{/id}","deployments_url":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/deployments","created_at":"2015-10-21T19:47:04Z","updated_at":"2016-08-27T08:01:20Z","pushed_at":"2016-08-30T19:08:01Z","git_url":"git://github.com/rollup/rollup-plugin-commonjs.git","ssh_url":"git@github.com:rollup/rollup-plugin-commonjs.git","clone_url":"https://github.com/rollup/rollup-plugin-commonjs.git","svn_url":"https://github.com/rollup/rollup-plugin-commonjs","homepage":null,"size":122,"stargazers_count":43,"watchers_count":43,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":16,"mirror_url":null,"open_issues_count":19,"forks":16,"open_issues":19,"watchers":43,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/pulls/92"},"html":{"href":"https://github.com/rollup/rollup-plugin-commonjs/pull/92"},"issue":{"href":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/issues/92"},"comments":{"href":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/issues/92/comments"},"review_comments":{"href":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/pulls/92/comments"},"review_comment":{"href":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/pulls/92/commits"},"statuses":{"href":"https://api.github.com/repos/rollup/rollup-plugin-commonjs/statuses/f2b6084f3f7687116c818657a5bc791f5cee0b2c"}},"merged":false,"mergeable":true,"mergeable_state":"unstable","merged_by":null,"comments":0,"review_comments":0,"commits":1,"additions":412,"deletions":223,"changed_files":11}}
{"actor":{"display_login":"Rich-Harris"}}