@@ -4645,27 +4645,25 @@ var __param = this.__param || function(index, decorator) { return function (targ
46454645 }
46464646 }
46474647
4648- function emitAMDModule ( node : SourceFile , startIndex : number ) {
4649- collectExternalModuleInfo ( node ) ;
4650-
4648+ function emitAMDDependencies ( node : SourceFile , includeNonAmdDependencies : boolean ) {
46514649 // An AMD define function has the following shape:
46524650 // define(id?, dependencies?, factory);
46534651 //
46544652 // This has the shape of
46554653 // define(name, ["module1", "module2"], function (module1Alias) {
4656- // The location of the alias in the parameter list in the factory function needs to
4654+ // The location of the alias in the parameter list in the factory function needs to
46574655 // match the position of the module name in the dependency list.
46584656 //
4659- // To ensure this is true in cases of modules with no aliases, e.g.:
4660- // `import "module"` or `<amd-dependency path= "a.css" />`
4657+ // To ensure this is true in cases of modules with no aliases, e.g.:
4658+ // `import "module"` or `<amd-dependency path= "a.css" />`
46614659 // we need to add modules without alias names to the end of the dependencies list
4662-
4663- let aliasedModuleNames : string [ ] = [ ] ; // names of modules with corresponding parameter in the
4660+
4661+ let aliasedModuleNames : string [ ] = [ ] ; // names of modules with corresponding parameter in the
46644662 // factory function.
46654663 let unaliasedModuleNames : string [ ] = [ ] ; // names of modules with no corresponding parameters in
46664664 // factory function.
4667- let importAliasNames : string [ ] = [ ] ; // names of the parameters in the factory function; these
4668- // paramters need to match the indexes of the corresponding
4665+ let importAliasNames : string [ ] = [ ] ; // names of the parameters in the factory function; these
4666+ // parameters need to match the indexes of the corresponding
46694667 // module names in aliasedModuleNames.
46704668
46714669 // Fill in amd-dependency tags
@@ -4687,7 +4685,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
46874685 externalModuleName = getLiteralText ( < LiteralExpression > moduleName ) ;
46884686 }
46894687
4690- // Find the name of the module alais , if there is one
4688+ // Find the name of the module alias , if there is one
46914689 let importAliasName : string ;
46924690 let namespaceDeclaration = getNamespaceDeclarationNode ( importNode ) ;
46934691 if ( namespaceDeclaration && ! isDefaultImport ( importNode ) ) {
@@ -4697,20 +4695,15 @@ var __param = this.__param || function(index, decorator) { return function (targ
46974695 importAliasName = getGeneratedNameForNode ( < ImportDeclaration | ExportDeclaration > importNode ) ;
46984696 }
46994697
4700- if ( importAliasName ) {
4698+ if ( includeNonAmdDependencies && importAliasName ) {
47014699 aliasedModuleNames . push ( externalModuleName ) ;
47024700 importAliasNames . push ( importAliasName ) ;
47034701 }
47044702 else {
47054703 unaliasedModuleNames . push ( externalModuleName ) ;
47064704 }
47074705 }
4708-
4709- writeLine ( ) ;
4710- write ( "define(" ) ;
4711- if ( node . amdModuleName ) {
4712- write ( "\"" + node . amdModuleName + "\", " ) ;
4713- }
4706+
47144707 write ( "[\"require\", \"exports\"" ) ;
47154708 if ( aliasedModuleNames . length ) {
47164709 write ( ", " ) ;
@@ -4725,6 +4718,17 @@ var __param = this.__param || function(index, decorator) { return function (targ
47254718 write ( ", " ) ;
47264719 write ( importAliasNames . join ( ", " ) ) ;
47274720 }
4721+ }
4722+
4723+ function emitAMDModule ( node : SourceFile , startIndex : number ) {
4724+ collectExternalModuleInfo ( node ) ;
4725+
4726+ writeLine ( ) ;
4727+ write ( "define(" ) ;
4728+ if ( node . amdModuleName ) {
4729+ write ( "\"" + node . amdModuleName + "\", " ) ;
4730+ }
4731+ emitAMDDependencies ( node , /*includeNonAmdDependencies*/ true ) ;
47284732 write ( ") {" ) ;
47294733 increaseIndent ( ) ;
47304734 emitExportStarHelper ( ) ;
@@ -4746,6 +4750,31 @@ var __param = this.__param || function(index, decorator) { return function (targ
47464750 emitExportEquals ( /*emitAsReturn*/ false ) ;
47474751 }
47484752
4753+ function emitUMDModule ( node : SourceFile , startIndex : number ) {
4754+ collectExternalModuleInfo ( node ) ;
4755+
4756+ // Module is detected first to support Browserify users that load into a browser with an AMD loader
4757+ writeLines ( `(function (deps, factory) {
4758+ if (typeof module === 'object' && typeof module.exports === 'object') {
4759+ var v = factory(require, exports); if (v !== undefined) module.exports = v;
4760+ }
4761+ else if (typeof define === 'function' && define.amd) {
4762+ define(deps, factory);
4763+ }
4764+ })(` ) ;
4765+ emitAMDDependencies ( node , false ) ;
4766+ write ( ") {" ) ;
4767+ increaseIndent ( ) ;
4768+ emitExportStarHelper ( ) ;
4769+ emitCaptureThisForNodeIfNecessary ( node ) ;
4770+ emitLinesStartingAt ( node . statements , startIndex ) ;
4771+ emitTempDeclarations ( /*newLine*/ true ) ;
4772+ emitExportEquals ( /*emitAsReturn*/ true ) ;
4773+ decreaseIndent ( ) ;
4774+ writeLine ( ) ;
4775+ write ( "});" ) ;
4776+ }
4777+
47494778 function emitES6Module ( node : SourceFile , startIndex : number ) {
47504779 externalImports = undefined ;
47514780 exportSpecifiers = undefined ;
@@ -4830,6 +4859,9 @@ var __param = this.__param || function(index, decorator) { return function (targ
48304859 else if ( compilerOptions . module === ModuleKind . AMD ) {
48314860 emitAMDModule ( node , startIndex ) ;
48324861 }
4862+ else if ( compilerOptions . module === ModuleKind . UMD ) {
4863+ emitUMDModule ( node , startIndex ) ;
4864+ }
48334865 else {
48344866 emitCommonJSModule ( node , startIndex ) ;
48354867 }
0 commit comments