diff --git a/index.d.ts b/index.d.ts index 8f93174..42cd3ea 100644 --- a/index.d.ts +++ b/index.d.ts @@ -23,9 +23,7 @@ declare namespace tss { private createService(); private getTypeScriptBinDir(); private getDefaultLibFileName(options); - private getFile(outputFiles, fileName); private toJavaScript(service, fileName); - private normalizeSlashes(path); private formatDiagnostics(diagnostics); } } diff --git a/index.ts b/index.ts index 0bdeb4e..7db70a4 100644 --- a/index.ts +++ b/index.ts @@ -27,14 +27,17 @@ namespace tss { * @constructor */ constructor(options: ts.CompilerOptions = {}, private doSemanticChecks = true) { - // accept null - options = options || {}; + options = Object.assign({}, options); if (options.target == null) { options.target = ts.ScriptTarget.ES5; } if (options.module == null) { options.module = ts.ModuleKind.None; } + if (options.sourceMap) { + options.sourceMap = false; + options.inlineSourceMap = true; + } this.options = options; } @@ -113,7 +116,7 @@ namespace tss { // TODO: Use options.newLine getNewLine: () => os.EOL, log: (message: string) => console.log(message), - trace: (message: string) => console.debug(message), + trace: (message: string) => console.trace(message), error: (message: string) => console.error(message), readFile: readFile, fileExists: fileExists @@ -154,31 +157,6 @@ namespace tss { } } - /** - * converts {"version":3,"file":"file.js","sourceRoot":"","sources":["file.ts"],"names":[], - * "mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC"} - * to {"version":3,"sources":["foo/test.ts"],"names":[], - * "mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC","file":"foo/test.ts","sourcesContent":["var x = 'test';"]} - * derived from : https://github.com/thlorenz/convert-source-map - * @internal - */ - private getInlineSourceMap(mapText: string, fileName: string): string { - let sourceMap = JSON.parse(mapText); - sourceMap.file = fileName; - sourceMap.sources = [fileName]; - sourceMap.sourcesContent = [this.files[fileName].text]; - delete sourceMap.sourceRoot; - return JSON.stringify(sourceMap); - } - - private getFile (outputFiles: ts.OutputFile[], fileName: string): ts.OutputFile { - const files = outputFiles.filter((file) => { - const full = this.normalizeSlashes(path.resolve(process.cwd(), fileName)); - return file.name === fileName || file.name === full; - }); - return files[0]; - } - private toJavaScript(service: ts.LanguageService, fileName: string): string { let output = service.getEmitOutput(fileName); @@ -193,37 +171,12 @@ namespace tss { throw new Error(this.formatDiagnostics(allDiagnostics)); } - let outDir = (this.options && this.options.outDir) ? this.options.outDir : ''; - let fileNameWithoutRoot = 'rootDir' in this.options ? fileName.replace(new RegExp('^' + this.options.rootDir), '') : fileName; - let outputFileName: string; - if (this.options.jsx === ts.JsxEmit.Preserve) { - outputFileName = path.join(outDir, fileNameWithoutRoot.replace(/\.tsx$/, '.jsx')); - } else { - outputFileName = path.join(outDir, fileNameWithoutRoot.replace(/\.tsx?$/, '.js')); - } - // for Windows #37 - outputFileName = this.normalizeSlashes(outputFileName); - let file = this.getFile(output.outputFiles, outputFileName); - let text = file.text; - - // If we have sourceMaps convert them to inline sourceMaps - if (this.options.sourceMap) { - let sourceMapFileName = outputFileName + '.map'; - let sourceMapFile = this.getFile(output.outputFiles, sourceMapFileName) - - // Transform sourcemap - let sourceMapText = sourceMapFile.text; - sourceMapText = this.getInlineSourceMap(sourceMapText, fileName); - let base64SourceMapText = new Buffer(sourceMapText).toString('base64'); - let sourceMapComment = '//# sourceMappingURL=data:application/json;base64,' + base64SourceMapText; - text = text.replace('//# sourceMappingURL=' + path.basename(sourceMapFileName), sourceMapComment); + if (output.outputFiles.length !== 1) { + const names = output.outputFiles.map(_ => _.name); + throw new Error(`Output should be only 1 file, but ${names.length} files: ${names.join(', ')}`); } - return text; - } - - private normalizeSlashes(path: string): string { - return path.replace(/\\/g, "/"); + return output.outputFiles[0].text; } private formatDiagnostics(diagnostics: ts.Diagnostic[]): string { diff --git a/test/test.js b/test/test.js index a97381e..5fd883a 100644 --- a/test/test.js +++ b/test/test.js @@ -157,16 +157,16 @@ describe('typescript-simple', function() { }); }); - context('tss sourceMaps option is true', function() { + context('`sourceMap` option is true', function() { var tss; beforeEach(function() { tss = new TypeScriptSimple({target: ts.ScriptTarget.ES5, sourceMap: true}, false); }); - it('should result in inline sourceMaps', function() { + it('should result in `inlineSourceMap', function() { var src = 'var x = "test";'; var srcFile = 'foo/test.ts'; - var sourceMap = '{"version":3,"file":"foo/test.ts","sources":["foo/test.ts"],"names":[],"mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC","sourcesContent":["var x = \\"test\\";"]}'; + var sourceMap = '{"version":3,"file":"test.js","sources":["test.ts"],"sourceRoot":"","names":[],"mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC"}'; var expectedPrefix = 'var x = "test";' + eol + '//# sourceMappingURL=data:application/json;base64,'; var actual = tss.compile(src, srcFile); var match = /(^[\s\S]*;base64,)(.*)$/.exec(actual); @@ -176,13 +176,13 @@ describe('typescript-simple', function() { }); }); - context('native inlineSourceMap option is true', function() { + context('native `inlineSourceMap` option is true', function() { var tss; beforeEach(function() { tss = new TypeScriptSimple({target: ts.ScriptTarget.ES5, inlineSourceMap: true, inlineSources: true}, false); }); - it('should result in inline sourceMaps', function() { + it('should result in inline sourceMap', function() { var src = 'var x = "test";'; var srcFile = 'foo/test.ts'; var sourceMap = '{"version":3,"file":"test.js","sourceRoot":"","sources":["test.ts"],"names":[],"mappings":"AAAA,IAAI,CAAC,GAAG,MAAM,CAAC","sourcesContent":["var x = \\"test\\";"]}'; @@ -238,11 +238,11 @@ describe('typescript-simple', function() { assert.equal(tss.compile(src), expected); }); - it('should result in inline sourceMaps', function() { + it('should result in inline sourceMap', function() { var tss = new TypeScriptSimple({jsx: ts.JsxEmit.Preserve, sourceMap: true}); var src = 'var foo: any = ;'; var srcFile = 'foo/test.tsx'; - var sourceMap = '{"version":3,"file":"foo/test.tsx","sources":["foo/test.tsx"],"names":[],"mappings":"AAAA,IAAI,GAAG,GAAQ,CAAC,GAAG,GAAG,CAAC","sourcesContent":["var foo: any = ;"]}'; + var sourceMap = '{"version":3,"file":"test.jsx","sourceRoot":"","sources":["test.tsx"],"names":[],"mappings":"AAAA,IAAI,GAAG,GAAQ,CAAC,GAAG,GAAG,CAAC"}'; var expectedPrefix = 'var foo = ;' + eol + '//# sourceMappingURL=data:application/json;base64,'; var actual = tss.compile(src, srcFile); var match = /(^[\s\S]*;base64,)(.*)$/.exec(actual); diff --git a/tsconfig.json b/tsconfig.json index de4d012..eb4476d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,9 @@ "compilerOptions": { "target": "es5", "module": "commonjs", + "lib": [ + "ES6" + ], "declaration": true, "removeComments": false, "stripInternal": true,