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 "pretty-text-bundle" %>
<%= script "application" %>
<%= script "plugin" %>
<%= script "plugin-third-party" %>
<%- if staff? %>
<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',
'browser-update.js', 'embed.css', 'break_string.js', 'ember_jquery.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
Dir.glob("#{config.root}/app/assets/javascripts/locales/*.js.erb").each do |file|

View File

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

View File

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

View File

@ -393,6 +393,37 @@ JS
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
def register_assets!

View File

@ -2,8 +2,34 @@
module Plugin; end
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]
attr_accessor *FIELDS
attr_accessor(*FIELDS)
def self.parse(text)
metadata = self.new
@ -13,6 +39,10 @@ class Plugin::Metadata
metadata
end
def official?
OFFICIAL_PLUGINS.include?(name)
end
def parse_line(line)
line = line.strip

View File

@ -24,4 +24,31 @@ TEXT
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