theblacksmith / typescript-compiler

Typescript compiler wrapper
MIT License
28 stars 15 forks source link

Trying to use compileString but can't get it to work. #17

Open richtera opened 8 years ago

richtera commented 8 years ago

It gives me a strange error as if it had conversion problems and converted the StringSource to an object.

const name = 'script.ts';
const source = 'export class Some { }';
const tsc = require('typescript-compiler');
console.log(source, name);
const src = new tsc.StringSource(source, name);
console.log(src);
const js = tsc.compileString(src);
console.log(js);

I get this weird error with the codeframe all messed up:

export class Some { } script.ts
StringSource {
  contents: 'export class Some { }',
  filename: 'script.ts',
  type: 1 }

{ sources: {},
  sourceMaps: [],
  errors: [ 'error TS6053: File \'export class Some { }.ts\' not found.\n' ] }
{ [SyntaxError: script.ts: Unexpected token (1:8)]
  pos: 8,
  loc: Position { line: 1, column: 8 },
  _babel: true,
  codeFrame: '\u001b[0m> 1 | \u001b[33m[\u001b[39mobject Object\u001b[33m]\u001b[39m\n    |         ^\u001b[0m' }
SyntaxError: script.ts: Unexpected token (1:8)
> 1 | [object Object]
    |         ^
    at Parser.pp.raise (/Users/andy/Development/selfstudy-ng2/node_modules/babylon/lib/parser/location.js:22:13)
    at Parser.pp.unexpected (/Users/andy/Development/selfstudy-ng2/node_modules/babylon/lib/parser/util.js:91:8)
    at Parser.pp.expect (/Users/andy/Development/selfstudy-ng2/node_modules/babylon/lib/parser/util.js:83:33)
    at Parser.pp.parseExprList (/Users/andy/Development/selfstudy-ng2/node_modules/babylon/lib/parser/expression.js:993:12)
    at Parser.pp.parseExprAtom (/Users/andy/Development/selfstudy-ng2/node_modules/babylon/lib/parser/expression.js:476:28)
    at Parser.pp.parseExprSubscripts (/Users/andy/Development/selfstudy-ng2/node_modules/babylon/lib/parser/expression.js:270:19)
    at Parser.pp.parseMaybeUnary (/Users/andy/Development/selfstudy-ng2/node_modules/babylon/lib/parser/expression.js:250:19)
    at Parser.pp.parseExprOps (/Users/andy/Development/selfstudy-ng2/node_modules/babylon/lib/parser/expression.js:180:19)
    at Parser.pp.parseMaybeConditional (/Users/andy/Development/selfstudy-ng2/node_modules/babylon/lib/parser/expression.js:157:19)
    at Parser.pp.parseMaybeAssign (/Users/andy/Development/selfstudy-ng2/node_modules/babylon/lib/parser/expression.js:120:19)
{ source: 'export class Some { }',
  err: 
   { [SyntaxError: script.ts: Unexpected token (1:8)]
     pos: 8,
     loc: Position { line: 1, column: 8 },
     _babel: true,
     codeFrame: '\u001b[0m> 1 | \u001b[33m[\u001b[39mobject Object\u001b[33m]\u001b[39m\n    |         ^\u001b[0m' } }

Not sure where to start, sorry.

richtera commented 8 years ago

I got further. Calling compileString doesn't really do anything if there are errors (I had assumed it would throw errors when onError was not defined.) After seeing the first errors I realized that I needed a command line to be passed for this to do anything. Now I have:

    const js = tsc.compileString(src, '--project ./tsconfig.json --module commonjs --target es5',
      null, err => {
      console.log("error", err);
    });

This will work for simple typescript sources but no matter what I do I was unable to get it to understand decorators. Is there something else I have to do? I am also confused about command line versus compiler options versus --project settings. They seems to override each other in unexpected ways.