mirror of
https://github.com/discourse/discourse.git
synced 2025-05-23 23:31:18 +08:00
DEV: Debundle plugin javascript assets and don't load if disabled (#7566)
And don't load javascript assets if plugin is disabled. * precompile auto generated plugin js assets * SPEC: remove spec test functions * remove plugin js from test_helper Co-Authored-By: Régis Hanol <regis@hanol.fr> * DEV: using equality is slightly easier to read than inequality Co-Authored-By: Régis Hanol <regis@hanol.fr> * DEV: use `select` method instead of `find_all` for readability Co-Authored-By: Régis Hanol <regis@hanol.fr>
This commit is contained in:
@ -529,6 +529,24 @@ class Plugin::Instance
|
||||
Discourse::Utils.execute_command('rm', '-f', target)
|
||||
Discourse::Utils.execute_command('ln', '-s', public_data, target)
|
||||
end
|
||||
|
||||
ensure_directory(Plugin::Instance.js_path)
|
||||
|
||||
contents = []
|
||||
handlebars_includes.each { |hb| contents << "require_asset('#{hb}')" }
|
||||
javascript_includes.each { |js| contents << "require_asset('#{js}')" }
|
||||
|
||||
each_globbed_asset do |f, is_dir|
|
||||
contents << (is_dir ? "depend_on('#{f}')" : "require_asset('#{f}')")
|
||||
end
|
||||
|
||||
File.delete(js_file_path) if js_asset_exists?
|
||||
|
||||
if contents.present?
|
||||
contents.insert(0, "<%")
|
||||
contents << "%>"
|
||||
write_asset(js_file_path, contents.join("\n"))
|
||||
end
|
||||
end
|
||||
|
||||
def auth_provider(opts)
|
||||
@ -629,8 +647,24 @@ class Plugin::Instance
|
||||
end
|
||||
end
|
||||
|
||||
def asset_name
|
||||
@asset_name ||= File.dirname(path).split("/").last
|
||||
end
|
||||
|
||||
def js_asset_exists?
|
||||
File.exists?(js_file_path)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def self.js_path
|
||||
File.expand_path "#{Rails.root}/app/assets/javascripts/plugins"
|
||||
end
|
||||
|
||||
def js_file_path
|
||||
@file_path ||= "#{Plugin::Instance.js_path}/#{asset_name}.js.erb"
|
||||
end
|
||||
|
||||
def register_assets!
|
||||
assets.each do |asset, opts|
|
||||
DiscoursePluginRegistry.register_asset(asset, opts)
|
||||
|
Reference in New Issue
Block a user