Skip to content

Commit

Permalink
Introduce babel support
Browse files Browse the repository at this point in the history
  • Loading branch information
bary822 committed Nov 28, 2017
1 parent da17e17 commit f25ffe3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
12 changes: 11 additions & 1 deletion lib/slim/embedded.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,16 @@ def collect_newlines(body)
# Basic tilt engine
class TiltEngine < Engine
def on_slim_embedded(engine, body)
tilt_engine = Tilt[engine] || raise(Temple::FilterError, "Tilt engine #{engine} is not available.")
begin
tilt_engine = Tilt[engine]
rescue
if engine == :balel
raise(Temple::FilterError, "babel engine is available for Tilt version 2.0.2 or above.")
else
raise(Temple::FilterError, "Tilt engine #{engine} is not available.")
end
end

tilt_options = options[engine.to_sym] || {}
[:multi, tilt_render(tilt_engine, tilt_options, collect_text(body)), collect_newlines(body)]
end
Expand Down Expand Up @@ -253,6 +262,7 @@ def on_slim_embedded(engine, body)
# These engines are executed at compile time
register :coffee, JavaScriptEngine, engine: TiltEngine
register :opal, JavaScriptEngine, engine: TiltEngine
register :babel, JavaScriptEngine, engine: TiltEngine
register :less, TagEngine, tag: :style, attributes: { type: 'text/css' }, engine: TiltEngine
register :styl, TagEngine, tag: :style, attributes: { type: 'text/css' }, engine: TiltEngine
register :sass, TagEngine, :pretty, tag: :style, attributes: { type: 'text/css' }, engine: SassEngine
Expand Down
22 changes: 20 additions & 2 deletions test/core/test_embedded_engines.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def test_render_with_wiki
def test_render_with_javascript
# Keep the trailing space behind "javascript: "!
source = %q{
javascript:
javascript:
$(function() {});
Expand All @@ -139,6 +139,24 @@ def test_render_with_opal
assert_match '$puts("hello from opal")', render(source)
end

def test_render_with_babel
begin
# HACK: babel-transpiler registers itself in Tilt
require 'babel-transpiler'
rescue LoadError
return
end

source = %q{
babel:
const str = 'World'
alert(`Hello ${str}`)
p Hi
}
assert_html %{<script>'use strict';\n\nvar str = 'World';\nalert('Hello ' + str);</script><p>Hi</p>}, source
end


def test_render_with_javascript_with_tabs
source = "javascript:\n\t$(function() {});\n\talert('hello')\np Hi"
assert_html "<script>$(function() {});\nalert('hello')</script><p>Hi</p>", source
Expand All @@ -148,7 +166,7 @@ def test_render_with_javascript_including_variable
# Keep the trailing space behind "javascript: "!
source = %q{
- func = "alert('hello');"
javascript:
javascript:
$(function() { #{func} });
}
assert_html %q|<script>$(function() { alert(&#39;hello&#39;); });</script>|, source
Expand Down

0 comments on commit f25ffe3

Please sign in to comment.