DEV: debundle plugin css assets and don't load if disabled (#7646)

This commit is contained in:
Vinoth Kannan
2019-08-20 22:09:52 +05:30
committed by GitHub
parent 36425eb9f0
commit 5bd6b70d98
14 changed files with 76 additions and 52 deletions

View File

@ -25,6 +25,4 @@
/* These files doesn't actually exist, they are injected by Stylesheet::Compiler. */ /* These files doesn't actually exist, they are injected by Stylesheet::Compiler. */
@import "plugins";
@import "plugins_desktop";
@import "category_backgrounds"; @import "category_backgrounds";

View File

@ -38,6 +38,4 @@
/* These files doesn't actually exist, they are injected by Stylesheet::Compiler. */ /* These files doesn't actually exist, they are injected by Stylesheet::Compiler. */
@import "plugins";
@import "plugins_mobile";
@import "category_backgrounds"; @import "category_backgrounds";

View File

@ -11,3 +11,7 @@
<%- if theme_ids.present? %> <%- if theme_ids.present? %>
<%= discourse_stylesheet_link_tag(mobile_view? ? :mobile_theme : :desktop_theme) %> <%= discourse_stylesheet_link_tag(mobile_view? ? :mobile_theme : :desktop_theme) %>
<%- end %> <%- 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 %>

View File

@ -214,10 +214,16 @@ module Discourse
end end
end end
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) def self.find_plugin_js_assets(args)
self.find_plugins(args).find_all do |plugin| self.find_plugins(args).find_all do |plugin|
plugin.js_asset_exists? plugin.js_asset_exists?
end.map { |plugin| "plugins/#{plugin.asset_name}" } end.map { |plugin| "plugins/#{plugin.directory_name}" }
end end
def self.assets_digest def self.assets_digest

View File

@ -36,8 +36,8 @@ class DiscoursePlugin
@registry.register_js(file, opts) @registry.register_js(file, opts)
end end
def register_css(file) def register_css(file, plugin_directory_name)
@registry.register_css(file) @registry.register_css(file, plugin_directory_name)
end end
def register_archetype(name, options = {}) def register_archetype(name, options = {})

View File

@ -47,15 +47,15 @@ class DiscoursePluginRegistry
end end
def stylesheets def stylesheets
@stylesheets ||= Set.new @stylesheets ||= Hash.new
end end
def mobile_stylesheets def mobile_stylesheets
@mobile_stylesheets ||= Set.new @mobile_stylesheets ||= Hash.new
end end
def desktop_stylesheets def desktop_stylesheets
@desktop_stylesheets ||= Set.new @desktop_stylesheets ||= Hash.new
end end
def sass_variables def sass_variables
@ -116,8 +116,9 @@ class DiscoursePluginRegistry
self.svg_icons << icon self.svg_icons << icon
end end
def register_css(filename) def register_css(filename, plugin_directory_name)
self.class.stylesheets << filename self.class.stylesheets[plugin_directory_name] ||= Set.new
self.class.stylesheets[plugin_directory_name] << filename
end end
def self.register_locale(locale, options = {}) def self.register_locale(locale, options = {})
@ -153,7 +154,7 @@ class DiscoursePluginRegistry
JS_REGEX = /\.js$|\.js\.erb$|\.js\.es6|\.js\.no-module\.es6$/ JS_REGEX = /\.js$|\.js\.erb$|\.js\.es6|\.js\.no-module\.es6$/
HANDLEBARS_REGEX = /\.hbs$|\.js\.handlebars$/ 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_REGEX if asset =~ JS_REGEX
if opts == :admin if opts == :admin
self.admin_javascripts << asset self.admin_javascripts << asset
@ -166,19 +167,26 @@ class DiscoursePluginRegistry
end end
elsif asset =~ /\.css$|\.scss$/ elsif asset =~ /\.css$|\.scss$/
if opts == :mobile if opts == :mobile
self.mobile_stylesheets << asset self.mobile_stylesheets[plugin_directory_name] ||= Set.new
self.mobile_stylesheets[plugin_directory_name] << asset
elsif opts == :desktop elsif opts == :desktop
self.desktop_stylesheets << asset self.desktop_stylesheets[plugin_directory_name] ||= Set.new
self.desktop_stylesheets[plugin_directory_name] << asset
elsif opts == :variables elsif opts == :variables
self.sass_variables << asset self.sass_variables << asset
else else
self.stylesheets << asset self.stylesheets[plugin_directory_name] ||= Set.new
self.stylesheets[plugin_directory_name] << asset
end end
elsif asset =~ HANDLEBARS_REGEX elsif asset =~ HANDLEBARS_REGEX
self.handlebars << asset self.handlebars << asset
end end
end end
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) def self.register_seed_data(key, value)
self.seed_data[key] = value self.seed_data[key] = value
end end

