Class: DiscourseJsProcessor

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

Defined Under Namespace

Classes: TranspileError, Transpiler

Constant Summary collapse

DISCOURSE_COMMON_BABEL_PLUGINS =

To generate a list of babel plugins used by ember-cli, set babel: { debug: true } in ember-cli-build.js, then run ‘yarn ember build -prod`

[
  ["proposal-decorators", { legacy: true }],
  "proposal-class-properties",
  "proposal-private-methods",
  "proposal-class-static-block",
  "transform-parameters",
  "proposal-export-namespace-from",
]

Class Method Summary collapse

Class Method Details

.call(input) ⇒ Object



28
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 28

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])

  # add sourceURL until we can do proper source maps
  if !Rails.env.production? && !ember_cli?(input[:filename])
    plugin_name = root_path[%r{/plugins/([\w-]+)/assets}, 1]
    source_url =
      if plugin_name
        "plugins/#{plugin_name}/assets/javascripts/#{logical_path}"
      else
        logical_path
      end

    data = "eval(#{data.inspect} + \"\\n//# sourceURL=#{source_url}\");\n"
  end

  { data: data }
end

.ember_cli?(filename) ⇒ Boolean

Returns:

  • (Boolean)


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

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

.plugin_transpile_pathsObject



20
21
22
# File 'lib/discourse_js_processor.rb', line 20

def self.plugin_transpile_paths
  @@plugin_transpile_paths ||= Set.new
end

.should_transpile?(filename) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/discourse_js_processor.rb', line 57

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/")

  if %w[
       start-discourse
       onpopstate-handler
       google-tag-manager
       google-universal-analytics-v3
       google-universal-analytics-v4
       activate-account
       auto-redirect
       embed-application
       app-boot
     ].any? { |f| relative_path == "#{js_root}/#{f}.js" }
    return true
  end

  return true if plugin_transpile_paths.any? { |prefix| relative_path.start_with?(prefix) }

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

.skip_module?(data) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/discourse_js_processor.rb', line 96

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



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

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