2014-05-05 17:04:09 -05:00
|
|
|
require 'execjs'
|
2015-03-05 12:08:32 -06:00
|
|
|
require 'babel/transpiler'
|
2014-05-05 17:04:09 -05:00
|
|
|
|
|
|
|
module Tilt
|
2015-03-05 12:08:32 -06:00
|
|
|
|
|
|
|
class Console
|
|
|
|
def initialize(prefix=nil)
|
|
|
|
@prefix = prefix || ''
|
|
|
|
end
|
|
|
|
|
|
|
|
def log(msg)
|
|
|
|
Rails.logger.info("#{@prefix}#{msg}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def error(msg)
|
|
|
|
Rails.logger.error("#{@prefix}#{msg}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-05 17:04:09 -05:00
|
|
|
class ES6ModuleTranspilerTemplate < Tilt::Template
|
|
|
|
self.default_mime_type = 'application/javascript'
|
|
|
|
|
|
|
|
@mutex = Mutex.new
|
|
|
|
@ctx_init = Mutex.new
|
|
|
|
|
|
|
|
def prepare
|
|
|
|
# intentionally left empty
|
|
|
|
# Tilt requires this method to be defined
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.create_new_context
|
|
|
|
ctx = V8::Context.new(timeout: 5000)
|
2015-03-05 12:08:32 -06:00
|
|
|
ctx.eval("var self = this; #{File.read(Babel::Transpiler.script_path)}")
|
2014-05-05 17:04:09 -05:00
|
|
|
ctx.eval("module = {}; exports = {};");
|
|
|
|
ctx.load("#{Rails.root}/lib/es6_module_transpiler/support/es6-module-transpiler.js")
|
|
|
|
ctx
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.v8
|
|
|
|
return @ctx if @ctx
|
|
|
|
|
|
|
|
# ensure we only init one of these
|
|
|
|
@ctx_init.synchronize do
|
|
|
|
return @ctx if @ctx
|
|
|
|
@ctx = create_new_context
|
|
|
|
end
|
|
|
|
|
|
|
|
@ctx
|
|
|
|
end
|
|
|
|
|
|
|
|
class JavaScriptError < StandardError
|
|
|
|
attr_accessor :message, :backtrace
|
|
|
|
|
|
|
|
def initialize(message, backtrace)
|
|
|
|
@message = message
|
|
|
|
@backtrace = backtrace
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.protect
|
|
|
|
rval = nil
|
|
|
|
@mutex.synchronize do
|
|
|
|
begin
|
|
|
|
rval = yield
|
|
|
|
# This may seem a bit odd, but we don't want to leak out
|
|
|
|
# objects that require locks on the v8 vm, to get a backtrace
|
|
|
|
# you need a lock, if this happens in the wrong spot you can
|
|
|
|
# deadlock a process
|
|
|
|
rescue V8::Error => e
|
|
|
|
raise JavaScriptError.new(e.message, e.backtrace)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rval
|
|
|
|
end
|
|
|
|
|
2015-11-19 07:29:56 -06:00
|
|
|
def whitelisted?(path)
|
|
|
|
|
|
|
|
@@whitelisted ||= Set.new(
|
|
|
|
["discourse/models/nav-item",
|
|
|
|
"discourse/models/user-action",
|
|
|
|
"discourse/routes/discourse",
|
|
|
|
"discourse/models/category",
|
|
|
|
"discourse/models/trust-level",
|
|
|
|
"discourse/models/site",
|
|
|
|
"discourse/models/user",
|
|
|
|
"discourse/models/session",
|
|
|
|
"discourse/models/model",
|
2015-11-20 19:27:06 -06:00
|
|
|
"discourse/models/topic",
|
|
|
|
"discourse/models/post",
|
2015-11-19 07:29:56 -06:00
|
|
|
"discourse/views/grouped"]
|
|
|
|
)
|
|
|
|
|
|
|
|
@@whitelisted.include?(path) || path =~ /discourse\/mixins/
|
|
|
|
end
|
|
|
|
|
2014-05-05 17:04:09 -05:00
|
|
|
def evaluate(scope, locals, &block)
|
|
|
|
return @output if @output
|
|
|
|
|
|
|
|
klass = self.class
|
|
|
|
klass.protect do
|
2015-03-05 12:08:32 -06:00
|
|
|
klass.v8['console'] = Console.new("BABEL: #{scope.logical_path}: ")
|
2014-05-05 17:04:09 -05:00
|
|
|
@output = klass.v8.eval(generate_source(scope))
|
|
|
|
end
|
2014-05-12 14:53:22 -05:00
|
|
|
|
|
|
|
# For backwards compatibility with plugins, for now export the Global format too.
|
|
|
|
# We should eventually have an upgrade system for plugins to use ES6 or some other
|
|
|
|
# resolve based API.
|
2015-11-30 17:58:01 -06:00
|
|
|
if whitelisted?(scope.logical_path) &&
|
2015-02-03 12:14:05 -06:00
|
|
|
scope.logical_path =~ /(discourse|admin)\/(controllers|components|views|routes|mixins|models)\/(.*)/
|
|
|
|
|
2014-07-25 12:02:46 -05:00
|
|
|
type = Regexp.last_match[2]
|
|
|
|
file_name = Regexp.last_match[3].gsub(/[\-\/]/, '_')
|
2014-05-20 10:13:43 -05:00
|
|
|
class_name = file_name.classify
|
|
|
|
|
|
|
|
# Rails removes pluralization when calling classify
|
|
|
|
if file_name.end_with?('s') && (!class_name.end_with?('s'))
|
|
|
|
class_name << "s"
|
|
|
|
end
|
2014-06-09 14:22:11 -05:00
|
|
|
require_name = module_name(scope.root_path, scope.logical_path)
|
2014-08-06 17:07:27 -05:00
|
|
|
|
2015-02-03 12:14:05 -06:00
|
|
|
if require_name !~ /\-test$/ && require_name !~ /^discourse\/plugins\//
|
2014-08-12 18:04:36 -05:00
|
|
|
result = "#{class_name}#{type.classify}"
|
|
|
|
|
|
|
|
# HAX
|
|
|
|
result = "Controller" if result == "ControllerController"
|
2015-03-10 14:01:15 -05:00
|
|
|
result = "Route" if result == "DiscourseRoute"
|
2015-04-08 13:17:21 -05:00
|
|
|
result = "View" if result == "ViewView"
|
2015-07-07 14:18:19 -05:00
|
|
|
|
2014-08-12 18:04:36 -05:00
|
|
|
result.gsub!(/Mixin$/, '')
|
2014-09-25 10:32:08 -05:00
|
|
|
result.gsub!(/Model$/, '')
|
2014-08-12 18:04:36 -05:00
|
|
|
|
2015-07-07 14:18:19 -05:00
|
|
|
if result != "PostMenuView"
|
|
|
|
@output << "\n\nDiscourse.#{result} = require('#{require_name}').default;\n"
|
|
|
|
end
|
2014-08-06 17:07:27 -05:00
|
|
|
end
|
2014-05-12 14:53:22 -05:00
|
|
|
end
|
|
|
|
|
2014-05-05 17:04:09 -05:00
|
|
|
@output
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def generate_source(scope)
|
2015-02-06 12:25:48 -06:00
|
|
|
js_source = ::JSON.generate(data, quirks_mode: true)
|
2016-01-04 14:18:09 -06:00
|
|
|
js_source = "babel.transform(#{js_source}, {ast: false, whitelist: ['es6.constants', 'es6.properties.shorthand', 'es6.arrowFunctions', 'es6.blockScoping', 'es6.destructuring', 'es6.spread', 'es6.parameters', 'es6.templateLiterals', 'es6.regex.unicode', 'es7.decorators', 'es6.classes']})['code']"
|
2015-02-06 12:25:48 -06:00
|
|
|
"new module.exports.Compiler(#{js_source}, '#{module_name(scope.root_path, scope.logical_path)}', #{compiler_options}).#{compiler_method}()"
|
2014-05-05 17:04:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def module_name(root_path, logical_path)
|
2014-05-15 15:31:45 -05:00
|
|
|
path = nil
|
|
|
|
|
2014-05-20 15:54:59 -05:00
|
|
|
root_base = File.basename(Rails.root)
|
2014-05-15 15:31:45 -05:00
|
|
|
# If the resource is a plugin, use the plugin name as a prefix
|
2014-05-20 15:54:59 -05:00
|
|
|
if root_path =~ /(.*\/#{root_base}\/plugins\/[^\/]+)\//
|
2014-05-15 15:31:45 -05:00
|
|
|
plugin_path = "#{Regexp.last_match[1]}/plugin.rb"
|
|
|
|
|
|
|
|
plugin = Discourse.plugins.find {|p| p.path == plugin_path }
|
|
|
|
path = "discourse/plugins/#{plugin.name}/#{logical_path.sub(/javascripts\//, '')}" if plugin
|
2014-05-05 17:04:09 -05:00
|
|
|
end
|
|
|
|
|
2014-05-15 15:31:45 -05:00
|
|
|
path ||= logical_path
|
2014-05-05 17:04:09 -05:00
|
|
|
if ES6ModuleTranspiler.transform
|
|
|
|
path = ES6ModuleTranspiler.transform.call(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
path
|
|
|
|
end
|
|
|
|
|
|
|
|
def compiler_method
|
|
|
|
type = {
|
|
|
|
amd: 'AMD',
|
|
|
|
cjs: 'CJS',
|
|
|
|
globals: 'Globals'
|
|
|
|
}[ES6ModuleTranspiler.compile_to.to_sym]
|
|
|
|
|
|
|
|
"to#{type}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def compiler_options
|
|
|
|
::JSON.generate(ES6ModuleTranspiler.compiler_options, quirks_mode: true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|