ts-transform-default-export
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -144,3 +144,3 @@ "use strict"; | ||
if (!options.allowNamedExports && hasNamedExports) { | ||
throw new Error("Unable to transform the default export of the module \"" + file.fileName + "\"." + | ||
throw new Error("(ts-transform-default-export) Unable to transform the default export of the module \"" + file.fileName + "\"." + | ||
' The module has named exports, which could be lost during the transformation.' + | ||
@@ -147,0 +147,0 @@ ' To ignore this, set the `allowNamedExports` option to `true`'); |
{ | ||
"name": "ts-transform-default-export", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"license": "ISC", | ||
@@ -5,0 +5,0 @@ "description": "`export default foo` → `export = foo` → `module.exports = foo`", |
# ts-transform-default-export | ||
[![npm package](https://img.shields.io/npm/v/ts-transform-default-export)](https://www.npmjs.com/package/ts-transform-default-export) | ||
[![CI](https://img.shields.io/github/workflow/status/axtgr/ts-transform-default-export/CI?label=CI&logo=github)](https://github.com/axtgr/ts-transform-default-export/actions) | ||
[![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) | ||
[![CI](https://img.shields.io/github/workflow/status/axtgr/ts-transform-default-export/CI?label=CI&logo=github)](https://github.com/axtgr/ts-transform-default-export/actions) | ||
@@ -23,4 +24,3 @@ A TypeScript transformer that converts a default export such as one of these: | ||
Only files that match the `files` or `include` property of your `tsconfig.json` will be transformed. | ||
This is an intentional restriction to make it possible to control which files are processed. | ||
This is especially useful when making a package compatible with both CommonJS and ES modules. | ||
@@ -31,10 +31,29 @@ ## Installation | ||
Currently there is no native way to add a custom transformer to your TypeScript compilation pipeline. | ||
Instead, you have to use a third-party tool ([TTypescript](https://github.com/cevek/ttypescript), | ||
[ts-patch](https://github.com/nonara/ts-patch)) or a plugin for your bundler. For concrete instructions | ||
refer to the docs of the tool of your choice. | ||
## Usage | ||
The type of this transformer is `program`, the most common one. To transform the module file, add it | ||
to the `before` stage. To transform the emitted declaration file, add it to `afterDeclarations`. | ||
After the package is installed, you need to add it to your TypeScript compilation pipeline. | ||
Currently there is [no native way to do it](https://github.com/Microsoft/TypeScript/issues/14419), | ||
so you'll have to use a third-party tool ([TTypescript](https://github.com/cevek/ttypescript), | ||
[ts-patch](https://github.com/nonara/ts-patch)) or a plugin for your bundler (e.g. | ||
[rollup-plugin-ts](https://github.com/wessberg/rollup-plugin-ts)). For concrete instructions | ||
refer to the docs of the tool of your choice. When adding the transformer, keep in mind | ||
that its type is `program`, the most common one. | ||
The transformer can be added to the `before` or `afterDeclarations` stages of compilation. | ||
- When added to the `before` stage, it will transform only modules themselves. In this | ||
case the resulting code will not match its type declarations. | ||
- When added to `afterDeclarations`, it will transform only declaration files. This will | ||
also produce mismatching type declarations. However, this can be useful if your build | ||
tool transforms the modules for you (e.g. `rollup` with `output.exports = 'default'`) | ||
and you want to make the declarations compatible. | ||
- When added to both `before` and `afterDeclarations`, both modules and declarations | ||
will be transformed. This is the most common case that produces matching files. | ||
Only files that match the `files` or `include` property of your `tsconfig.json` will be | ||
transformed. This is an intentional restriction to make it possible to control which files | ||
are processed. | ||
### Example `tsconfig.json` for TTypescript | ||
@@ -49,3 +68,3 @@ | ||
"afterDeclarations": true, | ||
"keepOriginalExport": true // Transformer option | ||
"keepOriginalExport": true // Option of the transformer | ||
}] | ||
@@ -57,2 +76,40 @@ }, | ||
### Example `rollup.config.js` with `rollup-plugin-ts` | ||
This configuration is interesting in that it produces two bundled modules (`index.js` | ||
for CommonJS and `index.mjs` for ESM), two source maps and a _single_ declaration file | ||
(`index.d.ts`) that is compatible with _both_ the modules. | ||
```js | ||
import typescript from 'rollup-plugin-ts' | ||
import transformDefaultExport from 'ts-transform-default-export' | ||
export default { | ||
input: 'src/index.ts', | ||
output: [ | ||
{ | ||
dir: 'dist', | ||
format: 'cjs', | ||
sourcemap: true, | ||
exports: 'default', | ||
}, | ||
{ | ||
dir: 'dist', | ||
format: 'es', | ||
sourcemap: true, | ||
entryFileNames: '[name].mjs', | ||
}, | ||
], | ||
plugins: [ | ||
typescript({ | ||
transformers: ({ program }) => ({ | ||
afterDeclarations: transformDefaultExport(program, { | ||
keepOriginalExport: true, | ||
}), | ||
}), | ||
}), | ||
], | ||
} | ||
``` | ||
## Options | ||
@@ -62,3 +119,4 @@ | ||
Whether to keep the original default export in the code when transforming it. | ||
Whether to keep the original default export in the code when transforming it. Useful | ||
if you want to get a declaration file that is compatible with both CommonJS and ES modules. | ||
@@ -76,2 +134,3 @@ - When `false` (default): | ||
Whether to throw when there are named exports in the module along with the default one. | ||
This is important because when a default export is converted to `export =`, named exports | ||
@@ -78,0 +137,0 @@ could get lost. For example, `export { foo as default, bar }` becomes `exports.bar = bar; module.exports = foo`, |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19198
152