@@ -15,160 +15,161 @@ const EXTERNS_FILE_NAME = "externs.js";
15
15
const DEFAULT_CONFIG_FILE = "tsconfig.json" ;
16
16
17
17
const optionsSchema = {
18
- type : "object" ,
19
- properties : {
20
- tsconfig : {
21
- anyOf : [
22
- {
23
- type : "string"
18
+ type : "object" ,
19
+ properties : {
20
+ tsconfig : {
21
+ anyOf : [
22
+ {
23
+ type : "string"
24
+ } ,
25
+ {
26
+ type : "boolean"
27
+ }
28
+ ]
24
29
} ,
25
- {
26
- type : "boolean "
30
+ externDir : {
31
+ type : "string "
27
32
}
28
- ]
29
- } ,
30
- externDir : {
31
- type : "string"
32
33
}
33
- }
34
34
} ;
35
35
36
36
interface RealOptions extends OptionObject {
37
- externDir : string ;
38
- tsconfig : string ;
39
- externFile : string ;
40
- compilerConfig : ReturnType < typeof ts . parseJsonConfigFileContent > ;
37
+ externDir : string ;
38
+ tsconfig : string ;
39
+ externFile : string ;
40
+ compilerConfig : ReturnType < typeof ts . parseJsonConfigFileContent > ;
41
41
}
42
42
43
43
const setup = ( loaderCTX : webpack . loader . LoaderContext ) : RealOptions => {
44
- const options = getOptions ( loaderCTX ) ;
45
- validateOptions ( optionsSchema , options , LOADER_NAME ) ;
46
-
47
- const externDir =
48
- options . externDir != null ? options . externDir : DEFAULT_EXTERN_DIR ;
49
- const externFile = path . resolve ( externDir , EXTERNS_FILE_NAME ) ;
50
-
51
- fs . ensureDirSync ( externDir ) ;
52
- const tsconfig =
53
- typeof options . tsconfig === "string"
54
- ? options . tsconfig
55
- : DEFAULT_CONFIG_FILE ;
56
-
57
- const compilerConfigFile = ts . readConfigFile (
58
- tsconfig ,
59
- ( configPath : string ) => {
60
- return fs . readFileSync ( configPath , "utf-8" ) ;
61
- }
62
- ) ;
63
-
64
- const compilerConfig = ts . parseJsonConfigFileContent (
65
- compilerConfigFile . config ,
66
- ts . sys ,
67
- "." ,
68
- { } ,
69
- tsconfig
70
- ) ;
71
-
72
- return {
73
- tsconfig,
74
- externDir,
75
- externFile,
76
- compilerConfig
77
- } ;
44
+ const options = getOptions ( loaderCTX ) ;
45
+ validateOptions ( optionsSchema , options , LOADER_NAME ) ;
46
+
47
+ const externDir =
48
+ options . externDir != null ? options . externDir : DEFAULT_EXTERN_DIR ;
49
+ const externFile = path . resolve ( externDir , EXTERNS_FILE_NAME ) ;
50
+
51
+ fs . ensureDirSync ( externDir ) ;
52
+ const tsconfig =
53
+ typeof options . tsconfig === "string"
54
+ ? options . tsconfig
55
+ : DEFAULT_CONFIG_FILE ;
56
+
57
+ const compilerConfigFile = ts . readConfigFile (
58
+ tsconfig ,
59
+ ( configPath : string ) => {
60
+ return fs . readFileSync ( configPath , "utf-8" ) ;
61
+ }
62
+ ) ;
63
+
64
+ const compilerConfig = ts . parseJsonConfigFileContent (
65
+ compilerConfigFile . config ,
66
+ ts . sys ,
67
+ "." ,
68
+ { } ,
69
+ tsconfig
70
+ ) ;
71
+
72
+ return {
73
+ tsconfig,
74
+ externDir,
75
+ externFile,
76
+ compilerConfig
77
+ } ;
78
78
} ;
79
79
80
80
type LoaderCTX = webpack . loader . LoaderContext ;
81
81
82
82
const handleDiagnostics = (
83
- ctx : LoaderCTX ,
84
- diagnostics : ReadonlyArray < ts . Diagnostic > ,
85
- diagnosticHost : ts . FormatDiagnosticsHost ,
86
- type : "error" | "warning"
83
+ ctx : LoaderCTX ,
84
+ diagnostics : ReadonlyArray < ts . Diagnostic > ,
85
+ diagnosticHost : ts . FormatDiagnosticsHost ,
86
+ type : "error" | "warning"
87
87
) : void => {
88
- const formatted = ts . formatDiagnosticsWithColorAndContext (
89
- diagnostics ,
90
- diagnosticHost
91
- ) ;
92
-
93
- if ( type === "error" ) {
94
- ctx . emitError ( Error ( formatted ) ) ;
95
- } else {
96
- ctx . emitWarning ( formatted ) ;
97
- }
88
+ const formatted = ts . formatDiagnosticsWithColorAndContext (
89
+ diagnostics ,
90
+ diagnosticHost
91
+ ) ;
92
+
93
+ if ( type === "error" ) {
94
+ ctx . emitError ( Error ( formatted ) ) ;
95
+ } else {
96
+ ctx . emitWarning ( formatted ) ;
97
+ }
98
98
} ;
99
99
100
100
const tsickleLoader : webpack . loader . Loader = function (
101
- this : LoaderCTX ,
102
- _source : string | Buffer
101
+ this : LoaderCTX ,
102
+ _source : string | Buffer
103
103
) {
104
- const {
105
- compilerConfig : { options } ,
106
- externFile
107
- } = setup ( this ) ;
108
-
109
- // normalize the path to unix-style
110
- const sourceFileName = this . resourcePath . replace ( / \\ / g, "/" ) ;
111
- const compilerHost = ts . createCompilerHost ( options ) ;
112
- const program = ts . createProgram ( [ sourceFileName ] , options , compilerHost ) ;
113
- const diagnostics = ts . getPreEmitDiagnostics ( program ) ;
114
-
115
- const diagnosticsHost : ts . FormatDiagnosticsHost = {
116
- getNewLine : ( ) => EOL ,
117
- getCanonicalFileName : fileName => fileName ,
118
- getCurrentDirectory : ( ) => path . dirname ( sourceFileName )
119
- } ;
120
-
121
- if ( diagnostics . length > 0 ) {
122
- handleDiagnostics ( this , diagnostics , diagnosticsHost , "error" ) ;
123
- return ;
124
- }
125
-
126
- const tsickleHost : tsickle . TsickleHost = {
127
- shouldSkipTsickleProcessing : filename => sourceFileName !== filename ,
128
- shouldIgnoreWarningsForPath : ( ) => false ,
129
- pathToModuleName : name => name ,
130
- fileNameToModuleId : name => name ,
131
- options : { } , // TODO: set possible options here
132
- es5Mode : true ,
133
- moduleResolutionHost : compilerHost ,
134
- googmodule : false ,
135
- transformDecorators : true ,
136
- transformTypesToClosure : true ,
137
- typeBlackListPaths : new Set ( ) ,
138
- untyped : false ,
139
- logWarning : warning =>
140
- handleDiagnostics ( this , [ warning ] , diagnosticsHost , "warning" )
141
- } ;
142
-
143
- const jsFiles = new Map < string , string > ( ) ;
144
-
145
- const output = tsickle . emitWithTsickle (
146
- program ,
147
- tsickleHost ,
148
- compilerHost ,
149
- options ,
150
- undefined ,
151
- ( path : string , contents : string ) => jsFiles . set ( path , contents )
152
- ) ;
153
-
154
- const sourceFileAsJs = tsToJS ( sourceFileName ) ;
155
- for ( const [ path , source ] of jsFiles ) {
156
- if ( sourceFileAsJs . indexOf ( path ) === - 1 ) {
157
- continue ;
104
+ const {
105
+ compilerConfig : { options } ,
106
+ externFile
107
+ } = setup ( this ) ;
108
+
109
+ // normalize the path to unix-style
110
+ const sourceFileName = this . resourcePath . replace ( / \\ / g, "/" ) ;
111
+ const compilerHost = ts . createCompilerHost ( options ) ;
112
+ const program = ts . createProgram ( [ sourceFileName ] , options , compilerHost ) ;
113
+ const diagnostics = ts . getPreEmitDiagnostics ( program ) ;
114
+
115
+ const diagnosticsHost : ts . FormatDiagnosticsHost = {
116
+ getNewLine : ( ) => EOL ,
117
+ getCanonicalFileName : fileName => fileName ,
118
+ getCurrentDirectory : ( ) => path . dirname ( sourceFileName )
119
+ } ;
120
+
121
+ if ( diagnostics . length > 0 ) {
122
+ handleDiagnostics ( this , diagnostics , diagnosticsHost , "error" ) ;
123
+ return ;
158
124
}
159
125
160
- const tsPathName = jsToTS ( path ) ;
161
- const extern = output . externs [ tsPathName ] ;
162
- if ( extern != null ) {
163
- fs . appendFileSync ( externFile , fixExtern ( extern ) ) ;
164
- }
126
+ const tsickleHost : tsickle . TsickleHost = {
127
+ shouldSkipTsickleProcessing : filename => sourceFileName !== filename ,
128
+ shouldIgnoreWarningsForPath : ( ) => false ,
129
+ pathToModuleName : name => name ,
130
+ fileNameToModuleId : name => name ,
131
+ options : { } , // TODO: set possible options here
132
+ es5Mode : true ,
133
+ moduleResolutionHost : compilerHost ,
134
+ googmodule : false ,
135
+ transformDecorators : true ,
136
+ transformTypesToClosure : true ,
137
+ typeBlackListPaths : new Set ( ) ,
138
+ untyped : false ,
139
+ logWarning : warning =>
140
+ handleDiagnostics ( this , [ warning ] , diagnosticsHost , "warning" )
141
+ } ;
142
+
143
+ const jsFiles = new Map < string , string > ( ) ;
144
+
145
+ const output = tsickle . emitWithTsickle (
146
+ program ,
147
+ tsickleHost ,
148
+ compilerHost ,
149
+ options ,
150
+ undefined ,
151
+ ( path : string , contents : string ) => jsFiles . set ( path , contents )
152
+ ) ;
153
+
154
+ const sourceFileAsJs = tsToJS ( sourceFileName ) ;
155
+ for ( const [ path , source ] of jsFiles ) {
156
+ if ( sourceFileAsJs . indexOf ( path ) === - 1 ) {
157
+ continue ;
158
+ }
159
+
160
+ const tsPathName = jsToTS ( path ) ;
161
+ const extern = output . externs [ tsPathName ] ;
162
+ if ( extern != null ) {
163
+ console . info ( `appending extern for ${ path } to::\n${ extern } \n` ) ;
164
+ fs . appendFileSync ( externFile , fixExtern ( extern ) ) ;
165
+ }
165
166
166
- return fixCode ( source ) ;
167
- }
167
+ return fixCode ( source ) ;
168
+ }
168
169
169
- this . emitError (
170
- Error ( `missing compiled result for source file: ${ sourceFileName } ` )
171
- ) ;
170
+ this . emitError (
171
+ Error ( `missing compiled result for source file: ${ sourceFileName } ` )
172
+ ) ;
172
173
} ;
173
174
174
175
export default tsickleLoader ;
0 commit comments