@@ -6,8 +6,8 @@ import ts from "typescript";
6
6
import { EOL } from "os" ;
7
7
import webpack from "webpack" ;
8
8
import { fixCode , fixExtern } from "./fix-output" ;
9
- import { jsToTS , tsToJS } from "./path-utils" ;
10
9
import { JSONSchema7 } from "json-schema" ;
10
+ import { RawSourceMap } from "source-map" ;
11
11
12
12
const LOADER_NAME = "tsickle-loader" ;
13
13
const DEFAULT_EXTERN_DIR = "dist/externs" ;
@@ -64,7 +64,7 @@ const setup = (loaderCTX: LoaderCTX): RealOptions => {
64
64
const compilerConfig = ts . parseJsonConfigFileContent (
65
65
compilerConfigFile . config ,
66
66
ts . sys ,
67
- "." ,
67
+ path . dirname ( tsconfig || '' ) ,
68
68
{ } ,
69
69
tsconfig
70
70
) ;
@@ -142,38 +142,49 @@ const tsickleLoader = function (
142
142
rootDirsRelative : ( f : string ) => f ,
143
143
} ;
144
144
145
- const jsFiles = new Map < string , string > ( ) ;
145
+ let transpiledSources : string [ ] = [ ] ;
146
+ let transpiledSourceMaps : string [ ] = [ ] ;
146
147
147
- const output = tsickle . emitWithTsickle (
148
+ const output = tsickle . emit (
148
149
program ,
149
150
tsickleHost ,
150
- compilerHost ,
151
- options ,
152
- undefined ,
153
- ( path : string , contents : string ) => jsFiles . set ( path , contents )
151
+ ( jsFileName : string , contents : string , _writeByteOrderMark : boolean , _onError , tsSourceFiles ) => {
152
+ for ( const source of tsSourceFiles ?? [ ] ) {
153
+ if ( source . fileName === sourceFileName ) {
154
+ if ( jsFileName . endsWith ( '.map' ) ) {
155
+ transpiledSourceMaps . push ( contents ) ;
156
+ } else {
157
+ transpiledSources . push ( contents ) ;
158
+ }
159
+ }
160
+ }
161
+ } ,
162
+ program . getSourceFile ( sourceFileName )
154
163
) ;
155
164
156
- const sourceFileAsJs = tsToJS ( sourceFileName ) ;
157
- for ( const [ path , source ] of jsFiles ) {
158
- if ( sourceFileAsJs . indexOf ( path ) === - 1 ) {
159
- continue ;
160
- }
161
-
162
- const tsPathName = jsToTS ( path ) ;
163
- const extern = output . externs [ tsPathName ] ;
164
- if ( extern != null ) {
165
- // console.info(`appending extern for ${path} to (${externFile}) ::\n${extern}\n` );
166
- fs . appendFileSync ( externFile , fixExtern ( extern ) ) ;
167
- }
165
+ if ( transpiledSources . length !== 1 ) {
166
+ this . emitError (
167
+ Error ( `missing compiled result for source file: ${ sourceFileName } ` )
168
+ ) ;
169
+ return ;
170
+ }
171
+ if ( this . sourceMap && transpiledSourceMaps . length !== 1 ) {
172
+ this . emitError (
173
+ Error ( `tsconfig must specify sourceMap: "true" when sourcemaps are enabled!` )
174
+ ) ;
175
+ return ;
176
+ }
168
177
169
- const fixed = fixCode ( source ) ;
170
- // console.info("FIXED CODE:: \n", fixed);
171
- return fixed ;
178
+ const extern = output . externs [ sourceFileName ] ;
179
+ if ( extern != null ) {
180
+ fs . appendFileSync ( externFile , fixExtern ( extern ) ) ;
172
181
}
173
182
174
- this . emitError (
175
- Error ( `missing compiled result for source file: ${ sourceFileName } ` )
176
- ) ;
183
+ let sourceMap : RawSourceMap | undefined = undefined ;
184
+ if ( this . sourceMap ) {
185
+ sourceMap = JSON . parse ( transpiledSourceMaps [ 0 ] ) ;
186
+ }
187
+ this . callback ( null , fixCode ( transpiledSources [ 0 ] ) , sourceMap ) ;
177
188
} ;
178
189
179
190
export default tsickleLoader ;
0 commit comments