UNPKG

3.77 kBTypeScriptView Raw
1/* eslint-disable no-use-before-define */
2import type _typescript from 'typescript';
3
4import type { FilterPattern } from '@rollup/pluginutils';
5import type { Plugin } from 'rollup';
6import type {
7 CompilerOptions,
8 CompilerOptionsValue,
9 CustomTransformers,
10 Program,
11 TsConfigSourceFile,
12 TypeChecker
13} from 'typescript';
14
15type ElementType<T extends Array<any> | undefined> = T extends (infer U)[] ? U : never;
16
17export type TransformerStage = keyof CustomTransformers;
18type StagedTransformerFactory<T extends TransformerStage> = ElementType<CustomTransformers[T]>;
19type TransformerFactory<T extends TransformerStage> =
20 | StagedTransformerFactory<T>
21 | ProgramTransformerFactory<T>
22 | TypeCheckerTransformerFactory<T>;
23
24export type CustomTransformerFactories = {
25 [stage in TransformerStage]?: Array<TransformerFactory<stage>>;
26};
27
28interface ProgramTransformerFactory<T extends TransformerStage> {
29 type: 'program';
30
31 factory(program: Program): StagedTransformerFactory<T>;
32}
33
34interface TypeCheckerTransformerFactory<T extends TransformerStage> {
35 type: 'typeChecker';
36
37 factory(typeChecker: TypeChecker): StagedTransformerFactory<T>;
38}
39
40export interface RollupTypescriptPluginOptions {
41 /**
42 * If using incremental this is the folder where the cached
43 * files will be created and kept for Typescript incremental
44 * compilation.
45 */
46 cacheDir?: string;
47 /**
48 * Determine which files are transpiled by Typescript (all `.ts` and
49 * `.tsx` files by default).
50 */
51 include?: FilterPattern;
52 /**
53 * Determine which files are ignored by Typescript
54 */
55 exclude?: FilterPattern;
56 /**
57 * Sets the `resolve` value for the underlying filter function. If not set will use the `rootDir` property
58 * @see {@link https://github.com/rollup/plugins/tree/master/packages/pluginutils#createfilter} @rollup/pluginutils `createFilter`
59 */
60 filterRoot?: string | false;
61 /**
62 * When set to false, ignores any options specified in the config file.
63 * If set to a string that corresponds to a file path, the specified file
64 * will be used as config file.
65 */
66 tsconfig?: string | false;
67 /**
68 * Overrides TypeScript used for transpilation
69 */
70 typescript?: typeof _typescript;
71 /**
72 * Overrides the injected TypeScript helpers with a custom version.
73 */
74 tslib?: Promise<string> | string;
75 /**
76 * TypeScript custom transformers
77 */
78 transformers?: CustomTransformerFactories;
79 /**
80 * When set to false, force non-cached files to always be emitted in the output directory.output
81 * If not set, will default to true with a warning.
82 */
83 outputToFilesystem?: boolean;
84 /**
85 * Pass additional compiler options to the plugin.
86 */
87 compilerOptions?: PartialCompilerOptions;
88 /**
89 * Override force setting of `noEmit` and `emitDeclarationOnly` and use what is defined in `tsconfig.json`
90 */
91 noForceEmit?: boolean;
92}
93
94export interface FlexibleCompilerOptions extends CompilerOptions {
95 [option: string]: CompilerOptionsValue | TsConfigSourceFile | undefined | any;
96}
97
98/** Properties of `CompilerOptions` that are normally enums */
99export type EnumCompilerOptions = 'module' | 'moduleResolution' | 'newLine' | 'jsx' | 'target';
100
101/** JSON representation of Typescript compiler options */
102export type JsonCompilerOptions = Omit<FlexibleCompilerOptions, EnumCompilerOptions> &
103 Record<EnumCompilerOptions, string>;
104
105/** Compiler options set by the plugin user. */
106export type PartialCompilerOptions =
107 | Partial<FlexibleCompilerOptions>
108 | Partial<JsonCompilerOptions>;
109
110export type RollupTypescriptOptions = RollupTypescriptPluginOptions & PartialCompilerOptions;
111
112/**
113 * Seamless integration between Rollup and Typescript.
114 */
115export default function typescript(options?: RollupTypescriptOptions): Plugin;