-
Notifications
You must be signed in to change notification settings - Fork 756
/
Copy pathjsx_transformer.rb
35 lines (29 loc) · 1.09 KB
/
jsx_transformer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# frozen_string_literal: true
module React
module JSX
# A {React::JSX}-compliant transformer which uses the deprecated `JSXTransformer.js` to transform JSX.
class JSXTransformer
DEFAULT_ASSET_PATH = "JSXTransformer.js"
def initialize(options)
@transform_options = {
stripTypes: options.fetch(:strip_types, false),
harmony: options.fetch(:harmony, false)
}
@asset_path = options.fetch(:asset_path, DEFAULT_ASSET_PATH)
# If execjs uses therubyracer, there is no 'global'. Make sure
# we have it so JSX script can work properly.
js_code = "var global = global || this;#{jsx_transform_code}"
@context = ExecJS.compile(js_code)
end
def transform(code)
result = @context.call("JSXTransformer.transform", code, @transform_options)
result["code"]
end
# search for transformer file using sprockets - allows user to override
# this file in their own application
def jsx_transform_code
::Rails.application.assets[@asset_path].to_s
end
end
end
end