1 |
|
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 |
|
6 |
|
7 |
|
8 |
|
9 | > A Typescript Rollup plugin that bundles declarations and respects Browserslists
|
10 |
|
11 |
|
12 |
|
13 |
|
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 |
|
24 |
|
25 |
|
26 |
|
27 | ## Description
|
28 |
|
29 |
|
30 |
|
31 | This is a Rollup plugin that enables integration between Typescript, Babel, Browserslists, and Rollup.
|
32 | It is first and foremost a Typescript plugin that enables full interoperability with Rollup. With it comes
|
33 | very powerful bundling and tree-shaking of generated Typescript declaration files that works seamlessly with code splitting.
|
34 |
|
35 |
|
36 |
|
37 | ### Features
|
38 |
|
39 |
|
40 |
|
41 | In 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 |
|
50 |
|
51 |
|
52 |
|
53 |
|
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 |
|
115 |
|
116 |
|
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 |
|
139 |
|
140 |
|
141 |
|
142 | ## Usage
|
143 |
|
144 |
|
145 |
|
146 | Using the plugin is as simple as it can be. Here's an example within a Rollup config:
|
147 |
|
148 | ```javascript
|
149 | import ts from "@wessberg/rollup-plugin-ts";
|
150 | export default {
|
151 | // ...
|
152 | plugins: [
|
153 | ts({
|
154 | /* Plugin options */
|
155 | })
|
156 | ]
|
157 | };
|
158 | ```
|
159 |
|
160 | Without 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 |
|
167 | This plugin works very well with just Typescript.
|
168 | The `tsconfig.json` file closest to your project will be resolved and used in combination with Rollup.
|
169 | If 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
|
172 | ts({
|
173 | tsconfig: PRODUCTION ? "tsconfig.prod.json" : "tsconfig.json"
|
174 | });
|
175 | ```
|
176 |
|
177 | You 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
|
180 | ts({
|
181 | tsconfig: {
|
182 | target: ScriptTarget.ES2018,
|
183 | allowSyntheticDefaultImports: true,
|
184 | allowJs: true
|
185 | }
|
186 | });
|
187 | ```
|
188 |
|
189 | You 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
|
192 | ts({
|
193 | tsconfig: resolvedConfig => ({...resolvedConfig, allowJs: false})
|
194 | });
|
195 | ```
|
196 |
|
197 | The 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.
|
198 | But 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
|
201 | ts({
|
202 | tsconfig: {
|
203 | fileName: "my-awesome-tsconfig.json",
|
204 | hook: resolvedConfig => ({...resolvedConfig, allowJs: false})
|
205 | }
|
206 | });
|
207 | ```
|
208 |
|
209 | If 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`.
|
210 | If 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 |
|
214 | This 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.
|
215 | You don't have to do anything!
|
216 |
|
217 | ### Combining Typescript with a Browserslist
|
218 |
|
219 | If 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 |
|
221 | You can explicitly pass in Browserslist options. Here's an example with a raw Browserslist query:
|
222 |
|
223 | ```javascript
|
224 | ts({
|
225 | browserslist: ["last 1 version", "> 1%"]
|
226 | });
|
227 | ```
|
228 |
|
229 | You can also provide a configuration object instead of a raw query. Here's one with a baked-in query:
|
230 |
|
231 | ```javascript
|
232 | ts({
|
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
|
242 | ts({
|
243 | browserslist: {
|
244 | path: ".mybrowserslistrc"
|
245 | }
|
246 | });
|
247 | ```
|
248 |
|
249 | #### Using the plugin with Typescript, but without Browserslists
|
250 |
|
251 | If no Browserslist can be found, or if you simply don't want to use one, that's completely OK!
|
252 | In such cases, the `target` property of the nearest `tsconfig` will be used (or use the Typescript default setting if no such file exists).
|
253 |
|
254 | You can explicitly request that no Browserslist will be used by setting the `browserslist` property to `false` in the plugin options:
|
255 |
|
256 | ```javascript
|
257 | ts({
|
258 | browserslist: false
|
259 | });
|
260 | ```
|
261 |
|
262 | ### Combining Typescript with Babel
|
263 |
|
264 | This 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.
|
265 | One 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 |
|
267 | To use Babel, simply set the `transpiler` plugin option to `"babel"`:
|
268 |
|
269 | ```javascript
|
270 | ts({
|
271 | transpiler: "babel"
|
272 | });
|
273 | ```
|
274 |
|
275 | That'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.
|
276 | By 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 |
|
280 | This 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,
|
281 | these 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.
|
282 | All of this works automatically.
|
283 |
|
284 | #### `@babel/runtime` and external helpers
|
285 |
|
286 | This 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.
|
287 | You don't have to do anything.
|
288 |
|
289 | #### `@babel/runtime` and polyfills
|
290 |
|
291 | Babel 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).
|
292 | If 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 |
|
296 | This 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.
|
297 | This 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 |
|
301 | Typescript declaration files are normally distributed in a folder structure that resembles the structure of the source folder.
|
302 | With `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 |
|
306 | Rollup is a bundler, and with it, we can produce clean, small files that are easy to distribute.
|
307 | With `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 |
|
311 | And, 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
|
320 | ts({
|
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
|
331 | shows how you can provide one explicitly
|
332 |
|
333 | ```javascript
|
334 | ts({
|
335 | browserslist: ["last 1 version", "> 1%"]
|
336 | });
|
337 |
|
338 | // or
|
339 | ts({
|
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.
|
347 | And, [as described here](#typescript-with-browserslist-example), the same goes for Browserslists.
|
348 |
|
349 | ```javascript
|
350 | ts({
|
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
|
362 | ts({
|
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 |
|
373 | This example shows how you can use this plugin to accomplish quite advanced things:
|
374 |
|
375 | ```javascript
|
376 | const IS_PRODUCTION = process.env.NODE_ENV === "production";
|
377 | const BUNDLE_TARGET = process.env.BUNDLE_TARGET;
|
378 | const APP_ROOT = "/some/project/root/folder";
|
379 | const awesomeFrameworkTransformers = getAwesomeFrameworkCustomTransformers();
|
380 |
|
381 | ts({
|
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 |
|
405 | You 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
|
408 | ts({
|
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 |
|
417 | These can be provided in the plugin options for the `hook` property:
|
418 |
|
419 | ```typescript
|
420 | ts({
|
421 | hook: {
|
422 | // Add hooks here
|
423 | }
|
424 | });
|
425 | ```
|
426 |
|
427 | The next few subsections describe the different hooks that can be provided
|
428 |
|
429 | ### The `outputPath` hook
|
430 |
|
431 | Type: `(path: string, kind: "declaration" | "declarationMap" | "buildInfo") => string | undefined`
|
432 |
|
433 | The `outputPath` hook can be used to rewrite the location on the filesystem that assets produced by `rollup-plugin-ts` are written to.
|
434 | It is invoked immediately before assets such as _declarations_ or _declaration maps_ are emitted.
|
435 |
|
436 | The hook is invoked with the output path as well as the kind of asset the path represents as arguments.
|
437 | If 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
|
440 | ts({
|
441 | hook: {
|
442 | outputPath: (path, kind) => rewritePathSomehow(path, kind)
|
443 | }
|
444 | });
|
445 | ```
|
446 |
|
447 | For 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 |
|
451 | Type: `(diagnostics: readonly Diagnostic[]) => readonly Diagnostic[]|undefined`
|
452 |
|
453 | The `diagnostics` hook can be used to read, alter, and extend the diagnostics generated by TypeScript immediately before they are emitted as errors via Rollup.
|
454 | Normally, 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 |
|
456 | You 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 |
|
460 | The plugin options are documented in more detail across this README, but the full list of options is:
|
461 |
|
462 | #### `transpiler`
|
463 |
|
464 | Type: `"typescript" | "babel"`
|
465 |
|
466 | Can be any of `"babel"` or `"typescript"` (default: `"typescript"`).
|
467 | See [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 |
|
471 | Type: `string | Partial<IBabelInputOptions>`
|
472 |
|
473 | This 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 |
|
477 | Type: `string | Partial<CompilerOptions> | Partial<Record<keyof CompilerOptions, string | number | boolean>> | ParsedCommandLine | TsConfigResolver | TsConfigResolverWithFileName`
|
478 |
|
479 | Provide the Typescript [CompilerOptions](https://www.typescriptlang.org/docs/handbook/compiler-options.html) to use, or a path to a `tsconfig` with this property.
|
480 | See [this section](#using-it-with-just-typescript) for details on the many ways this property can be configured.
|
481 |
|
482 | #### `browserslist`
|
483 |
|
484 | Type: `false | string[] | string | BrowserslistConfig`
|
485 |
|
486 | Provide the [Browserslist](https://github.com/browserslist/browserslist) to use, or a path to a `.browserslistrc` with this property.
|
487 | See [this section](#combining-typescript-with-a-browserslist) for details on the many ways this property can be configured.
|
488 |
|
489 | #### `cwd`
|
490 |
|
491 | Type: `string`
|
492 |
|
493 | Use this property to overwrite whatever is considered the root directory. The default value is `process.cwd()`.
|
494 |
|
495 | #### `typescript`
|
496 |
|
497 | Type: `typeof import("typescript")`
|
498 |
|
499 | Use this property to pass a specific version of TypeScript to use.
|
500 |
|
501 | #### `transformers`
|
502 |
|
503 | Type: `(CustomTransformers | CustomTransformersFunction)[] | CustomTransformers | CustomTransformersFunction`
|
504 |
|
505 | Use this property to provide Typescript [`CustomTransformers`](https://github.com/Microsoft/TypeScript/pull/13940).
|
506 | See [this section](#using-customtransformers) for more details on how to configure this property.
|
507 |
|
508 | #### `include`
|
509 |
|
510 | Type: `string[]|string`
|
511 |
|
512 | This 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 |
|
516 | Type: `string[]|string`
|
517 |
|
518 | This 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 |
|
522 | Type: `boolean`
|
523 |
|
524 | If 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 |
|
528 | Optionally 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 |
|
532 | Use this property to get hooks into the internals of `rollup-plugin-ts`.
|
533 | See [this section](#hooks) for more details.
|
534 |
|
535 | ## Ignored/overridden options
|
536 |
|
537 | Typescript and Babel are powerful tools in their own right. Combined with Rollup, they become even more powerful.
|
538 | To 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 |
|
542 | The 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 |
|
561 | The 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 |
|
570 | The 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 |
|
584 | If 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.
|
585 | By 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 |
|
587 | If you provide these presets or plugins yourself through the found or provided Babel config, _your_ config options will take precedence.
|
588 |
|
589 | Here'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 |
|
602 |
|
603 | ## Contributing
|
604 |
|
605 | Do you want to contribute? Awesome! Please follow [these recommendations](./CONTRIBUTING.md).
|
606 |
|
607 |
|
608 |
|
609 |
|
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 |
|
618 |
|
619 |
|
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 |
|
630 |
|
631 |
|
632 |
|
633 | ## FAQ
|
634 |
|
635 |
|
636 |
|
637 | #### Does this plugin work with Code Splitting?
|
638 |
|
639 | Absolutely, even with Declaration files. Things will work seamlessly.
|
640 |
|
641 | #### Why wouldn't you use just Typescript?
|
642 |
|
643 | The Typescript compiler, while extremely powerful, has the restriction that it can only target a specific ECMAScript version.
|
644 | In reality, browsers and other ECMAScript environments has varying support for newer language features.
|
645 | Some browsers support even those features that haven't been publicized yet, while others support only parts of the latest language features.
|
646 |
|
647 | In effect, a browser may support a lot of modern features such as classes and proper lexical scoping, but lack others.
|
648 | With 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 |
|
650 | Babel, on the other hand, is far more granular in its design and applies syntax transformations on a feature-by-feature basis.
|
651 | Combined with something like `@babel/preset-env`, individual transformations can be applied for only those language features that are missing.
|
652 | This 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 |
|
656 | Babel 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 |
|
658 | The 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_.
|
659 | This gives Typescript the advantage over Babel that it has a greater understanding of your codebase in general and can understand your types across files.
|
660 | In 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 |
|
662 | 1. Emit Typescript diagnostics
|
663 | 2. Emit Typescript declaration (`.d.ts`) files and Typescript declaration map (`.d.ts.map`) files.
|
664 | 3. 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)
|
665 | 4. 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 |
|
669 | First, Typescript will be used for:
|
670 |
|
671 | 1. Reporting diagnostics.
|
672 | 2. Emitting Typescript declaration (`.d.ts`) files and Typescript declaration map (`.d.ts.map`) files.
|
673 | 3. Removing Typescript-specific features such as types, type-only imports, enums, and Typescript decorators.
|
674 |
|
675 | Babel 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 |
|
682 |
|
683 | ## License
|
684 |
|
685 | MIT © [Frederik Wessberg](mailto:frederikwessberg@hotmail.com) ([@FredWessberg](https://twitter.com/FredWessberg)) ([Website](https://github.com/wessberg))
|
686 |
|
687 |
|