Add inlineSourceMap option

This commit is contained in:
Mohamed Hegazy
2015-04-08 00:14:23 -07:00
parent 73e22ed9c2
commit a998abb153
16 changed files with 294 additions and 24 deletions

View File

@@ -30,6 +30,10 @@ module ts {
type: "boolean",
description: Diagnostics.Print_this_message,
},
{
name: "inlineSourceMap",
type: "boolean",
},
{
name: "listFiles",
type: "boolean",

View File

@@ -445,6 +445,10 @@ module ts {
Option_noEmitOnError_cannot_be_specified_with_option_separateCompilation: { code: 5045, category: DiagnosticCategory.Error, key: "Option 'noEmitOnError' cannot be specified with option 'separateCompilation'." },
Option_out_cannot_be_specified_with_option_separateCompilation: { code: 5046, category: DiagnosticCategory.Error, key: "Option 'out' cannot be specified with option 'separateCompilation'." },
Option_separateCompilation_can_only_be_used_when_either_option_module_is_provided_or_option_target_is_ES6_or_higher: { code: 5047, category: DiagnosticCategory.Error, key: "Option 'separateCompilation' can only be used when either option'--module' is provided or option 'target' is 'ES6' or higher." },
Option_sourceMap_cannot_be_specified_with_option_inlineSourceMap: { code: 5048, category: DiagnosticCategory.Error, key: "Option 'sourceMap' cannot be specified with option 'inlineSourceMap'." },
Option_sourceRoot_cannot_be_specified_with_option_inlineSourceMap: { code: 5049, category: DiagnosticCategory.Error, key: "Option 'sourceRoot' cannot be specified with option 'inlineSourceMap'." },
Option_mapRoot_cannot_be_specified_with_option_inlineSourceMap: { code: 5050, category: DiagnosticCategory.Error, key: "Option 'mapRoot' cannot be specified with option 'inlineSourceMap'." },
Option_out_cannot_be_specified_with_option_inlineSourceMap: { code: 5051, category: DiagnosticCategory.Error, key: "Option 'out' cannot be specified with option 'inlineSourceMap'." },
Concatenate_and_emit_output_to_single_file: { code: 6001, category: DiagnosticCategory.Message, key: "Concatenate and emit output to single file." },
Generates_corresponding_d_ts_file: { code: 6002, category: DiagnosticCategory.Message, key: "Generates corresponding '.d.ts' file." },
Specifies_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations: { code: 6003, category: DiagnosticCategory.Message, key: "Specifies the location where debugger should locate map files instead of generated locations." },

View File

@@ -661,7 +661,7 @@
},
"Invalid use of '{0}'. Class definitions are automatically in strict mode.": {
"category": "Error",
"code": 1210
"code": 1210
},
"A class declaration without the 'default' modifier must have a name": {
"category": "Error",
@@ -1772,6 +1772,23 @@
"category": "Error",
"code": 5047
},
"Option 'sourceMap' cannot be specified with option 'inlineSourceMap'.": {
"category": "Error",
"code": 5048
},
"Option 'sourceRoot' cannot be specified with option 'inlineSourceMap'.": {
"category": "Error",
"code": 5049
},
"Option 'mapRoot' cannot be specified with option 'inlineSourceMap'.": {
"category": "Error",
"code": 5050
},
"Option 'out' cannot be specified with option 'inlineSourceMap'.": {
"category": "Error",
"code": 5051
},
"Concatenate and emit output to single file.": {
"category": "Message",
"code": 6001

View File

@@ -52,7 +52,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
let compilerOptions = host.getCompilerOptions();
let languageVersion = compilerOptions.target || ScriptTarget.ES3;
let sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap ? [] : undefined;
let sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined;
let diagnostics: Diagnostic[] = [];
let newLine = host.getNewLine();
@@ -169,7 +169,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
/** Sourcemap data that will get encoded */
let sourceMapData: SourceMapData;
if (compilerOptions.sourceMap) {
if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) {
initializeEmitterWithSourceMaps();
}
@@ -564,19 +564,25 @@ var __param = this.__param || function(index, decorator) { return function (targ
recordSourceMapSpan(comment.end);
}
function serializeSourceMapContents(version: number, file: string, sourceRoot: string, sources: string[], names: string[], mappings: string) {
function serializeSourceMapContents(version: number, file: string, sourceRoot: string, sources: string[], names: string[], mappings: string, sourcesContent?: string[]) {
if (typeof JSON !== "undefined") {
return JSON.stringify({
version: version,
file: file,
sourceRoot: sourceRoot,
sources: sources,
names: names,
mappings: mappings
});
let map: any = {
version,
file,
sourceRoot,
sources,
names,
mappings
};
if (sourcesContent !== undefined) {
map.sourcesContent = sourcesContent;
}
return JSON.stringify(map);
}
return "{\"version\":" + version + ",\"file\":\"" + escapeString(file) + "\",\"sourceRoot\":\"" + escapeString(sourceRoot) + "\",\"sources\":[" + serializeStringArray(sources) + "],\"names\":[" + serializeStringArray(names) + "],\"mappings\":\"" + escapeString(mappings) + "\"}";
return "{\"version\":" + version + ",\"file\":\"" + escapeString(file) + "\",\"sourceRoot\":\"" + escapeString(sourceRoot) + "\",\"sources\":[" + serializeStringArray(sources) + "],\"names\":[" + serializeStringArray(names) + "],\"mappings\":\"" + escapeString(mappings) + "\" " + (sourcesContent !== undefined ? ",\"sourcesContent\":[" + serializeStringArray(sourcesContent) + "]" : "") + "}";
function serializeStringArray(list: string[]): string {
let output = "";
@@ -591,19 +597,34 @@ var __param = this.__param || function(index, decorator) { return function (targ
}
function writeJavaScriptAndSourceMapFile(emitOutput: string, writeByteOrderMark: boolean) {
// Write source map file
encodeLastRecordedSourceMapSpan();
writeFile(host, diagnostics, sourceMapData.sourceMapFilePath, serializeSourceMapContents(
let fileContents = compilerOptions.inlineSourceMap ? [currentSourceFile.text] : undefined;
let sourceMapText = serializeSourceMapContents(
3,
sourceMapData.sourceMapFile,
sourceMapData.sourceMapSourceRoot,
sourceMapData.sourceMapSources,
sourceMapData.sourceMapNames,
sourceMapData.sourceMapMappings), /*writeByteOrderMark*/ false);
sourceMapData.sourceMapMappings,
fileContents);
sourceMapDataList.push(sourceMapData);
let sourceMapUrl: string;
if (compilerOptions.inlineSourceMap) {
// Encode the sourceMap into the sourceMap url
let base64SourceMapText = convertToBase64(sourceMapText);
sourceMapUrl = `//# sourceMappingURL=data:application/json;base64,${base64SourceMapText}`;
}
else {
// Write source map file
writeFile(host, diagnostics, sourceMapData.sourceMapFilePath, sourceMapText, /*writeByteOrderMark*/ false);
sourceMapUrl = `//# sourceMappingURL=${sourceMapData.jsSourceMappingURL}`;
}
// Write sourcemap url to the js file and write the js file
writeJavaScriptFile(emitOutput + "//# sourceMappingURL=" + sourceMapData.jsSourceMappingURL, writeByteOrderMark);
writeJavaScriptFile(emitOutput + sourceMapUrl, writeByteOrderMark);
}
// Initialize source map data
@@ -861,7 +882,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
function emitLiteral(node: LiteralExpression) {
let text = getLiteralText(node);
if (compilerOptions.sourceMap && (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap) && (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
writer.writeLiteral(text);
}
// For versions below ES6, emit binary & octal literals in their canonical decimal form.

View File

@@ -472,6 +472,21 @@ module ts {
}
}
if (options.inlineSourceMap) {
if (options.sourceMap) {
diagnostics.add(createCompilerDiagnostic(Diagnostics.Option_sourceMap_cannot_be_specified_with_option_inlineSourceMap));
}
if (options.mapRoot) {
diagnostics.add(createCompilerDiagnostic(Diagnostics.Option_mapRoot_cannot_be_specified_with_option_inlineSourceMap));
}
if (options.sourceRoot) {
diagnostics.add(createCompilerDiagnostic(Diagnostics.Option_sourceRoot_cannot_be_specified_with_option_inlineSourceMap));
}
if (options.out) {
diagnostics.add(createCompilerDiagnostic(Diagnostics.Option_out_cannot_be_specified_with_option_inlineSourceMap));
}
}
if (!options.sourceMap && (options.mapRoot || options.sourceRoot)) {
// Error to specify --mapRoot or --sourceRoot without mapSourceFiles
if (options.mapRoot) {

View File

@@ -1587,6 +1587,7 @@ module ts {
diagnostics?: boolean;
emitBOM?: boolean;
help?: boolean;
inlineSourceMap?: boolean;
listFiles?: boolean;
locale?: string;
mapRoot?: string;

View File

@@ -159,7 +159,7 @@ class CompilerBaselineRunner extends RunnerBase {
// Source maps?
it('Correct sourcemap content for ' + fileName, () => {
if (options.sourceMap) {
if (options.sourceMap || options.inlineSourceMap) {
Harness.Baseline.runBaseline('Correct sourcemap content for ' + fileName, justName.replace(/\.ts$/, '.sourcemap.txt'), () => {
var record = result.getSourceMapRecord();
if (options.noEmitOnError && result.errors.length !== 0 && record === undefined) {
@@ -228,11 +228,14 @@ class CompilerBaselineRunner extends RunnerBase {
});
it('Correct Sourcemap output for ' + fileName, () => {
if (options.sourceMap) {
if (result.sourceMaps.length !== result.files.length) {
throw new Error('Number of sourcemap files should be same as js files.');
}
if (options.sourceMap && result.sourceMaps.length !== result.files.length) {
throw new Error('Number of sourcemap files should be same as js files.');
}
else if (options.inlineSourceMap && result.sourceMaps.length > 0) {
throw new Error('No sourcemap files should be generated if inlineSourceMaps was set.');
}
if (options.sourceMap) {
Harness.Baseline.runBaseline('Correct Sourcemap output for ' + fileName, justName.replace(/\.ts/, '.js.map'), () => {
if (options.noEmitOnError && result.errors.length !== 0 && result.sourceMaps.length === 0) {
// We need to return null here or the runBaseLine will actually create a empty file.

View File

@@ -1016,6 +1016,10 @@ module Harness {
options.sourceRoot = setting.value;
break;
case 'maproot':
options.mapRoot = setting.value;
break;
case 'sourcemap':
options.sourceMap = !!setting.value;
break;
@@ -1069,6 +1073,10 @@ module Harness {
includeBuiltFiles.push({ unitName: builtFileName, content: normalizeLineEndings(IO.readFile(builtFileName), newLine) });
break;
case 'inlinesourcemap':
options.inlineSourceMap = setting.value === 'true';
break;
default:
throw new Error('Unsupported compiler setting ' + setting.flag);
}
@@ -1465,7 +1473,7 @@ module Harness {
"noimplicitany", "noresolve", "newline", "newlines", "emitbom",
"errortruncation", "usecasesensitivefilenames", "preserveconstenums",
"includebuiltfile", "suppressimplicitanyindexerrors", "stripinternal",
"separatecompilation"];
"separatecompilation", "inlinesourcemap", "maproot", "sourceroot"];
function extractCompilerSettings(content: string): CompilerSetting[] {