Skip to content

Commit

Permalink
feat(typescript): Refine options interface (#284)
Browse files Browse the repository at this point in the history
Used internal types to improve the interface
  • Loading branch information
NotWoods authored Mar 29, 2020
1 parent cbc341d commit 9af578c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 37 deletions.
19 changes: 5 additions & 14 deletions packages/typescript/src/options/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
/** Properties of `CompilerOptions` that are normally enums */
export interface EnumCompilerOptions {
module: string;
moduleResolution: string;
newLine: string;
jsx: string;
target: string;
}
import { PartialCompilerOptions } from '../../types';

export { EnumCompilerOptions, JsonCompilerOptions } from '../../types';
export { PartialCompilerOptions };

/** Typescript compiler options */
export type CompilerOptions = import('typescript').CompilerOptions;
/** JSON representation of Typescript compiler options */
export type JsonCompilerOptions = Omit<CompilerOptions, keyof EnumCompilerOptions> &
EnumCompilerOptions;
/** Compiler options set by the plugin user. */
export type PartialCustomOptions = Partial<CompilerOptions> | Partial<JsonCompilerOptions>;

export const DEFAULT_COMPILER_OPTIONS: PartialCustomOptions = {
export const DEFAULT_COMPILER_OPTIONS: PartialCompilerOptions = {
module: 'esnext',
noEmitOnError: true,
skipLibCheck: true
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript/src/options/normalize.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-param-reassign */
import { resolve } from 'path';

import { CompilerOptions, PartialCustomOptions } from './interfaces';
import { CompilerOptions, PartialCompilerOptions } from './interfaces';

export const DIRECTORY_PROPS = ['outDir', 'declarationDir'] as const;

Expand All @@ -12,7 +12,7 @@ export const DIRECTORY_PROPS = ['outDir', 'declarationDir'] as const;
* @param compilerOptions Compiler options to _mutate_.
* @param relativeTo Paths are resolved relative to this path.
*/
export function makePathsAbsolute(compilerOptions: PartialCustomOptions, relativeTo: string) {
export function makePathsAbsolute(compilerOptions: PartialCompilerOptions, relativeTo: string) {
for (const pathProp of DIRECTORY_PROPS) {
if (compilerOptions[pathProp]) {
compilerOptions[pathProp] = resolve(relativeTo, compilerOptions[pathProp] as string);
Expand Down
6 changes: 2 additions & 4 deletions packages/typescript/src/options/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { createFilter } from '@rollup/pluginutils';
import * as defaultTs from 'typescript';

import { RollupTypescriptOptions } from '../../types';
import { RollupTypescriptOptions, PartialCompilerOptions } from '../../types';
import getTsLibPath from '../tslib';

import { PartialCustomOptions } from './interfaces';

/**
* Separate the Rollup plugin options from the Typescript compiler options,
* and normalize the Rollup options.
Expand All @@ -24,7 +22,7 @@ export default function getPluginOptions(options: RollupTypescriptOptions) {
return {
filter,
tsconfig,
compilerOptions: compilerOptions as PartialCustomOptions,
compilerOptions: compilerOptions as PartialCompilerOptions,
typescript: typescript || defaultTs,
tslib: tslib || getTsLibPath()
};
Expand Down
8 changes: 4 additions & 4 deletions packages/typescript/src/options/tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
DEFAULT_COMPILER_OPTIONS,
EnumCompilerOptions,
FORCED_COMPILER_OPTIONS,
PartialCustomOptions
PartialCompilerOptions
} from './interfaces';
import { normalizeCompilerOptions, makePathsAbsolute } from './normalize';

Expand Down Expand Up @@ -59,9 +59,9 @@ function readTsConfigFile(ts: typeof import('typescript'), tsConfigPath: string)
* This indicates that the internal CompilerOptions type is used rather than the JsonCompilerOptions.
*/
function containsEnumOptions(
compilerOptions: PartialCustomOptions
compilerOptions: PartialCompilerOptions
): compilerOptions is Partial<CompilerOptions> {
const enums: Array<keyof EnumCompilerOptions> = [
const enums: Array<EnumCompilerOptions> = [
'module',
'target',
'jsx',
Expand Down Expand Up @@ -89,7 +89,7 @@ const configCache = new Map() as import('typescript').Map<
export function parseTypescriptConfig(
ts: typeof import('typescript'),
tsconfig: RollupTypescriptOptions['tsconfig'],
compilerOptions: PartialCustomOptions
compilerOptions: PartialCompilerOptions
) {
/* eslint-disable no-undefined */
const cwd = process.cwd();
Expand Down
27 changes: 14 additions & 13 deletions packages/typescript/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,8 @@
import { FilterPattern } from '@rollup/pluginutils';
import { Plugin } from 'rollup';
import { CompilerOptionsValue, TsConfigSourceFile } from 'typescript';

export interface RollupTypescriptOptions {
/**
* Other Typescript compiler options.
*/
[option: string]:
| CompilerOptionsValue
| TsConfigSourceFile
| RollupTypescriptOptions['include']
| RollupTypescriptOptions['typescript']
| RollupTypescriptOptions['tslib']
| undefined;
import { CompilerOptions } from 'typescript';

export interface RollupTypescriptPluginOptions {
/**
* Determine which files are transpiled by Typescript (all `.ts` and
* `.tsx` files by default).
Expand All @@ -40,6 +29,18 @@ export interface RollupTypescriptOptions {
tslib?: Promise<string> | string;
}

/** Properties of `CompilerOptions` that are normally enums */
export type EnumCompilerOptions = 'module' | 'moduleResolution' | 'newLine' | 'jsx' | 'target';

/** JSON representation of Typescript compiler options */
export type JsonCompilerOptions = Omit<CompilerOptions, EnumCompilerOptions> &
Record<EnumCompilerOptions, string>;

/** Compiler options set by the plugin user. */
export type PartialCompilerOptions = Partial<CompilerOptions> | Partial<JsonCompilerOptions>;

export type RollupTypescriptOptions = RollupTypescriptPluginOptions & PartialCompilerOptions;

/**
* Seamless integration between Rollup and Typescript.
*/
Expand Down

0 comments on commit 9af578c

Please sign in to comment.