Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

support for svelte.dev#149

artbot1 started this conversation inIdeas
Oct 26, 2021· 4 comments· 4 replies
Discussion options

ruby2js supports filters for react and vue (v3 not yet ready, seems much more complicated than v2) and ...

recently i got in touch with svelte.dev

it doesn't use the virtual DOM (like react etc.) but compiles/transepiles into vanilla JS (manipulating DOM directly, but only when needed) making components really reusable.

also the notation is much more intuitive than using react/vue etc.

i think it would be awesome to have some kind of support for svelte.dev in ruby2js (
maybe it could also be build into serbea.dev/bridgetownrb.com) ???

what do you think about a possible svelte.dev and ruby2js coupling ...

You must be logged in to vote

Replies: 4 comments 4 replies

Comment options

Technically, svelte (and, for that matter, Vue 3) are supersets of HTML, not JS. This means either one of three things:

  • Ruby2JS when run with a svelte filter would output svelte enhanced HTML, not JS; and that intermediate output would then be need to be passed through a svelte compiler in order to produce the JS that ultimately will be served to the browser.
  • Ruby2JS will produce svelte enhanced HTML internally, and then transparently call out to the svelte compiler and return the generated JS as the result
  • Ruby2JS's svelte filter compiles direct to JS, essentially producing the equivalent of what the svelte compiler would have produced, but doing so directly.

I would suggest focusing on the first option initially, and then perhaps proceeding to either the second or the third option.

I don't have enough (or for that matter any) svelte experience to design a reasonable mapping of svelte to Ruby, but if somebody could develop a set of specs (and by all that I mean a list of "this input should produce that output"), I can help with the implementation.

You must be logged in to vote
0 replies
Comment options

i'm not a svelte expert (yet) but as far as i understand it a transformation of

<script lang="ruby">  ...   svelte_dollar_block do      ...   end<script>

to

<script>   ...  $: {  }<script>

would be enough

transformations of conditional/loop svelte blocks (outside of <script> ...</script> could be omitted in the first version (or even at all)

{#if }{/if}

but i'm really not sure if it would be all ...

You must be logged in to vote
0 replies
Comment options

rubys
Nov 8, 2021
Maintainer

I'm not sure where/how this would fit into the existing project, but to keep the discussion going, here is a script that will convert source that contains<script lang="ruby"> blocks into a source where the ruby scripts are replaced with #"auto" data-snippet-clipboard-copy-content="require 'nokogiri'require 'ruby2js'if ARGV.length == 0 source = STDIN.readelse source = ARGV.map {|file| IO.read file}.joinendfragment = Nokogiri::HTML5.fragment(source)fragment.search('script[lang=ruby]').each do |script| script.remove_attribute 'lang' script.content = Ruby2JS.convert(script.content).to_sendputs fragment.to_html">

require'nokogiri'require'ruby2js'ifARGV.length ==0source=STDIN.readelsesource=ARGV.map{|file|IO.readfile}.joinendfragment=Nokogiri::HTML5.fragment(source)fragment.search('script[lang=ruby]').eachdo |script|script.remove_attribute'lang'script.content=Ruby2JS.convert(script.content).to_sendputsfragment.to_html
You must be logged in to vote
4 replies
@artbot1
Comment options

the problem is not the html stuff (can be solved as in your solution), but the
special $: { } svelte construct, which i called:

svelte_dollar_block do      ...end

such a construct should be build into ruby2js (with a better name :-)

@jaredcwhite
Comment options

The original$: API could use a better name. 😉

But seriously, I wonder if block syntax even makes sense. For example, this is valid Svelte code:

$: doubled = count * 2

It's almost like a decorator, which doesn't immediately map to regular Ruby syntax. But I'll give it a bit of thought…

@jaredcwhite
Comment options

So this is valid Ruby syntax:

$:ifcount >=10alert("count is dangerously high!")count=9end$:doubled=count *3

Normally$: is the load/require path in Ruby, but I don't believe that maps to any concept in Ruby2JS currently (correct me if I'm wrong@rubys). So in theory, if we could parse it so that it outputs "as is" rather than a standalone statement (and short-circuitslet in an immediate variable assignment, it should output valid Svelte-flavored JS without any need for instroducing additional concepts. Just a thought.

@rubys
Comment options

First, I will confirm that$: is not otherwise mapped.

Next, I will note that for Jared's latest suggestion to parse correctly, the$:must be on a separate line ... or followed by a semicolon.

Trying to think outside of the box:

  1. while this is not currently done anywhere else, perhaps a svelte filter could define a pre-processor that does a textual conversion of$: followed by a space to$:; whenever this string is found at the beginning of a line.

  2. It would indeed feel more "rubyish" to replace the$: sequence with a readable word. If that readable word happened to be a JavaScript reserved word, that wouldn't add any additional limitations to what JavaScript could be produced.var comes to mind as a short, readable string that could readily be repurposed to indicate that this assignment or block of code is reactive.

Comment options

Why not just convert ruby symbol (:id) into svelte's $:id? There's anyway no direct equivalent of ruby's symbol in js.

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Ideas
Labels
None yet
4 participants
@artbot1@rubys@jaredcwhite@yerlaser

[8]ページ先頭

©2009-2025 Movatter.jp