Skip to content
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

Introduce babel support #791

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.jp.md
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ p: markdown: Tag with **inline** markdown!
| less: | less | コンパイル時 | less コードを埋め込み、style タグで囲む |
| styl: | styl | コンパイル時 | stylus コードを埋め込み、 style タグで囲む |
| coffee: | coffee-script | コンパイル時 | CoffeeScript をコンパイルし、 script タグで囲む |
| babel: | babel-transpiler | コンパイル時 | ES6 をトランスパイルし、 script タグで囲む。**Tilt >= 2.0.2が必要** |
| asciidoc: | asciidoctor | コンパイル時 + 展開 | AsciiDoc をコンパイルし、テキスト中の # \{variables} を展開 |
| markdown: | redcarpet/rdiscount/kramdown | コンパイル時 + 展開 | Markdown をコンパイルし、テキスト中の # \{variables} を展開 |
| textile: | redcloth | コンパイル時 + 展開 | textile をコンパイルし、テキスト中の # \{variables} を展開 |
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -871,6 +871,7 @@ Supported engines:
| less: | less | Compile time | Embed less css code and wrap in style tag |
| styl: | styl | Compile time | Embed stylus css code and wrap in style tag |
| coffee: | coffee-script | Compile time | Compile coffee script code and wrap in script tag |
| babel: | babel-transpiler | Compile time | Transpile ES6 code and wrap in script tag. **Requires Tilt >= 2.0.2** |
| asciidoc: | asciidoctor | Compile time + Interpolation | Compile AsciiDoc code and interpolate #\{variables} in text |
| markdown: | redcarpet/rdiscount/kramdown | Compile time + Interpolation | Compile markdown code and interpolate #\{variables} in text |
| textile: | redcloth | Compile time + Interpolation | Compile textile code and interpolate #\{variables} in text |
Expand Down
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove this specific error message

Copy link
Author

@bary822 bary822 May 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you meant to mention line 139, I can be convinced to remove it.

However, on the other hand, it's true that Tilt with version 2.0.1 or earlier does not support babel-transpiler which causes failures on some travis matrix.

Could you share the background why we are testing against various version of Tilt gem? So we can come up with appropriate error message, or alternative solution to handle it. Thanks.

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