View File

@ -431,7 +431,7 @@ class Plugin::Instance
full_path = File.dirname(path) << "/assets/" << file full_path = File.dirname(path) << "/assets/" << file
end end
assets << [full_path, opts] assets << [full_path, opts, directory_name]
end end
def register_service_worker(file, opts = nil) def register_service_worker(file, opts = nil)
@ -654,8 +654,12 @@ class Plugin::Instance
end end
end end
def asset_name def directory_name
@asset_name ||= File.dirname(path).split("/").last @directory_name ||= File.dirname(path).split("/").last
end
def css_asset_exists?
DiscoursePluginRegistry.stylesheets_exists?(directory_name)
end end
def js_asset_exists? def js_asset_exists?
@ -669,12 +673,12 @@ class Plugin::Instance
end end
def js_file_path def js_file_path
@file_path ||= "#{Plugin::Instance.js_path}/#{asset_name}.js.erb" @file_path ||= "#{Plugin::Instance.js_path}/#{directory_name}.js.erb"
end end
def register_assets! def register_assets!
assets.each do |asset, opts| assets.each do |asset, opts, plugin_directory_name|
DiscoursePluginRegistry.register_asset(asset, opts) DiscoursePluginRegistry.register_asset(asset, opts, plugin_directory_name)
end end
end end

View File

@ -12,7 +12,7 @@ module Stylesheet
if Importer.special_imports[asset.to_s] if Importer.special_imports[asset.to_s]
filename = "theme_#{options[:theme_id]}.scss" 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}\";"
else else
filename = "#{asset}.scss" filename = "#{asset}.scss"
path = "#{ASSET_ROOT}/#{filename}" path = "#{ASSET_ROOT}/#{filename}"

View File

@ -19,16 +19,19 @@ module Stylesheet
Import.new("#{theme_dir(@theme_id)}/theme_field.scss", source: @theme_field) Import.new("#{theme_dir(@theme_id)}/theme_field.scss", source: @theme_field)
end end
register_import "plugins" do Discourse.plugins.each do |plugin|
import_files(DiscoursePluginRegistry.stylesheets) plugin_directory_name = plugin.directory_name
end
register_import "plugins_mobile" do ["", "mobile", "desktop"].each do |type|
import_files(DiscoursePluginRegistry.mobile_stylesheets) asset_name = type.present? ? "#{plugin_directory_name}_#{type}" : plugin_directory_name
end stylesheets = type.present? ? DiscoursePluginRegistry.send("#{type}_stylesheets") : DiscoursePluginRegistry.stylesheets
register_import "plugins_desktop" do if stylesheets[plugin_directory_name].present?
import_files(DiscoursePluginRegistry.desktop_stylesheets) register_import asset_name do
import_files(stylesheets[plugin_directory_name])
end
end
end
end end
register_import "plugins_variables" do register_import "plugins_variables" do

View File

@ -294,9 +294,9 @@ class Stylesheet::Manager
# so we could end up poisoning the cache with a bad file that can not be removed # so we could end up poisoning the cache with a bad file that can not be removed
def plugins_digest def plugins_digest
assets = [] assets = []
assets += DiscoursePluginRegistry.stylesheets.to_a DiscoursePluginRegistry.stylesheets.each { |_, paths| assets += paths.to_a }
assets += DiscoursePluginRegistry.mobile_stylesheets.to_a DiscoursePluginRegistry.mobile_stylesheets.each { |_, paths| assets += paths.to_a }
assets += DiscoursePluginRegistry.desktop_stylesheets.to_a DiscoursePluginRegistry.desktop_stylesheets.each { |_, paths| assets += paths.to_a }
Digest::SHA1.hexdigest(assets.sort.join) Digest::SHA1.hexdigest(assets.sort.join)
end end

View File

