UNPKG

6.71 kBMarkdownView Raw
1[npm]: https://img.shields.io/npm/v/@rollup/plugin-typescript
2[npm-url]: https://www.npmjs.com/package/@rollup/plugin-typescript
3[size]: https://packagephobia.now.sh/badge?p=@rollup/plugin-typescript
4[size-url]: https://packagephobia.now.sh/result?p=@rollup/plugin-typescript
5
6[![npm][npm]][npm-url]
7[![size][size]][size-url]
8[![libera manifesto](https://img.shields.io/badge/libera-manifesto-lightgrey.svg)](https://liberamanifesto.com)
9
10# @rollup/plugin-typescript
11
12🍣 A Rollup plugin for seamless integration between Rollup and Typescript.
13
14## Requirements
15
16This plugin requires an [LTS](https://github.com/nodejs/Release) Node version (v8.0.0+) and Rollup v1.20.0+. Due to the use of `tslib` to inject helpers, this plugin requires at least [TypeScript 2.1](https://github.com/Microsoft/TypeScript/wiki/Roadmap#21-december-2016). See also [here](https://blog.mariusschulz.com/2016/12/16/typescript-2-1-external-helpers-library#the-importhelpers-flag-and-tslib).
17
18## Install
19
20Using npm:
21
22```console
23npm install @rollup/plugin-typescript --save-dev
24```
25
26Note that both `typescript` and `tslib` are peer dependencies of this plugin that need to be installed separately.
27
28## Why?
29
30See [rollup-plugin-babel](https://github.com/rollup/rollup-plugin-babel).
31
32## Usage
33
34Create a `rollup.config.js` [configuration file](https://www.rollupjs.org/guide/en/#configuration-files) and import the plugin:
35
36```js
37// rollup.config.js
38import typescript from '@rollup/plugin-typescript';
39
40export default {
41 input: 'src/index.ts',
42 output: {
43 dir: 'output',
44 format: 'cjs'
45 },
46 plugins: [typescript()]
47};
48```
49
50Then call `rollup` either via the [CLI](https://www.rollupjs.org/guide/en/#command-line-reference) or the [API](https://www.rollupjs.org/guide/en/#javascript-api).
51
52## Options
53
54The plugin loads any [`compilerOptions`](http://www.typescriptlang.org/docs/handbook/compiler-options.html) from the `tsconfig.json` file by default. Passing options to the plugin directly overrides those options:
55
56```js
57...
58export default {
59 input: './main.ts',
60 plugins: [
61 typescript({lib: ["es5", "es6", "dom"], target: "es5"})
62 ]
63}
64```
65
66The following options are unique to `rollup-plugin-typescript`:
67
68### `exclude`
69
70Type: `String` | `Array[...String]`<br>
71Default: `null`
72
73A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should _ignore_. By default no files are ignored.
74
75### `include`
76
77Type: `String` | `Array[...String]`<br>
78Default: `null`
79
80A [minimatch pattern](https://github.com/isaacs/minimatch), or array of patterns, which specifies the files in the build the plugin should operate on. By default all `.ts` and `.tsx` files are targeted.
81
82### `tsconfig`
83
84Type: `String` | `Boolean`<br>
85Default: `true`
86
87When set to false, ignores any options specified in the config file. If set to a string that corresponds to a file path, the specified file will be used as config file.
88
89### `typescript`
90
91Type: `import('typescript')`<br>
92Default: _peer dependency_
93
94Overrides the TypeScript module used for transpilation.
95
96```js
97typescript({
98 typescript: require('some-fork-of-typescript')
99});
100```
101
102### `tslib`
103
104Type: `String`<br>
105Default: _peer dependency_
106
107Overrides the injected TypeScript helpers with a custom version.
108
109```js
110typescript({
111 tslib: fs.readFileSync(require.resolve('some-fork-of-tslib'))
112});
113```
114
115### Typescript compiler options
116
117Some of Typescript's [CompilerOptions](https://www.typescriptlang.org/docs/handbook/compiler-options.html) affect how Rollup builds files.
118
119#### `noEmitOnError`
120
121Type: `Boolean`<br>
122Default: `true`
123
124If a type error is detected, the Rollup build is aborted when this option is set to true.
125
126#### `files`, `include`, `exclude`
127
128Type: `Array[...String]`<br>
129Default: `[]`
130
131Declaration files are automatically included if they are listed in the `files` field in your `tsconfig.json` file. Source files in these fields are ignored as Rollup's configuration is used instead.
132
133#### Ignored options
134
135These compiler options are ignored by Rollup:
136
137- `declaration`, `declarationMap`: This plugin currently cannot emit declaration files.
138- `incremental`, `tsBuildInfoFile`: This plugin currently does not support incremental compilation using Typescript.
139- `noEmitHelpers`, `importHelpers`: The `tslib` helper module always must be used.
140- `noEmit`, `emitDeclarationOnly`: Typescript needs to emit code for the plugin to work with.
141- `noResolve`: Preventing Typescript from resolving code may break compilation
142
143### Importing CommonJS
144
145Though it is not recommended, it is possible to configure this plugin to handle imports of CommonJS files from TypeScript. For this, you need to specify `CommonJS` as the module format and add `rollup-plugin-commonjs` to transpile the CommonJS output generated by TypeScript to ES Modules so that rollup can process it.
146
147```js
148// rollup.config.js
149import typescript from 'rollup-plugin-typescript';
150import commonjs from 'rollup-plugin-commonjs';
151
152export default {
153 input: './main.ts',
154 plugins: [
155 typescript({ module: 'CommonJS' }),
156 commonjs({ extensions: ['.js', '.ts'] }) // the ".ts" extension is required
157 ]
158};
159```
160
161Note that this will often result in less optimal output.
162
163### Preserving JSX output
164
165Whenever choosing to preserve JSX output to be further consumed by another transform step via `tsconfig` `compilerOptions` by setting `jsx: 'preserve'` or [overriding options](#options), please bear in mind that, by itself, this plugin won't be able to preserve JSX output, usually failing with:
166
167```sh
168[!] Error: Unexpected token (Note that you need plugins to import files that are not JavaScript)
169file.tsx (1:15)
1701: export default <span>Foobar</span>
171 ^
172```
173
174To prevent that, make sure to use the acorn plugin, namely `acorn-jsx`, which will make Rollup's parser acorn handle JSX tokens. (See https://rollupjs.org/guide/en/#acorninjectplugins)
175
176After adding `acorn-jsx` plugin, your Rollup config would look like the following, correctly preserving your JSX output.
177
178```js
179import jsx from 'acorn-jsx';
180import typescript from 'rollup-plugin-typescript';
181
182export default {
183 // … other options …
184 acornInjectPlugins: [jsx()],
185 plugins: [typescript({ jsx: 'preserve' })]
186};
187```
188
189### Faster compiling
190
191Previous versions of this plugin used Typescript's `transpileModule` API, which is faster but does not perform typechecking and does not support cross-file features like `const enum`s and emit-less types. If you want this behaviour, you can use [@rollup/plugin-sucrase](https://github.com/rollup/plugins/tree/master/packages/sucrase) instead.
192
193## Meta
194
195[CONTRIBUTING](/.github/CONTRIBUTING.md)
196
197[LICENSE (MIT)](/LICENSE)