FEATURE: split JavaScript application bundle, so plugins live in own file

This adds plugin.js and plugin_third_party.js files
This commit is contained in:
Sam 2016-11-15 11:42:55 +11:00
parent 67edb6ce5c
commit f4c754b389
11 changed files with 141 additions and 23 deletions

View File

@ -1,17 +0,0 @@
<%
require_asset ("./main_include.js")
# Include plugin javascripts/handlebars templates
DiscoursePluginRegistry.javascripts.each { |js| require_asset(js) }
DiscoursePluginRegistry.handlebars.each { |hb| require_asset(hb) }
DiscoursePluginRegistry.each_globbed_asset do |f, ext|
if File.directory?(f)
depend_on(f)
elsif f.to_s.end_with?(".#{ext}")
require_asset(f)
end
end
%>

View File

@ -0,0 +1,15 @@
<%
# Include plugin javascripts/handlebars templates
Discourse.unofficial_plugins.each do |plugin|
plugin.javascript_includes.each { |js| require_asset(js) }
plugin.handlebars_includes.each { |hb| require_asset(hb) }
plugin.each_globbed_asset do |f, is_dir|
if is_dir
depend_on(f)
else
require_asset(f)
end
end
end
%>

View File

@ -0,0 +1,16 @@
<%
# Include plugin javascripts/handlebars templates
Discourse.official_plugins.each do |plugin|
plugin.javascript_includes.each { |js| require_asset(js) }
plugin.handlebars_includes.each { |hb| require_asset(hb) }
plugin.each_globbed_asset do |f, is_dir|
if is_dir
depend_on(f)
else
require_asset(f)
end
end
end
%>

View File

@ -28,6 +28,8 @@
<%= script "vendor" %> <%= script "vendor" %>
<%= script "pretty-text-bundle" %> <%= script "pretty-text-bundle" %>
<%= script "application" %> <%= script "application" %>
<%= script "plugin" %>
<%= script "plugin-third-party" %>
<%- if staff? %> <%- if staff? %>
<script src="<%= Discourse.base_uri %>/extra-locales/admin"></script> <script src="<%= Discourse.base_uri %>/extra-locales/admin"></script>

View File

@ -73,7 +73,7 @@ module Discourse
'admin.js', 'admin.css', 'shiny/shiny.css', 'preload-store.js.es6', 'admin.js', 'admin.css', 'shiny/shiny.css', 'preload-store.js.es6',
'browser-update.js', 'embed.css', 'break_string.js', 'ember_jquery.js', 'browser-update.js', 'embed.css', 'break_string.js', 'ember_jquery.js',
'pretty-text-bundle.js', 'wizard.css', 'wizard-application.js', 'pretty-text-bundle.js', 'wizard.css', 'wizard-application.js',
'wizard-vendor.js'] 'wizard-vendor.js', 'plugin.js', 'plugin-third-party.js']
# Precompile all available locales # Precompile all available locales
Dir.glob("#{config.root}/app/assets/javascripts/locales/*.js.erb").each do |file| Dir.glob("#{config.root}/app/assets/javascripts/locales/*.js.erb").each do |file|

View File

@ -97,6 +97,7 @@ module Discourse
set set
end end
def self.activate_plugins! def self.activate_plugins!
all_plugins = Plugin::Instance.find_all("#{Rails.root}/plugins") all_plugins = Plugin::Instance.find_all("#{Rails.root}/plugins")
@ -138,6 +139,14 @@ module Discourse
@plugins ||= [] @plugins ||= []
end end
def self.official_plugins
plugins.find_all{|p| p.metadata.official?}
end
def self.unofficial_plugins
plugins.find_all{|p| !p.metadata.official?}
end
def self.assets_digest def self.assets_digest
@assets_digest ||= begin @assets_digest ||= begin
digest = Digest::MD5.hexdigest(ActionView::Base.assets_manifest.assets.values.sort.join) digest = Digest::MD5.hexdigest(ActionView::Base.assets_manifest.assets.values.sort.join)

View File

