2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
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)
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
result = new(filename) { source }.render(context)
|
2017-04-17 10:11:51 -04:00
|
|
|
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")}")
|
2017-06-29 16:22:19 -04:00
|
|
|
ctx.eval(File.read(Ember::Source.bundled_path_for('ember-template-compiler.js')))
|
2018-12-03 22:48:13 -05:00
|
|
|
ctx.eval("module = {}; exports = {};")
|
2017-07-27 21:20:09 -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) })
|
2016-05-19 08:25:08 -04:00
|
|
|
ctx.eval <<JS
|
|
|
|
console = {
|
|
|
|
prefix: "",
|
|
|
|
log: function(msg){ rails.logger.info(console.prefix + msg); },
|
|
|
|
error: function(msg){ rails.logger.error(console.prefix + msg); }
|
|
|
|
}
|
2017-06-29 16:22:19 -04:00
|
|
|
|
2016-05-19 08:25:08 -04:00
|
|
|
JS
|
2017-06-29 16:22:19 -04:00
|
|
|
source = File.read("#{Rails.root}/lib/javascripts/widget-hbs-compiler.js.es6")
|
|
|
|
js_source = ::JSON.generate(source, quirks_mode: true)
|
|
|
|
js = ctx.eval("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'] }).code")
|
|
|
|
ctx.eval(js)
|
|
|
|
|
2014-05-05 18:04:09 -04:00
|
|
|
ctx
|
|
|
|
end
|
|
|
|
|
2016-11-01 22:34:20 -04:00
|
|
|
def self.reset_context
|
2017-07-20 00:17:45 -04:00
|
|
|
@ctx&.dispose
|
2016-11-01 22:34:20 -04:00
|
|
|
@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
|
|
|
|
|
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-06-29 16:22:19 -04:00
|
|
|
transpiled = babel_source(
|
|
|
|
source,
|
|
|
|
module_name: module_name(root_path, logical_path),
|
|
|
|
filename: logical_path
|
|
|
|
)
|
2017-07-05 14:14:30 -04:00
|
|
|
@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-06-29 16:22:19 -04:00
|
|
|
|
|
|
|
source = babel_source(
|
|
|
|
data,
|
|
|
|
module_name: module_name(scope.root_path, scope.logical_path),
|
|
|
|
filename: scope.logical_path
|
|
|
|
)
|
|
|
|
|
|
|
|
@output = klass.v8.eval(source)
|
2014-05-05 18:04:09 -04:00
|
|
|
end
|
2014-05-12 15:53:22 -04:00
|
|
|
|
2014-05-05 18:04:09 -04:00
|
|
|
@output
|
|
|
|
end
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
def babel_source(source, opts = nil)
|
2017-07-05 14:14:30 -04:00
|
|
|
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
|
|
|
|
2018-10-08 21:18:23 -04:00
|
|
|
if opts[:module_name] && transpile_into_module?
|
2017-06-29 16:22:19 -04:00
|
|
|
filename = opts[:filename] || 'unknown'
|
|
|
|
"Babel.transform(#{js_source}, { moduleId: '#{opts[:module_name]}', filename: '#{filename}', ast: false, presets: ['es2015'], plugins: [['transform-es2015-modules-amd', {noInterop: true}], 'transform-decorators-legacy', exports.WidgetHbsCompiler] }).code"
|
2017-07-05 14:14:30 -04:00
|
|
|
else
|
2017-06-29 16:22:19 -04:00
|
|
|
"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', exports.WidgetHbsCompiler] }).code"
|
2017-07-05 14:14:30 -04:00
|
|
|
end
|
2016-03-18 14:41:27 -04:00
|
|
|
end
|
|
|
|
|
2014-05-05 18:04:09 -04:00
|
|
|
private
|
|
|
|
|
2018-10-08 21:18:23 -04:00
|
|
|
def transpile_into_module?
|
|
|
|
file.nil? || file.exclude?('.no-module')
|
|
|
|
end
|
|
|
|
|
2014-05-05 18:04:09 -04:00
|
|
|
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"
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
plugin = Discourse.plugins.find { |p| p.path == plugin_path }
|
2014-05-15 16:31:45 -04:00
|
|
|
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
|