Class: DiscourseJsProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/discourse_js_processor.rb

Defined Under Namespace

Classes: TranspileError, Transpiler

Class Method Summary collapse

Class Method Details

.call(input) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/discourse_js_processor.rb', line 13

def self.call(input)
  root_path = input[:load_path] || ""
  logical_path =
    (input[:filename] || "").sub(root_path, "").gsub(/\.(js|es6).*$/, "").sub(%r{^/}, "")
  data = input[:data]

  data = transpile(data, root_path, logical_path) if should_transpile?(input[:filename])

  { data: data }
end

.ember_cli?(filename) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/discourse_js_processor.rb', line 9

def self.ember_cli?(filename)
  filename.include?("/app/assets/javascripts/discourse/dist/")
end

.should_transpile?(filename) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/discourse_js_processor.rb', line 29

def self.should_transpile?(filename)
  filename ||= ""

  # skip ember cli
  return false if ember_cli?(filename)

  # es6 is always transpiled
  return true if filename.end_with?(".es6") || filename.end_with?(".es6.erb")

  # For .js check the path...
  return false unless filename.end_with?(".js") || filename.end_with?(".js.erb")

  relative_path = filename.sub(Rails.root.to_s, "").sub(%r{^/*}, "")

  js_root = "app/assets/javascripts"
  test_root = "test/javascripts"

  return false if relative_path.start_with?("#{js_root}/locales/")
  return false if relative_path.start_with?("#{js_root}/plugins/")

  !!(relative_path =~ %r{^#{js_root}/[^/]+/} || relative_path =~ %r{^#{test_root}/[^/]+/})
end

.skip_module?(data) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/discourse_js_processor.rb', line 52

def self.skip_module?(data)
  !!(data.present? && data =~ %r{^// discourse-skip-module$})
end

.transpile(data, root_path, logical_path, theme_id: nil, extension: nil) ⇒ Object



24
25
26
27
# File 'lib/discourse_js_processor.rb', line 24

def self.transpile(data, root_path, logical_path, theme_id: nil, extension: nil)
  transpiler = Transpiler.new(skip_module: skip_module?(data))
  transpiler.perform(data, root_path, logical_path, theme_id: theme_id, extension: extension)
end