{"id":17419164,"url":"https://github.com/axtgr/ts-transform-default-export","last_synced_at":"2025-07-08T09:42:08.889Z","repository":{"id":57381280,"uuid":"286724410","full_name":"axtgr/ts-transform-default-export","owner":"axtgr","description":"export default foo → export = foo → module.exports = foo","archived":false,"fork":false,"pushed_at":"2023-10-16T19:47:55.000Z","size":429,"stargazers_count":4,"open_issues_count":8,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-08T17:22:00.556Z","etag":null,"topics":["commonjs","esm","export","transformer"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"isc","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/axtgr.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"custom":["https://www.buymeacoffee.com/axtgr"]}},"created_at":"2020-08-11T11:18:37.000Z","updated_at":"2024-07-28T16:11:40.000Z","dependencies_parsed_at":"2024-06-19T06:30:09.691Z","dependency_job_id":null,"html_url":"https://github.com/axtgr/ts-transform-default-export","commit_stats":{"total_commits":38,"total_committers":1,"mean_commits":38.0,"dds":0.0,"last_synced_commit":"aa6b1f436ab468191571bfdcf8ac39520b33289f"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"purl":"pkg:github/axtgr/ts-transform-default-export","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/axtgr%2Fts-transform-default-export","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/axtgr%2Fts-transform-default-export/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/axtgr%2Fts-transform-default-export/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/axtgr%2Fts-transform-default-export/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/axtgr","download_url":"https://codeload.github.com/axtgr/ts-transform-default-export/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/axtgr%2Fts-transform-default-export/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259571642,"owners_count":22878183,"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":["commonjs","esm","export","transformer"],"created_at":"2024-10-17T02:17:06.224Z","updated_at":"2025-06-13T03:04:24.782Z","avatar_url":"https://github.com/axtgr.png","language":"TypeScript","funding_links":["https://www.buymeacoffee.com/axtgr"],"categories":[],"sub_categories":[],"readme":"# ts-transform-default-export\n\n[![npm package](https://img.shields.io/npm/v/ts-transform-default-export)](https://www.npmjs.com/package/ts-transform-default-export)\n[![CI](https://img.shields.io/github/workflow/status/axtgr/ts-transform-default-export/CI?label=CI\u0026logo=github)](https://github.com/axtgr/ts-transform-default-export/actions)\n[![Buy me a beer](https://img.shields.io/badge/%F0%9F%8D%BA-Buy%20me%20a%20beer-red?style=flat)](https://www.buymeacoffee.com/axtgr)\n\nA TypeScript transformer that converts a default export such as one of these:\n\n```ts\nexport default function foo() {}\nexport default foo\nexport { foo as default }\n```\n\nto its CommonJS counterpart:\n\n```ts\nexport = foo\n```\n\nWhen such a module is then transpiled to CommonJS or UMD, the export will become `module.exports = foo`,\nmaking the module consumable by `require('foo')` instead of `require('foo').default`.\n\nThis is useful when making a package compatible with both CommonJS and ES modules.\n\n## Installation\n\n`npm install --save-dev ts-transform-default-export`\n\n## Usage\n\nAfter the package is installed, you need to add it to your TypeScript compilation pipeline.\nCurrently there is [no native way to do it](https://github.com/Microsoft/TypeScript/issues/14419),\nso you'll have to use a third-party tool ([TTypescript](https://github.com/cevek/ttypescript),\n[ts-patch](https://github.com/nonara/ts-patch)) or a plugin for your bundler (e.g.\n[rollup-plugin-ts](https://github.com/wessberg/rollup-plugin-ts)). For concrete instructions\nrefer to the docs of the tool of your choice. When adding the transformer, keep in mind\nthat its type is `program`, the most common one.\n\nThe transformer can be added to the `before` or `afterDeclarations` stages of compilation.\n\n- When added to the `before` stage, it will transform only modules themselves. In this\n  case the resulting code will not match its type declarations.\n\n- When added to `afterDeclarations`, it will transform only declaration files. This will\n  also produce mismatching type declarations. However, this can be useful if your build\n  tool transforms the modules for you (e.g. `rollup` with `output.exports = 'default'`)\n  and you want to make the declarations compatible.\n\n- When added to both `before` and `afterDeclarations`, both modules and declarations\n  will be transformed. This is the most common case that produces matching files.\n\nOnly files that match the `files` or `include` property of your `tsconfig.json` will be\ntransformed. This is an intentional restriction to make it possible to control which files\nare processed.\n\n### Example `tsconfig.json` for TTypescript\n\n```js\n{\n  \"compilerOptions\": {\n    \"module\": \"CommonJS\",\n    \"plugins\": [{\n      \"transform\": \"ts-transform-default-export\",\n      \"afterDeclarations\": true,\n      \"keepOriginalExport\": true // Option of the transformer\n    }]\n  },\n  \"include\": [\"src/index.ts\"]\n}\n```\n\n### Example `rollup.config.js` with `rollup-plugin-ts`\n\n```js\nimport typescript from 'rollup-plugin-ts'\nimport transformDefaultExport from 'ts-transform-default-export'\n\nexport default {\n  input: 'src/index.ts',\n  output: [\n    {\n      dir: 'dist',\n      format: 'cjs',\n      sourcemap: true,\n      exports: 'default',\n      entryFileNames: '[name].js',\n      plugins: [],\n    },\n    {\n      dir: 'dist',\n      format: 'umd',\n      sourcemap: true,\n      name: 'lib',\n      exports: 'default',\n      entryFileNames: '[name].umd.js',\n      plugins: [terser()],\n    },\n  ],\n  plugins: [\n    typescript({\n      transformers: ({ program }) =\u003e ({\n        afterDeclarations: transformDefaultExport(program),\n      }),\n    }),\n  ],\n}\n```\n\n## Options\n\n### `keepOriginalExport`: boolean\n\nWhether to keep the original default export in the code when transforming it. Useful\nif you want to get a declaration file that is compatible with both CommonJS and ES modules.\n\n- When `false` (default):\n\n  `export default foo` → `export = foo`\n\n- When `true`:\n\n  `export default foo` → `export default foo; export = foo`\n\n### `allowNamedExports`: boolean\n\nWhether to throw when there are named exports in the module along with the default one.\n\nThis is important because when a default export is converted to `export =`, named exports\ncould get lost. For example, `export { foo as default, bar }` becomes `exports.bar = bar; module.exports = foo`,\nso `bar` is overwritten.\n\nYou can work around this by assigning the named exports to the default export's value\nif possible (`foo.bar = bar; export { foo as default, bar }`) and setting this option to true.\n\n- When `false` (default):\n\n  `export { foo as default, bar }` → throws an error\n\n- When `true` (and `keepOriginalExport` is `false`):\n\n  `export { foo as default, bar }` → `export { bar }; export = foo`\n\n- When `true` (and `keepOriginalExport` is `true`):\n\n  `export { foo as default, bar }` → `export { foo as default, bar }; export = foo`\n\n## License\n\n[ISC](LICENSE)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faxtgr%2Fts-transform-default-export","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faxtgr%2Fts-transform-default-export","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faxtgr%2Fts-transform-default-export/lists"}