Skip to content

Commit 500cada

Browse files
committed
add transpileModule function that can return emitted source map
1 parent 6f00275 commit 500cada

12 files changed

+167
-68
lines changed

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,6 @@ namespace ts {
509509
Option_noEmit_cannot_be_specified_with_option_out_or_outDir: { code: 5040, category: DiagnosticCategory.Error, key: "Option 'noEmit' cannot be specified with option 'out' or 'outDir'." },
510510
Option_noEmit_cannot_be_specified_with_option_declaration: { code: 5041, category: DiagnosticCategory.Error, key: "Option 'noEmit' cannot be specified with option 'declaration'." },
511511
Option_project_cannot_be_mixed_with_source_files_on_a_command_line: { code: 5042, category: DiagnosticCategory.Error, key: "Option 'project' cannot be mixed with source files on a command line." },
512-
Option_sourceMap_cannot_be_specified_with_option_isolatedModules: { code: 5043, category: DiagnosticCategory.Error, key: "Option 'sourceMap' cannot be specified with option 'isolatedModules'." },
513512
Option_declaration_cannot_be_specified_with_option_isolatedModules: { code: 5044, category: DiagnosticCategory.Error, key: "Option 'declaration' cannot be specified with option 'isolatedModules'." },
514513
Option_noEmitOnError_cannot_be_specified_with_option_isolatedModules: { code: 5045, category: DiagnosticCategory.Error, key: "Option 'noEmitOnError' cannot be specified with option 'isolatedModules'." },
515514
Option_out_cannot_be_specified_with_option_isolatedModules: { code: 5046, category: DiagnosticCategory.Error, key: "Option 'out' cannot be specified with option 'isolatedModules'." },

src/compiler/diagnosticMessages.json

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,10 +2025,6 @@
20252025
"category": "Error",
20262026
"code": 5042
20272027
},
2028-
"Option 'sourceMap' cannot be specified with option 'isolatedModules'.": {
2029-
"category": "Error",
2030-
"code": 5043
2031-
},
20322028
"Option 'declaration' cannot be specified with option 'isolatedModules'.": {
20332029
"category": "Error",
20342030
"code": 5044

src/compiler/program.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ namespace ts {
341341
});
342342
}
343343

344-
function getDeclarationDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
344+
function getDeclarationDiagnosticsForFile(sourceFile: SourceFile, cancellationToken: CancellationToken): Diagnostic[] {
345345
return runWithCancellationToken(() => {
346346
if (!isDeclarationFile(sourceFile)) {
347347
let resolver = getDiagnosticsProducingTypeChecker().getEmitResolver(sourceFile, cancellationToken);
@@ -350,7 +350,7 @@ namespace ts {
350350
return ts.getDeclarationDiagnostics(getEmitHost(writeFile), resolver, sourceFile);
351351
}
352352
});
353-
}
353+
}
354354

