REFACTOR: Do not rely on module order for discourse-markdown loading

REFACTOR: Do not rely on module order for discourse-markdown loading

Before this patch, discourse-markdown depended on the modules in its bundle being defined in a specific order or it wouldn’t load properly.

Now, any file in the bundle can export a priority const (default 0) and files will be loaded in order of ascending priority instead. This allows us to use a bundle packaged in any order we want.

diff --git a/app/assets/javascripts/markdown-it-bundle.js b/app/assets/javascripts/markdown-it-bundle.js
index 052891a..0980765 100644
--- a/app/assets/javascripts/markdown-it-bundle.js
+++ b/app/assets/javascripts/markdown-it-bundle.js
@@ -1,20 +1,2 @@
 //= require markdown-it.js
-//= require ./pretty-text/engines/discourse-markdown/helpers
-//= require ./pretty-text/engines/discourse-markdown/mentions
-//= require ./pretty-text/engines/discourse-markdown/quotes
-//= require ./pretty-text/engines/discourse-markdown/emoji
-//= require ./pretty-text/engines/discourse-markdown/onebox
-//= require ./pretty-text/engines/discourse-markdown/bbcode-block
-//= require ./pretty-text/engines/discourse-markdown/bbcode-inline
-//= require ./pretty-text/engines/discourse-markdown/code
-//= require ./pretty-text/engines/discourse-markdown/category-hashtag
-//= require ./pretty-text/engines/discourse-markdown/censored
-//= require ./pretty-text/engines/discourse-markdown/table
-//= require ./pretty-text/engines/discourse-markdown/paragraph
-//= require ./pretty-text/engines/discourse-markdown/newline
-//= require ./pretty-text/engines/discourse-markdown/html-img
-//= require ./pretty-text/engines/discourse-markdown/text-post-process
-//= require ./pretty-text/engines/discourse-markdown/upload-protocol
-//= require ./pretty-text/engines/discourse-markdown/inject-line-number
-//= require ./pretty-text/engines/discourse-markdown/resize-controls
-//= require ./pretty-text/engines/discourse-markdown/d-wrap
+//= require_tree ./pretty-text/engines/discourse-markdown
diff --git a/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown-it.js b/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown-it.js
index a685a31..94e471f 100644
--- a/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown-it.js
+++ b/app/assets/javascripts/pretty-text/addon/engines/discourse-markdown-it.js
@@ -291,22 +291,28 @@ export function setup(opts, siteSettings, state) {
     if (check.test(entry)) {
       const module = requirejs(entry);
       if (module && module.setup) {
-        const featureName = entry.split("/").reverse()[0];
-        features.push(featureName);
-        module.setup(
-          createHelper(
-            featureName,
-            opts,
-            optionCallbacks,
-            pluginCallbacks,
-            getOptions,
-            whiteListed
-          )
-        );
+        const id = entry.split("/").reverse()[0];
+        let priority = module.priority || 0;
+        features.unshift({ id, setup: module.setup, priority });
       }
     }
   });
 
+  features
+    .sort((a, b) => a.priority - b.priority)
+    .forEach((f) => {
+      f.setup(
+        createHelper(
+          f.id,
+          opts,
+          optionCallbacks,
+          pluginCallbacks,
+          getOptions,
+          whiteListed
+        )
+      );
+    });
+
   Object.entries(state.whiteListed || {}).forEach((entry) => {
     whiteListed.push(entry);
   });
@@ -317,8 +323,8 @@ export function setup(opts, siteSettings, state) {
 
   // enable all features by default
   features.forEach((feature) => {
-    if (!opts.features.hasOwnProperty(feature)) {
-      opts.features[feature] = true;
+    if (!opts.features.hasOwnProperty(feature.id)) {
+      opts.features[feature.id] = true;
     }
   });
 
diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/resize-controls.js b/app/assets/javascripts/pretty-text/engines/discourse-markdown/resize-controls.js
index 7059169..0fa31d1 100644
--- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/resize-controls.js
+++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/resize-controls.js
@@ -61,6 +61,9 @@ function buildScaleButton(selectedScale, scale) {
   );
 }
 
+// We need this to load after `upload-protocol` which is priority 0
+export const priority = 1;
+
 export function setup(helper) {
   const opts = helper.getOptions();
   if (opts.previewing) {

GitHub sha: ba3014f9

1 Like