UNPKG

11.2 kBMarkdownView Raw
1# rollup-plugin-typescript2
2
3[![npm-version](https://img.shields.io/npm/v/rollup-plugin-typescript2.svg?maxAge=259200)](https://npmjs.org/package/rollup-plugin-typescript2)
4![npm-monthly-downloads](https://img.shields.io/npm/dm/rollup-plugin-typescript2.svg?maxAge=259200)
5[![Codeship Status](https://app.codeship.com/projects/fe9cf8f0-e8d4-0134-ec88-4e3d33dcd7ed/status?branch=master)](https://app.codeship.com/projects/207445)
6[![Codacy Badge](https://api.codacy.com/project/badge/Grade/e19b72ab9658405bbfb32dd6d65d1856)](https://www.codacy.com/app/zolenkoe/rollup-plugin-typescript2?utm_source=github.com&utm_medium=referral&utm_content=ezolenko/rollup-plugin-typescript2&utm_campaign=Badge_Grade)
7
8Rollup plugin for typescript with compiler errors.
9
10This is a rewrite of original rollup-plugin-typescript, starting and borrowing from [this fork](https://github.com/alexlur/rollup-plugin-typescript).
11
12This version is somewhat slower than original, but it will print out typescript syntactic and semantic diagnostic messages (the main reason for using typescript after all).
13
14## Installation
15
16```bash
17# with npm
18npm install rollup-plugin-typescript2 typescript --save-dev
19# with yarn
20yarn add rollup-plugin-typescript2 typescript --dev
21```
22
23## Usage
24
25```js
26// rollup.config.js
27import typescript from 'rollup-plugin-typescript2';
28
29export default {
30 input: './main.ts',
31
32 plugins: [
33 typescript(/*{ plugin options }*/)
34 ]
35}
36```
37
38The plugin inherits all compiler options and file lists from your `tsconfig.json` file. If your tsconfig has another name or another relative path from the root directory, see `tsconfigDefaults`, `tsconfig` and `tsconfigOverride` options below. This also allows for passing in different tsconfig files depending on your build target.
39
40#### Some compiler options are forced
41
42* `noEmitHelpers`: false
43* `importHelpers`: true
44* `noResolve`: false
45* `noEmit`: false
46* `inlineSourceMap`: false (see [#71](https://github.com/ezolenko/rollup-plugin-typescript2/issues/71))
47* `outDir`: `./placeholder` in cache root, see [83](https://github.com/ezolenko/rollup-plugin-typescript2/issues/83) and [Microsoft/TypeScript/issues/24715](https://github.com/Microsoft/TypeScript/issues/24715)
48* `declarationDir`: `process.cwd()` (*only if `useTsconfigDeclarationDir` is false in the plugin options*)
49* `moduleResolution`: `node` (*`classic` is [deprecated](https://www.typescriptlang.org/docs/handbook/module-resolution.html). It also breaks this plugin, see [#12](https://github.com/ezolenko/rollup-plugin-typescript2/issues/12) and [#14](https://github.com/ezolenko/rollup-plugin-typescript2/issues/14)*)
50* `allowNonTsExtensions`: true to let other plugins on the chain generate typescript, update plugin's include filter to pick them up (see [#111](https://github.com/ezolenko/rollup-plugin-typescript2/issues/111))
51
52#### Some compiler options have more than one compatible value.
53
54* `module`: defaults to `ES2015`, other valid value is `ESNext` (required for dynamic imports, see [#54](https://github.com/ezolenko/rollup-plugin-typescript2/issues/54)).
55
56### Compatibility
57
58#### rollup-plugin-node-resolve
59
60Must be before rollup-plugin-typescript2 in the plugin list, especially when `browser: true` option is used, see [#66](https://github.com/ezolenko/rollup-plugin-typescript2/issues/66)
61
62#### rollup-plugin-commonjs
63
64See explanation for `rollupCommonJSResolveHack` option below.
65
66#### plugins using async/await
67
68See `objectHashIgnoreUnknownHack` below.
69
70#### rollup-plugin-babel
71
72This plugin transpiles code, but doesn't change file extension. Babel plugin, even though it claims it processes all files, only looks at code with those extensions by default: `.js,.jsx,.es6,.es,.mjs`. To workaround add `ts` and `tsx` to the list of babel extensions.
73
74```js
75...
76import { DEFAULT_EXTENSIONS } from '@babel/core';
77...
78 babel({
79 extensions: [
80 ...DEFAULT_EXTENSIONS,
81 '.ts',
82 '.tsx'
83 ]
84 }),
85...
86```
87
88See [#108](https://github.com/ezolenko/rollup-plugin-typescript2/issues/108)
89
90### Plugin options
91
92* `tsconfigDefaults`: `{}`
93
94 The object passed as `tsconfigDefaults` will be merged with loaded `tsconfig.json`. Final config passed to typescript will be the result of values in `tsconfigDefaults` replaced by values in loaded `tsconfig.json`, replaced by values in `tsconfigOverride` and then replaced by hard `compilerOptions` overrides on top of that (see above).
95
96 For simplicity and other tools' sake, try to minimize usage of defaults and overrides and keep everything in `tsconfig.json` file (tsconfigs can themselves be chained, so save some turtles).
97
98 ```js
99 let defaults = { compilerOptions: { declaration: true } };
100 let override = { compilerOptions: { declaration: false } };
101
102 // ...
103 plugins: [
104 typescript({
105 tsconfigDefaults: defaults,
106 tsconfig: "tsconfig.json",
107 tsconfigOverride: override
108 })
109 ]
110 ```
111
112 This is a [deep merge](https://lodash.com/docs/4.17.4#merge) (objects are merged, arrays are concatenated, primitives are replaced, etc), increase `verbosity` to 3 and look for `parsed tsconfig` if you get something unexpected.
113
114* `tsconfig`: `undefined`
115
116 Path to `tsconfig.json`. Set this if your tsconfig has another name or relative location from the project directory. By default will try to load `./tsconfig.json`, but will not fail if file is missing unless the value is set explicitly.
117
118* `tsconfigOverride`: `{}`
119
120 See `tsconfigDefaults`.
121
122* `check`: true
123
124 Set to false to avoid doing any diagnostic checks on the code.
125
126* `verbosity`: 1
127
128 - 0 -- Error
129 - 1 -- Warning
130 - 2 -- Info
131 - 3 -- Debug
132
133* `clean`: false
134
135 Set to true for clean build (wipes out cache on every build).
136
137* `cacheRoot`: `./.rts2_cache`
138
139 Path to cache. Defaults to a folder in the current directory. Can be safely moved out with something like `${require('temp-dir')}/.rpt2_cache`, but watch out for multiple concurrent builds of the same repo.
140
141* `include`: `[ "*.ts+(|x)", "**/*.ts+(|x)" ]`
142
143 By default passes all .ts files through typescript compiler.
144
145* `exclude`: `[ "*.d.ts", "**/*.d.ts" ]`
146
147 But excludes type definitions.
148
149* `abortOnError`: true
150
151 Bail out on first syntactic or semantic error. In some cases setting this to false will result in exception in rollup itself (for example for unresolvable imports).
152
153* `rollupCommonJSResolveHack`: false
154
155 On windows typescript resolver favors POSIX path, while commonjs plugin (and maybe others?) uses native path as module id. This can result in `namedExports` being ignored if rollup happened to use typescript's resolution. Set to true to pass resolved module path through `resolve()` to match up with `rollup-plugin-commonjs`.
156
157* `objectHashIgnoreUnknownHack`: false
158
159 The plugin uses rollup config as part of cache key. `object-hash` is used to generate a hash, but it can't hash certain elements at the moment. Setting this option to true will make `object-hash` ignore unknowns, at the cost of not invalidating the cache if ignored elements are changed. Only enable this if you need it (`Error: Unknown object type "asyncfunction"` for example) and make sure to run with `clean: true` once in a while and definitely before a release. (See [#105](https://github.com/ezolenko/rollup-plugin-typescript2/issues/105))
160
161* `useTsconfigDeclarationDir`: false
162
163 If true, declaration files will be emitted in the directory given in the tsconfig. If false, the declaration files will be placed inside the destination directory given in the Rollup configuration.
164
165* `typescript`: typescript module installed with the plugin
166
167 When typescript version installed by the plugin (latest 2.x) is unacceptable, you can import your own typescript module and pass it in as `typescript: require("typescript")`. Must be 2.0+, things might break if transpiler interfaces changed enough from what the plugin was built against.
168
169* `transformers`: `undefined`
170
171 **experimental**, typescript 2.4.1+
172
173 Transformers will likely be available in tsconfig eventually, so this is not a stable interface, see [Microsoft/TypeScript/issues/14419](https://github.com/Microsoft/TypeScript/issues/14419).
174
175 For example, integrating [kimamula/ts-transformer-keys](https://github.com/kimamula/ts-transformer-keys):
176
177 ```js
178 const keysTransformer = require('ts-transformer-keys/transformer').default;
179 const transformer = (service) =>
180 {
181 before: [ keysTransformer(service.getProgram()) ],
182 after: []
183 };
184
185 // ...
186 plugins: [
187 typescript({ transformers: [transformer] })
188 ]
189 ```
190
191### Declarations
192
193This plugin respects `declaration: true` in your `tsconfig.json` file. When set, it will emit `*.d.ts` files for your bundle. The resulting file(s) can then be used with the `types` property in your `package.json` file as described [here](https://www.typescriptlang.org/docs/handbook/declaration-files/publishing.html).
194By default, the declaration files will be located in the same directory as the generated Rollup bundle. If you want to override this behavior and instead use the declarationDir set `useTsconfigDeclarationDir` to `true` in the plugin options.
195
196### Watch mode
197
198The way typescript handles type-only imports and ambient types effectively hides them from rollup watch, because import statements are not generated and changing them doesn't trigger a rebuild.
199
200Otherwise the plugin should work in watch mode. Make sure to run a normal build after watch session to catch any type errors.
201
202### Requirements
203
204TypeScript `2.4+`
205Rollup `0.68+`
206Node `6.4.0+` (basic es6 support)
207
208### Reporting bugs
209
210Report any bugs on github: <https://github.com/ezolenko/rollup-plugin-typescript2/issues>.
211
212Attach your `tsconfig.json`, `package.json` (for versions of dependencies), rollup script and anything else that could influence module resolution, ambient types and typescript compilation.
213
214Check if problem is reproducible after running `npm prune` to clear any rogue types from npm_modules (by default typescript grabs all ambient types).
215
216Check if you get the same problem with `clean` option set to true (might indicate a bug in the cache).
217
218If makes sense, check if running `tsc` directly produces similar results.
219
220Attach plugin output with `verbosity` option set to 3 (this will list all files being transpiled and their imports).
221
222### Contributing
223
224Use the normal github process of forking, making a branch and creating a PR when ready. Fix all linting problems (run `npm lint`), fix any failed checks that are run on the PR (basically lint right now). Use an editor that supports editorconfig, or match the settings from `.editorconfig` file manually.
225
226Fastest way to test changes is to do a self build, the plugin is part of its own build system:
227- make changes
228- run `npm build` (uses build committed to master branch)
229- check that you get expected changes in `dist`
230- run `npm build-self` (uses fresh local build)
231- check `dist` for the expected changes
232- run `npm build-self` _again_ to make sure plugin built by new version can still build itself
233
234If `build-self` breaks at some point, fix the problem and restart from `build` step (a known good copy).
235
236This repo badly needs unittests and integration tests with various scenarios and expected outcomes though.