allow transpiler to provide alternative names for dependencies

This commit is contained in:
Vladimir Matveev
2015-08-14 13:00:41 -07:00
parent 5fbe3fc75c
commit 7e8cfa0859
4 changed files with 109 additions and 12 deletions

View File

@@ -5343,13 +5343,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
emitExportMemberAssignments(<Identifier>node.name);
}
}
function tryRenameExternalModule(moduleName: LiteralExpression): string {
if (currentSourceFile.renamedDependencies && hasProperty(currentSourceFile.renamedDependencies, moduleName.text)) {
return `"${currentSourceFile.renamedDependencies[moduleName.text]}"`
}
return undefined;
}
function emitRequire(moduleName: Expression) {
if (moduleName.kind === SyntaxKind.StringLiteral) {
write("require(");
emitStart(moduleName);
emitLiteral(<LiteralExpression>moduleName);
emitEnd(moduleName);
let text = tryRenameExternalModule(<LiteralExpression>moduleName);
if (text) {
write(text);
}
else {
emitStart(moduleName);
emitLiteral(<LiteralExpression>moduleName);
emitEnd(moduleName);
}
emitToken(SyntaxKind.CloseParenToken, moduleName.end);
}
else {
@@ -5752,7 +5765,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
function getExternalModuleNameText(importNode: ImportDeclaration | ExportDeclaration | ImportEqualsDeclaration): string {
let moduleName = getExternalModuleName(importNode);
if (moduleName.kind === SyntaxKind.StringLiteral) {
return getLiteralText(<LiteralExpression>moduleName);
return tryRenameExternalModule(<LiteralExpression>moduleName) || getLiteralText(<LiteralExpression>moduleName);
}
return undefined;
@@ -6320,7 +6333,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
if (i !== 0) {
write(", ");
}
}
write(text);
}
write(`], function(${exportFunctionForFile}) {`);
@@ -6333,7 +6347,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
writeLine();
write("});");
}
function emitAMDDependencies(node: SourceFile, includeNonAmdDependencies: boolean) {
// An AMD define function has the following shape:
// define(id?, dependencies?, factory);

View File

@@ -1243,6 +1243,10 @@ namespace ts {
moduleName: string;
referencedFiles: FileReference[];
languageVariant: LanguageVariant;
// this map is used by transpiler to supply alternative names for dependencies (i.e. in case of bundling)
/* @internal */
renamedDependencies?: Map<string>;
/**
* lib.d.ts should have a reference comment like

View File

@@ -1767,6 +1767,7 @@ namespace ts {
fileName?: string;
reportDiagnostics?: boolean;
moduleName?: string;
renamedDependencies?: Map<string>;
}
export interface TranspileOutput {
@@ -1784,7 +1785,7 @@ namespace ts {
* - noLib = true
* - noResolve = true
*/
export function transpileModule(input: string, transpileOptions?: TranspileOptions): TranspileOutput {
export function transpileModule(input: string, transpileOptions: TranspileOptions): TranspileOutput {
let options = transpileOptions.compilerOptions ? clone(transpileOptions.compilerOptions) : getDefaultCompilerOptions();
options.isolatedModules = true;
@@ -1807,6 +1808,8 @@ namespace ts {
sourceFile.moduleName = transpileOptions.moduleName;
}
sourceFile.renamedDependencies = transpileOptions.renamedDependencies;
let newLine = getNewLineCharacter(options);
// Output