1
- 'use strict' ;
2
-
3
1
const async = Npm . require ( 'async' ) ;
4
2
const Future = Npm . require ( 'fibers/future' ) ;
3
+ const TSBuild = Npm . require ( 'meteor-typescript' ) . TSBuild ;
5
4
6
5
TypeScriptCompiler = class TypeScriptCompiler {
7
- constructor ( extraOptions , maxParallelism , logFn ) {
6
+ constructor ( extraOptions , maxParallelism ) {
8
7
TypeScript . validateExtraOptions ( extraOptions ) ;
9
8
10
9
this . extraOptions = extraOptions ;
11
10
this . maxParallelism = maxParallelism || 10 ;
12
- this . tsconfig = null ;
11
+ this . tsconfig = TypeScript . getDefaultOptions ( ) ;
13
12
this . cfgHash = null ;
14
- this . logFn = logFn || console . log ;
15
13
}
16
14
17
15
processFilesForTarget ( inputFiles ) {
16
+ this . extendFiles ( inputFiles ) ;
17
+
18
18
// If tsconfig.json has changed, create new one.
19
19
this . processConfig ( inputFiles ) ;
20
20
21
- let filesSourceMap = new Map ( ) ;
21
+ let archMap = { } ;
22
+ let filesMap = { } ;
22
23
inputFiles . forEach ( ( inputFile , index ) => {
23
24
if ( this . isConfigFile ( inputFile ) ) return ;
24
25
25
- filesSourceMap . set ( this . getExtendedPath ( inputFile ) , index ) ;
26
+ let arch = inputFile . getArch ( ) ;
27
+ let archFiles = archMap [ arch ] ;
28
+ if ( ! archFiles ) {
29
+ archFiles = [ ] ;
30
+ archMap [ arch ] = archFiles ;
31
+ }
32
+ archFiles . push ( inputFile ) ;
33
+ filesMap [ this . getExtendedPath ( inputFile ) ] = index ;
26
34
} ) ;
35
+
27
36
let getFileContent = filePath => {
28
- let index = filesSourceMap . get ( filePath ) ;
37
+ let index = filesMap [ filePath ] ;
29
38
return index !== undefined ?
30
39
inputFiles [ index ] . getContentsAsString ( ) : null ;
31
40
} ;
32
41
33
- // Filters out typings and tsconfig.
34
- // Other files should be compiled.
35
- let tsFiles = inputFiles . filter ( inputFile =>
36
- ! ( this . isConfigFile ( inputFile ) || this . isDeclarationFile ( inputFile ) ) ) ;
42
+ // Assemble options.
43
+ let typings = this . tsconfig . typings ;
44
+ let compilerOptions = this . tsconfig . compilerOptions ;
45
+ compilerOptions = TypeScript . getCompilerOptions (
46
+ compilerOptions , this . extraOptions ) ;
47
+ let buildOptions = { compilerOptions, typings } ;
37
48
49
+ let compileDebug = new DebugLog ( 'compilation' ) ;
38
50
const future = new Future ;
39
- async . eachLimit ( tsFiles , this . maxParallelism , ( inputFile , cb ) => {
40
- let compilerOptions = this . tsconfig ?
41
- this . tsconfig . compilerOptions : null ;
42
-
43
- compilerOptions = TypeScript . getCompilerOptions (
44
- compilerOptions , this . extraOptions ) ;
45
-
46
- let source = inputFile . getContentsAsString ( ) ;
47
- let inputFilePath = inputFile . getPathInPackage ( ) ;
48
- let outputFilePath = removeTsExt ( inputFilePath ) + '.js' ;
49
- let toBeAdded = {
50
- sourcePath : inputFilePath ,
51
- path : outputFilePath ,
52
- data : source ,
53
- hash : inputFile . getSourceHash ( ) ,
54
- sourceMap : null ,
55
- bare : this . isBareFile ( inputFile , compilerOptions )
56
- } ;
57
-
58
- let filePath = this . getExtendedPath ( inputFile ) ;
59
- let typings = this . tsconfig ? this . tsconfig . typings : [ ] ;
60
- let moduleName = this . getFileModuleName ( inputFile , compilerOptions ) ;
61
- let arch = inputFile . getArch ( ) ;
62
- let tsOptions = {
63
- compilerOptions,
64
- moduleName,
65
- filePath,
66
- typings,
67
- arch
68
- } ;
69
-
70
- let error = null ;
71
- try {
72
- let result = TypeScript . compile ( getFileContent , tsOptions ) ;
73
- this . processDiagnostics ( inputFile ,
74
- result . diagnostics , compilerOptions ) ;
51
+ async . each ( _ . keys ( archMap ) , ( arch , cb ) => {
52
+ let archFiles = archMap [ arch ] ;
53
+ let filePaths = archFiles . map ( inputFile => this . getExtendedPath ( inputFile ) ) ;
54
+ compileDebug . log ( 'process files: %s' , filePaths ) ;
55
+ buildOptions . arch = arch ;
56
+
57
+ let buildDebug = new DebugLog ( 'tsBuild' ) ;
58
+ let tsBuild = new TSBuild ( filePaths , getFileContent , buildOptions ) ;
59
+
60
+ archFiles . forEach ( inputFile => {
61
+ if ( this . isDeclarationFile ( inputFile ) ) return ;
62
+
63
+ let co = compilerOptions ;
64
+ let source = inputFile . getContentsAsString ( ) ;
65
+ let inputFilePath = inputFile . getPathInPackage ( ) ;
66
+ let outputFilePath = removeTsExt ( inputFilePath ) + '.js' ;
67
+ let toBeAdded = {
68
+ sourcePath : inputFilePath ,
69
+ path : outputFilePath ,
70
+ data : source ,
71
+ hash : inputFile . getSourceHash ( ) ,
72
+ sourceMap : null ,
73
+ bare : inputFile . isBare ( )
74
+ } ;
75
+
76
+ let filePath = this . getExtendedPath ( inputFile ) ;
77
+ let moduleName = this . getFileModuleName ( inputFile , co ) ;
78
+
79
+ let emitDebug = new DebugLog ( 'tsEmit' ) ;
80
+ let result = tsBuild . emit ( filePath , moduleName ) ;
81
+ this . processDiagnostics ( inputFile , result . diagnostics , co ) ;
82
+ emitDebug . end ( ) ;
75
83
76
84
toBeAdded . data = result . code ;
85
+ toBeAdded . bare = toBeAdded . bare || ! result . isExternal ;
77
86
toBeAdded . hash = result . hash ;
78
87
toBeAdded . sourceMap = result . sourceMap ;
79
88
80
89
inputFile . addJavaScript ( toBeAdded ) ;
81
- } catch ( e ) {
82
- error = e ;
83
- } finally {
84
- cb ( error ) ;
85
- }
90
+ } ) ;
91
+
92
+ cb ( ) ;
93
+
94
+ buildDebug . end ( ) ;
86
95
} , future . resolver ( ) ) ;
96
+
87
97
future . wait ( ) ;
98
+
99
+ compileDebug . end ( ) ;
100
+ }
101
+
102
+ extendFiles ( inputFiles ) {
103
+ inputFiles . forEach ( inputFile => _ . defaults ( inputFile , FileMixin ) ) ;
88
104
}
89
105
90
106
processDiagnostics ( inputFile , diagnostics , compilerOptions ) {
@@ -104,13 +120,12 @@ TypeScriptCompiler = class TypeScriptCompiler {
104
120
// And log out other errors except package files.
105
121
if ( compilerOptions && compilerOptions . diagnostics ) {
106
122
diagnostics . semanticErrors . forEach ( diagnostic => {
107
- let error = {
123
+ inputFile . warn ( {
108
124
message : diagnostic . message ,
109
125
sourcePath : this . getExtendedPath ( inputFile ) ,
110
126
line : diagnostic . line ,
111
127
column : diagnostic . column
112
- } ;
113
- this . logFn ( `${ error . sourcePath } (${ error . line } , ${ error . column } ): ${ error . message } ` ) ;
128
+ } ) ;
114
129
} ) ;
115
130
}
116
131
}
@@ -125,13 +140,6 @@ TypeScriptCompiler = class TypeScriptCompiler {
125
140
return noExt ? removeTsExt ( filePath ) : filePath ;
126
141
}
127
142
128
- isBareFile ( inputFile , compilerOptions ) {
129
- let fileOptions = inputFile . getFileOptions ( ) ;
130
- let packageName = inputFile . getPackageName ( ) ;
131
- return ( fileOptions . bare ||
132
- ( ! packageName && compilerOptions . module === 'none' ) ) ;
133
- }
134
-
135
143
getFileModuleName ( inputFile , options ) {
136
144
return options . module !== 'none' ?
137
145
this . getExtendedPath ( inputFile , true ) : null ;
0 commit comments