From 122753b50a4f87981dccda3a0dc65d0dfb3250fb Mon Sep 17 00:00:00 2001 From: Wesley Wigham Date: Thu, 1 Oct 2015 19:23:12 -0700 Subject: [PATCH] sourcemap correctness --- src/compiler/emitter.ts | 17 ++++++++++---- src/compiler/program.ts | 51 ++++++++++++++++++++++------------------- 2 files changed, 39 insertions(+), 29 deletions(-) diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index df2687768fc..3a4b9a4a3bf 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -473,12 +473,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi emitSourceFile(root); } else { - forEach(host.getSourceFiles(), emitEmitHelpers); + if (modulekind) { + forEach(host.getSourceFiles(), emitEmitHelpers); + } forEach(host.getSourceFiles(), sourceFile => { if (!isExternalModuleOrDeclarationFile(sourceFile)) { emitSourceFile(sourceFile); } - else if (isExternalModule(sourceFile)) { + else if (modulekind && isExternalModule(sourceFile)) { emitConcatenatedModule(sourceFile); } }); @@ -499,7 +501,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi exportFunctionForFile = undefined; let canonicalName = resolveToSemiabsolutePath(sourceFile.fileName); sourceFile.moduleName = sourceFile.moduleName || canonicalName; - bundleEmitDelegates[modulekind](sourceFile, 0, /*resolvePath*/true); + emit(sourceFile); } function resolveToSemiabsolutePath(path: string): string { @@ -7051,8 +7053,13 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi let startIndex = emitDirectivePrologues(node.statements, /*startWithNewLine*/ false); if (isExternalModule(node) || compilerOptions.isolatedModules) { - let emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[ModuleKind.CommonJS]; - emitModule(node, startIndex); + if (root) { + let emitModule = moduleEmitDelegates[modulekind] || moduleEmitDelegates[ModuleKind.CommonJS]; + emitModule(node, startIndex); + } + else { + bundleEmitDelegates[modulekind](node, startIndex, /*resolvePath*/true); + } } else { externalImports = undefined; diff --git a/src/compiler/program.ts b/src/compiler/program.ts index b595d48dc57..f54906d85ee 100644 --- a/src/compiler/program.ts +++ b/src/compiler/program.ts @@ -377,6 +377,29 @@ namespace ts { } } + // there has to be common source directory if user specified --outdir || --sourceRoot + // if user specified --mapRoot, there needs to be common source directory if there would be multiple files being emitted + if (options.outDir || // there is --outDir specified + options.sourceRoot || // there is --sourceRoot specified + options.mapRoot) { // there is --mapRoot specified + + if (options.rootDir && checkSourceFilesBelongToPath(files, options.rootDir)) { + // If a rootDir is specified and is valid use it as the commonSourceDirectory + commonSourceDirectory = getNormalizedAbsolutePath(options.rootDir, host.getCurrentDirectory()); + } + else { + // Compute the commonSourceDirectory from the input files + commonSourceDirectory = computeCommonSourceDirectory(files); + } + + if (commonSourceDirectory && commonSourceDirectory[commonSourceDirectory.length - 1] !== directorySeparator) { + // Make sure directory path ends with directory separator so this string can directly + // used to replace with "" to get the relative path of the source file and the relative path doesn't + // start with / making it rooted path + commonSourceDirectory += directorySeparator; + } + } + verifyCompilerOptions(); // unconditionally set oldProgram to undefined to prevent it from being captured in closure @@ -934,6 +957,10 @@ namespace ts { } }); + if (!commonPathComponents) { // Can happen when all input files are .d.ts files + return currentDirectory; + } + return getNormalizedPathFromPathComponents(commonPathComponents); } @@ -1036,30 +1063,6 @@ namespace ts { programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Cannot_compile_modules_into_es6_when_targeting_ES5_or_lower)); } - // there has to be common source directory if user specified --outdir || --sourceRoot - // if user specified --mapRoot, there needs to be common source directory if there would be multiple files being emitted - if (options.outDir || // there is --outDir specified - options.sourceRoot || // there is --sourceRoot specified - (options.mapRoot && // there is --mapRoot specified and there would be multiple js files generated - (!outFile || firstExternalModuleSourceFile !== undefined))) { - - if (options.rootDir && checkSourceFilesBelongToPath(files, options.rootDir)) { - // If a rootDir is specified and is valid use it as the commonSourceDirectory - commonSourceDirectory = getNormalizedAbsolutePath(options.rootDir, host.getCurrentDirectory()); - } - else { - // Compute the commonSourceDirectory from the input files - commonSourceDirectory = computeCommonSourceDirectory(files); - } - - if (commonSourceDirectory && commonSourceDirectory[commonSourceDirectory.length - 1] !== directorySeparator) { - // Make sure directory path ends with directory separator so this string can directly - // used to replace with "" to get the relative path of the source file and the relative path doesn't - // start with / making it rooted path - commonSourceDirectory += directorySeparator; - } - } - if (options.noEmit) { if (options.out) { programDiagnostics.add(createCompilerDiagnostic(Diagnostics.Option_0_cannot_be_specified_with_option_1, "noEmit", "out"));