Skip to content

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

FIX: Load admin-specific JS when compiling via ember-cli (#18086)

David Taylor9ebebfb4ccb8
diff --git a/app/assets/javascripts/discourse-plugins/index.js b/app/assets/javascripts/discourse-plugins/index.js
index d2bff2ae3e8..0c719a9cafd 100644
--- a/app/assets/javascripts/discourse-plugins/index.js
+++ b/app/assets/javascripts/discourse-plugins/index.js
@@ -78,41 +78,77 @@ module.exports = {
return pluginDirectories.map((directory) => {
const name = directory.name;
const jsDirectory = path.resolve(root, name, "assets/javascripts");
+ const adminJsDirectory = path.resolve(
+ root,
+ name,
+ "admin/assets/javascripts"
+ );
const testDirectory = path.resolve(root, name, "test/javascripts");
const hasJs = fs.existsSync(jsDirectory);
+ const hasAdminJs = fs.existsSync(adminJsDirectory);
const hasTests = fs.existsSync(testDirectory);
- return { name, jsDirectory, testDirectory, hasJs, hasTests };
+ return {
+ name,
+ jsDirectory,
+ adminJsDirectory,
+ testDirectory,
+ hasJs,
+ hasAdminJs,
+ hasTests,
+ };
});
},
generatePluginsTree() {
const appTree = this._generatePluginAppTree();
const testTree = this._generatePluginTestTree();
- return mergeTrees([appTree, testTree]);
+ const adminTree = this._generatePluginAdminTree();
+ return mergeTrees([appTree, testTree, adminTree]);
},
_generatePluginAppTree() {
const trees = this.pluginInfos()
.filter((p) => p.hasJs)
- .map(({ name, jsDirectory }) => {
- let tree = new WatchedDir(jsDirectory);
+ .map(({ name, jsDirectory }) =>
+ this._buildAppTree({
+ directory: jsDirectory,
+ pluginName: name,
+ outputFile: `assets/plugins/${name}.js`,
+ })
+ );
+ return mergeTrees(trees);
+ },
- tree = fixLegacyExtensions(tree);
- tree = unColocateConnectors(tree);
- tree = namespaceModules(tree, name);
+ _generatePluginAdminTree() {
+ const trees = this.pluginInfos()
+ .filter((p) => p.hasAdminJs)
+ .map(({ name, adminJsDirectory }) =>
+ this._buildAppTree({
+ directory: adminJsDirectory,
+ pluginName: name,
+ outputFile: `assets/plugins/${name}_admin.js`,
+ })
+ );
+ return mergeTrees(trees);
+ },
- tree = RawHandlebarsCompiler(tree);
- tree = this.compileTemplates(tree);
+ _buildAppTree({ directory, pluginName, outputFile }) {
+ let tree = new WatchedDir(directory);
- tree = this.processedAddonJsFiles(tree);
+ tree = fixLegacyExtensions(tree);
+ tree = unColocateConnectors(tree);
+ tree = namespaceModules(tree, pluginName);
- return concat(mergeTrees([tree]), {
- inputFiles: ["**/*.js"],
- outputFile: `assets/plugins/${name}.js`,
- allowNone: true,
- });
- });
- return mergeTrees(trees);
+ tree = RawHandlebarsCompiler(tree);
+ tree = this.compileTemplates(tree);
+
+ tree = this.processedAddonJsFiles(tree);
+
+ return concat(mergeTrees([tree]), {
+ inputFiles: ["**/*.js"],
+ outputFile,
+ allowNone: true,
+ });
},
_generatePluginTestTree() {
diff --git a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js
index 38d273fd5da..a95655cbf37 100644
--- a/app/assets/javascripts/discourse/lib/bootstrap-json/index.js
+++ b/app/assets/javascripts/discourse/lib/bootstrap-json/index.js
@@ -386,7 +386,7 @@ module.exports = {
.findAddonByName("discourse-plugins")
.pluginInfos();
- for (const { name, hasJs } of pluginInfos) {
+ for (const { name, hasJs, hasAdminJs } of pluginInfos) {
if (hasJs) {
scripts.push({ src: `plugins/${name}.js`, name });
}
@@ -394,16 +394,19 @@ module.exports = {
if (fs.existsSync(`../plugins/${name}_extras.js.erb`)) {
scripts.push({ src: `plugins/${name}_extras.js`, name });
}
+
+ if (hasAdminJs) {
+ scripts.push({ src: `plugins/${name}_admin.js`, name });
+ }
}
} else {
scripts.push({
src: "discourse/tests/active-plugins.js",
name: "_all",
});
+ scripts.push({ src: "admin-plugins.js", name: "_admin" });
}
- scripts.push({ src: "admin-plugins.js", name: "_admin" });
-
return scripts
.map(
({ src, name }) =>
diff --git a/lib/discourse.rb b/lib/discourse.rb
index 526f6b8ee35..3abaafaa7b3 100644
--- a/lib/discourse.rb
+++ b/lib/discourse.rb
@@ -382,7 +382,7 @@ module Discourse
def self.find_plugin_js_assets(args)
plugins = self.find_plugins(args).select do |plugin|
- plugin.js_asset_exists? || plugin.extra_js_asset_exists?
+ plugin.js_asset_exists? || plugin.extra_js_asset_exists? || plugin.admin_js_asset_exists?
end
plugins = apply_asset_filters(plugins, :js, args[:request])
@@ -391,6 +391,8 @@ module Discourse
assets = []
assets << "plugins/#{plugin.directory_name}" if plugin.js_asset_exists?
assets << "plugins/#{plugin.directory_name}_extra" if plugin.extra_js_asset_exists?
+ # TODO: make admin asset only load for admins
+ assets << "plugins/#{plugin.directory_name}_admin" if plugin.admin_js_asset_exists?
assets
end
end
diff --git a/lib/plugin/instance.rb b/lib/plugin/instance.rb
index 05d3685b296..011a7423f02 100644
--- a/lib/plugin/instance.rb
+++ b/lib/plugin/instance.rb
@@ -862,6 +862,15 @@ class Plugin::Instance
EmberCli.plugin_assets? && File.exist?(extra_js_file_path)
end
+ def admin_js_asset_exists?
+ if EmberCli.plugin_assets?
+ # If this directory exists, ember-cli will output a .js file
+ File.exist?("#{File.dirname(@path)}/admin/assets/javascripts")
+ else
+ false
+ end
+ end
+
# Receives an array with two elements:
# 1. A symbol that represents the name of the value to filter.
# 2. A Proc that takes the existing ActiveRecord::Relation and the value received from the front-end.