{"id":13671245,"url":"https://github.com/59naga/babel-plugin-add-module-exports","last_synced_at":"2025-05-14T15:10:59.686Z","repository":{"id":2199472,"uuid":"45929652","full_name":"59naga/babel-plugin-add-module-exports","owner":"59naga","description":"【v0.2 no longer maintained】 Fix babel/babel#2212 - Follow the babel@5 behavior for babel@6","archived":false,"fork":false,"pushed_at":"2022-12-03T09:52:30.000Z","size":264,"stargazers_count":728,"open_issues_count":33,"forks_count":54,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-04-14T08:00:42.555Z","etag":null,"topics":["babel-plugin"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/59naga.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-11-10T17:48:54.000Z","updated_at":"2024-10-25T10:10:59.000Z","dependencies_parsed_at":"2023-01-13T12:15:15.865Z","dependency_job_id":null,"html_url":"https://github.com/59naga/babel-plugin-add-module-exports","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/59naga%2Fbabel-plugin-add-module-exports","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/59naga%2Fbabel-plugin-add-module-exports/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/59naga%2Fbabel-plugin-add-module-exports/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/59naga%2Fbabel-plugin-add-module-exports/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/59naga","download_url":"https://codeload.github.com/59naga/babel-plugin-add-module-exports/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254170056,"owners_count":22026219,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["babel-plugin"],"created_at":"2024-08-02T09:01:03.929Z","updated_at":"2025-05-14T15:10:59.655Z","avatar_url":"https://github.com/59naga.png","language":"JavaScript","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"readme":"babel-plugin-add-module-exports\n---\n\n\u003cp align=\"right\"\u003e\n  \u003ca href=\"https://npmjs.org/package/babel-plugin-add-module-exports\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/babel-plugin-add-module-exports.svg?style=flat-square\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://travis-ci.org/59naga/babel-plugin-add-module-exports\"\u003e\n    \u003cimg src=\"http://img.shields.io/travis/59naga/babel-plugin-add-module-exports.svg?style=flat-square\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\nWhy?\n---\n\nBabel@6 doesn't export default `module.exports` any more - [T2212 *Kill CommonJS default export behavior*](https://phabricator.babeljs.io/T2212).\n\nBabel@6 transforms the following file\n\n```js\nexport default 'foo'\n```\n\ninto\n\n```js\n'use strict';\nObject.defineProperty(exports, \"__esModule\", {\n  value: true\n});\nexports.default = 'foo';\n```\n\nTherefore, it is a need to use the ugly `.default` in node.js.\n\n```js\nrequire('./bundle.js') // { default: 'foo' }\nrequire('./bundle.js').default // 'foo'\n```\n\nThis plugin follows the babel@5 behavior - add the `module.exports` if **only** the `export default` declaration exists.\n\n```js\n'use strict';\nObject.defineProperty(exports, \"__esModule\", {\n  value: true\n});\nexports.default = 'foo';\nmodule.exports = exports['default'];\n```\n\nTherefore, our old codes still work fine - the `.default` goes away. :wink:\n\n```js\nrequire('./bundle.js') // foo\n```\n\nUsage\n---\n\nInstall this plugin from npm:\n\n```bash\nnpm install babel-plugin-add-module-exports --save-dev\n# or\nyarn add -D babel-plugin-add-module-exports\n```\n\nWrite the name to [babelrc](https://babeljs.io/docs/usage/babelrc/). It works with [preset-env](http://babeljs.io/docs/en/babel-preset-env/) to output CommonJS code:\n\n```json\n{\n  \"presets\": [\"@babel/env\"],\n  \"plugins\": [\"add-module-exports\"]\n}\n```\n\n### modules: false\n\n**However, the plugin doesn't change the pure-esmodule**.\nthis plugin makes changes only when exists `exports.default` (in other words, using [commonjs](https://babeljs.io/docs/en/babel-plugin-transform-es2015-modules-commonjs/)).\n\n```json\n{\n  \"presets\": [[\"@babel/env\", { \"modules\": false }]],\n  \"plugins\": [\"add-module-exports\"]\n}\n```\n\ninto\n\n```js\nexport default 'foo'\n```\n\n`1.0.0` Currently support is `commonjs` and `umd`.\nDoesn't support `amd`, `systemjs` modules(don't use. there are no plans to support at the moment).\n\n### with Webpack\n\nLikewise, webpack doesn't perform commonjs transformation for [codesplitting](https://webpack.js.org/guides/code-splitting/). Need to set commonjs conversion.\n\n```json\n{\n  \"presets\": [[\"@babel/env\", { \"modules\": \"commonjs\" }]],\n  \"plugins\": [\"add-module-exports\"]\n}\n```\n\nOptions\n---\n\n## `addDefaultProperty`\n\nIf you're exporting an object and wish to maintain compatibility with code using the `require('./bundle.js').default` syntax, you can optionally enable the `addDefaultProperty` option as follows:\n\n```json\n{\n  \"presets\": [\"env\"],\n  \"plugins\": [\n    [\n      \"add-module-exports\",\n      {\n        \"addDefaultProperty\": true\n      }\n    ]\n  ]\n}\n```\n\nThis will cause a second line of code to be added which aliases the `default` name to the exported object like so:\n\n```js\nmodule.exports = exports['default'];\nmodule.exports.default = exports['default']\n```\n\nSee also\n---\n* [babel-plugin-experimental-syntax-dynamic-import](https://github.com/59naga/babel-plugin-experimental-syntax-dynamic-import)\n\nLicense\n---\n[MIT](http://59naga.mit-license.org/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F59naga%2Fbabel-plugin-add-module-exports","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F59naga%2Fbabel-plugin-add-module-exports","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F59naga%2Fbabel-plugin-add-module-exports/lists"}