Skip to content
This repository was archived by the owner on Jun 27, 2018. It is now read-only.

Good bye 'text' of undefined #85

Merged
merged 2 commits into from
Jul 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
67 changes: 10 additions & 57 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);

Expand All @@ -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 {
Expand Down
14 changes: 7 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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\\";"]}';
Expand Down Expand Up @@ -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 = <bar />;';
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 = <bar />;"]}';
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 = <bar />;' + eol + '//# sourceMappingURL=data:application/json;base64,';
var actual = tss.compile(src, srcFile);
var match = /(^[\s\S]*;base64,)(.*)$/.exec(actual);
Expand Down
3 changes: 3 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"lib": [
"ES6"
],
"declaration": true,
"removeComments": false,
"stripInternal": true,
Expand Down