Emit module names when --out is specified for system and amd modules

This commit is contained in:
Mohamed Hegazy
2016-04-12 11:45:53 -07:00
parent a27b4d07ae
commit 5cd5976650
4 changed files with 47 additions and 6 deletions

View File

@@ -68,6 +68,7 @@ namespace ts {
const context: TransformationContext = {
getCompilerOptions: () => host.getCompilerOptions(),
getEmitResolver: () => resolver,
getEmitHost: () => host,
getNodeEmitFlags,
setNodeEmitFlags,
hoistVariableDeclaration,

View File

@@ -21,6 +21,7 @@ namespace ts {
const compilerOptions = context.getCompilerOptions();
const resolver = context.getEmitResolver();
const host = context.getEmitHost();
const languageVersion = getEmitScriptTarget(compilerOptions);
const moduleKind = getEmitModuleKind(compilerOptions);
const previousExpressionSubstitution = context.expressionSubstitution;
@@ -91,7 +92,7 @@ namespace ts {
*/
function transformAMDModule(node: SourceFile) {
const define = createIdentifier("define");
const moduleName = node.moduleName ? createLiteral(node.moduleName) : undefined;
const moduleName = tryGetModuleNameFromFile(node, host, compilerOptions);
return transformAsynchronousModule(node, define, moduleName, /*includeNonAmdDependencies*/ true);
}
@@ -771,8 +772,9 @@ namespace ts {
function getExternalModuleNameLiteral(importNode: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration) {
const moduleName = getExternalModuleName(importNode);
if (moduleName.kind === SyntaxKind.StringLiteral) {
return tryRenameExternalModule(<StringLiteral>moduleName)
|| createLiteral(<StringLiteral>moduleName);
return tryGetModuleNameFromDeclaration(importNode, host, resolver, compilerOptions)
|| tryRenameExternalModule(<StringLiteral>moduleName)
|| getSynthesizedClone(<StringLiteral>moduleName);
}
return undefined;
@@ -802,6 +804,23 @@ namespace ts {
}
}
function tryGetModuleNameFromFile(file: SourceFile, host: EmitHost, options: CompilerOptions): StringLiteral {
if (!file) {
return undefined;
}
if (file.moduleName) {
return createLiteral(file.moduleName);
}
if (!isDeclarationFile(file) && (options.out || options.outFile)) {
return createLiteral(getExternalModuleNameFromPath(host, file.fileName));
}
return undefined;
}
function tryGetModuleNameFromDeclaration(declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration, host: EmitHost, resolver: EmitResolver, compilerOptions: CompilerOptions) {
return tryGetModuleNameFromFile(resolver.getExternalModuleFileFromDeclaration(declaration), host, compilerOptions);
}
function createRequireCall(importNode: ImportDeclaration | ImportEqualsDeclaration | ExportDeclaration) {
const moduleName = getExternalModuleNameLiteral(importNode);
const args: Expression[] = [];

View File

@@ -20,6 +20,7 @@ namespace ts {
const compilerOptions = context.getCompilerOptions();
const resolver = context.getEmitResolver();
const host = context.getEmitHost();
const languageVersion = getEmitScriptTarget(compilerOptions);
const previousExpressionSubstitution = context.expressionSubstitution;
context.enableExpressionSubstitution(SyntaxKind.Identifier);
@@ -106,6 +107,7 @@ namespace ts {
// Add the body of the module.
addSystemModuleBody(statements, node, dependencyGroups);
const moduleName = tryGetModuleNameFromFile(node, host, compilerOptions);
const dependencies = createArrayLiteral(map(dependencyGroups, getNameOfDependencyGroup));
const body = createFunctionExpression(
/*asteriskToken*/ undefined,
@@ -127,8 +129,8 @@ namespace ts {
createStatement(
createCall(
createPropertyAccess(createIdentifier("System"), "register"),
node.moduleName
? [createLiteral(node.moduleName), dependencies, body]
moduleName
? [moduleName, dependencies, body]
: [dependencies, body]
)
)
@@ -1155,7 +1157,8 @@ namespace ts {
function getExternalModuleNameLiteral(importNode: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration) {
const moduleName = getExternalModuleName(importNode);
if (moduleName.kind === SyntaxKind.StringLiteral) {
return tryRenameExternalModule(<StringLiteral>moduleName)
return tryGetModuleNameFromDeclaration(importNode, host, resolver, compilerOptions)
|| tryRenameExternalModule(<StringLiteral>moduleName)
|| getSynthesizedClone(<StringLiteral>moduleName);
}
@@ -1187,6 +1190,23 @@ namespace ts {
}
}
function tryGetModuleNameFromFile(file: SourceFile, host: EmitHost, options: CompilerOptions): StringLiteral {
if (!file) {
return undefined;
}
if (file.moduleName) {
return createLiteral(file.moduleName);
}
if (!isDeclarationFile(file) && (options.out || options.outFile)) {
return createLiteral(getExternalModuleNameFromPath(host, file.fileName));
}
return undefined;
}
function tryGetModuleNameFromDeclaration(declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration, host: EmitHost, resolver: EmitResolver, compilerOptions: CompilerOptions) {
return tryGetModuleNameFromFile(resolver.getExternalModuleFileFromDeclaration(declaration), host, compilerOptions);
}
/**
* Gets a name to use for a DeclarationStatement.
* @param node The declaration statement.

View File

@@ -2892,6 +2892,7 @@ namespace ts {
export interface TransformationContext extends LexicalEnvironment {
getCompilerOptions(): CompilerOptions;
getEmitResolver(): EmitResolver;
getEmitHost(): EmitHost;
getNodeEmitFlags(node: Node): NodeEmitFlags;
setNodeEmitFlags<T extends Node>(node: T, flags: NodeEmitFlags): T;
hoistFunctionDeclaration(node: FunctionDeclaration): void;