Skip to content

Commit 4ea7f10

Browse files
authored
feat: support emitDeclarationOnly (#366)
- when `emitDeclarationOnly` is set, only perform type-checking and emit declarations, don't transform TS - `result.code` actually doesn't exist when `emitDeclarationOnly` is set anyway - this caused a confusing situation for users who were trying to use Babel (with Babel plugins) on TS and only use rpt2 for declarations - instead of getting any JS code, they would just get empty chunks, bc `result.code` is `undefined` - that's kind of buggy, it should probably either be forced to `false` or do what we're doing now, which is likely more intuitive / intended - so now, instead of getting an empty chunk, rpt2 will just pass to the next plugin, allowing for other plugins on the chain to process TS - this opens up some new use cases, like using in tandem with Babel plugins (as the issue illustrates) or using for type-checking and declaration generation while using Vite / ESBuild for compilation - add a new paragraph to "Declarations" docs about this new feature and what it does and some examples of how it could be used - note that these are unexplored / untested integrations, so point that out in the docs too - also modify a self-reference further up in the docs to use code backticks for `rollup-plugin-typescript2` - while at it, also add a line about declaration maps - and reformat the existing paragraph a bit to match the style and improve readability - one sentence per line, which is all on the same paragraph in Markdown anyway - add a `<br />` element to add a new-line _within_ the paragraph for better readability / spacing - Markdown supports this only with two trailing spaces, which is difficult to see and the trailing whitespace can be trimmed by editors, so prefer `<br />`
1 parent 4de17cf commit 4ea7f10

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The plugin inherits all compiler options and file lists from your `tsconfig.json
6161

6262
#### @rollup/plugin-node-resolve
6363

64-
Must be before rollup-plugin-typescript2 in the plugin list, especially when `browser: true` option is used, see [#66](https://github.com/ezolenko/rollup-plugin-typescript2/issues/66)
64+
Must be before `rollup-plugin-typescript2` in the plugin list, especially when `browser: true` option is used, see [#66](https://github.com/ezolenko/rollup-plugin-typescript2/issues/66).
6565

6666
#### @rollup/plugin-commonjs
6767

@@ -199,9 +199,17 @@ See [#108](https://github.com/ezolenko/rollup-plugin-typescript2/issues/108)
199199

200200
### Declarations
201201

202-
This plugin respects `declaration: true` in your `tsconfig.json` file. When set, it will emit `*.d.ts` files for your bundle. The resulting file(s) can then be used with the `types` property in your `package.json` file as described [here](https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html).
202+
This plugin respects `declaration: true` in your `tsconfig.json` file. When set, it will emit `*.d.ts` files for your bundle.
203+
The resulting file(s) can then be used with the `types` property in your `package.json` file as described [here](https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html).<br />
203204
By default, the declaration files will be located in the same directory as the generated Rollup bundle. If you want to override this behavior and instead use the declarationDir set `useTsconfigDeclarationDir` to `true` in the plugin options.
204205

206+
The above also applies to `declarationMap: true` and `*.d.ts.map` files for your bundle.
207+
208+
This plugin also respects `emitDeclarationOnly: true` and will only emit declarations (and declaration maps, if enabled) if set in your `tsconfig.json`.
209+
If you use `emitDeclarationOnly`, you will need another plugin to compile any TypeScript sources, such as `@rollup/plugin-babel`, `rollup-plugin-esbuild`, `rollup-plugin-swc`, etc.
210+
When composing Rollup plugins this way, `rollup-plugin-typescript2` will perform type-checking and declaration generation, while another plugin performs the TypeScript to JavaScript compilation.<br />
211+
Some scenarios where this can be particularly useful: you want to use Babel plugins on TypeScript source, or you want declarations and type-checking for your Vite builds (**NOTE**: this space has not been fully explored yet).
212+
205213
### Watch mode
206214

207215
The way typescript handles type-only imports and ambient types effectively hides them from rollup watch, because import statements are not generated and changing them doesn't trigger a rebuild.

src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,14 @@ const typescript: PluginImpl<RPT2Options> = (options) =>
247247
context.debug(() => `${blue("generated declarations")} for '${key}'`);
248248
}
249249

250+
// if a user sets this compilerOption, they probably want another plugin (e.g. Babel, ESBuild) to transform their TS instead, while rpt2 just type-checks and/or outputs declarations
251+
// note that result.code is non-existent if emitDeclarationOnly per https://github.com/ezolenko/rollup-plugin-typescript2/issues/268
252+
if (parsedConfig.options.emitDeclarationOnly)
253+
{
254+
context.debug(() => `${blue("emitDeclarationOnly")} enabled, not transforming TS'`);
255+
return undefined;
256+
}
257+
250258
const transformResult: TransformResult = { code: result.code, map: { mappings: "" } };
251259

252260
if (result.map)

0 commit comments

Comments
 (0)