UNPKG

7.86 kBMarkdownView Raw
1# rollup-plugin-typescript2
2
3[![npm-version](https://img.shields.io/npm/v/rollup-plugin-typescript2.svg?maxAge=2592000)](https://npmjs.org/package/rollup-plugin-typescript2)
4![npm-dependencies](https://img.shields.io/david/ezolenko/rollup-plugin-typescript2.svg?maxAge=2592000)
5![npm-monthly-downloads](https://img.shields.io/npm/dm/rollup-plugin-typescript2.svg?maxAge=2592000)
6[![Codeship Status](https://app.codeship.com/projects/fe9cf8f0-e8d4-0134-ec88-4e3d33dcd7ed/status?branch=master)](https://app.codeship.com/projects/207445)
7[![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)
8
9Rollup plugin for typescript with compiler errors.
10
11This is a rewrite of original rollup-plugin-typescript, starting and borrowing from [this fork](https://github.com/alexlur/rollup-plugin-typescript).
12
13This 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).
14
15## Usage
16
17```js
18// rollup.config.js
19import typescript from 'rollup-plugin-typescript2';
20
21export default {
22 entry: './main.ts',
23
24 plugins: [
25 typescript(/*{ plugin options }*/)
26 ]
27}
28```
29
30The 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.
31
32#### Some compiler options are forced
33
34* `noEmitHelpers`: false
35* `importHelpers`: true
36* `noResolve`: false
37* `noEmit`: false
38* `outDir`: `process.cwd()`
39* `declarationDir`: `process.cwd()` (*only if `useTsconfigDeclarationDir` is false in the plugin options*)
40* `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)*)
41
42#### Some compiler options have more than one compatible value.
43
44* `module`: defaults to `ES2015`, other valid value is `ESNext` (required for dynamic imports, see [#54](https://github.com/ezolenko/rollup-plugin-typescript2/issues/54)).
45
46### Compatibility
47
48#### rollup-plugin-node-resolve
49
50Must be before this plugin in the plugin list, especially when `browser: true` option is used, see [#66](https://github.com/ezolenko/rollup-plugin-typescript2/issues/66)
51
52#### rollup-plugin-commonjs
53
54See explanation for `rollupCommonJSResolveHack` option below.
55
56### Plugin options
57
58* `tsconfigDefaults`: `{}`
59
60 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).
61
62 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).
63
64 ```js
65 let defaults = { compilerOptions: { declaration: true } };
66 let override = { compilerOptions: { declaration: false } };
67
68 // ...
69 plugins: [
70 typescript({
71 tsconfigDefaults: defaults,
72 tsconfig: "tsconfig.json",
73 tsconfigOverride: override
74 })
75 ]
76 ```
77
78 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.
79
80* `tsconfig`: `undefined`
81
82 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.
83
84* `tsconfigOverride`: `{}`
85
86 See `tsconfigDefaults`.
87
88* `check`: true
89
90 Set to false to avoid doing any diagnostic checks on the code.
91
92* `verbosity`: 1
93
94 - 0 -- Error
95 - 1 -- Warning
96 - 2 -- Info
97 - 3 -- Debug
98
99* `clean`: false
100
101 Set to true for clean build (wipes out cache on every build).
102
103* `cacheRoot`: `./.rts2_cache`
104
105 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.
106
107* `include`: `[ "*.ts+(|x)", "**/*.ts+(|x)" ]`
108
109 By default passes all .ts files through typescript compiler.
110
111* `exclude`: `[ "*.d.ts", "**/*.d.ts" ]`
112
113 But excludes type definitions.
114
115* `abortOnError`: true
116
117 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).
118
119* `rollupCommonJSResolveHack`: false
120
121 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`.
122
123* `useTsconfigDeclarationDir`: false
124
125 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.
126
127* `typescript`: typescript module installed with the plugin
128
129 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.
130
131### Declarations
132
133This 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).
134By 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.
135
136### Watch mode
137
138The 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.
139
140Otherwise the plugin should work in watch mode. Make sure to run a normal build after watch session to catch any type errors.
141
142### Version
143
144This plugin currently requires TypeScript `2.4+`.
145
146### Rollup version
147
148This plugin currently requires rollup `0.50+`.
149
150### Reporting bugs
151
152Report any bugs on github: <https://github.com/ezolenko/rollup-plugin-typescript2/issues>.
153
154Attach your `tsconfig.json`, `package.json` (for versions of dependencies), rollup script and anything else that could influence module resolution, ambient types and typescript compilation.
155
156Check if problem is reproducible after running `npm prune` to clear any rogue types from npm_modules (by default typescript grabs all ambient types).
157
158Check if you get the same problem with `clean` option set to true (might indicate a bug in the cache).
159
160If makes sense, check if running `tsc` directly produces similar results.
161
162Attach plugin output with `verbosity` option set to 3 (this will list all files being transpiled and their imports).