# frozen_string_literal: true require 'execjs' require 'mini_racer' class DiscourseJsProcessor def self.call(input) root_path = input[:load_path] || '' logical_path = (input[:filename] || '').sub(root_path, '').gsub(/\.(js|es6).*$/, '').sub(/^\//, '') data = input[:data] if should_transpile?(input[:filename]) data = transpile(data, root_path, logical_path) end # add sourceURL until we can do proper source maps unless Rails.env.production? data = "eval(#{data.inspect} + \"\\n//# sourceURL=#{logical_path}\");\n" end { data: data } end def self.transpile(data, root_path, logical_path) transpiler = Transpiler.new(skip_module: skip_module?(data)) transpiler.perform(data, root_path, logical_path) end def self.should_transpile?(filename) 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(/^\/*/, '') relative_path.start_with?("app/assets/javascripts/discourse/") || relative_path.start_with?("app/assets/javascripts/admin/") || relative_path.start_with?("app/assets/javascripts/pretty-text/") || relative_path.start_with?("app/assets/javascripts/select-kit/") || relative_path.start_with?("app/assets/javascripts/wizard/") || relative_path.start_with?("app/assets/javascripts/discourse-common/") end def self.skip_module?(data) !!(data.present? && data =~ /^\/\/ discourse-skip-module$/) end class Transpiler @mutex = Mutex.new @ctx_init = Mutex.new def self.mutex @mutex end def self.create_new_context # timeout any eval that takes longer than 15 seconds ctx = MiniRacer::Context.new(timeout: 15000) ctx.eval("var self = this; #{File.read("#{Rails.root}/vendor/assets/javascripts/babel.js")}") ctx.eval(File.read(Ember::Source.bundled_path_for('ember-template-compiler.js'))) ctx.eval("module = {}; exports = {};") ctx.attach("rails.logger.info", proc { |err| Rails.logger.info(err.to_s) }) ctx.attach("rails.logger.error", proc { |err| Rails.logger.error(err.to_s) }) ctx.eval <