A platform for community discussion. Free, open, simple.
DEV: debundle plugin css assets and don't load if disabled (#7646)
Vinoth Kannan5bd6b70d985e
diff --git a/app/assets/stylesheets/desktop.scss b/app/assets/stylesheets/desktop.scssindex 76012fc9c09..70229afbbc1 100644--- a/app/assets/stylesheets/desktop.scss+++ b/app/assets/stylesheets/desktop.scss@@ -25,6 +25,4 @@/* These files doesn't actually exist, they are injected by Stylesheet::Compiler. */-@import "plugins";-@import "plugins_desktop";@import "category_backgrounds";diff --git a/app/assets/stylesheets/mobile.scss b/app/assets/stylesheets/mobile.scssindex 244c90102a9..a2158fe44a5 100644--- a/app/assets/stylesheets/mobile.scss+++ b/app/assets/stylesheets/mobile.scss@@ -38,6 +38,4 @@/* These files doesn't actually exist, they are injected by Stylesheet::Compiler. */-@import "plugins";-@import "plugins_mobile";@import "category_backgrounds";diff --git a/app/views/common/_discourse_stylesheet.html.erb b/app/views/common/_discourse_stylesheet.html.erbindex 45f6476e904..d16cb7cb4e7 100644--- a/app/views/common/_discourse_stylesheet.html.erb+++ b/app/views/common/_discourse_stylesheet.html.erb@@ -11,3 +11,7 @@<%- if theme_ids.present? %><%= discourse_stylesheet_link_tag(mobile_view? ? :mobile_theme : :desktop_theme) %><%- end %>++<%- Discourse.find_plugin_css_assets(include_official: allow_plugins?, include_unofficial: allow_third_party_plugins?).each do |file| %>+ <%= discourse_stylesheet_link_tag(file) %>+<%- end %>diff --git a/lib/discourse.rb b/lib/discourse.rbindex b7cf9a91c05..fb19a776f1e 100644--- a/lib/discourse.rb+++ b/lib/discourse.rb@@ -214,10 +214,16 @@ module Discourseendend+ def self.find_plugin_css_assets(args)+ self.find_plugins(args).find_all do |plugin|+ plugin.css_asset_exists?+ end.map { |plugin| plugin.directory_name }+ end+def self.find_plugin_js_assets(args)self.find_plugins(args).find_all do |plugin|plugin.js_asset_exists?- end.map { |plugin| "plugins/#{plugin.asset_name}" }+ end.map { |plugin| "plugins/#{plugin.directory_name}" }enddef self.assets_digestdiff --git a/lib/discourse_plugin.rb b/lib/discourse_plugin.rbindex 990df5b4e05..5d687e0788f 100644--- a/lib/discourse_plugin.rb+++ b/lib/discourse_plugin.rb@@ -36,8 +36,8 @@ class DiscoursePlugin@registry.register_js(file, opts)end- def register_css(file)- @registry.register_css(file)+ def register_css(file, plugin_directory_name)+ @registry.register_css(file, plugin_directory_name)enddef register_archetype(name, options = {})diff --git a/lib/discourse_plugin_registry.rb b/lib/discourse_plugin_registry.rbindex a8cefe0ae9d..f8111ed71be 100644--- a/lib/discourse_plugin_registry.rb+++ b/lib/discourse_plugin_registry.rb@@ -47,15 +47,15 @@ class DiscoursePluginRegistryenddef stylesheets- @stylesheets ||= Set.new+ @stylesheets ||= Hash.newenddef mobile_stylesheets- @mobile_stylesheets ||= Set.new+ @mobile_stylesheets ||= Hash.newenddef desktop_stylesheets- @desktop_stylesheets ||= Set.new+ @desktop_stylesheets ||= Hash.newenddef sass_variables@@ -116,8 +116,9 @@ class DiscoursePluginRegistryself.svg_icons << iconend- def register_css(filename)- self.class.stylesheets << filename+ def register_css(filename, plugin_directory_name)+ self.class.stylesheets[plugin_directory_name] ||= Set.new+ self.class.stylesheets[plugin_directory_name] << filenameenddef self.register_locale(locale, options = {})@@ -153,7 +154,7 @@ class DiscoursePluginRegistryJS_REGEX = /\.js$|\.js\.erb$|\.js\.es6|\.js\.no-module\.es6$/HANDLEBARS_REGEX = /\.hbs$|\.js\.handlebars$/- def self.register_asset(asset, opts = nil)+ def self.register_asset(asset, opts = nil, plugin_directory_name = nil)if asset =~ JS_REGEXif opts == :adminself.admin_javascripts << asset@@ -166,19 +167,26 @@ class DiscoursePluginRegistryendelsif asset =~ /\.css$|\.scss$/if opts == :mobile- self.mobile_stylesheets << asset+ self.mobile_stylesheets[plugin_directory_name] ||= Set.new+ self.mobile_stylesheets[plugin_directory_name] << assetelsif opts == :desktop- self.desktop_stylesheets << asset+ self.desktop_stylesheets[plugin_directory_name] ||= Set.new+ self.desktop_stylesheets[plugin_directory_name] << assetelsif opts == :variablesself.sass_variables << assetelse- self.stylesheets << asset+ self.stylesheets[plugin_directory_name] ||= Set.new+ self.stylesheets[plugin_directory_name] << assetendelsif asset =~ HANDLEBARS_REGEXself.handlebars << assetendend+ def self.stylesheets_exists?(plugin_directory_name)+ self.stylesheets[plugin_directory_name].present? || self.mobile_stylesheets[plugin_directory_name].present? || self.desktop_stylesheets[plugin_directory_name].present?+ end+def self.register_seed_data(key, value)self.seed_data[key] = valueenddiff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rbindex 67695e15a06..b8158fa7575 100644--- a/lib/plugin/instance.rb+++ b/lib/plugin/instance.rb@@ -431,7 +431,7 @@ class Plugin::Instancefull_path = File.dirname(path) << "/assets/" << fileend- assets << [full_path, opts]+ assets << [full_path, opts, directory_name]enddef register_service_worker(file, opts = nil)@@ -654,8 +654,12 @@ class Plugin::Instanceendend- def asset_name- @asset_name ||= File.dirname(path).split("/").last+ def directory_name+ @directory_name ||= File.dirname(path).split("/").last+ end++ def css_asset_exists?+ DiscoursePluginRegistry.stylesheets_exists?(directory_name)enddef js_asset_exists?@@ -669,12 +673,12 @@ class Plugin::Instanceenddef js_file_path- @file_path ||= "#{Plugin::Instance.js_path}/#{asset_name}.js.erb"+ @file_path ||= "#{Plugin::Instance.js_path}/#{directory_name}.js.erb"enddef register_assets!- assets.each do |asset, opts|- DiscoursePluginRegistry.register_asset(asset, opts)+ assets.each do |asset, opts, plugin_directory_name|+ DiscoursePluginRegistry.register_asset(asset, opts, plugin_directory_name)endenddiff --git a/lib/stylesheet/compiler.rb b/lib/stylesheet/compiler.rbindex 19925513a38..1dea3780832 100644--- a/lib/stylesheet/compiler.rb+++ b/lib/stylesheet/compiler.rb@@ -12,7 +12,7 @@ module Stylesheetif Importer.special_imports[asset.to_s]filename = "theme_#{options[:theme_id]}.scss"- file = "@import \"common/foundation/variables\"; @import \"theme_variables\"; @import \"#{asset}\";"+ file = "@import \"common/foundation/variables\"; @import \"common/foundation/mixins\"; @import \"theme_variables\"; @import \"#{asset}\";"elsefilename = "#{asset}.scss"path = "#{ASSET_ROOT}/#{filename}"diff --git a/lib/stylesheet/importer.rb b/lib/stylesheet/importer.rbindex 728731e3c59..7258a99978f 100644--- a/lib/stylesheet/importer.rb+++ b/lib/stylesheet/importer.rb@@ -19,16 +19,19 @@ module StylesheetImport.new("#{theme_dir(@theme_id)}/theme_field.scss", source: @theme_field)end- register_import "plugins" do- import_files(DiscoursePluginRegistry.stylesheets)- end+ Discourse.plugins.each do |plugin|+ plugin_directory_name = plugin.directory_name- register_import "plugins_mobile" do- import_files(DiscoursePluginRegistry.mobile_stylesheets)- end+ ["", "mobile", "desktop"].each do |type|+ asset_name = type.present? ? "#{plugin_directory_name}_#{type}" : plugin_directory_name+ stylesheets = type.present? ? DiscoursePluginRegistry.send("#{type}_stylesheets") : DiscoursePluginRegistry.stylesheets- register_import "plugins_desktop" do- import_files(DiscoursePluginRegistry.desktop_stylesheets)+ if stylesheets[plugin_directory_name].present?+ register_import asset_name do+ import_files(stylesheets[plugin_directory_name])+ end+ end+ endendregister_import "plugins_variables" dodiff --git a/lib/stylesheet/manager.rb b/lib/stylesheet/manager.rbindex bd7ccc8b443..2d9a2754ebe 100644--- a/lib/stylesheet/manager.rb+++ b/lib/stylesheet/manager.rb@@ -294,9 +294,9 @@ class Stylesheet::Manager# so we could end up poisoning the cache with a bad file that can not be removeddef plugins_digestassets = []- assets += DiscoursePluginRegistry.stylesheets.to_a- assets += DiscoursePluginRegistry.mobile_stylesheets.to_a- assets += DiscoursePluginRegistry.desktop_stylesheets.to_a+ DiscoursePluginRegistry.stylesheets.each { |_, paths| assets += paths.to_a }+ DiscoursePluginRegistry.mobile_stylesheets.each { |_, paths| assets += paths.to_a }+ DiscoursePluginRegistry.desktop_stylesheets.each { |_, paths| assets += paths.to_a }Digest::SHA1.hexdigest(assets.sort.join)enddiff --git a/spec/components/discourse_plugin_registry_spec.rb b/spec/components/discourse_plugin_registry_spec.rbindex f3c784eb3c2..2dd353e1a6a 100644--- a/spec/components/discourse_plugin_registry_spec.rb+++ b/spec/components/discourse_plugin_registry_spec.rb@@ -13,14 +13,14 @@ describe DiscoursePluginRegistry docontext '#stylesheets' doit 'defaults to an empty Set' doregistry.stylesheets = nil- expect(registry.stylesheets).to eq(Set.new)+ expect(registry.stylesheets).to eq(Hash.new)endendcontext '#mobile_stylesheets' doit 'defaults to an empty Set' doregistry.mobile_stylesheets = nil- expect(registry.mobile_stylesheets).to eq(Set.new)+ expect(registry.mobile_stylesheets).to eq(Hash.new)endend@@ -70,20 +70,22 @@ describe DiscoursePluginRegistry doendcontext '.register_css' do+ let(:plugin_directory_name) { "hello" }+before do- registry_instance.register_css('hello.css')+ registry_instance.register_css('hello.css', plugin_directory_name)endit 'is not leaking' do- expect(DiscoursePluginRegistry.new.stylesheets).to be_blank+ expect(DiscoursePluginRegistry.new.stylesheets[plugin_directory_name]).to be_nilendit 'is returned by DiscoursePluginRegistry.stylesheets' do- expect(registry_instance.stylesheets.include?('hello.css')).to eq(true)+ expect(registry_instance.stylesheets[plugin_directory_name].include?('hello.css')).to eq(true)endit "won't add the same file twice" do- expect { registry_instance.register_css('hello.css') }.not_to change(registry.stylesheets, :size)+ expect { registry_instance.register_css('hello.css', plugin_directory_name) }.not_to change(registry.stylesheets[plugin_directory_name], :size)endend@@ -157,11 +159,12 @@ describe DiscoursePluginRegistry doendit "does register general css properly" do- registry.register_asset("test.css")- registry.register_asset("test2.css")+ plugin_directory_name = "my_plugin"+ registry.register_asset("test.css", nil, plugin_directory_name)+ registry.register_asset("test2.css", nil, plugin_directory_name)- expect(registry.mobile_stylesheets.count).to eq(0)- expect(registry.stylesheets.count).to eq(2)+ expect(registry.mobile_stylesheets[plugin_directory_name]).to be_nil+ expect(registry.stylesheets[plugin_directory_name].count).to eq(2)endit "registers desktop css properly" dodiff --git a/spec/components/discourse_plugin_spec.rb b/spec/components/discourse_plugin_spec.rbindex b91ada89bed..42c4a72cb6e 100644--- a/spec/components/discourse_plugin_spec.rb+++ b/spec/components/discourse_plugin_spec.rb@@ -28,8 +28,8 @@ describe DiscoursePlugin doendit "delegates adding css to the registry" do- registry.expects(:register_css).with('test.css')- plugin.register_css('test.css')+ registry.expects(:register_css).with('test.css', 'test')+ plugin.register_css('test.css', 'test')endit "delegates creating archetypes" dodiff --git a/spec/components/plugin/instance_spec.rb b/spec/components/plugin/instance_spec.rbindex 93692401038..916fef5e8ba 100644--- a/spec/components/plugin/instance_spec.rb+++ b/spec/components/plugin/instance_spec.rb@@ -94,8 +94,8 @@ describe Plugin::Instance doplugin.send :register_assets!- expect(DiscoursePluginRegistry.mobile_stylesheets.count).to eq(0)- expect(DiscoursePluginRegistry.stylesheets.count).to eq(2)+ expect(DiscoursePluginRegistry.mobile_stylesheets[plugin.directory_name]).to be_nil+ expect(DiscoursePluginRegistry.stylesheets[plugin.directory_name].count).to eq(2)endit "remaps vendored_core_pretty_text asset" do@@ -220,10 +220,10 @@ describe Plugin::Instance doexpect(DiscoursePluginRegistry.javascripts.count).to eq(2)expect(DiscoursePluginRegistry.admin_javascripts.count).to eq(2)- expect(DiscoursePluginRegistry.desktop_stylesheets.count).to eq(2)+ expect(DiscoursePluginRegistry.desktop_stylesheets[plugin.directory_name].count).to eq(2)expect(DiscoursePluginRegistry.sass_variables.count).to eq(2)- expect(DiscoursePluginRegistry.stylesheets.count).to eq(2)- expect(DiscoursePluginRegistry.mobile_stylesheets.count).to eq(1)+ expect(DiscoursePluginRegistry.stylesheets[plugin.directory_name].count).to eq(2)+ expect(DiscoursePluginRegistry.mobile_stylesheets[plugin.directory_name].count).to eq(1)endenddiff --git a/spec/components/stylesheet/manager_spec.rb b/spec/components/stylesheet/manager_spec.rbindex e7350712580..e7054f3b7a3 100644--- a/spec/components/stylesheet/manager_spec.rb+++ b/spec/components/stylesheet/manager_spec.rb@@ -77,7 +77,7 @@ describe Stylesheet::Manager domanager = Stylesheet::Manager.new(:desktop_theme, theme.id)digest1 = manager.digest- DiscoursePluginRegistry.stylesheets.add "fake_file"+ DiscoursePluginRegistry.stylesheets["fake"] = Set.new(["fake_file"])manager = Stylesheet::Manager.new(:desktop_theme, theme.id)digest2 = manager.digest