1+ import * as path from 'path' ;
2+ import assign = require( 'object-assign' ) ;
3+ import { SchemaValidator , IBuildConfig } from '@microsoft/gulp-core-build' ;
4+ import ts = require( 'gulp-typescript' ) ;
5+ import * as typescript from 'typescript' ;
6+
7+ export interface ITsConfigFile < T > {
8+ compilerOptions : T ;
9+ }
10+
11+ /* tslint:disable:no-any */
12+ /*
13+ * A helper class which provides access to the TSConfig.json file for a particular project.
14+ * It also is a central place for managing the version of typescript which this project
15+ * should be built with.
16+ */
17+ export class TypeScriptConfiguration {
18+ private static _baseTsConfig : ITsConfigFile < ts . Settings > ;
19+ private static _typescript : any = require ( 'typescript' ) ;
20+
21+ /**
22+ * Gets `gulp-typescript` version of the config (used by TypeScriptTask)
23+ * Returns a new object each time.
24+ */
25+ public static getGulpTypescriptOptions ( buildConfig : IBuildConfig ) : ITsConfigFile < ts . Settings > {
26+ const file : ITsConfigFile < ts . Settings > = assign ( { } , this . _getTsConfigFile ( buildConfig ) ) ;
27+ assign ( file . compilerOptions , {
28+ rootDir : buildConfig . rootPath ,
29+ typescript : this . getTypescriptCompiler ( )
30+ } ) ;
31+ return file ;
32+ }
33+
34+ /*
35+ * Gets the `typescript` version of the config (used by ApiExtractorTask)
36+ * Note: these differ slightly from the values in the tsconfig.json
37+ * Returns a new object each time.
38+ *
39+ * Specifically, the issue in the difference between:
40+ * typescript.CompilerOptions
41+ * &
42+ * ts.Settings
43+ *
44+ * Insofar as `ts.Settings` accepts (and requires) enums for certain options, rather than strings.
45+ * The clearest example is `moduleResolution` below.
46+ */
47+ public static getTypescriptOptions ( buildConfig : IBuildConfig ) : ITsConfigFile < typescript . CompilerOptions > {
48+ const oldConfig : ITsConfigFile < ts . Settings > = this . getGulpTypescriptOptions ( buildConfig ) ;
49+ const newConfig : ITsConfigFile < typescript . CompilerOptions > = oldConfig as any ;
50+
51+ newConfig . compilerOptions . moduleResolution =
52+ oldConfig . compilerOptions . moduleResolution === 'node' ?
53+ typescript . ModuleResolutionKind . NodeJs : typescript . ModuleResolutionKind . Classic ;
54+
55+ return newConfig ;
56+ }
57+
58+ /**
59+ * Override the version of the typescript compiler
60+ */
61+ public static setTypescriptCompiler ( typescript : any ) : void {
62+ if ( this . _typescript ) {
63+ throw new Error ( 'The version of the typescript compiler should only be set once.' ) ;
64+ }
65+ if ( this . _baseTsConfig ) {
66+ throw new Error ( 'Set the version of the typescript compiler before tasks call getConfig()' ) ;
67+ }
68+ this . _typescript = typescript ;
69+ }
70+
71+ /**
72+ * Get the version of the typescript compiler which is to be used
73+ */
74+ public static getTypescriptCompiler ( ) : any {
75+ if ( ! this . _typescript ) {
76+ return require ( 'typescript' ) ;
77+ }
78+ return this . _typescript ;
79+ }
80+
81+ /**
82+ * Helper function which reads the tsconfig.json (or provides one), and memoizes it
83+ */
84+ private static _getTsConfigFile ( config : IBuildConfig ) : ITsConfigFile < ts . Settings > {
85+ if ( ! this . _baseTsConfig ) {
86+ try {
87+ this . _baseTsConfig = SchemaValidator . readCommentedJsonFile < any > (
88+ this . _getConfigPath ( config )
89+ ) ;
90+ } catch ( e ) {
91+ /* no-op */
92+ }
93+
94+ if ( ! this . _baseTsConfig ) {
95+ this . _baseTsConfig = {
96+ compilerOptions : {
97+ declaration : true ,
98+ experimentalDecorators : true ,
99+ jsx : 'react' ,
100+ moduleResolution : 'node' ,
101+ sourceMap : true ,
102+ target : 'es5' ,
103+ noUnusedParameters : true ,
104+ noUnusedLocals : true
105+ }
106+ } ;
107+ }
108+ }
109+ return this . _baseTsConfig ;
110+ }
111+
112+ /**
113+ * Extracts the path to the tsconfig.json based on the buildConfiguration
114+ */
115+ private static _getConfigPath ( buildConfig : IBuildConfig ) : string {
116+ return path . resolve ( path . join ( buildConfig . rootPath , 'tsconfig.json' ) ) ;
117+ }
118+ }
0 commit comments