diff --git a/app/assets/javascripts/application.js.erb b/app/assets/javascripts/application.js.erb index 89ec70313..64a1f7dd3 100644 --- a/app/assets/javascripts/application.js.erb +++ b/app/assets/javascripts/application.js.erb @@ -6,4 +6,16 @@ require_asset ("./main_include.js") DiscoursePluginRegistry.javascripts.each { |js| require_asset(js) } DiscoursePluginRegistry.handlebars.each { |hb| require_asset(hb) } +# Load any glob dependencies +DiscoursePluginRegistry.asset_globs.each do |g| + root, extension = *g + Dir.glob("#{root}/**/*") do |f| + if File.directory?(f) + depend_on(f) + elsif f.to_s.end_with?(".#{extension}") + require_asset(f) + end + end +end + %> diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rb index 4b0402eae..af605b277 100644 --- a/lib/discourse_plugin_registry.rb +++ b/lib/discourse_plugin_registry.rb @@ -4,23 +4,27 @@ class DiscoursePluginRegistry class << self - attr_accessor :javascripts - attr_accessor :server_side_javascripts - attr_accessor :admin_javascripts - attr_accessor :stylesheets - attr_accessor :mobile_stylesheets - attr_accessor :desktop_stylesheets - attr_accessor :sass_variables - attr_accessor :handlebars - attr_accessor :custom_html - attr_accessor :serialized_current_user_fields + attr_writer :javascripts + attr_writer :server_side_javascripts + attr_writer :admin_javascripts + attr_writer :stylesheets + attr_writer :mobile_stylesheets + attr_writer :desktop_stylesheets + attr_writer :sass_variables + attr_writer :handlebars + attr_writer :serialized_current_user_fields + attr_accessor :custom_html # Default accessor values def javascripts @javascripts ||= Set.new end + def asset_globs + @asset_globs ||= Set.new + end + def admin_javascripts @admin_javascripts ||= Set.new end @@ -68,6 +72,10 @@ class DiscoursePluginRegistry Archetype.register(name, options) end + def self.register_glob(root, extension) + self.asset_globs << [root, extension] + end + def self.register_asset(asset, opts=nil) if asset =~ /\.js$|\.js\.erb$|\.js\.es6$/ if opts == :admin @@ -143,6 +151,7 @@ class DiscoursePluginRegistry desktop_stylesheets.clear sass_variables.clear serialized_current_user_fields + asset_globs.clear end def self.setup(plugin_class) diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb index c33d04058..2e2311941 100644 --- a/lib/plugin/instance.rb +++ b/lib/plugin/instance.rb @@ -26,19 +26,10 @@ class Plugin::Instance @color_schemes = [] if @path - - # Automatically include all ES6 JS files - dir = File.dirname(@path) - Dir.glob("#{dir}/assets/javascripts/**/*.js.es6") do |f| - relative = f.sub("#{dir}/assets/", "") - register_asset(relative) - end - - # Automatically include all hbs files - Dir.glob("#{dir}/assets/javascripts/**/*.hbs") do |f| - relative = f.sub("#{dir}/assets/", "") - register_asset(relative) - end + # Automatically include all ES6 JS and hbs files + root_path = "#{File.dirname(@path)}/assets/javascripts" + DiscoursePluginRegistry.register_glob(root_path, 'js.es6') + DiscoursePluginRegistry.register_glob(root_path, 'hbs') end end