2014-05-05 18:04:09 -04:00
|
|
|
require 'execjs'
|
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
|
|
|
|
|
2017-04-17 10:11:51 -04:00
|
|
|
def self.call(input)
|
|
|
|
filename = input[:filename]
|
|
|
|
source = input[:data]
|
|
|
|
context = input[:environment].context_class.new(input)
|
|
|
|
|
|
|
|
result = new(filename){source}.render(context)
|
|
|
|
context.metadata.merge(data: result)
|
|
|
|
end
|
|
|
|
|
2014-05-05 18:04:09 -04:00
|
|
|
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)
|
2017-07-04 16:34:55 -04:00
|
|
|
ctx.eval("var self = this; #{File.read("#{Rails.root}/vendor/assets/javascripts/babel.js")}")
|
2014-05-05 18:04:09 -04:00
|
|
|
ctx.eval("module = {}; exports = {};");
|
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
|
|
|
|
|
2016-11-01 22:34:20 -04:00
|
|
|
def self.reset_context
|
|
|
|
@ctx = nil
|
|
|
|
end
|
|
|
|
|
2014-05-05 18:04:09 -04:00
|
|
|
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
|
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
def module_transpile(source, root_path, logical_path)
|
|
|
|
klass = self.class
|
|
|
|
klass.protect do
|
|
|
|
klass.v8.eval("console.prefix = 'BABEL: babel-eval: ';")
|
2017-07-05 14:14:30 -04:00
|
|
|
transpiled = babel_source(source, module_name: module_name(root_path, logical_path))
|
|
|
|
@output = klass.v8.eval(transpiled)
|
2016-06-14 14:31:51 -04:00
|
|
|
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}: ';")
|
2017-07-05 14:14:30 -04:00
|
|
|
@output = klass.v8.eval(babel_source(data, module_name: module_name(scope.root_path, scope.logical_path)))
|
2014-05-05 18:04:09 -04:00
|
|
|
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
|
|
|
|
|
2017-07-05 14:14:30 -04:00
|
|
|
def babel_source(source, opts=nil)
|
|
|
|
|
|
|
|
opts ||= {}
|
|
|
|
|
2016-03-18 14:41:27 -04:00
|
|
|
js_source = ::JSON.generate(source, quirks_mode: true)
|
2017-07-05 14:14:30 -04:00
|
|
|
|
|
|
|
if opts[:module_name]
|
|
|
|
"Babel.transform(#{js_source}, { moduleId: '#{opts[:module_name]}', ast: false, presets: ['es2015'], plugins: [['transform-es2015-modules-amd', {noInterop: true}], 'transform-decorators-legacy'] }).code"
|
|
|
|
else
|
|
|
|
"Babel.transform(#{js_source}, { ast: false, plugins: ['check-es2015-constants', 'transform-es2015-arrow-functions', 'transform-es2015-block-scoped-functions', 'transform-es2015-block-scoping', 'transform-es2015-classes', 'transform-es2015-computed-properties', 'transform-es2015-destructuring', 'transform-es2015-duplicate-keys', 'transform-es2015-for-of', 'transform-es2015-function-name', 'transform-es2015-literals', 'transform-es2015-object-super', 'transform-es2015-parameters', 'transform-es2015-shorthand-properties', 'transform-es2015-spread', 'transform-es2015-sticky-regex', 'transform-es2015-template-literals', 'transform-es2015-typeof-symbol', 'transform-es2015-unicode-regex', 'transform-regenerator', 'transform-decorators-legacy'] }).code"
|
|
|
|
end
|
2016-03-18 14:41:27 -04:00
|
|
|
end
|
|
|
|
|
2014-05-05 18:04:09 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
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
|