Skip to content

Commit a998abb

Browse files
committed
Add inlineSourceMap option
1 parent 73e22ed commit a998abb

16 files changed

+294
-24
lines changed

src/compiler/commandLineParser.ts

+4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ module ts {
3030
type: "boolean",
3131
description: Diagnostics.Print_this_message,
3232
},
33+
{
34+
name: "inlineSourceMap",
35+
type: "boolean",
36+
},
3337
{
3438
name: "listFiles",
3539
type: "boolean",

src/compiler/diagnosticInformationMap.generated.ts

+4
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,10 @@ module ts {
445445
Option_noEmitOnError_cannot_be_specified_with_option_separateCompilation: { code: 5045, category: DiagnosticCategory.Error, key: "Option 'noEmitOnError' cannot be specified with option 'separateCompilation'." },
446446
Option_out_cannot_be_specified_with_option_separateCompilation: { code: 5046, category: DiagnosticCategory.Error, key: "Option 'out' cannot be specified with option 'separateCompilation'." },
447447
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." },
448+
Option_sourceMap_cannot_be_specified_with_option_inlineSourceMap: { code: 5048, category: DiagnosticCategory.Error, key: "Option 'sourceMap' cannot be specified with option 'inlineSourceMap'." },
449+
Option_sourceRoot_cannot_be_specified_with_option_inlineSourceMap: { code: 5049, category: DiagnosticCategory.Error, key: "Option 'sourceRoot' cannot be specified with option 'inlineSourceMap'." },
450+
Option_mapRoot_cannot_be_specified_with_option_inlineSourceMap: { code: 5050, category: DiagnosticCategory.Error, key: "Option 'mapRoot' cannot be specified with option 'inlineSourceMap'." },
451+
Option_out_cannot_be_specified_with_option_inlineSourceMap: { code: 5051, category: DiagnosticCategory.Error, key: "Option 'out' cannot be specified with option 'inlineSourceMap'." },
448452
Concatenate_and_emit_output_to_single_file: { code: 6001, category: DiagnosticCategory.Message, key: "Concatenate and emit output to single file." },
449453
Generates_corresponding_d_ts_file: { code: 6002, category: DiagnosticCategory.Message, key: "Generates corresponding '.d.ts' file." },
450454
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." },

src/compiler/diagnosticMessages.json

+18-1
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@
661661
},
662662
"Invalid use of '{0}'. Class definitions are automatically in strict mode.": {
663663
"category": "Error",
664-
"code": 1210
664+
"code": 1210
665665
},
666666
"A class declaration without the 'default' modifier must have a name": {
667667
"category": "Error",
@@ -1772,6 +1772,23 @@
17721772
"category": "Error",
17731773
"code": 5047
17741774
},
1775+
"Option 'sourceMap' cannot be specified with option 'inlineSourceMap'.": {
1776+
"category": "Error",
1777+
"code": 5048
1778+
},
1779+
"Option 'sourceRoot' cannot be specified with option 'inlineSourceMap'.": {
1780+
"category": "Error",
1781+
"code": 5049
1782+
},
1783+
"Option 'mapRoot' cannot be specified with option 'inlineSourceMap'.": {
1784+
"category": "Error",
1785+
"code": 5050
1786+
},
1787+
"Option 'out' cannot be specified with option 'inlineSourceMap'.": {
1788+
"category": "Error",
1789+
"code": 5051
1790+
},
1791+
17751792
"Concatenate and emit output to single file.": {
17761793
"category": "Message",
17771794
"code": 6001

src/compiler/emitter.ts

+38-17
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
5252

5353
let compilerOptions = host.getCompilerOptions();
5454
let languageVersion = compilerOptions.target || ScriptTarget.ES3;
55-
let sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap ? [] : undefined;
55+
let sourceMapDataList: SourceMapData[] = compilerOptions.sourceMap || compilerOptions.inlineSourceMap ? [] : undefined;
5656
let diagnostics: Diagnostic[] = [];
5757
let newLine = host.getNewLine();
5858

@@ -169,7 +169,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
169169
/** Sourcemap data that will get encoded */
170170
let sourceMapData: SourceMapData;
171171

172-
if (compilerOptions.sourceMap) {
172+
if (compilerOptions.sourceMap || compilerOptions.inlineSourceMap) {
173173
initializeEmitterWithSourceMaps();
174174
}
175175

@@ -564,19 +564,25 @@ var __param = this.__param || function(index, decorator) { return function (targ
564564
recordSourceMapSpan(comment.end);
565565
}
566566

567-
function serializeSourceMapContents(version: number, file: string, sourceRoot: string, sources: string[], names: string[], mappings: string) {
567+
function serializeSourceMapContents(version: number, file: string, sourceRoot: string, sources: string[], names: string[], mappings: string, sourcesContent?: string[]) {
568568
if (typeof JSON !== "undefined") {
569-
return JSON.stringify({
570-
version: version,
571-
file: file,
572-
sourceRoot: sourceRoot,
573-
sources: sources,
574-
names: names,
575-
mappings: mappings
576-
});
569+
let map: any = {
570+
version,
571+
file,
572+
sourceRoot,
573+
sources,
574+
names,
575+
mappings
576+
};
577+
578+
if (sourcesContent !== undefined) {
579+
map.sourcesContent = sourcesContent;
580+
}
581+
582+
return JSON.stringify(map);
577583
}
578584

579-
return "{\"version\":" + version + ",\"file\":\"" + escapeString(file) + "\",\"sourceRoot\":\"" + escapeString(sourceRoot) + "\",\"sources\":[" + serializeStringArray(sources) + "],\"names\":[" + serializeStringArray(names) + "],\"mappings\":\"" + escapeString(mappings) + "\"}";
585+
return "{\"version\":" + version + ",\"file\":\"" + escapeString(file) + "\",\"sourceRoot\":\"" + escapeString(sourceRoot) + "\",\"sources\":[" + serializeStringArray(sources) + "],\"names\":[" + serializeStringArray(names) + "],\"mappings\":\"" + escapeString(mappings) + "\" " + (sourcesContent !== undefined ? ",\"sourcesContent\":[" + serializeStringArray(sourcesContent) + "]" : "") + "}";
580586

581587
function serializeStringArray(list: string[]): string {
582588
let output = "";
@@ -591,19 +597,34 @@ var __param = this.__param || function(index, decorator) { return function (targ
591597
}
592598

593599
function writeJavaScriptAndSourceMapFile(emitOutput: string, writeByteOrderMark: boolean) {
594-
// Write source map file
595600
encodeLastRecordedSourceMapSpan();
596-
writeFile(host, diagnostics, sourceMapData.sourceMapFilePath, serializeSourceMapContents(
601+
602+
let fileContents = compilerOptions.inlineSourceMap ? [currentSourceFile.text] : undefined;
603+
let sourceMapText = serializeSourceMapContents(
597604
3,
598605
sourceMapData.sourceMapFile,
599606
sourceMapData.sourceMapSourceRoot,
600607
sourceMapData.sourceMapSources,
601608
sourceMapData.sourceMapNames,
602-
sourceMapData.sourceMapMappings), /*writeByteOrderMark*/ false);
609+
sourceMapData.sourceMapMappings,
610+
fileContents);
611+
603612
sourceMapDataList.push(sourceMapData);
604613

614+
let sourceMapUrl: string;
615+
if (compilerOptions.inlineSourceMap) {
616+
// Encode the sourceMap into the sourceMap url
617+
let base64SourceMapText = convertToBase64(sourceMapText);
618+
sourceMapUrl = `//# sourceMappingURL=data:application/json;base64,${base64SourceMapText}`;
619+
}
620+
else {
621+
// Write source map file
622+
writeFile(host, diagnostics, sourceMapData.sourceMapFilePath, sourceMapText, /*writeByteOrderMark*/ false);
623+
sourceMapUrl = `//# sourceMappingURL=${sourceMapData.jsSourceMappingURL}`;
624+
}
625+
605626
// Write sourcemap url to the js file and write the js file
606-
writeJavaScriptFile(emitOutput + "//# sourceMappingURL=" + sourceMapData.jsSourceMappingURL, writeByteOrderMark);
627+
writeJavaScriptFile(emitOutput + sourceMapUrl, writeByteOrderMark);
607628
}
608629

609630
// Initialize source map data
@@ -861,7 +882,7 @@ var __param = this.__param || function(index, decorator) { return function (targ
861882
function emitLiteral(node: LiteralExpression) {
862883
let text = getLiteralText(node);
863884

864-
if (compilerOptions.sourceMap && (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
885+
if ((compilerOptions.sourceMap || compilerOptions.inlineSourceMap) && (node.kind === SyntaxKind.StringLiteral || isTemplateLiteralKind(node.kind))) {
865886
writer.writeLiteral(text);
866887
}
867888
// For versions below ES6, emit binary & octal literals in their canonical decimal form.

src/compiler/program.ts

+15
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,21 @@ module ts {
472472
}
473473
}
474474

475+
if (options.inlineSourceMap) {
476+
if (options.sourceMap) {
477+
diagnostics.add(createCompilerDiagnostic(Diagnostics.Option_sourceMap_cannot_be_specified_with_option_inlineSourceMap));
478+
}
479+
if (options.mapRoot) {
480+
diagnostics.add(createCompilerDiagnostic(Diagnostics.Option_mapRoot_cannot_be_specified_with_option_inlineSourceMap));
481+
}
482+
if (options.sourceRoot) {
483+
diagnostics.add(createCompilerDiagnostic(Diagnostics.Option_sourceRoot_cannot_be_specified_with_option_inlineSourceMap));
484+
}
485+
if (options.out) {
486+
diagnostics.add(createCompilerDiagnostic(Diagnostics.Option_out_cannot_be_specified_with_option_inlineSourceMap));
487+
}
488+
}
489+
475490
if (!options.sourceMap && (options.mapRoot || options.sourceRoot)) {
476491
// Error to specify --mapRoot or --sourceRoot without mapSourceFiles
477492
if (options.mapRoot) {

src/compiler/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,7 @@ module ts {
15871587
diagnostics?: boolean;
15881588
emitBOM?: boolean;
15891589
help?: boolean;
1590+
inlineSourceMap?: boolean;
15901591
listFiles?: boolean;
15911592
locale?: string;
15921593
mapRoot?: string;

src/harness/compilerRunner.ts

+8-5
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ class CompilerBaselineRunner extends RunnerBase {
159159

160160
// Source maps?
161161
it('Correct sourcemap content for ' + fileName, () => {
162-
if (options.sourceMap) {
162+
if (options.sourceMap || options.inlineSourceMap) {
163163
Harness.Baseline.runBaseline('Correct sourcemap content for ' + fileName, justName.replace(/\.ts$/, '.sourcemap.txt'), () => {
164164
var record = result.getSourceMapRecord();
165165
if (options.noEmitOnError && result.errors.length !== 0 && record === undefined) {
@@ -228,11 +228,14 @@ class CompilerBaselineRunner extends RunnerBase {
228228
});
229229

230230
it('Correct Sourcemap output for ' + fileName, () => {
231-
if (options.sourceMap) {
232-
if (result.sourceMaps.length !== result.files.length) {
233-
throw new Error('Number of sourcemap files should be same as js files.');
234-
}
231+
if (options.sourceMap && result.sourceMaps.length !== result.files.length) {
232+
throw new Error('Number of sourcemap files should be same as js files.');
233+
}
234+
else if (options.inlineSourceMap && result.sourceMaps.length > 0) {
235+
throw new Error('No sourcemap files should be generated if inlineSourceMaps was set.');
236+
}
235237

238+
if (options.sourceMap) {
236239
Harness.Baseline.runBaseline('Correct Sourcemap output for ' + fileName, justName.replace(/\.ts/, '.js.map'), () => {
237240
if (options.noEmitOnError && result.errors.length !== 0 && result.sourceMaps.length === 0) {
238241
// We need to return null here or the runBaseLine will actually create a empty file.

src/harness/harness.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,10 @@ module Harness {
10161016
options.sourceRoot = setting.value;
10171017
break;
10181018

1019+
case 'maproot':
1020+
options.mapRoot = setting.value;
1021+
break;
1022+
10191023
case 'sourcemap':
10201024
options.sourceMap = !!setting.value;
10211025
break;
@@ -1069,6 +1073,10 @@ module Harness {
10691073
includeBuiltFiles.push({ unitName: builtFileName, content: normalizeLineEndings(IO.readFile(builtFileName), newLine) });
10701074
break;
10711075

1076+
case 'inlinesourcemap':
1077+
options.inlineSourceMap = setting.value === 'true';
1078+
break;
1079+
10721080
default:
10731081
throw new Error('Unsupported compiler setting ' + setting.flag);
10741082
}
@@ -1465,7 +1473,7 @@ module Harness {
14651473
"noimplicitany", "noresolve", "newline", "newlines", "emitbom",
14661474
"errortruncation", "usecasesensitivefilenames", "preserveconstenums",
14671475
"includebuiltfile", "suppressimplicitanyindexerrors", "stripinternal",
1468-
"separatecompilation"];
1476+
"separatecompilation", "inlinesourcemap", "maproot", "sourceroot"];
14691477

14701478
function extractCompilerSettings(content: string): CompilerSetting[] {
14711479

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
tests/cases/compiler/inlineSourceMap.ts(3,1): error TS2304: Cannot find name 'console'.
2+
3+
4+
==== tests/cases/compiler/inlineSourceMap.ts (1 errors) ====
5+
6+
var x = 0;
7+
console.log(x);
8+
~~~~~~~
9+
!!! error TS2304: Cannot find name 'console'.

tests/baselines/reference/inlineSourceMap.js

+9
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
===================================================================
2+
JsFile: inlineSourceMap.js
3+
mapUrl: inlineSourceMap.js.map
4+
sourceRoot:
5+
sources: inlineSourceMap.ts
6+
===================================================================
7+
-------------------------------------------------------------------
8+
emittedFile:tests/cases/compiler/inlineSourceMap.js
9+
sourceFile:inlineSourceMap.ts
10+
-------------------------------------------------------------------
11+
>>>var x = 0;
12+
1 >
13+
2 >^^^^
14+
3 > ^
15+
4 > ^^^
16+
5 > ^
17+
6 > ^
18+
7 > ^^^^^^->
19+
1 >
20+
>
21+
2 >var
22+
3 > x
23+
4 > =
24+
5 > 0
25+
6 > ;
26+
1 >Emitted(1, 1) Source(2, 1) + SourceIndex(0)
27+
2 >Emitted(1, 5) Source(2, 5) + SourceIndex(0)
28+
3 >Emitted(1, 6) Source(2, 6) + SourceIndex(0)
29+
4 >Emitted(1, 9) Source(2, 9) + SourceIndex(0)
30+
5 >Emitted(1, 10) Source(2, 10) + SourceIndex(0)
31+
6 >Emitted(1, 11) Source(2, 11) + SourceIndex(0)
32+
---
33+
>>>console.log(x);
34+
1->
35+
2 >^^^^^^^
36+
3 > ^
37+
4 > ^^^
38+
5 > ^
39+
6 > ^
40+
7 > ^
41+
8 > ^
42+
9 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
43+
1->
44+
>
45+
2 >console
46+
3 > .
47+
4 > log
48+
5 > (
49+
6 > x
50+
7 > )
51+
8 > ;
52+
1->Emitted(2, 1) Source(3, 1) + SourceIndex(0)
53+
2 >Emitted(2, 8) Source(3, 8) + SourceIndex(0)
54+
3 >Emitted(2, 9) Source(3, 9) + SourceIndex(0)
55+
4 >Emitted(2, 12) Source(3, 12) + SourceIndex(0)
56+
5 >Emitted(2, 13) Source(3, 13) + SourceIndex(0)
57+
6 >Emitted(2, 14) Source(3, 14) + SourceIndex(0)
58+
7 >Emitted(2, 15) Source(3, 15) + SourceIndex(0)
59+
8 >Emitted(2, 16) Source(3, 16) + SourceIndex(0)
60+
---
61+
>>>//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5saW5lU291cmNlTWFwLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiaW5saW5lU291cmNlTWFwLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUNBLElBQUksQ0FBQyxHQUFHLENBQUMsQ0FBQztBQUNWLE9BQU8sQ0FBQyxHQUFHLENBQUMsQ0FBQyxDQUFDLENBQUMiLCJzb3VyY2VzQ29udGVudCI6WyJcbnZhciB4ID0gMDtcbmNvbnNvbGUubG9nKHgpOyJdfQ==
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error TS5051: Option 'out' cannot be specified with option 'inlineSourceMap'.
2+
error TS5050: Option 'mapRoot' cannot be specified with option 'inlineSourceMap'.
3+
error TS5049: Option 'sourceRoot' cannot be specified with option 'inlineSourceMap'.
4+
error TS5048: Option 'sourceMap' cannot be specified with option 'inlineSourceMap'.
5+
tests/cases/compiler/inlineSourceMap2.ts(5,1): error TS2304: Cannot find name 'console'.
6+
7+
8+
!!! error TS5051: Option 'out' cannot be specified with option 'inlineSourceMap'.
9+
!!! error TS5050: Option 'mapRoot' cannot be specified with option 'inlineSourceMap'.
10+
!!! error TS5049: Option 'sourceRoot' cannot be specified with option 'inlineSourceMap'.
11+
!!! error TS5048: Option 'sourceMap' cannot be specified with option 'inlineSourceMap'.
12+
==== tests/cases/compiler/inlineSourceMap2.ts (1 errors) ====
13+
14+
// configuration errors
15+
16+
var x = 0;
17+
console.log(x);
18+
~~~~~~~
19+
!!! error TS2304: Cannot find name 'console'.

tests/baselines/reference/inlineSourceMap2.js

+12
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)