# frozen_string_literal: true require 'execjs' require 'mini_racer' class DiscourseJsProcessor DISCOURSE_COMMON_BABEL_PLUGINS = [ 'proposal-optional-chaining', ['proposal-decorators', { legacy: true } ], 'transform-template-literals', 'proposal-class-properties', 'proposal-class-static-block', 'proposal-private-property-in-object', 'proposal-private-methods', 'proposal-numeric-separator', 'proposal-logical-assignment-operators', 'proposal-nullish-coalescing-operator', 'proposal-json-strings', 'proposal-optional-catch-binding', 'transform-parameters', 'proposal-async-generator-functions', 'proposal-object-rest-spread', 'proposal-export-namespace-from', ] def self.plugin_transpile_paths @@plugin_transpile_paths ||= Set.new end def self.ember_cli?(filename) filename.include?("/app/assets/javascripts/discourse/dist/") end 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 if !Rails.env.production? && !ember_cli?(input[:filename]) plugin_name = root_path[/\/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 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 ||= '' # 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(/^\/*/, '') 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/") return true 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 if plugin_transpile_paths.any? { |prefix| relative_path.start_with?(prefix) } !!(relative_path =~ /^#{js_root}\/[^\/]+\// || relative_path =~ /^#{test_root}\/[^\/]+\//) 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, ensure_gc_after_idle: 2000) ctx.eval("#{File.read("#{Rails.root}/app/assets/javascripts/node_modules/@babel/standalone/babel.js")}") ctx.eval(File.read(Ember::Source.bundled_path_for('ember-template-compiler.js'))) ctx.eval("module = {}; exports = {};") ctx.eval("const DISCOURSE_COMMON_BABEL_PLUGINS = #{DISCOURSE_COMMON_BABEL_PLUGINS.to_json};") 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 <