-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Transpile in browser and show warnings #2808
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
Comments
What sort of warnings do you want to show? can you elaborate on the scenario you are trying to achive |
ts.transpile is the correct entry point. we are looking of slimming it down. for the short term it still needs the checker code, but in the future it should be possible to do it with just the parser and the emitter, which is much smaller. As for errors, if you are in transpile mode, there are a lot of errors that you would get, e.g. undefined globals, unknown module imports, missing .d.ts.. etc.. If you are in transpile mode you only get syntactic errors, for other errors you will need to do a full compilation. if what you want is to do a full compilation in the browser, you should be looking at this sample instead and implement a CompilerHost. |
@mhegazy thanks for the prompt answer! My scenario is similar to the one on typescript website, where users can code and execute typescript directly in the browser. The difference is, that there would be no stored files and code is stored in the database. Therefore, what I will not need is checking for module imports but I will need everything else (parameter access, type checking and more) What I could not grasp from the example you sent me, is how to do the compilation directly from the source without using files or how to get previously mentioned errors from transpiler. Thanks! |
if you want to do something like the typescript playground, then you can not use transpile; this is not going to give you the checking errors you are looking for. you also need to include the library file (lib.d.ts). one option if you are compiling a single file is to use typescript-simple. Otherwise, I can help getting you started using the API, a few questions: Do you want to do any editor functionality (e.g. completion, quick info on hover..etc..)? do you want the emitted js? do you want to run it later on? if so do you want to have source map support? |
@mhegazy The typescript simple looks nice but it's very much bound to the server version. TO be honest, I had luck with npm package typescript-compiler, which needed to be only slightly adjusted to work directly in the browser. I have created a Meteor package out of it and will soon post a link here. I will also run the edited code in the browser, but I already have a functionality for that, Thanks! PS: The project I work on teaches Uni students programming. We already have more than 600 students beta testing our system, but currently the biggest drawback was compilation, which was not detecting enough errors. Typescript seems like a perfect match for us. |
@tomitrescak looks like what you want is to use the Language Service API, this allows you to add additional features in the feature like code completion, type information, parameter help, folding, formatting, etc.. I would start by looking at this example: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#incremental-build-support-using-the-language-services you will need to implement
Create a new LS object and pass in your host implantation: var services = ts.createLanguageService(servicesHost, ts.createDocumentRegistry()) to get the JS output, call: var output = services.getEmitOutput("file1.ts");
// And you can execute it
eval(output.outputFiles[0].text); to get errors: var diagnostics = services.getCompilerOptionsDiagnostics() // global errors, e.g. using the wrong get/set with --target ES3
.concat(services.getSyntacticDiagnostics("file1.ts")) // parse errors, e.g. identifier expected
.concat(services.getSemanticDiagnostics("file1.ts")); // semantic errors e.g. number not assignable to string Hope this helps. Please feel free to ping me if you have other questions. would be glad to help out more in any way. |
Mohamed, thanks for your help. Soon I will write a blogpost recapitulating what I done so I can share it with the world. Thanks again! |
Thanks @tomitrescak. would love to read the blog as well. please let us know if there are any issues you run into or any rough edges that you would run into with the API. |
Is there a lite version of ts services? Its about 3mb to load? |
not currently. we can minify it, and that will shrink it considerably, given that it has comments included. we can also build a new js file with only parts needed for transpilation. it has not been requested before. feel free to log an issue for this. |
We use minified version and with server packing comes down to bout 400kb. Still a chunky one but works beautifully. |
@tomitrescak could you please point to the blog post where you describe the solution of using typescript in the browser? |
What would be the best approach to transpile ts to us live in browser and to obtain transpiration warnings?
I've found that typescript.js has a method transpile, but
How can I minimise the transputer and obtain warnings? Please, I need to code and execute ts directly in browser.
Thanks
The text was updated successfully, but these errors were encountered: