From b4f306ce0332a51386e7bf971e2d904b69db4675 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 18 Mar 2016 14:41:27 -0400 Subject: [PATCH] FEATURE: Site Customizations can use the plugin api --- app/assets/javascripts/discourse.js | 17 ++++++++++ app/models/site_customization.rb | 22 +++++++++++++ .../tilt/es6_module_transpiler_template.rb | 16 +++++++-- spec/models/site_customization_spec.rb | 33 +++++++++++++++++++ 4 files changed, 86 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/discourse.js b/app/assets/javascripts/discourse.js index ab0a429832b..48e0b663d42 100644 --- a/app/assets/javascripts/discourse.js +++ b/app/assets/javascripts/discourse.js @@ -6,6 +6,8 @@ define('ember', ['exports'], function(__exports__) { __exports__.default = Ember; }); +var _pluginCallbacks = []; + window.Discourse = Ember.Application.createWithMixins(Discourse.Ajax, { rootElement: '#main', _docTitle: document.title, @@ -127,6 +129,18 @@ window.Discourse = Ember.Application.createWithMixins(Discourse.Ajax, { } }); + // Plugins that are registered via `") end + doc.css('script[type="text/discourse-plugin"]').each do |node| + if node['version'].present? + begin + code = transpile(node.inner_html, node['version']) + node.replace("") + rescue Tilt::ES6ModuleTranspilerTemplate::JavaScriptError => ex + node.replace("") + end + end + end + doc.to_s end diff --git a/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb b/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb index d54524fe68a..b0d6f1b9172 100644 --- a/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb +++ b/lib/es6_module_transpiler/tilt/es6_module_transpiler_template.rb @@ -94,6 +94,14 @@ module Tilt @@whitelisted.include?(path) || path =~ /discourse\/mixins/ end + def babel_transpile(source) + klass = self.class + klass.protect do + klass.v8['console'] = Console.new("BABEL: babel-eval: ") + @output = klass.v8.eval(babel_source(source)) + end + end + def evaluate(scope, locals, &block) return @output if @output @@ -139,11 +147,15 @@ module Tilt @output end + 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 + private def generate_source(scope) - js_source = ::JSON.generate(data, quirks_mode: true) - 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']" + js_source = babel_source(data) "new module.exports.Compiler(#{js_source}, '#{module_name(scope.root_path, scope.logical_path)}', #{compiler_options}).#{compiler_method}()" end diff --git a/spec/models/site_customization_spec.rb b/spec/models/site_customization_spec.rb index 571e95ddd08..ca87b12bcac 100644 --- a/spec/models/site_customization_spec.rb +++ b/spec/models/site_customization_spec.rb @@ -119,4 +119,37 @@ HTML expect(SiteCustomization.custom_head_tag).to match(/test<\/b>/) end + context "plugin api" do + def transpile(html) + c = SiteCustomization.create!(user_id: -1, name: "test", head_tag: html, body_tag: html) + c.head_tag_baked + end + + it "transpiles ES6 code" do + html = < + const x = 1; + +HTML + + transpiled = transpile(html) + expect(transpiled).to match(/\/) + expect(transpiled).to match(/var x = 1;/) + expect(transpiled).to match(/_registerPluginCode\('0.1'/) + end + + it "converts errors to a script type that is not evaluated" do + html = < + const x = 1; + x = 2; + +HTML + + transpiled = transpile(html) + expect(transpiled).to match(/text\/discourse-js-error/) + expect(transpiled).to match(/read-only/) + end + end + end