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 | [](https://liberamanifesto.com)
|
9 |
|
10 | # @rollup/plugin-typescript
|
11 |
|
12 | 🍣 A Rollup plugin for seamless integration between Rollup and Typescript.
|
13 |
|
14 | ## Requirements
|
15 |
|
16 | This 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 |
|
20 | Using npm:
|
21 |
|
22 | ```console
|
23 | npm install @rollup/plugin-typescript --save-dev
|
24 | ```
|
25 |
|
26 | Note that both `typescript` and `tslib` are peer dependencies of this plugin that need to be installed separately.
|
27 |
|
28 | ## Why?
|
29 |
|
30 | See [rollup-plugin-babel](https://github.com/rollup/rollup-plugin-babel).
|
31 |
|
32 | ## Usage
|
33 |
|
34 | Create 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
|
38 | import typescript from '@rollup/plugin-typescript';
|
39 |
|
40 | export default {
|
41 | input: 'src/index.ts',
|
42 | output: {
|
43 | dir: 'output',
|
44 | format: 'cjs'
|
45 | },
|
46 | plugins: [typescript()]
|
47 | };
|
48 | ```
|
49 |
|
50 | Then 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 |
|
54 | The 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 | ...
|
58 | export default {
|
59 | input: './main.ts',
|
60 | plugins: [
|
61 | typescript({lib: ["es5", "es6", "dom"], target: "es5"})
|
62 | ]
|
63 | }
|
64 | ```
|
65 |
|
66 | The following options are unique to `rollup-plugin-typescript`:
|
67 |
|
68 | ### `exclude`
|
69 |
|
70 | Type: `String` | `Array[...String]`<br>
|
71 | Default: `null`
|
72 |
|
73 | A [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 |
|
77 | Type: `String` | `Array[...String]`<br>
|
78 | Default: `null`
|
79 |
|
80 | A [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 |
|
84 | Type: `String` | `Boolean`<br>
|
85 | Default: `true`
|
86 |
|
87 | When 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 |
|
91 | Type: `import('typescript')`<br>
|
92 | Default: _peer dependency_
|
93 |
|
94 | Overrides the TypeScript module used for transpilation.
|
95 |
|
96 | ```js
|
97 | typescript({
|
98 | typescript: require('some-fork-of-typescript')
|
99 | });
|
100 | ```
|
101 |
|
102 | ### `tslib`
|
103 |
|
104 | Type: `String`<br>
|
105 | Default: _peer dependency_
|
106 |
|
107 | Overrides the injected TypeScript helpers with a custom version.
|
108 |
|
109 | ```js
|
110 | typescript({
|
111 | tslib: fs.readFileSync(require.resolve('some-fork-of-tslib'))
|
112 | });
|
113 | ```
|
114 |
|
115 | ### Typescript compiler options
|
116 |
|
117 | Some of Typescript's [CompilerOptions](https://www.typescriptlang.org/docs/handbook/compiler-options.html) affect how Rollup builds files.
|
118 |
|
119 | #### `noEmitOnError`
|
120 |
|
121 | Type: `Boolean`<br>
|
122 | Default: `true`
|
123 |
|
124 | If a type error is detected, the Rollup build is aborted when this option is set to true.
|
125 |
|
126 | #### `files`, `include`, `exclude`
|
127 |
|
128 | Type: `Array[...String]`<br>
|
129 | Default: `[]`
|
130 |
|
131 | Declaration 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 |
|
135 | These 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 |
|
145 | Though 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
|
149 | import typescript from 'rollup-plugin-typescript';
|
150 | import commonjs from 'rollup-plugin-commonjs';
|
151 |
|
152 | export 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 |
|
161 | Note that this will often result in less optimal output.
|
162 |
|
163 | ### Preserving JSX output
|
164 |
|
165 | Whenever 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)
|
169 | file.tsx (1:15)
|
170 | 1: export default <span>Foobar</span>
|
171 | ^
|
172 | ```
|
173 |
|
174 | To 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 |
|
176 | After adding `acorn-jsx` plugin, your Rollup config would look like the following, correctly preserving your JSX output.
|
177 |
|
178 | ```js
|
179 | import jsx from 'acorn-jsx';
|
180 | import typescript from 'rollup-plugin-typescript';
|
181 |
|
182 | export default {
|
183 | // … other options …
|
184 | acornInjectPlugins: [jsx()],
|
185 | plugins: [typescript({ jsx: 'preserve' })]
|
186 | };
|
187 | ```
|
188 |
|
189 | ### Faster compiling
|
190 |
|
191 | Previous 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)
|