@ -16,6 +16,10 @@ class DiscoursePluginRegistry
attr_accessor :custom_html attr_accessor :custom_html
def plugins
@plugins ||= []
end
# Default accessor values # Default accessor values
def javascripts def javascripts
@javascripts ||= Set.new @javascripts ||= Set.new
@ -93,8 +97,11 @@ class DiscoursePluginRegistry
end end
end end
JS_REGEX = /\.js$|\.js\.erb$|\.js\.es6$/
HANDLEBARS_REGEX = /\.hbs$|\.js\.handlebars$/
def self.register_asset(asset, opts=nil) def self.register_asset(asset, opts=nil)
if asset =~ /\.js$|\.js\.erb$|\.js\.es6$/ if asset =~ JS_REGEX
if opts == :admin if opts == :admin
self.admin_javascripts << asset self.admin_javascripts << asset
else else
@ -111,9 +118,7 @@ class DiscoursePluginRegistry
self.stylesheets << asset self.stylesheets << asset
end end
elsif asset =~ /\.hbs$/ elsif asset =~ HANDLEBARS_REGEX
self.handlebars << asset
elsif asset =~ /\.js\.handlebars$/
self.handlebars << asset self.handlebars << asset
end end
end end

View File

@ -393,6 +393,37 @@ JS
end end
end end
def handlebars_includes
assets.map do |asset, opts|
next if opts == :admin
next unless asset =~ DiscoursePluginRegistry::HANDLEBARS_REGEX
asset
end.compact
end
def javascript_includes
assets.map do |asset, opts|
next if opts == :admin
next unless asset =~ DiscoursePluginRegistry::JS_REGEX
asset
end.compact
end
def each_globbed_asset
if @path
# Automatically include all ES6 JS and hbs files
root_path = "#{File.dirname(@path)}/assets/javascripts"
Dir.glob("#{root_path}/**/*") do |f|
if File.directory?(f)
yield [f,true]
elsif f.to_s.ends_with?(".js.es6") || f.to_s.ends_with?(".hbs")
yield [f,false]
end
end
end
end
protected protected
def register_assets! def register_assets!

View File

@ -2,8 +2,34 @@
module Plugin; end module Plugin; end
class Plugin::Metadata class Plugin::Metadata
OFFICIAL_PLUGINS = Set.new([
"customer-flair",
"discourse-adplugin",
"discourse-akismet",
"discourse-backup-uploads-to-s3",
"discourse-cakeday",
"Canned Replies",
"discourse-data-explorer",
"discourse-details",
"discourse-nginx-performance-report",
"discourse-push-notifications",
"discourse-slack-official",
"discourse-solved",
"Spoiler Alert!",
"staff-notes",
"GitHub badges",
"hosted-site",
"lazyYT",
"logster-rate-limit-checker",
"poll",
"discourse-plugin-linkedin-auth",
"discourse-plugin-office365-auth",
"discourse-oauth2-basic"
])
FIELDS ||= [:name, :about, :version, :authors, :url, :required_version] FIELDS ||= [:name, :about, :version, :authors, :url, :required_version]
attr_accessor *FIELDS attr_accessor(*FIELDS)
def self.parse(text) def self.parse(text)
metadata = self.new metadata = self.new
@ -13,6 +39,10 @@ class Plugin::Metadata
metadata metadata
end end
def official?
OFFICIAL_PLUGINS.include?(name)
end
def parse_line(line) def parse_line(line)
line = line.strip line = line.strip

View File

@ -24,4 +24,31 @@ TEXT
end end
end end
def official(name)
metadata = Plugin::Metadata.parse <<TEXT
# name: #{name}
TEXT
expect(metadata.official?).to eq(true)
end
def unofficial(name)
metadata = Plugin::Metadata.parse <<TEXT
# name: #{name}
TEXT
expect(metadata.official?).to eq(false)
end
it "correctly detects official vs unofficial plugins" do
official("customer-flair")
official("discourse-adplugin")
official("discourse-akismet")
official("discourse-backup-uploads-to-s3")
official("discourse-cakeday")
official("Canned Replies")
official("discourse-data-explorer")
unofficial("babble")
end
end end