2014-05-05 18:04:09 -04:00
|
|
|
require 'execjs'
|
2015-03-05 13:08:32 -05:00
|
|
|
require 'babel/transpiler'
|
2016-05-19 08:25:08 -04:00
|
|
|
require 'mini_racer'
|
2014-05-05 18:04:09 -04:00
|
|
|
|
|
|
|
module Tilt
|
2015-03-05 13:08:32 -05:00
|
|
|
|
2014-05-05 18:04:09 -04: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
|
2016-04-21 19:52:12 -04:00
|
|
|
# timeout any eval that takes longer than 15 seconds
|
2016-05-19 08:25:08 -04:00
|
|
|
ctx = MiniRacer::Context.new(timeout: 15000)
|
2015-03-05 13:08:32 -05:00
|
|
|
ctx.eval("var self = this; #{File.read(Babel::Transpiler.script_path)}")
|
2014-05-05 18:04:09 -04:00
|
|
|
ctx.eval("module = {}; exports = {};");
|
|
|
|
ctx.load("#{Rails.root}/lib/es6_module_transpiler/support/es6-module-transpiler.js")
|
2016-05-19 08:25:08 -04:00
|
|
|
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 <<JS
|
|
|
|
console = {
|
|
|
|
prefix: "",
|
|
|
|
log: function(msg){ rails.logger.info(console.prefix + msg); },
|
|
|
|
error: function(msg){ rails.logger.error(console.prefix + msg); }
|
|
|
|
}
|
|
|
|
JS
|
2014-05-05 18:04:09 -04:00
|
|
|
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
|
|
|
|
@mutex.synchronize do
|
2016-05-19 08:25:08 -04:00
|
|
|
yield
|
2014-05-05 18:04:09 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-19 08:29:56 -05: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 20:27:06 -05:00
|
|
|
"discourse/models/topic",
|
|
|
|
"discourse/models/post",
|
2015-11-19 08:29:56 -05:00
|
|
|
"discourse/views/grouped"]
|
|
|
|
)
|
|
|
|
|
|
|
|
@@whitelisted.include?(path) || path =~ /discourse\/mixins/
|
|
|
|
end
|
|
|
|
|
2016-03-18 14:41:27 -04:00
|
|
|
def babel_transpile(source)
|
|
|
|
klass = self.class
|
|
|
|
klass.protect do
|
2016-05-19 08:25:08 -04:00
|
|
|
klass.v8.eval("console.prefix = 'BABEL: babel-eval: ';")
|
2016-03-18 14:41:27 -04:00
|
|
|
@output = klass.v8.eval(babel_source(source))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-05-05 18:04:09 -04:00
|
|
|
def evaluate(scope, locals, &block)
|
|
|
|
return @output if @output
|
|
|
|
|
|
|
|
klass = self.class
|
|
|
|
klass.protect do
|
2016-05-19 08:25:08 -04:00
|
|
|
klass.v8.eval("console.prefix = 'BABEL: #{scope.logical_path}: ';")
|
2014-05-05 18:04:09 -04:00
|
|
|
@output = klass.v8.eval(generate_source(scope))
|
|
|
|
end
|
2014-05-12 15:53:22 -04: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 18:58:01 -05:00
|
|
|
if whitelisted?(scope.logical_path) &&
|
2015-02-03 13:14:05 -05:00
|
|
|
scope.logical_path =~ /(discourse|admin)\/(controllers|components|views|routes|mixins|models)\/(.*)/
|
|
|
|
|
2014-07-25 13:02:46 -04:00
|
|
|
type = Regexp.last_match[2]
|
|
|
|
file_name = Regexp.last_match[3].gsub(/[\-\/]/, '_')
|
2014-05-20 11:13:43 -04: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 15:22:11 -04:00
|
|
|
require_name = module_name(scope.root_path, scope.logical_path)
|
2014-08-06 18:07:27 -04:00
|
|
|
|
2015-02-03 13:14:05 -05:00
|
|
|
if require_name !~ /\-test$/ && require_name !~ /^discourse\/plugins\//
|
2014-08-12 19:04:36 -04:00
|
|
|
result = "#{class_name}#{type.classify}"
|
|
|
|
|
|
|
|
# HAX
|
|
|
|
result = "Controller" if result == "ControllerController"
|
2015-03-10 15:01:15 -04:00
|
|
|
result = "Route" if result == "DiscourseRoute"
|
2015-04-08 14:17:21 -04:00
|
|
|
result = "View" if result == "ViewView"
|
2015-07-07 15:18:19 -04:00
|
|
|
|
2014-08-12 19:04:36 -04:00
|
|
|
result.gsub!(/Mixin$/, '')
|
2014-09-25 11:32:08 -04:00
|
|
|
result.gsub!(/Model$/, '')
|
2014-08-12 19:04:36 -04:00
|
|
|
|
2015-07-07 15:18:19 -04:00
|
|
|
if result != "PostMenuView"
|
|
|
|
@output << "\n\nDiscourse.#{result} = require('#{require_name}').default;\n"
|
|
|
|
end
|
2014-08-06 18:07:27 -04:00
|
|
|
end
|
2014-05-12 15:53:22 -04:00
|
|
|
end
|
|
|
|
|
2014-05-05 18:04:09 -04:00
|
|
|
@output
|
|
|
|
end
|
|
|
|
|
2016-03-18 14:41:27 -04:00
|
|
|
def babel_source(source)
|
|
|
|
js_source = ::JSON.generate(source, quirks_mode: true)
|
|
|
|
"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']"
|
|
|
|
end
|
|
|
|
|
2014-05-05 18:04:09 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def generate_source(scope)
|
2016-03-18 14:41:27 -04:00
|
|
|
js_source = babel_source(data)
|
2015-02-06 13:25:48 -05:00
|
|
|
"new module.exports.Compiler(#{js_source}, '#{module_name(scope.root_path, scope.logical_path)}', #{compiler_options}).#{compiler_method}()"
|
2014-05-05 18:04:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def module_name(root_path, logical_path)
|
2014-05-15 16:31:45 -04:00
|
|
|
path = nil
|
|
|
|
|
2014-05-20 16:54:59 -04:00
|
|
|
root_base = File.basename(Rails.root)
|
2014-05-15 16:31:45 -04:00
|
|
|
# If the resource is a plugin, use the plugin name as a prefix
|
2014-05-20 16:54:59 -04:00
|
|
|
if root_path =~ /(.*\/#{root_base}\/plugins\/[^\/]+)\//
|
2014-05-15 16:31:45 -04: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 18:04:09 -04:00
|
|
|
end
|
|
|
|
|
2014-05-15 16:31:45 -04:00
|
|
|
path ||= logical_path
|
2014-05-05 18:04:09 -04: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
|