UNPKG

44 kBMarkdownView Raw
1<!-- SHADOW_SECTION_LOGO_START -->
2
3<div><img alt="Logo" src="https://raw.githubusercontent.com/wessberg/rollup-plugin-ts/master/documentation/asset/rollup-plugin-ts-logo.png" height="150" /></div>
4
5<!-- SHADOW_SECTION_LOGO_END -->
6
7<!-- SHADOW_SECTION_DESCRIPTION_SHORT_START -->
8
9> A Typescript Rollup plugin that bundles declarations and respects Browserslists
10
11<!-- SHADOW_SECTION_DESCRIPTION_SHORT_END -->
12
13<!-- SHADOW_SECTION_BADGES_START -->
14
15<a href="https://npmcharts.com/compare/%40wessberg%2Frollup-plugin-ts?minimal=true"><img alt="Downloads per month" src="https://img.shields.io/npm/dm/%40wessberg%2Frollup-plugin-ts.svg" /></a>
16<a href="https://www.npmjs.com/package/%40wessberg%2Frollup-plugin-ts"><img alt="NPM version" src="https://badge.fury.io/js/%40wessberg%2Frollup-plugin-ts.svg" /></a>
17<a href="https://david-dm.org/wessberg/rollup-plugin-ts"><img alt="Dependencies" src="https://img.shields.io/david/wessberg%2Frollup-plugin-ts.svg" /></a>
18<a href="https://github.com/wessberg/rollup-plugin-ts/graphs/contributors"><img alt="Contributors" src="https://img.shields.io/github/contributors/wessberg%2Frollup-plugin-ts.svg" /></a>
19<a href="https://github.com/prettier/prettier"><img alt="code style: prettier" src="https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square" /></a>
20<a href="https://opensource.org/licenses/MIT"><img alt="License: MIT" src="https://img.shields.io/badge/License-MIT-yellow.svg" /></a>
21<a href="https://www.patreon.com/bePatron?u=11315442"><img alt="Support on Patreon" src="https://img.shields.io/badge/patreon-donate-green.svg" /></a>
22
23<!-- SHADOW_SECTION_BADGES_END -->
24
25<!-- SHADOW_SECTION_DESCRIPTION_LONG_START -->
26
27## Description
28
29<!-- SHADOW_SECTION_DESCRIPTION_LONG_END -->
30
31This is a Rollup plugin that enables integration between Typescript, Babel, Browserslists, and Rollup.
32It is first and foremost a Typescript plugin that enables full interoperability with Rollup. With it comes
33very powerful bundling and tree-shaking of generated Typescript declaration files that works seamlessly with code splitting.
34
35<!-- SHADOW_SECTION_FEATURES_START -->
36
37### Features
38
39<!-- SHADOW_SECTION_FEATURES_END -->
40
41In comparison with the [official plugin](https://github.com/rollup/plugins/tree/master/packages/typescript), this one has several significant improvements:
42
43- Compiler diagnostics are correctly emitted and brought into the Rollup build lifecycle
44- [Emit-less types](https://github.com/rollup/rollup-plugin-typescript/issues/28) are correctly handled
45- Generation and bundling of Definition files (`.d.ts`) are supported and fully supports code splitting
46- A [Browserslist](https://github.com/browserslist/browserslist) can be provided instead of a target version of ECMAScript such that your code is transpiled in relation to the baseline of browsers defined in your Browserslist instead.
47- Babel can be used as the transpiler, rather than Typescript, such that Typescript handles diagnostics, declarations, and stripping away types, and Babel is used for syntax transformation.
48
49<!-- SHADOW_SECTION_FEATURE_IMAGE_START -->
50
51<!-- SHADOW_SECTION_FEATURE_IMAGE_END -->
52
53<!-- SHADOW_SECTION_TOC_START -->
54
55## Table of Contents
56
57- [Description](#description)
58 - [Features](#features)
59- [Table of Contents](#table-of-contents)
60- [Install](#install)
61 - [npm](#npm)
62 - [Yarn](#yarn)
63 - [pnpm](#pnpm)
64- [Usage](#usage)
65 - [Using it with just Typescript](#using-it-with-just-typescript)
66 - [Typescript and tslib helpers](#typescript-and-tslib-helpers)
67 - [Combining Typescript with a Browserslist](#combining-typescript-with-a-browserslist)
68 - [Using the plugin with Typescript, but without Browserslists](#using-the-plugin-with-typescript-but-without-browserslists)
69 - [Combining Typescript with Babel](#combining-typescript-with-babel)
70 - [Special handling for minification plugins/presets](#special-handling-for-minification-pluginspresets)
71 - [`@babel/runtime` and external helpers](#babelruntime-and-external-helpers)
72 - [`@babel/runtime` and polyfills](#babelruntime-and-polyfills)
73 - [Using `CustomTransformers`](#using-customtransformers)
74- [Declaration files](#declaration-files)
75- [Examples](#examples)
76 - [Pure Typescript example](#pure-typescript-example)
77 - [Typescript with Browserslist example](#typescript-with-browserslist-example)
78 - [Typescript, Babel, and Browserslist example](#typescript-babel-and-browserslist-example)
79 - [Pure Typescript with CustomTransformers](#pure-typescript-with-customtransformers)
80 - [Advanced example of using Typescript, Babel, and Browserslists together](#advanced-example-of-using-typescript-babel-and-browserslists-together)
81 - [Passing a specific TypeScript version](#passing-a-specific-typescript-version)
82- [Hooks](#hooks)
83 - [The `outputPath` hook](#the-outputpath-hook)
84 - [The `diagnostics` hook](#the-diagnostics-hook)
85- [Full list of plugin options](#full-list-of-plugin-options)
86 - [`transpiler`](#transpiler)
87 - [`babelConfig`](#babelconfig)
88 - [`tsconfig`](#tsconfig)
89 - [`browserslist`](#browserslist)
90 - [`cwd`](#cwd)
91 - [`typescript`](#typescript)
92 - [`transformers`](#transformers)
93 - [`include`](#include)
94 - [`exclude`](#exclude)
95 - [`transpileOnly`](#transpileonly)
96 - [`fileSystem`](#filesystem)
97 - [`hook`](#hook)
98- [Ignored/overridden options](#ignoredoverridden-options)
99 - [Ignored/overridden Typescript options](#ignoredoverridden-typescript-options)
100 - [Ignored/overridden Babel options](#ignoredoverridden-babel-options)
101 - [Default Babel plugins](#default-babel-plugins)
102- [Contributing](#contributing)
103- [Maintainers](#maintainers)
104- [Backers](#backers)
105 - [Patreon](#patreon)
106- [FAQ](#faq)
107 - [Does this plugin work with Code Splitting?](#does-this-plugin-work-with-code-splitting)
108 - [Why wouldn't you use just Typescript?](#why-wouldnt-you-use-just-typescript)
109 - [Okay, then why wouldn't you use just babel?](#okay-then-why-wouldnt-you-use-just-babel)
110 - [When combined with Babel, what does Typescript do, and what does Babel do?](#when-combined-with-babel-what-does-typescript-do-and-what-does-babel-do)
111 - [Why is @babel/plugin-transform-runtime and tslib included by default?](#why-is-babelplugin-transform-runtime-and-tslib-included-by-default)
112- [License](#license)
113
114<!-- SHADOW_SECTION_TOC_END -->
115
116<!-- SHADOW_SECTION_INSTALL_START -->
117
118## Install
119
120### npm
121
122```
123$ npm install @wessberg/rollup-plugin-ts
124```
125
126### Yarn
127
128```
129$ yarn add @wessberg/rollup-plugin-ts
130```
131
132### pnpm
133
134```
135$ pnpm add @wessberg/rollup-plugin-ts
136```
137
138<!-- SHADOW_SECTION_INSTALL_END -->
139
140<!-- SHADOW_SECTION_USAGE_START -->
141
142## Usage
143
144<!-- SHADOW_SECTION_USAGE_END -->
145
146Using the plugin is as simple as it can be. Here's an example within a Rollup config:
147
148```javascript
149import ts from "@wessberg/rollup-plugin-ts";
150export default {
151 // ...
152 plugins: [
153 ts({
154 /* Plugin options */
155 })
156 ]
157};
158```
159
160Without any options, the plugin will _"just work"_:
161
162- The `tsconfig.json` file closest to the current working directory will be resolved, if any. Otherwise, the default Typescript options will be used.
163- The `.browserslistrc` file or `browserslist` property within the `package.json` file closest to the current working directory will be resolved and used to decide the Typescript ECMAScript version target, if any. Otherwise, the declared `target` within the resolved `tsconfig.json` file will be used, if any such file exists, and if not, the default Typescript target will be used.
164
165### Using it with just Typescript
166
167This plugin works very well with just Typescript.
168The `tsconfig.json` file closest to your project will be resolved and used in combination with Rollup.
169If your config has a different name, or if you use different configs dynamically depending on the environment, you can provide the location for the `tsconfig` in the plugin options:
170
171```javascript
172ts({
173 tsconfig: PRODUCTION ? "tsconfig.prod.json" : "tsconfig.json"
174});
175```
176
177You an also pass in [CompilerOptions](https://www.typescriptlang.org/docs/handbook/compiler-options.html) directly, rather than provide the path to a `tsconfig`:
178
179```javascript
180ts({
181 tsconfig: {
182 target: ScriptTarget.ES2018,
183 allowSyntheticDefaultImports: true,
184 allowJs: true
185 }
186});
187```
188
189You can also pass in a function that receives whatever `CompilerOptions` that could be resolved relative to the current working directory, but then allow you to override the options:
190
191```javascript
192ts({
193 tsconfig: resolvedConfig => ({...resolvedConfig, allowJs: false})
194});
195```
196
197The above example is based on the assumption that a file can be resolved with the name `tsconfig.json`, and if not, the Typescript's default `CompilerOptions` will be used.
198But if you want to provide the name of the `tsconfig` to override, you can also pass in an object following the following form:
199
200```javascript
201ts({
202 tsconfig: {
203 fileName: "my-awesome-tsconfig.json",
204 hook: resolvedConfig => ({...resolvedConfig, allowJs: false})
205 }
206});
207```
208
209If there is a `.browserslistrc` file or the nearest `package.json` contains a Browserslist configuration, a target ECMAScript version will be decided based on that one, rather than respecting the `target` property of the matched `tsconfig`.
210If you do not want this behavior, you can [disable it as described here](#using-the-plugin-with-typescript-but-without-browserslists).
211
212#### Typescript and tslib helpers
213
214This plugin makes sure that the helper functions that may be emitted within the output generated by Typescript will not be duplicated across files and chunks. Instead, they will automatically be divided into chunks and imported across Rollup chunks.
215You don't have to do anything!
216
217### Combining Typescript with a Browserslist
218
219If there is a `.browserslistrc` file or the nearest `package.json` contains a Browserslist configuration, this is the default behavior! Rather than use the `target` property of the nearest `tsconfig`, it will be decided based on the Browserslist.
220
221You can explicitly pass in Browserslist options. Here's an example with a raw Browserslist query:
222
223```javascript
224ts({
225 browserslist: ["last 1 version", "> 1%"]
226});
227```
228
229You can also provide a configuration object instead of a raw query. Here's one with a baked-in query:
230
231```javascript
232ts({
233 browserslist: {
234 query: ["last 1 version", "> 1%"]
235 }
236});
237```
238
239...And here's one with a `path` property pointing to a file that contains a Browserslist:
240
241```javascript
242ts({
243 browserslist: {
244 path: ".mybrowserslistrc"
245 }
246});
247```
248
249#### Using the plugin with Typescript, but without Browserslists
250
251If no Browserslist can be found, or if you simply don't want to use one, that's completely OK!
252In such cases, the `target` property of the nearest `tsconfig` will be used (or use the Typescript default setting if no such file exists).
253
254You can explicitly request that no Browserslist will be used by setting the `browserslist` property to `false` in the plugin options:
255
256```javascript
257ts({
258 browserslist: false
259});
260```
261
262### Combining Typescript with Babel
263
264This plugin makes it really easy to use Typescript for reporting diagnostics, generating declaration files, and stripping types, but then using Babel for all other syntax transformations.
265One very strong use case for this is to use [`@babel/preset-env`](https://babeljs.io/docs/en/babel-preset-env). Another one is that you get the entire ecosystem of Babel plugins at your disposal.
266
267To use Babel, simply set the `transpiler` plugin option to `"babel"`:
268
269```javascript
270ts({
271 transpiler: "babel"
272});
273```
274
275That's it! The plugin will attempt to locate a `babel.config.js` file or a `.babelrc` file and use the options, plugins, and presets found there.
276By default, some combination of presets and plugins will be applied depending on the config options you provide. See [this section](#default-babel-plugins) for more details.
277
278#### Special handling for minification plugins/presets
279
280This plugin will apply syntax transformations from Babel presets and plugins on a file-by-file basis. However, if a a minification-related plugin or preset such as [babel-preset-minify](https://github.com/babel/minify/tree/master/packages/babel-preset-minify) is found within the Babel options,
281these transformations will be applied per chunk. This enables the minification presets and plugins to perform better as it can now mangle in relation to the entire chunk and better remove unwanted characters such as whitespace.
282All of this works automatically.
283
284#### `@babel/runtime` and external helpers
285
286This plugin will automatically make sure to avoid duplication of emitted Babel helpers. Rollup will automatically split these into chunks and re-use them across the chunks that Rollup generates.
287You don't have to do anything.
288
289#### `@babel/runtime` and polyfills
290
291Babel supports injecting polyfills where needed and in relation to the target environment. By default, this plugin **will not** add polyfills to your chunks since there are arguably better ways of applying polyfills such as lazy-loading depending on feature support or using something like [Polyfill.app](https://github.com/wessberg/polyfiller).
292If you would like this behavior, simply add either `@babel/plugin-transform-runtime` to your Babel config with the `corejs` option set to true, or add `@babel/preset-env` to your Babel config with the `useBuiltIns` option set to `usage`.
293
294### Using `CustomTransformers`
295
296This plugin enables you to pass in [`CustomTransformers`](https://github.com/Microsoft/TypeScript/pull/13940) which allows you to transform the Typescript AST during code transpilation.
297This enables you to very efficiently transform Typescript before code generation and additionally enables you to use this plugin with tools that leverage this, such as some modern web frameworks and libraries do.
298
299## Declaration files
300
301Typescript declaration files are normally distributed in a folder structure that resembles the structure of the source folder.
302With `tsc`, you would get something like this:
303
304<img alt="TSC emitted code" src="https://raw.githubusercontent.com/wessberg/rollup-plugin-ts/master/documentation/asset/tsc-output-example.png" height="250" />
305
306Rollup is a bundler, and with it, we can produce clean, small files that are easy to distribute.
307With `rollup-plugin-ts`, declaration files will be bundled, tree-shaken and emitted alongside the chunks emitted by Rollup:
308
309<img alt="Plugin emitted code" src="https://raw.githubusercontent.com/wessberg/rollup-plugin-ts/master/documentation/asset/plugin-output-example.png" height="250" />
310
311And, it even works in complex code splitting scenarios:
312
313<img alt="Plugin emitted code with code splitting" src="https://raw.githubusercontent.com/wessberg/rollup-plugin-ts/master/documentation/asset/plugin-output-example-code-splitting.png" height="250" />
314
315## Examples
316
317### Pure Typescript example
318
319```javascript
320ts({
321 // If your tsconfig is already called 'tsconfig.json', this option can be left out
322 tsconfig: "tsconfig.json",
323 // If there is no .browserslistrc within your project, and if your package.json doesn't include a Browserslist property, this option can be left out
324 browserslist: false
325});
326```
327
328### Typescript with Browserslist example
329
330[As described here](#combining-typescript-with-a-browserslist), by default, the plugin will attempt to locate a Browserslist automatically. This example
331shows how you can provide one explicitly
332
333```javascript
334ts({
335 browserslist: ["last 1 version", "> 1%"]
336});
337
338// or
339ts({
340 browserslist: {path: ".mybrowserslistrc"}
341});
342```
343
344### Typescript, Babel, and Browserslist example
345
346[As described here](#combining-typescript-with-babel), a `babel.config.js` or `.babelrc` file will automatically be found by the plugin if available. This example shows how you can provide one explicitly.
347And, [as described here](#typescript-with-browserslist-example), the same goes for Browserslists.
348
349```javascript
350ts({
351 transpiler: "babel",
352 browserslist: ["last 1 version", "> 1%"],
353 babelConfig: {
354 plugins: ["my-babel-plugin"]
355 }
356});
357```
358
359### Pure Typescript with CustomTransformers
360
361```javascript
362ts({
363 transformers: {
364 before: [myTransformer1, myTransformer2],
365 after: [myTransformer3, myTransformer4],
366 afterDeclarations: [myTransformer5, myTransformer6]
367 }
368});
369```
370
371### Advanced example of using Typescript, Babel, and Browserslists together
372
373This example shows how you can use this plugin to accomplish quite advanced things:
374
375```javascript
376const IS_PRODUCTION = process.env.NODE_ENV === "production";
377const BUNDLE_TARGET = process.env.BUNDLE_TARGET;
378const APP_ROOT = "/some/project/root/folder";
379const awesomeFrameworkTransformers = getAwesomeFrameworkCustomTransformers();
380
381ts({
382 // Use Babel for Syntax transformations
383 transpiler: "babel",
384 // Don't use process.cwd(), but instead another root directory
385 cwd: APP_ROOT,
386 // Load a different tsconfig file in production
387 tsconfig: IS_PRODUCTION ? "tsconfig.prod.json" : "tsconfig.json",
388 // Load a different browserslist if currently targeting a modern environment
389 browserslist: {
390 path: BUNDLE_TARGET === "modern" ? ".browserlistrc-modern" : ".browserslistrc-legacy"
391 },
392 // Load a different babel config file in production
393 babelConfig: IS_PRODUCTION ? "babel.config.prod.js" : "babel.config.js",
394
395 // Exclude files within node_modules when not in production
396 exclude: IS_PRODUCTION ? [] : ["node_modules/**/*.*"],
397
398 // Apply CustomTransformers, for example to transform the Source Code with a framework that uses some
399 transformers: awesomeFrameworkTransformers
400});
401```
402
403### Passing a specific TypeScript version
404
405You can use pass a specific TypeScript to use as an option. This may be useful if you are using different TypeScript versions across packages inside a monorepo and you want to use a specific one with `rollup-plugin-ts`:
406
407```typescript
408ts({
409 typescript: specialTypescriptVersion
410});
411```
412
413## Hooks
414
415`rollup-plugin-ts` provides a few hooks that allow you to hook into and augment the internal behavior of the plugin.
416
417These can be provided in the plugin options for the `hook` property:
418
419```typescript
420ts({
421 hook: {
422 // Add hooks here
423 }
424});
425```
426
427The next few subsections describe the different hooks that can be provided
428
429### The `outputPath` hook
430
431Type: `(path: string, kind: "declaration" | "declarationMap" | "buildInfo") => string | undefined`
432
433The `outputPath` hook can be used to rewrite the location on the filesystem that assets produced by `rollup-plugin-ts` are written to.
434It is invoked immediately before assets such as _declarations_ or _declaration maps_ are emitted.
435
436The hook is invoked with the output path as well as the kind of asset the path represents as arguments.
437If you return a `string` from the hook, the alternative location will be used instead. If you return undefined, the current path will be used.
438
439```typescript
440ts({
441 hook: {
442 outputPath: (path, kind) => rewritePathSomehow(path, kind)
443 }
444});
445```
446
447For example, the `path` may be `/some/path/index.d.ts`, and `kind` be `declaration`, and you might want to rewrite this to `/some/path/my-typings.d.ts`.
448
449### The `diagnostics` hook
450
451Type: `(diagnostics: readonly Diagnostic[]) => readonly Diagnostic[]|undefined`
452
453The `diagnostics` hook can be used to read, alter, and extend the diagnostics generated by TypeScript immediately before they are emitted as errors via Rollup.
454Normally, Rollup will crash on the first discovered error, but there may be several diagnostics, all of which may be of interest to you. This hook gives you access to all of them.
455
456You can also use this hook if you want to silence specific kinds of Diagnostics or even add your own.
457
458## Full list of plugin options
459
460The plugin options are documented in more detail across this README, but the full list of options is:
461
462#### `transpiler`
463
464Type: `"typescript" | "babel"`
465
466Can be any of `"babel"` or `"typescript"` (default: `"typescript"`).
467See [this section](#combining-typescript-with-babel) and [this section](#when-combined-with-babel-what-does-typescript-do-and-what-does-babel-do) for details on how `rollup-plugin-ts` works when `"babel"` is being used as a transpiler.
468
469#### `babelConfig`
470
471Type: `string | Partial<IBabelInputOptions>`
472
473This option will only be respected when `"babel"` is being used as the `transpiler` and can be used to provide a [Babel config](https://babeljs.io/docs/en/options) or a path to one.
474
475#### `tsconfig`
476
477Type: `string | Partial<CompilerOptions> | Partial<Record<keyof CompilerOptions, string | number | boolean>> | ParsedCommandLine | TsConfigResolver | TsConfigResolverWithFileName`
478
479Provide the Typescript [CompilerOptions](https://www.typescriptlang.org/docs/handbook/compiler-options.html) to use, or a path to a `tsconfig` with this property.
480See [this section](#using-it-with-just-typescript) for details on the many ways this property can be configured.
481
482#### `browserslist`
483
484Type: `false | string[] | string | BrowserslistConfig`
485
486Provide the [Browserslist](https://github.com/browserslist/browserslist) to use, or a path to a `.browserslistrc` with this property.
487See [this section](#combining-typescript-with-a-browserslist) for details on the many ways this property can be configured.
488
489#### `cwd`
490
491Type: `string`
492
493Use this property to overwrite whatever is considered the root directory. The default value is `process.cwd()`.
494
495#### `typescript`
496
497Type: `typeof import("typescript")`
498
499Use this property to pass a specific version of TypeScript to use.
500
501#### `transformers`
502
503Type: `(CustomTransformers | CustomTransformersFunction)[] | CustomTransformers | CustomTransformersFunction`
504
505Use this property to provide Typescript [`CustomTransformers`](https://github.com/Microsoft/TypeScript/pull/13940).
506See [this section](#using-customtransformers) for more details on how to configure this property.
507
508#### `include`
509
510Type: `string[]|string`
511
512This option takes a minimatch pattern or an array of minimatch patterns and only transforms files with filenames that the pattern matches.
513
514#### `exclude`
515
516Type: `string[]|string`
517
518This option takes a minimatch pattern or an array of minimatch patterns and only transforms files with filenames that the pattern doesn't match.
519
520#### `transpileOnly`
521
522Type: `boolean`
523
524If this option is `true`, diagnostics won't be generated. This will improve performance since Typescript but ignores all syntactical and semantic errors or warnings that may arise.
525
526#### `fileSystem`
527
528Optionally the [FileSystem](https://github.com/wessberg/rollup-plugin-ts/blob/master/src/util/file-system/file-system.ts) to use. This is useful for example when you want to provide a virtual FileSystem to read from or write to.
529
530#### `hook`
531
532Use this property to get hooks into the internals of `rollup-plugin-ts`.
533See [this section](#hooks) for more details.
534
535## Ignored/overridden options
536
537Typescript and Babel are powerful tools in their own right. Combined with Rollup, they become even more powerful.
538To provide a seamless experience, Rollup always take precedence when conflicts arise. As a natural consequence of this, some options provided to Typescript and Babel will be ignored or overridden.
539
540### Ignored/overridden Typescript options
541
542The following [CompilerOptions](https://www.typescriptlang.org/docs/handbook/compiler-options.html) from a `tsconfig` will be ignored:
543
544| Property | Reason |
545| --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
546| `outDir` | Rollup, not Typescript, will decide where to emit chunks. |
547| `outFile` | This option produces flat output and only works with the module formats AMD and SystemJS. Rollup will be the decider of how to split code. |
548| `sourceMap` | Typescript will always be instructed to produce SourceMaps. Rollup then decides whether or not to include them (and if they should be inlined). |
549| `inlineSourceMap` | Typescript will always be instructed to produce SourceMaps. Rollup then decides whether or not to include them (and if they should be inlined). |
550| `inlineSources` | Since `inlineSourceMap` is ignored, this option won't take effect. |
551| `importHelpers` | Helpers will always be imported. This makes it possible for Rollup to code-split properly and share Typescript helpers across chunks. |
552| `moduleResolution` | Node-module resolution will always be used. This is required for `importHelpers` to work and in general, to make Typescript able to resolve external libraries. Note that you still need to add the [nodeResolve](https://github.com/rollup/rollup-plugin-node-resolve) plugin in order to include external libraries within your bundle unless `allowJs` is `true` within your `tsconfig` |
553| `noEmit` | Typescript should always be able to emit assets, but those will be delegated to Rollup. |
554| `noEmitOnError` | See above. |
555| `emitDeclarationOnly` | See above. |
556| `noEmitHelpers` | Typescript should always be able to emit helpers, since the `importHelpers` option is forced |
557| `noResolve` | Typescript should always be able to resolve things. Otherwise, compilation might break. |
558| `watch` | Rollup, not Typescript, will watch files if run in watch mode. Efficient caching will still be used for optimum performance. |
559| `preserveWatchOutput` | See above |
560
561The following additional options will also be ignored:
562
563| Property | Reason |
564| --------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
565| `include` | Rollup itself will decide which files to include in the transformation process based on your code. This plugin itself takes a `include` property which you should use instead if you want to explicitly allow specific files or globs. |
566| `exclude` | See above. |
567
568### Ignored/overridden Babel options
569
570The following [Babel options](https://babeljs.io/docs/en/options) will be ignored:
571
572| Property | Reason |
573| ------------ | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
574| `sourceMaps` | Babel will always be instructed to produce SourceMaps. Rollup then decides whether or not to include them (and if they should be inlined). |
575| `cwd` | The `cwd` option provided to the plugin will always be used. |
576| `include` | Rollup itself will decide which files to include in the transformation process based on your code. This plugin itself takes a `include` property which you should use instead if you want to explicitly allow specific files or globs. |
577| `exclude` | See above |
578| `ignore` | See above |
579| `only` | See above |
580| `sourceType` | Will always use `module`. Rollup will then decide what to do based on the output format |
581
582### Default Babel plugins
583
584If you decide to use Babel as the transpiler with the `transpiler` plugin option set to `"babel"`, some best-practice default plugins and presets will be applied such that you don't have to configure anything on your own.
585By default, the plugin will conditionally apply the `@babel/preset-env` preset if a Browserslist is provided or located, as well as plugins for handling [shipped proposals](https://babeljs.io/docs/en/babel-preset-env#shippedproposals). And, the `@babel/plugin-runtime` plugin will be used for extracting Babel helpers and reusing them across your chunks to avoid code duplication.
586
587If you provide these presets or plugins yourself through the found or provided Babel config, _your_ config options will take precedence.
588
589Here's table with a full overview of the specifics:
590
591| Preset/Plugin | Condition | Reason |
592| -------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
593| `@babel/preset-env` | A Browserslist is provided or found automatically, and you don't provide this preset yourself within your Babel config | This preset enables you to base your syntax transformations on the specific browsers/environment you want your application or library to target. It will "_Just Work"_ |
594| `@babel/plugin-transform-runtime` | You don't provide this plugin yourself within your Babel config | Depending on your configuration, async functions may be rewritten to use [Regenerator Runtime](https://github.com/facebook/regenerator/tree/master/packages/regenerator-runtime), and there may be one or more Babel helpers injected within your code. `@babel/plugin-runtime` allows this plugin to avoid duplicating these helpers, and instead make them shared across Chunks seamlessly via Rollup. |
595| `@babel/plugin-proposal-object-rest-spread` | You don't provide this plugin yourself within your Babel config | This plugin is needed in order to enable parsing [Object Rest/Spread](https://github.com/tc39/proposal-object-rest-spread) syntax, something that is a Stage 4 proposal for ECMAScript and [will be part of ES2018](https://github.com/tc39/proposals/blob/master/finished-proposals.md). |
596| `@babel/plugin-proposal-async-generator-functions` | You don't provide this plugin yourself within your Babel config | This plugin is needed in order to support [Asynchronous Iteration](https://github.com/tc39/proposal-async-iteration) syntax, something that is a Stage 4 proposal for ECMAScript and [will be part of ES2018](https://github.com/tc39/proposals/blob/master/finished-proposals.md). |
597| `@babel/plugin-proposal-optional-catch-binding` | You don't provide this plugin yourself within your Babel config | This plugin is needed in order to support [Optional Catch Binding](https://github.com/tc39/proposal-optional-catch-binding) syntax, something that is a Stage 4 proposal for ECMAScript and [will be part of ES2019](https://github.com/tc39/proposals/blob/master/finished-proposals.md). |
598| `@babel/plugin-proposal-unicode-property-regex` | You don't provide this plugin yourself within your Babel config | This plugin is needed in order to support [RegExp Unicode Property Escapes](https://github.com/tc39/proposal-regexp-unicode-property-escapes) syntax, something that is a Stage 4 proposal for ECMAScript and [will be part of ES2018](https://github.com/tc39/proposals/blob/master/finished-proposals.md). |
599| `@babel/plugin-proposal-json-strings` | You don't provide this plugin yourself within your Babel config | This plugin is needed in order to support [JSON superset](https://github.com/tc39/proposal-json-superset) syntax, something that is a Stage 4 proposal for ECMAScript and [will be part of ES2019](https://github.com/tc39/proposals/blob/master/finished-proposals.md). |
600
601<!-- SHADOW_SECTION_CONTRIBUTING_START -->
602
603## Contributing
604
605Do you want to contribute? Awesome! Please follow [these recommendations](./CONTRIBUTING.md).
606
607<!-- SHADOW_SECTION_CONTRIBUTING_END -->
608
609<!-- SHADOW_SECTION_MAINTAINERS_START -->
610
611## Maintainers
612
613| <img alt="Frederik Wessberg" src="https://avatars2.githubusercontent.com/u/20454213?s=460&v=4" height="70" /> |
614| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
615| [Frederik Wessberg](mailto:frederikwessberg@hotmail.com)<br><strong>Twitter</strong>: [@FredWessberg](https://twitter.com/FredWessberg)<br><strong>Github</strong>: [@wessberg](https://github.com/wessberg)<br>_Lead Developer_ |
616
617<!-- SHADOW_SECTION_MAINTAINERS_END -->
618
619<!-- SHADOW_SECTION_BACKERS_START -->
620
621## Backers
622
623### Patreon
624
625[Become a backer](https://www.patreon.com/bePatron?u=11315442) and get your name, avatar, and Twitter handle listed here.
626
627<a href="https://www.patreon.com/bePatron?u=11315442"><img alt="Backers on Patreon" src="https://patreon-badge.herokuapp.com/11315442.png" width="500" /></a>
628
629<!-- SHADOW_SECTION_BACKERS_END -->
630
631<!-- SHADOW_SECTION_FAQ_START -->
632
633## FAQ
634
635<!-- SHADOW_SECTION_FAQ_END -->
636
637#### Does this plugin work with Code Splitting?
638
639Absolutely, even with Declaration files. Things will work seamlessly.
640
641#### Why wouldn't you use just Typescript?
642
643The Typescript compiler, while extremely powerful, has the restriction that it can only target a specific ECMAScript version.
644In reality, browsers and other ECMAScript environments has varying support for newer language features.
645Some browsers support even those features that haven't been publicized yet, while others support only parts of the latest language features.
646
647In effect, a browser may support a lot of modern features such as classes and proper lexical scoping, but lack others.
648With Typescript, it's _"all-or-nothing"_: If you want to support a Browser with partial support, you must target the latest publicized ECMAScript version that the browser fully supports.
649
650Babel, on the other hand, is far more granular in its design and applies syntax transformations on a feature-by-feature basis.
651Combined with something like `@babel/preset-env`, individual transformations can be applied for only those language features that are missing.
652This means that you can use things like classes and lexical scoping in browsers that otherwise doesn't fully support all of the ES2015 specification.
653
654#### Okay, then why wouldn't you use just babel?
655
656Babel has recently received support for [parsing and transforming Typescript](https://babeljs.io/docs/en/babel-plugin-transform-typescript). It would be intriguing to just use Babel for everything. However, there are significant caveats:
657
658The Babel compiler works on a file-by-file basis, meaning it is simple to use and reason about, whereas the Typescript compiler works with _Programs_, or in other words sets of related _SourceFiles_.
659This gives Typescript the advantage over Babel that it has a greater understanding of your codebase in general and can understand your types across files.
660In the context of this plugin, this enables Typescript to do things that you simply wouldn't be able to do with the Typescript plugin for Babel:
661
6621. Emit Typescript diagnostics
6632. Emit Typescript declaration (`.d.ts`) files and Typescript declaration map (`.d.ts.map`) files.
6643. Remove type-only imports that wouldn't otherwise be transformed by Rollup and would lead to issues like [this one](https://github.com/rollup/rollup-plugin-typescript/issues/28)
6654. Use `const enums` and all other files that will require type information.
666
667#### When combined with Babel, what does Typescript do, and what does Babel do?
668
669First, Typescript will be used for:
670
6711. Reporting diagnostics.
6722. Emitting Typescript declaration (`.d.ts`) files and Typescript declaration map (`.d.ts.map`) files.
6733. Removing Typescript-specific features such as types, type-only imports, enums, and Typescript decorators.
674
675Babel will then be used for all other syntax transformation from then on, depending on the combination of default, provided, and forced presets and plugins.
676
677#### Why is @babel/plugin-transform-runtime and tslib included by default?
678
679[See this explainer](./documentation/explainer/why_is_babel-plugin-transform-runtime_and_tslib_included_by_default.md).
680
681<!-- SHADOW_SECTION_LICENSE_START -->
682
683## License
684
685MIT © [Frederik Wessberg](mailto:frederikwessberg@hotmail.com) ([@FredWessberg](https://twitter.com/FredWessberg)) ([Website](https://github.com/wessberg))
686
687<!-- SHADOW_SECTION_LICENSE_END -->