@@ -4645,27 +4645,25 @@ var __param = this.__param || function(index, decorator) { return function (targ
4645
4645
}
4646
4646
}
4647
4647
4648
- function emitAMDModule ( node : SourceFile , startIndex : number ) {
4649
- collectExternalModuleInfo ( node ) ;
4650
-
4648
+ function emitAMDDependencies ( node : SourceFile , includeNonAmdDependencies : boolean ) {
4651
4649
// An AMD define function has the following shape:
4652
4650
// define(id?, dependencies?, factory);
4653
4651
//
4654
4652
// This has the shape of
4655
4653
// 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
4657
4655
// match the position of the module name in the dependency list.
4658
4656
//
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" />`
4661
4659
// 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
4664
4662
// factory function.
4665
4663
let unaliasedModuleNames : string [ ] = [ ] ; // names of modules with no corresponding parameters in
4666
4664
// 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
4669
4667
// module names in aliasedModuleNames.
4670
4668
4671
4669
// Fill in amd-dependency tags
@@ -4687,7 +4685,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
4687
4685
externalModuleName = getLiteralText ( < LiteralExpression > moduleName ) ;
4688
4686
}
4689
4687
4690
- // Find the name of the module alais , if there is one
4688
+ // Find the name of the module alias , if there is one
4691
4689
let importAliasName : string ;
4692
4690
let namespaceDeclaration = getNamespaceDeclarationNode ( importNode ) ;
4693
4691
if ( namespaceDeclaration && ! isDefaultImport ( importNode ) ) {
@@ -4697,20 +4695,15 @@ var __param = this.__param || function(index, decorator) { return function (targ
4697
4695
importAliasName = getGeneratedNameForNode ( < ImportDeclaration | ExportDeclaration > importNode ) ;
4698
4696
}
4699
4697
4700
- if ( importAliasName ) {
4698
+ if ( includeNonAmdDependencies && importAliasName ) {
4701
4699
aliasedModuleNames . push ( externalModuleName ) ;
4702
4700
importAliasNames . push ( importAliasName ) ;
4703
4701
}
4704
4702
else {
4705
4703
unaliasedModuleNames . push ( externalModuleName ) ;
4706
4704
}
4707
4705
}
4708
-
4709
- writeLine ( ) ;
4710
- write ( "define(" ) ;
4711
- if ( node . amdModuleName ) {
4712
- write ( "\"" + node . amdModuleName + "\", " ) ;
4713
- }
4706
+
4714
4707
write ( "[\"require\", \"exports\"" ) ;
4715
4708
if ( aliasedModuleNames . length ) {
4716
4709
write ( ", " ) ;
@@ -4725,6 +4718,17 @@ var __param = this.__param || function(index, decorator) { return function (targ
4725
4718
write ( ", " ) ;
4726
4719
write ( importAliasNames . join ( ", " ) ) ;
4727
4720
}
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 ) ;
4728
4732
write ( ") {" ) ;
4729
4733
increaseIndent ( ) ;
4730
4734
emitExportStarHelper ( ) ;
@@ -4746,6 +4750,31 @@ var __param = this.__param || function(index, decorator) { return function (targ
4746
4750
emitExportEquals ( /*emitAsReturn*/ false ) ;
4747
4751
}
4748
4752
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
+
4749
4778
function emitES6Module ( node : SourceFile , startIndex : number ) {
4750
4779
externalImports = undefined ;
4751
4780
exportSpecifiers = undefined ;
@@ -4830,6 +4859,9 @@ var __param = this.__param || function(index, decorator) { return function (targ
4830
4859
else if ( compilerOptions . module === ModuleKind . AMD ) {
4831
4860
emitAMDModule ( node , startIndex ) ;
4832
4861
}
4862
+ else if ( compilerOptions . module === ModuleKind . UMD ) {
4863
+ emitUMDModule ( node , startIndex ) ;
4864
+ }
4833
4865
else {
4834
4866
emitCommonJSModule ( node , startIndex ) ;
4835
4867
}
0 commit comments