jessedoyle / duktape.cr

Evaluate JavaScript from Crystal!
MIT License
137 stars 17 forks source link

Stenciljs server side rendering #38

Closed ansarizafar closed 6 years ago

ansarizafar commented 7 years ago

Is it possible to use duktape.cr to create a server rendered https://stenciljs.com application on a crystal server.

jessedoyle commented 7 years ago

Hi @ansarizafar - thanks for using Duktape!

I'm not that familiar with stenciljs, so I'll have to look into it over the next few days.

Duktape is a fully-featured javascript engine and should be capable of running vanilla JS. With that being said, the engine currently does not shim any browser-specific functionality (i.e. document, console.log, etc.). Therefore if stenciljs uses any browser-specific functions, the engine will likely fail with a ReferenceError on the function.

Of course given the primitives in this shard, anyone should be able to write functions that provide the browser capabilities.

Hope this helps! I'll look into stenciljs over the next few days and get back to you.

ansarizafar commented 6 years ago

@jessedoyle Check this https://github.com/ionic-team/stencil/issues/254#issuecomment-338896765

jessedoyle commented 6 years ago

Thanks for the update @ansarizafar! That issue was a good read.

Here's what I got from reading it:

  1. All node dependencies are encapsulated within the stenciljs sys object.
  2. The sys object can be implemented on the host interpreter and stenciljs should function fine. Take a look at the interface.
  3. The Typescript compiler would have to be turned into a shard (this shouldn't be too difficult, I've done it with CoffeeScript).

So I guess an answer to your initial question is that using stenciljs for server-side rendering is possible, but some work is needed first. I think the bulk of the work would be re-implementing all of the functionality that the sys object provides.

The current version of duktape.cr (0.12.0) brings support for handling various BuiltIn functions to Duktape interpreter contexts.

I don't think that this shard should ever provide built-in functionality to mock out behaviour for various frameworks. But you could definitely leverage the primitives in duktape.cr to create a shard to run stenciljs from within Crystal.

I hope this helps!