convert javascript to dart - javascript

I am reading
To leverage one of the many existing libraries written in JavaScript, use package:js. If a TypeScript type definition file exists for a JavaScript library (see DefinitelyTyped for more info), you can use the js_facade_gen tool to generate Dart code for that library. from https://webdev.dartlang.org/guides/web-programming.
I am curious how convert javascript library to dart file in the example https://github.com/google/chartjs.dart. I guess there are two steps: 1, generate TypeScript type definition file (ts file) from the javascript library; 2, Use js_facade_gen to convert ts file to dart.
I hope get more details. but the document is lacking. Any hints welcomed. Thanks

There is no way to convert JavaScript to Dart automatically.
package:js allows you to call JavaScript from Dart in Dart browser apps.

Related

TypeScript/JavaScript in Angular

I'm a beginner level in writing in TypeScript and working with Angular, but I am experienced in JavaScript. From what I've read, any TypeScript would be translated into JavaScript and compiled. So when I code in the TypeScript file (e.g. the whatever.component.ts file), does that mean I can mix or introduce JavaScript language code inside where I also write TypeScript in the .ts file?
How does this all exist within the .ts file in regards to a code readability aspect for someone who is just learning TypeScript/Angular?
You can of course use JavaScript anyhow you like it. However, TypeScript is built to help the developers maintaining projects by using variables types. The types can then easily be read and previewed by good IDE with the appropriate extensions.
Get used to adding types. Use interfaces for custom objects, embrace the Angular way and you will forget you ever had to deal with JavaScript very soon.
A good base for productive IDE would be Visual Studio Code with the Angular Essentials extension.
Welcome to the Angular community!

Kotlin: What is a kjsm file?

I have been trying to use the Kotlin -> js compiler by following this tutorial.
When I run kotlinc-js --help, then the help text mentions the following:
-kjsm Generate kjsm-files (for creating libraries)
What is a kjsm file?
A kjsm-file is a Kotlin JavaScript Meta file (see KotlinJavaScriptMetaFileType).
Such a file appears to be used to provide meta data for native JavaScript objects so that the Kotlin compiler can type-check things and so that an IDE can provide code completion, etc. e.g. If you look in kotlin-js-library-1.0.6.jar you will find, among other kjsm-files, a Window.kjsm file which defines the Window Web API available in web browsers.
You would want to generate your own kjsm-files whenever you are creating a library so that your interfaces can be used by the compiler/IDE in modules which depend on your Kotlin JavaScript library.

Why create Typescript as a separate language if it just outputs to Javascript?

Couldn't Typescript simply be a JavaScript library to include with other libraries such as Angular? Is that what it is? Wouldn't being a JS library give greater support for outputting ES5 specific JS?
Librairies are runtime objects.
Typescript is a language that extends Javascript with new keywords, such as types.
These keywords need to be parsed and compiled/transpiled to Javascript before being executed by the Javascript engine.

TypeScript various file extensions explained?

In trying to understand TypeScript a little more, what are the relations between all of the file extensions?
TypeScript, *.ts
Definition, *.d.ts
Map, *.map
JavaScript, *.js
I initially started entering the question above thinking to myself that someone would come along and help me out. Then I noticed an "answer your own question" option and I was inspired by Jeff Atwood's encouraging blog post - so I decided I should attempt to answer my own question. I had to do some research but now I have the understanding I was originally looking for.
TypeScript, *.ts
A typed superset of JavaScript that "compiles" to plain JavaScript. These files have the potential to utilize type-safety and strongly-typed syntax, with IDE intellisense.
Definition, *.d.ts
A *.d.ts file is used to provide TypeScript type information about an API that's written in JavaScript. Type definition files contain the defining types for all of the public APIs within a corresponding .js, for example - JQuery has jQuery.js without the jQuery.d.ts a TypeScript file consuming jQuery wouldn't know about its types, therefore intellisense is gone.
Map, *.map
A .map file is a source map file that let tools "map" between the emitted JavaScript code and the TypeScript source files that created it. This concept has been around since CoffeeScript.
JavaScript, *.js
According to MDN:
JavaScript is a cross-platform, object-oriented scripting language. It is a small and lightweight language. Inside a host environment (for example, a web browser), JavaScript can be connected to the objects of its environment to provide programmatic control over them.
The relationship between a .ts file and a .js file is that a TypeScript file compiles down to a JavaScript file.

How to compile Haxe libraries to Javascript without enumerating every classes?

I'm trying to make use of a Haxe library in Javascript. Since I already have a project written in Javascript, porting that to Haxe + using the given Haxe library isn't really a solution I'm ready to take at the moment.
Is there a way I can just compile the library from it's Haxe source folder to a JS file that I can then include in my <script> tags and refer to in my project? If I'm not mistaking, when a Haxe project normally compiles, it only includes the classes actually used in your project (correct?) - therefore I need some kind of tool or compiler options that will include all the classes in a given directory.
I would assume packages will get namespaced into Javascript objects at that point.
In case this helps, the library I'm looking to transcompile is Nape (Physics engine written in Haxe).
Looks like I found a way!
From FlashDevelop, create a new Haxe project (for JS).
Then, add a reference to the library that you wish to compile, either by:
Finding it in your Haxe install directory/haxe/lib/name_of_library/version_of_library.
Or, simply by adding the library name in your project settings, under Libraries (as shown in the picture below).
Third (most important step), add --macro include('your_library_root_packagename') in the Additional Compiler Options (replace with your library's top-level package name, obviously!)
I believe that if you have a library consisting of many top-level packages, you would need to enumerate several of those --macro compiler options. In my case I just needed the one.
Finally, CTRL+ENTER... wait for it to transcompile and Voila!
You have a Library in Javascript format!

Categories

Resources