@@ -6,8 +6,8 @@ import ts from "typescript";
66import { EOL } from "os" ;
77import webpack from "webpack" ;
88import { fixCode , fixExtern } from "./fix-output" ;
9- import { jsToTS , tsToJS } from "./path-utils" ;
109import { JSONSchema7 } from "json-schema" ;
10+ import { RawSourceMap } from "source-map" ;
1111
1212const LOADER_NAME = "tsickle-loader" ;
1313const DEFAULT_EXTERN_DIR = "dist/externs" ;
@@ -64,7 +64,7 @@ const setup = (loaderCTX: LoaderCTX): RealOptions => {
6464 const compilerConfig = ts . parseJsonConfigFileContent (
6565 compilerConfigFile . config ,
6666 ts . sys ,
67- "." ,
67+ path . dirname ( tsconfig || '' ) ,
6868 { } ,
6969 tsconfig
7070 ) ;
@@ -142,38 +142,49 @@ const tsickleLoader = function (
142142 rootDirsRelative : ( f : string ) => f ,
143143 } ;
144144
145- const jsFiles = new Map < string , string > ( ) ;
145+ let transpiledSources : string [ ] = [ ] ;
146+ let transpiledSourceMaps : string [ ] = [ ] ;
146147
147- const output = tsickle . emitWithTsickle (
148+ const output = tsickle . emit (
148149 program ,
149150 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 )
154163 ) ;
155164
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+ }
168177
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 ) ) ;
172181 }
173182
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 ) ;
177188} ;
178189
179190export default tsickleLoader ;
0 commit comments