@@ -15,161 +15,162 @@ 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"
24
- } ,
25
- {
26
- type : "boolean"
27
- }
28
- ]
18
+ type : "object" ,
19
+ properties : {
20
+ tsconfig : {
21
+ anyOf : [
22
+ {
23
+ type : "string"
29
24
} ,
30
- externDir : {
31
- type : "string "
25
+ {
26
+ type : "boolean "
32
27
}
28
+ ]
29
+ } ,
30
+ externDir : {
31
+ type : "string"
33
32
}
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 ;
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 : string ) =>
128
+ sourceFileName !== filename ,
129
+ shouldIgnoreWarningsForPath : ( ) => false ,
130
+ pathToModuleName : ( name : string ) => name ,
131
+ fileNameToModuleId : ( name : string ) => name ,
132
+ options : { } , // TODO: set possible options here
133
+ es5Mode : true ,
134
+ moduleResolutionHost : compilerHost ,
135
+ googmodule : false ,
136
+ transformDecorators : true ,
137
+ transformTypesToClosure : true ,
138
+ typeBlackListPaths : new Set ( ) ,
139
+ untyped : false ,
140
+ logWarning : ( warning : unknown ) =>
141
+ handleDiagnostics ( this , [ warning ] , diagnosticsHost , "warning" )
142
+ } ;
143
+
144
+ const jsFiles = new Map < string , string > ( ) ;
145
+
146
+ const output = tsickle . emitWithTsickle (
147
+ program ,
148
+ tsickleHost ,
149
+ compilerHost ,
150
+ options ,
151
+ undefined ,
152
+ ( path : string , contents : string ) => jsFiles . set ( path , contents )
153
+ ) ;
154
+
155
+ const sourceFileAsJs = tsToJS ( sourceFileName ) ;
156
+ for ( const [ path , source ] of jsFiles ) {
157
+ if ( sourceFileAsJs . indexOf ( path ) === - 1 ) {
158
+ continue ;
124
159
}
125
160
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
- }
166
-
167
- return fixCode ( source ) ;
161
+ const tsPathName = jsToTS ( path ) ;
162
+ const extern = output . externs [ tsPathName ] ;
163
+ if ( extern != null ) {
164
+ console . info ( `appending extern for ${ path } to::\n${ extern } \n` ) ;
165
+ fs . appendFileSync ( externFile , fixExtern ( extern ) ) ;
168
166
}
169
167
170
- this . emitError (
171
- Error ( `missing compiled result for source file: ${ sourceFileName } ` )
172
- ) ;
168
+ return fixCode ( source ) ;
169
+ }
170
+
171
+ this . emitError (
172
+ Error ( `missing compiled result for source file: ${ sourceFileName } ` )
173
+ ) ;
173
174
} ;
174
175
175
176
export default tsickleLoader ;
0 commit comments