@@ -908,15 +908,24 @@ export function annotate(program: ts.Program, file: ts.SourceFile, options: Opti
908
908
*/
909
909
class PostProcessor extends Rewriter {
910
910
/**
911
- * defaultImportSymbols collects the names of imported goog.modules. That is, if the code has
912
- * var foo = goog.require('bar');
913
- * which comes from the TS input:
911
+ * defaultImportSymbols collects the names of imported goog.modules.
912
+ * If the original TS input is:
914
913
* import foo from 'goog:bar';
915
- * Then defaultImportSymbols['foo'] is true.
916
- * This is used to rewrite "foo.default" into just "foo".
914
+ * then TS produces:
915
+ * var foo = require('goog:bar');
916
+ * and this class rewrites it to:
917
+ * var foo = require('goog.bar');
918
+ * After this step, defaultImportSymbols['foo'] is true.
919
+ * (This is used to rewrite 'foo.default' into just 'foo'.)
917
920
*/
918
921
defaultImportSymbols : { [ varName : string ] : boolean } = { } ;
919
922
923
+ /**
924
+ * moduleVariables maps from module names to the variables they're assigned to.
925
+ * Continuing the above example, moduleVariables['goog.bar'] = 'foo'.
926
+ */
927
+ moduleVariables : { [ moduleName : string ] : string } = { } ;
928
+
920
929
/** strippedStrict is true once we've stripped a "use strict"; from the input. */
921
930
strippedStrict : boolean = false ;
922
931
@@ -1053,7 +1062,12 @@ class PostProcessor extends Rewriter {
1053
1062
1054
1063
let modName = this . pathToModuleName ( this . file . fileName , require ) ;
1055
1064
this . writeRange ( node . getFullStart ( ) , node . getStart ( ) ) ;
1056
- this . emit ( `var ${ varName } = goog.require('${ modName } ');` ) ;
1065
+ if ( this . moduleVariables . hasOwnProperty ( modName ) ) {
1066
+ this . emit ( `var ${ varName } = ${ this . moduleVariables [ modName ] } ;` ) ;
1067
+ } else {
1068
+ this . emit ( `var ${ varName } = goog.require('${ modName } ');` ) ;
1069
+ this . moduleVariables [ modName ] = varName ;
1070
+ }
1057
1071
return true ;
1058
1072
}
1059
1073
@@ -1074,6 +1088,7 @@ class PostProcessor extends Rewriter {
1074
1088
if ( ! this . defaultImportSymbols . hasOwnProperty ( lhsIdent . text ) ) break ;
1075
1089
// Emit the same expression, with spaces to replace the ".default" part
1076
1090
// so that source maps still line up.
1091
+ this . writeRange ( node . getFullStart ( ) , node . getStart ( ) ) ;
1077
1092
this . emit ( `${ lhsIdent . text } ` ) ;
1078
1093
return true ;
1079
1094
default :
0 commit comments