@@ -4621,19 +4621,6 @@ var __param = this.__param || function(index, decorator) { return function (targ
46214621 }
46224622 }
46234623
4624- function sortAMDModules ( amdModules : { name : string ; path : string } [ ] ) {
4625- // AMD modules with declared variable names go first
4626- return amdModules . sort ( ( moduleA , moduleB ) => {
4627- if ( moduleA . name === moduleB . name ) {
4628- return 0 ;
4629- } else if ( ! moduleA . name ) {
4630- return 1 ;
4631- } else {
4632- return - 1 ;
4633- }
4634- } ) ;
4635- }
4636-
46374624 function emitExportStarHelper ( ) {
46384625 if ( hasExportStars ) {
46394626 writeLine ( ) ;
@@ -4649,44 +4636,83 @@ var __param = this.__param || function(index, decorator) { return function (targ
46494636
46504637 function emitAMDModule ( node : SourceFile , startIndex : number ) {
46514638 collectExternalModuleInfo ( node ) ;
4652- writeLine ( ) ;
4653- write ( "define(" ) ;
4654- sortAMDModules ( node . amdDependencies ) ;
4655- if ( node . amdModuleName ) {
4656- write ( "\"" + node . amdModuleName + "\", " ) ;
4639+
4640+ // An AMD define function has the following shape:
4641+ // define(id?, dependencies?, factory);
4642+ //
4643+ // This has the shape of
4644+ // define(name, ["module1", "module2"], function (module1Alias) {
4645+ // The location of the alias in the parameter list in the factory function needs to
4646+ // match the position of the module name in the dependency list.
4647+ //
4648+ // To ensure this is true in cases of modules with no aliases, e.g.:
4649+ // `import "module"` or `<amd-dependency path= "a.css" />`
4650+ // we need to add modules without alias names to the end of the dependencies list
4651+
4652+ let aliasedModuleNames : string [ ] = [ ] ; // names of modules with corresponding parameter in the
4653+ // factory function.
4654+ let unaliasedModuleNames : string [ ] = [ ] ; // names of modules with no corresponding parameters in
4655+ // factory function.
4656+ let importAliasNames : string [ ] = [ ] ; // names of the parameters in the factory function; these
4657+ // paramters need to match the indexes of the corresponding
4658+ // module names in aliasedModuleNames.
4659+
4660+ // Fill in amd-dependency tags
4661+ for ( let amdDependency of node . amdDependencies ) {
4662+ if ( amdDependency . name ) {
4663+ aliasedModuleNames . push ( "\"" + amdDependency . path + "\"" ) ;
4664+ importAliasNames . push ( amdDependency . name ) ;
4665+ }
4666+ else {
4667+ unaliasedModuleNames . push ( "\"" + amdDependency . path + "\"" ) ;
4668+ }
46574669 }
4658- write ( "[\"require\", \"exports\"" ) ;
4670+
46594671 for ( let importNode of externalImports ) {
4660- write ( ", " ) ;
4672+ // Find the name of the external module
4673+ let externalModuleName = "" ;
46614674 let moduleName = getExternalModuleName ( importNode ) ;
46624675 if ( moduleName . kind === SyntaxKind . StringLiteral ) {
4663- emitLiteral ( < LiteralExpression > moduleName ) ;
4676+ externalModuleName = getLiteralText ( < LiteralExpression > moduleName ) ;
4677+ }
4678+
4679+ // Find the name of the module alais, if there is one
4680+ let importAliasName : string ;
4681+ let namespaceDeclaration = getNamespaceDeclarationNode ( importNode ) ;
4682+ if ( namespaceDeclaration && ! isDefaultImport ( importNode ) ) {
4683+ importAliasName = getSourceTextOfNodeFromSourceFile ( currentSourceFile , namespaceDeclaration . name ) ;
4684+ }
4685+ else {
4686+ importAliasName = getGeneratedNameForNode ( < ImportDeclaration | ExportDeclaration > importNode ) ;
4687+ }
4688+
4689+ if ( importAliasName ) {
4690+ aliasedModuleNames . push ( externalModuleName ) ;
4691+ importAliasNames . push ( importAliasName ) ;
46644692 }
46654693 else {
4666- write ( "\"\"" ) ;
4694+ unaliasedModuleNames . push ( externalModuleName ) ;
46674695 }
46684696 }
4669- for ( let amdDependency of node . amdDependencies ) {
4670- let text = "\"" + amdDependency . path + "\"" ;
4697+
4698+ writeLine ( ) ;
4699+ write ( "define(" ) ;
4700+ if ( node . amdModuleName ) {
4701+ write ( "\"" + node . amdModuleName + "\", " ) ;
4702+ }
4703+ write ( "[\"require\", \"exports\"" ) ;
4704+ if ( aliasedModuleNames . length ) {
46714705 write ( ", " ) ;
4672- write ( text ) ;
4706+ write ( aliasedModuleNames . join ( ", " ) ) ;
46734707 }
4674- write ( "], function (require, exports" ) ;
4675- for ( let importNode of externalImports ) {
4708+ if ( unaliasedModuleNames . length ) {
46764709 write ( ", " ) ;
4677- let namespaceDeclaration = getNamespaceDeclarationNode ( importNode ) ;
4678- if ( namespaceDeclaration && ! isDefaultImport ( importNode ) ) {
4679- emit ( namespaceDeclaration . name ) ;
4680- }
4681- else {
4682- write ( getGeneratedNameForNode ( < ImportDeclaration | ExportDeclaration > importNode ) ) ;
4683- }
4710+ write ( unaliasedModuleNames . join ( ", " ) ) ;
46844711 }
4685- for ( let amdDependency of node . amdDependencies ) {
4686- if ( amdDependency . name ) {
4687- write ( ", " ) ;
4688- write ( amdDependency . name ) ;
4689- }
4712+ write ( "], function (require, exports" ) ;
4713+ if ( importAliasNames . length ) {
4714+ write ( ", " ) ;
4715+ write ( importAliasNames . join ( ", " ) ) ;
46904716 }
46914717 write ( ") {" ) ;
46924718 increaseIndent ( ) ;
0 commit comments