Skip to content

A platform for community discussion. Free, open, simple.

DEV: Improve plugin js loading logic (#38278)

David Taylord77a85155b64
diff --git a/app/views/layouts/_plugin_js.html.erb b/app/views/layouts/_plugin_js.html.erb
index d9aacaa1e73..301d72cc90e 100644
--- a/app/views/layouts/_plugin_js.html.erb
+++ b/app/views/layouts/_plugin_js.html.erb
@@ -11,7 +11,6 @@
rel="modulepreload"
href="<%= script_asset_path(asset[:name]) %>"
nonce="<%= csp_nonce_placeholder %>"
- data-plugin-name="<%= asset[:plugin].directory_name %>"
<%= tag.attributes **asset[:plugin_attributes] %>
>
<% else %>
diff --git a/frontend/discourse/app/app.js b/frontend/discourse/app/app.js
index 3129ee88abb..da05289858c 100644
--- a/frontend/discourse/app/app.js
+++ b/frontend/discourse/app/app.js
@@ -71,12 +71,12 @@ async function loadPluginFromModulePreload(link) {
);
if (DEBUG) {
- let { addError } = importSync("discourse/static/development-error");
- addError(error, link.dataset.pluginName, link.href);
-
if (isRailsTesting() || isTesting()) {
throw new Error(error);
}
+
+ let { addError } = importSync("discourse/static/development-error");
+ addError(error, link.dataset.pluginName, link.href);
}
}
}
diff --git a/lib/discourse.rb b/lib/discourse.rb
index 66ce2269b3c..129453d1c1b 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -522,6 +522,7 @@ module Discourse
asset[:plugin_attributes] = {
"data-official": !!asset[:plugin].metadata&.official?,
"data-preinstalled": asset[:plugin].preinstalled?,
+ "data-plugin-name": asset[:plugin].directory_name,
}
end
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb
index fe0d6489fcc..4de61e08a6b 100644
--- a/lib/plugin/instance.rb
+++ b/lib/plugin/instance.rb
@@ -963,8 +963,7 @@ class Plugin::Instance
end
def js_asset_exists?
- # If assets/javascripts exists, ember-cli will output a .js file
- File.exist?("#{File.dirname(@path)}/assets/javascripts")
+ Plugin::JsManager.js_asset_exists?(directory_name)
end
def extra_js_asset_exists?
@@ -972,11 +971,11 @@ class Plugin::Instance
end
def admin_js_asset_exists?
- File.exist?("#{File.dirname(@path)}/admin/assets/javascripts")
+ Plugin::JsManager.admin_js_asset_exists?(directory_name)
end
def test_js_asset_exists?
- File.exist?("#{File.dirname(@path)}/test/javascripts")
+ Plugin::JsManager.test_js_asset_exists?(directory_name)
end
# Receives an array with two elements:
diff --git a/lib/plugin/js_manager.rb b/lib/plugin/js_manager.rb
index b58e01f7309..12fe506593c 100644
--- a/lib/plugin/js_manager.rb
+++ b/lib/plugin/js_manager.rb
@@ -2,20 +2,32 @@
module Plugin
class JsManager
- @manifest_data = {}
+ @cache = {}
- def self.read_manifest(plugin_name)
- manifest_path = "#{Rails.root}/app/assets/generated/#{plugin_name}/manifest.json"
- JSON.parse(File.read(manifest_path))
- rescue Errno::ENOENT
- {}
+ def self.js_asset_exists?(plugin_name)
+ maybe_cache("js_asset_exists_#{plugin_name}") do
+ has_source_files_in_dir(plugin_name, "assets/javascripts")
+ end
end
- def self.parsed_manifest(plugin_name)
- if Rails.env.production?
- @manifest_data[plugin_name] ||= read_manifest(plugin_name)
- else
- read_manifest(plugin_name)
+ def self.admin_js_asset_exists?(plugin_name)
+ maybe_cache("admin_js_asset_exists_#{plugin_name}") do
+ has_source_files_in_dir(plugin_name, "admin/assets/javascripts")
+ end
+ end
+
+ def self.test_js_asset_exists?(plugin_name)
+ maybe_cache("test_js_asset_exists_#{plugin_name}") do
+ has_source_files_in_dir(plugin_name, "test/javascripts")
+ end
+ end
+
+ def self.read_manifest(plugin_name)
+ maybe_cache("manifest_#{plugin_name}") do
+ manifest_path = "#{Rails.root}/app/assets/generated/#{plugin_name}/manifest.json"
+ JSON.parse(File.read(manifest_path))
+ rescue Errno::ENOENT
+ {}
end
end
@@ -181,6 +193,8 @@ module Plugin
end
end
+ private
+
def minify?
Rails.env.production?
end
@@ -192,5 +206,17 @@ module Plugin
def log(message)
STDERR.puts "[Plugin::JsManager] #{message}"
end
+
+ private_class_method def self.maybe_cache(key, &blk)
+ if Rails.env.production?
+ @cache.fetch(key, &blk)
+ else
+ blk.call
+ end
+ end
+
+ private_class_method def self.has_source_files_in_dir(plugin_name, dir)
+ Dir.glob("plugins/#{plugin_name}/#{dir}/**/*.{js,hbs,gjs,es6}") { break true } || false
+ end
end
end
diff --git a/spec/support/shared_examples/core_features.rb b/spec/support/shared_examples/core_features.rb
index bf6b7f3186f..3776ffe44f4 100644
--- a/spec/support/shared_examples/core_features.rb
+++ b/spec/support/shared_examples/core_features.rb
@@ -214,4 +214,30 @@ RSpec.shared_examples_for "having working core features" do |skip_examples: []|
end
end
end
+
+ describe "plugin javascript assets" do
+ it "has expected javascript asset" do
+ enabled_plugins = Discourse.plugins.filter(&:enabled?)
+
+ visit "/"
+ expect(page).to have_css("#site-logo")
+
+ plugin_script_tags =
+ page
+ .all(
+ "script[data-plugin-name], link[rel=modulepreload][data-plugin-name]",
+ visible: :all,
+ minimum: 0,
+ )
+ .map { |tag| tag["data-plugin-name"] }
+
+ enabled_plugins.each do |plugin|
+ if plugin.js_asset_exists?
+ expect(plugin_script_tags).to include(plugin.name)
+ else
+ expect(plugin_script_tags).not_to include(plugin.name)
+ end
+ end
+ end
+ end
end