@ -13,14 +13,14 @@ describe DiscoursePluginRegistry do
context '#stylesheets' do context '#stylesheets' do
it 'defaults to an empty Set' do it 'defaults to an empty Set' do
registry.stylesheets = nil registry.stylesheets = nil
expect(registry.stylesheets).to eq(Set.new) expect(registry.stylesheets).to eq(Hash.new)
end end
end end
context '#mobile_stylesheets' do context '#mobile_stylesheets' do
it 'defaults to an empty Set' do it 'defaults to an empty Set' do
registry.mobile_stylesheets = nil registry.mobile_stylesheets = nil
expect(registry.mobile_stylesheets).to eq(Set.new) expect(registry.mobile_stylesheets).to eq(Hash.new)
end end
end end
@ -70,20 +70,22 @@ describe DiscoursePluginRegistry do
end end
context '.register_css' do context '.register_css' do
let(:plugin_directory_name) { "hello" }
before do before do
registry_instance.register_css('hello.css') registry_instance.register_css('hello.css', plugin_directory_name)
end end
it 'is not leaking' do it 'is not leaking' do
expect(DiscoursePluginRegistry.new.stylesheets).to be_blank expect(DiscoursePluginRegistry.new.stylesheets[plugin_directory_name]).to be_nil
end end
it 'is returned by DiscoursePluginRegistry.stylesheets' do it '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)
end end
it "won't add the same file twice" do it "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)
end end
end end
@ -157,11 +159,12 @@ describe DiscoursePluginRegistry do
end end
it "does register general css properly" do it "does register general css properly" do
registry.register_asset("test.css") plugin_directory_name = "my_plugin"
registry.register_asset("test2.css") 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.mobile_stylesheets[plugin_directory_name]).to be_nil
expect(registry.stylesheets.count).to eq(2) expect(registry.stylesheets[plugin_directory_name].count).to eq(2)
end end
it "registers desktop css properly" do it "registers desktop css properly" do

View File

@ -28,8 +28,8 @@ describe DiscoursePlugin do
end end
it "delegates adding css to the registry" do it "delegates adding css to the registry" do
registry.expects(:register_css).with('test.css') registry.expects(:register_css).with('test.css', 'test')
plugin.register_css('test.css') plugin.register_css('test.css', 'test')
end end
it "delegates creating archetypes" do it "delegates creating archetypes" do

View File

@ -94,8 +94,8 @@ describe Plugin::Instance do
plugin.send :register_assets! plugin.send :register_assets!
expect(DiscoursePluginRegistry.mobile_stylesheets.count).to eq(0) expect(DiscoursePluginRegistry.mobile_stylesheets[plugin.directory_name]).to be_nil
expect(DiscoursePluginRegistry.stylesheets.count).to eq(2) expect(DiscoursePluginRegistry.stylesheets[plugin.directory_name].count).to eq(2)
end end
it "remaps vendored_core_pretty_text asset" do it "remaps vendored_core_pretty_text asset" do
@ -220,10 +220,10 @@ describe Plugin::Instance do
expect(DiscoursePluginRegistry.javascripts.count).to eq(2) expect(DiscoursePluginRegistry.javascripts.count).to eq(2)
expect(DiscoursePluginRegistry.admin_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.sass_variables.count).to eq(2)
expect(DiscoursePluginRegistry.stylesheets.count).to eq(2) expect(DiscoursePluginRegistry.stylesheets[plugin.directory_name].count).to eq(2)
expect(DiscoursePluginRegistry.mobile_stylesheets.count).to eq(1) expect(DiscoursePluginRegistry.mobile_stylesheets[plugin.directory_name].count).to eq(1)
end end
end end

View File

@ -77,7 +77,7 @@ describe Stylesheet::Manager do
manager = Stylesheet::Manager.new(:desktop_theme, theme.id) manager = Stylesheet::Manager.new(:desktop_theme, theme.id)
digest1 = manager.digest digest1 = manager.digest
DiscoursePluginRegistry.stylesheets.add "fake_file" DiscoursePluginRegistry.stylesheets["fake"] = Set.new(["fake_file"])
manager = Stylesheet::Manager.new(:desktop_theme, theme.id) manager = Stylesheet::Manager.new(:desktop_theme, theme.id)
digest2 = manager.digest digest2 = manager.digest