Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
richtera opened this issue Sep 15, 2016 · 1 comment
Open

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

richtera opened this issue Sep 15, 2016 · 1 comment

Comments

@richtera
Copy link

richtera commented Sep 15, 2016

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant