@@ -9,8 +9,13 @@ var path = require('path');
9
9
var util = require ( 'util' ) ;
10
10
11
11
module . exports = function ( ts ) {
12
- function Host ( currentDirectory , languageVersion ) {
13
- this . currentDirectory = currentDirectory ;
12
+ function replaceFileExtension ( file , extension ) {
13
+ return file . replace ( / \. \w + $ / i, extension ) ;
14
+ }
15
+
16
+ function Host ( currentDirectory , outputDirectory , languageVersion ) {
17
+ this . currentDirectory = this . getCanonicalFileName ( path . resolve ( currentDirectory ) ) ;
18
+ this . outputDirectory = this . getCanonicalFileName ( path . resolve ( outputDirectory ) ) ;
14
19
this . languageVersion = languageVersion ;
15
20
this . files = { } ;
16
21
this . previousFiles = { } ;
@@ -31,13 +36,21 @@ module.exports = function (ts) {
31
36
log ( 'Resetting (version %d)' , this . version ) ;
32
37
} ;
33
38
34
- Host . prototype . _normalizedRelative = function ( filename ) {
35
- return ts . normalizePath ( path . relative ( this . currentDirectory , path . resolve ( filename ) ) ) ;
36
- } ;
37
-
38
39
Host . prototype . _addFile = function ( filename , root ) {
39
- var normalized = this . _normalizedRelative ( filename ) ;
40
- log ( 'Parsing %s (norm: %s)' , filename , normalized ) ;
40
+
41
+ // Ensure that the relative, non-canonical file name is what's passed
42
+ // to 'createSourceFile', as that's the name that will be used in error
43
+ // messages, etc.
44
+
45
+ var relative = ts . normalizeSlashes ( path . relative (
46
+ this . currentDirectory ,
47
+ path . resolve (
48
+ this . currentDirectory ,
49
+ filename
50
+ )
51
+ ) ) ;
52
+ var canonical = this . _currentCanonical ( filename ) ;
53
+ log ( 'Parsing %s' , canonical ) ;
41
54
42
55
var text ;
43
56
try {
@@ -47,52 +60,44 @@ module.exports = function (ts) {
47
60
}
48
61
49
62
var file ;
50
- var current = this . files [ normalized ] ;
51
- var previous = this . previousFiles [ normalized ] ;
63
+ var current = this . files [ canonical ] ;
64
+ var previous = this . previousFiles [ canonical ] ;
52
65
var version ;
53
66
54
67
if ( current && current . contents === text ) {
55
68
file = current . ts ;
56
69
version = current . version ;
57
- log ( 'Reused current file %s (version %d)' , normalized , version ) ;
70
+ log ( 'Reused current file %s (version %d)' , canonical , version ) ;
58
71
} else if ( previous && previous . contents === text ) {
59
72
file = previous . ts ;
60
73
version = previous . version ;
61
- log ( 'Reused previous file %s (version %d)' , normalized , version ) ;
74
+ log ( 'Reused previous file %s (version %d)' , canonical , version ) ;
62
75
} else {
63
- file = ts . createSourceFile ( filename , text , this . languageVersion , true ) ;
76
+ file = ts . createSourceFile ( relative , text , this . languageVersion , true ) ;
64
77
version = this . version ;
65
- log ( 'New version of source file %s (version %d)' , normalized , version ) ;
78
+ log ( 'New version of source file %s (version %d)' , canonical , version ) ;
66
79
}
67
80
68
- this . files [ normalized ] = {
69
- filename : filename ,
81
+ this . files [ canonical ] = {
82
+ filename : relative ,
70
83
contents : text ,
71
84
ts : file ,
72
85
root : root ,
73
86
version : version
74
87
} ;
75
-
76
- this . _emitFile ( normalized ) ;
88
+ this . emit ( 'file' , canonical , relative ) ;
77
89
78
90
return file ;
79
91
} ;
80
92
81
- Host . prototype . _emitFile = function ( normalized ) {
82
- var idPath = './' + normalized ;
83
- var fullPath = path . resolve ( idPath ) ;
84
- this . emit ( 'file' , fullPath , idPath ) ;
85
- }
86
-
87
93
Host . prototype . getSourceFile = function ( filename ) {
88
- var normalized = this . _normalizedRelative ( filename ) ;
89
-
90
- if ( this . files [ normalized ] )
91
- return this . files [ normalized ] . ts ;
92
-
93
- if ( normalized === '__lib.d.ts' )
94
+ if ( filename === '__lib.d.ts' ) {
94
95
return this . libDefault ;
95
-
96
+ }
97
+ var canonical = this . _currentCanonical ( filename ) ;
98
+ if ( this . files [ canonical ] ) {
99
+ return this . files [ canonical ] . ts ;
100
+ }
96
101
return this . _addFile ( filename , false ) ;
97
102
} ;
98
103
@@ -103,9 +108,9 @@ module.exports = function (ts) {
103
108
} ;
104
109
105
110
Host . prototype . writeFile = function ( filename , data ) {
106
- var normalized = this . _normalizedRelative ( filename ) ;
107
- log ( 'Cache write %s (norm: %s) ' , filename , normalized ) ;
108
- this . output [ normalized ] = data ;
111
+ var canonical = this . _currentCanonical ( filename ) ;
112
+ log ( 'Cache write %s' , canonical ) ;
113
+ this . output [ canonical ] = data ;
109
114
} ;
110
115
111
116
Host . prototype . getCurrentDirectory = function ( ) {
@@ -130,21 +135,59 @@ module.exports = function (ts) {
130
135
} ;
131
136
132
137
Host . prototype . readFile = function ( filename ) {
133
- var normalized = this . _normalizedRelative ( filename ) ;
134
- return ts . sys . readFile ( normalized ) ;
138
+ return ts . sys . readFile ( filename ) ;
135
139
} ;
136
140
141
+ Host . prototype . _currentCanonical = function ( filename ) {
142
+ return this . getCanonicalFileName ( path . resolve (
143
+ this . currentDirectory ,
144
+ filename
145
+ ) ) ;
146
+ }
147
+
137
148
Host . prototype . _rootDir = function ( ) {
138
149
var dirs = [ ] ;
139
150
for ( var filename in this . files ) {
140
151
if ( ! Object . hasOwnProperty . call ( this . files , filename ) ) continue ;
141
152
if ( / \. d \. t s $ / . test ( filename ) ) continue ;
142
153
143
- dirs . push ( path . dirname ( filename ) ) ;
154
+ dirs . push ( this . getCanonicalFileName ( path . dirname ( filename ) ) ) ;
144
155
}
145
156
var result = commondir ( this . currentDirectory , dirs ) ;
146
- return result ;
157
+ return this . getCanonicalFileName ( result ) ;
147
158
} ;
148
159
160
+ Host . prototype . _rootFilenames = function ( ) {
161
+
162
+ var rootFilenames = [ ] ;
163
+
164
+ for ( var filename in this . files ) {
165
+ if ( ! Object . hasOwnProperty . call ( this . files , filename ) ) continue ;
166
+ if ( ! this . files [ filename ] . root ) continue ;
167
+ rootFilenames . push ( filename ) ;
168
+ }
169
+ return rootFilenames ;
170
+ }
171
+
172
+ Host . prototype . _output = function ( filename , extension ) {
173
+
174
+ var inputCanonical = this . _currentCanonical ( filename ) ;
175
+ var outputRelative = path . relative (
176
+ this . _rootDir ( ) ,
177
+ replaceFileExtension ( inputCanonical , extension )
178
+ ) ;
179
+ var outputCanonical = this . getCanonicalFileName ( path . resolve (
180
+ this . outputDirectory ,
181
+ outputRelative
182
+ ) ) ;
183
+ log ( 'Cache read %s' , outputCanonical ) ;
184
+
185
+ var output = this . output [ outputCanonical ] ;
186
+ if ( ! output ) {
187
+ log ( 'Cache miss on %s' , outputCanonical ) ;
188
+ }
189
+ return output ;
190
+ }
191
+
149
192
return Host ;
150
193
} ;
0 commit comments