355355
function getOptionsDiagnostics(): Diagnostic[] {
356356
let allDiagnostics: Diagnostic[] = [];
@@ -602,10 +602,6 @@ namespace ts {
602602

603603
function verifyCompilerOptions() {
604604
if (options.isolatedModules) {
605-
if (options.sourceMap) {
606-
diagnostics.add(createCompilerDiagnostic(Diagnostics.Option_sourceMap_cannot_be_specified_with_option_isolatedModules));
607-
}
608-
609605
if (options.declaration) {
610606
diagnostics.add(createCompilerDiagnostic(Diagnostics.Option_declaration_cannot_be_specified_with_option_isolatedModules));
611607
}

src/services/services.ts

Lines changed: 48 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,18 +1754,31 @@ namespace ts {
17541754
sourceFile.version = version;
17551755
sourceFile.scriptSnapshot = scriptSnapshot;
17561756
}
1757-
1757+
1758+
export interface TranspileOptions {
1759+
compilerOptions?: CompilerOptions;
1760+
fileName?: string;
1761+
reportDiagnostics?: boolean;
1762+
moduleName?: string;
1763+
}
1764+
1765+
export interface TranspileOutput {
1766+
outputText: string;
1767+
diagnostics?: Diagnostic[];
1768+
sourceMapText?: string;
1769+
}
1770+
17581771
/*
17591772
* This function will compile source text from 'input' argument using specified compiler options.
17601773
* If not options are provided - it will use a set of default compiler options.
1761-
* Extra compiler options that will unconditionally be used bu this function are:
1774+
* Extra compiler options that will unconditionally be used by this function are:
17621775
* - isolatedModules = true
17631776
* - allowNonTsExtensions = true
17641777
* - noLib = true
17651778
* - noResolve = true
1766-
*/
1767-
export function transpile(input: string, compilerOptions?: CompilerOptions, fileName?: string, diagnostics?: Diagnostic[], moduleName?: string): string {
1768-
let options = compilerOptions ? clone(compilerOptions) : getDefaultCompilerOptions();
1779+
*/
1780+
export function transpileModule(input: string, transpileOptions?: TranspileOptions): TranspileOutput {
1781+
let options = transpileOptions.compilerOptions ? clone(transpileOptions.compilerOptions) : getDefaultCompilerOptions();
17691782

17701783
options.isolatedModules = true;
17711784

@@ -1781,23 +1794,30 @@ namespace ts {
17811794
options.noResolve = true;
17821795

17831796
// Parse
1784-
let inputFileName = fileName || "module.ts";
1797+
let inputFileName = transpileOptions.fileName || "module.ts";
17851798
let sourceFile = createSourceFile(inputFileName, input, options.target);
1786-
if (moduleName) {
1787-
sourceFile.moduleName = moduleName;
1799+
if (transpileOptions.moduleName) {
1800+
sourceFile.moduleName = transpileOptions.moduleName;
17881801
}
17891802

17901803
let newLine = getNewLineCharacter(options);
17911804

17921805
// Output
17931806
let outputText: string;
1807+
let sourceMapText: string;
17941808

17951809
// Create a compilerHost object to allow the compiler to read and write files
17961810
let compilerHost: CompilerHost = {
17971811
getSourceFile: (fileName, target) => fileName === inputFileName ? sourceFile : undefined,
17981812
writeFile: (name, text, writeByteOrderMark) => {
1799-
Debug.assert(outputText === undefined, "Unexpected multiple outputs for the file: " + name);
1800-
outputText = text;
1813+
if (fileExtensionIs(name, ".map")) {
1814+
Debug.assert(sourceMapText === undefined, `Unexpected multiple source map outputs for the file '${name}'`);
1815+
sourceMapText = text;
1816+
}
1817+
else {
1818+
Debug.assert(outputText === undefined, "Unexpected multiple outputs for the file: " + name);
1819+
outputText = text;
1820+
}
18011821
},
18021822
getDefaultLibFileName: () => "lib.d.ts",
18031823
useCaseSensitiveFileNames: () => false,
@@ -1807,16 +1827,29 @@ namespace ts {
18071827
};
18081828

18091829
let program = createProgram([inputFileName], options, compilerHost);
1810-
1811-
addRange(/*to*/ diagnostics, /*from*/ program.getSyntacticDiagnostics(sourceFile));
1812-
addRange(/*to*/ diagnostics, /*from*/ program.getOptionsDiagnostics());
1813-
1830+
1831+
let diagnostics: Diagnostic[];
1832+
if (transpileOptions.reportDiagnostics) {
1833+
diagnostics = [];
1834+
addRange(/*to*/ diagnostics, /*from*/ program.getSyntacticDiagnostics(sourceFile));
1835+
addRange(/*to*/ diagnostics, /*from*/ program.getOptionsDiagnostics());
1836+
}
18141837
// Emit
18151838
program.emit();
18161839

18171840
Debug.assert(outputText !== undefined, "Output generation failed");
18181841

1819-
return outputText;
1842+
return { outputText, diagnostics, sourceMapText };
1843+
}
1844+
1845+
/*
1846+
* This is a shortcut function for transpileModule - it accepts transpileOptions as parameters and returns only outputText part of the result.
1847+
*/
1848+
export function transpile(input: string, compilerOptions?: CompilerOptions, fileName?: string, diagnostics?: Diagnostic[], moduleName?: string): string {
1849+
let output = transpileModule(input, { compilerOptions, fileName, reportDiagnostics: !!diagnostics, moduleName });
1850+
// addRange correctly handles cases when wither 'from' or 'to' argument is missing
1851+
addRange(diagnostics, output.diagnostics);
1852+
return output.outputText;
18201853
}
18211854

18221855
export function createLanguageServiceSourceFile(fileName: string, scriptSnapshot: IScriptSnapshot, scriptTarget: ScriptTarget, version: string, setNodeParents: boolean): SourceFile {

tests/baselines/reference/isolatedModulesSourceMap.errors.txt

Lines changed: 0 additions & 7 deletions
This file was deleted.

tests/baselines/reference/isolatedModulesSourceMap.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/isolatedModulesSourceMap.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/baselines/reference/isolatedModulesSourceMap.sourcemap.txt

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,26 @@ sources: isolatedModulesSourceMap.ts
88
emittedFile:tests/cases/compiler/isolatedModulesSourceMap.js
99
sourceFile:isolatedModulesSourceMap.ts
1010
-------------------------------------------------------------------
11-
>>>export var x;
11+
>>>export var x = 1;
1212
1 >
1313
2 >^^^^^^^^^^^
1414
3 > ^
15-
4 > ^
16-
5 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
15+
4 > ^^^
16+
5 > ^
17+
6 > ^
18+
7 > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^->
1719
1 >
1820
>
1921
2 >export var
2022
3 > x
21-
4 > ;
23+
4 > =
24+
5 > 1
25+
6 > ;
2226
1 >Emitted(1, 1) Source(2, 1) + SourceIndex(0)
2327
2 >Emitted(1, 12) Source(2, 12) + SourceIndex(0)
2428
3 >Emitted(1, 13) Source(2, 13) + SourceIndex(0)
25-
4 >Emitted(1, 14) Source(2, 14) + SourceIndex(0)
29+
4 >Emitted(1, 16) Source(2, 16) + SourceIndex(0)
30+
5 >Emitted(1, 17) Source(2, 17) + SourceIndex(0)
31+
6 >Emitted(1, 18) Source(2, 18) + SourceIndex(0)
2632
---
2733
>>>//# sourceMappingURL=isolatedModulesSourceMap.js.map
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
=== tests/cases/compiler/isolatedModulesSourceMap.ts ===
2+
3+
export var x = 1;
4+
>x : Symbol(x, Decl(isolatedModulesSourceMap.ts, 1, 10))
5+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
=== tests/cases/compiler/isolatedModulesSourceMap.ts ===
2+
3+
export var x = 1;
4+
>x : number
5+
>1 : number
6+

0 commit comments

Comments
 (0)