DEV - write JS files to versioned subdirectories (#10707)

DEV - write JS files to versioned subdirectories (#10707)

  • DEV - write JS files to versioned subdirectories

  • Cleaner JS in cacheBuster

diff --git a/app/assets/javascripts/discourse/app/lib/load-script.js b/app/assets/javascripts/discourse/app/lib/load-script.js
index c5b87ca..1ac65c9 100644
--- a/app/assets/javascripts/discourse/app/lib/load-script.js
+++ b/app/assets/javascripts/discourse/app/lib/load-script.js
@@ -51,7 +51,7 @@ export default function loadScript(url, opts) {
     return Promise.resolve();
   }
 
-  if (PUBLIC_JS_VERSIONS && !opts.css) {
+  if (PUBLIC_JS_VERSIONS) {
     url = cacheBuster(url);
   }
 
@@ -110,12 +110,12 @@ export default function loadScript(url, opts) {
 
 export function cacheBuster(url) {
   if (PUBLIC_JS_VERSIONS) {
-    // eslint-disable-next-line no-unused-vars
-    const [_, folder, lib] = url.split("/");
+    let [folder, ...lib] = url.split("/").filter(Boolean);
     if (folder === "javascripts") {
-      const version = PUBLIC_JS_VERSIONS[lib];
-      if (version) {
-        return `${url}?v=${version}`;
+      lib = lib.join("/");
+      const versionedPath = PUBLIC_JS_VERSIONS[lib];
+      if (versionedPath) {
+        return `/javascripts/${versionedPath}`;
       }
     }
   }
diff --git a/app/assets/javascripts/discourse/app/lib/public-js-versions.js b/app/assets/javascripts/discourse/app/lib/public-js-versions.js
index 453e040..ba845fe 100644
--- a/app/assets/javascripts/discourse/app/lib/public-js-versions.js
+++ b/app/assets/javascripts/discourse/app/lib/public-js-versions.js
@@ -2,11 +2,13 @@
 // Update it by running `rake javascript:update`
 
 export const PUBLIC_JS_VERSIONS = {
-  ace: "1.4.12",
-  "Chart.min.js": "2.9.3",
-  "chartjs-plugin-datalabels.min.js": "0.7.0",
-  "jquery.magnific-popup.min.js": "1.1.0",
-  "pikaday.js": "1.8.0",
-  "spectrum.js": "1.8.0",
-  workbox: "4.3.1",
+  "ace/ace.js": "ace.js/1.4.12/ace.js",
+  "Chart.min.js": "chart.js/2.9.3/Chart.min.js",
+  "chartjs-plugin-datalabels.min.js":
+    "chartjs-plugin-datalabels/0.7.0/chartjs-plugin-datalabels.min.js",
+  "jquery.magnific-popup.min.js":
+    "magnific-popup/1.1.0/jquery.magnific-popup.min.js",
+  "pikaday.js": "pikaday/1.8.0/pikaday.js",
+  "spectrum.js": "spectrum-colorpicker/1.8.0/spectrum.js",
+  "spectrum.css": "spectrum-colorpicker/1.8.0/spectrum.css",
 };
diff --git a/lib/tasks/javascript.rake b/lib/tasks/javascript.rake
index 80d725b..a2a3a29 100644
--- a/lib/tasks/javascript.rake
+++ b/lib/tasks/javascript.rake
@@ -31,42 +31,14 @@ def write_template(path, task_name, template)
   puts "#{basename} prettified"
 end
 
-task 'javascript:update_constants' => :environment do
-  task_name = 'update_constants'
-
-  write_template("discourse/app/lib/constants.js", task_name, <<~JS)
-    export const SEARCH_PRIORITIES = #{Searchable::PRIORITIES.to_json};
-
-    export const SEARCH_PHRASE_REGEXP = '#{Search::PHRASE_MATCH_REGEXP_PATTERN}';
-  JS
-
-  write_template("pretty-text/addon/emoji/data.js", task_name, <<~JS)
-    export const emojis = #{Emoji.standard.map(&:name).flatten.inspect};
-    export const tonableEmojis = #{Emoji.tonable_emojis.flatten.inspect};
-    export const aliases = #{Emoji.aliases.inspect.gsub("=>", ":")};
-    export const searchAliases = #{Emoji.search_aliases.inspect.gsub("=>", ":")};
-    export const translations = #{Emoji.translations.inspect.gsub("=>", ":")};
-    export const replacements = #{Emoji.unicode_replacements_json};
-  JS
-
-  write_template("pretty-text/addon/emoji/version.js", task_name, <<~JS)
-    export const IMAGE_VERSION = "#{Emoji::EMOJI_VERSION}";
-  JS
-end
-
-task 'javascript:update' do
-  require 'uglifier'
-
-  yarn = system("yarn install")
-  abort('Unable to run "yarn install"') unless yarn
-
-  dependencies = [
+def dependencies
+  [
     {
       source: 'bootstrap/js/modal.js',
       destination: 'bootstrap-modal.js'
     }, {
       source: 'ace-builds/src-min-noconflict/ace.js',
-      destination: 'ace',
+      destination: 'ace.js',
       public: true
     }, {
       source: 'chart.js/dist/Chart.min.js',
@@ -86,8 +58,7 @@ task 'javascript:update' do
       public: true
     }, {
       source: 'spectrum-colorpicker/spectrum.css',
-      public: true,
-      skip_versioning: true
+      public: true
     }, {
       source: 'favcount/favcount.js'
     }, {
@@ -142,23 +113,28 @@ task 'javascript:update' do
     }, {
       source: 'workbox-sw/build/.',
       destination: 'workbox',
-      public: true
+      public: true,
+      skip_versioning: true
     }, {
       source: 'workbox-routing/build/.',
       destination: 'workbox',
-      public: true
+      public: true,
+      skip_versioning: true
     }, {
       source: 'workbox-core/build/.',
       destination: 'workbox',
-      public: true
+      public: true,
+      skip_versioning: true
     }, {
       source: 'workbox-strategies/build/.',
       destination: 'workbox',
-      public: true
+      public: true,
+      skip_versioning: true
     }, {
       source: 'workbox-expiration/build/.',
       destination: 'workbox',
-      public: true
+      public: true,
+      skip_versioning: true
     }, {
       source: '@popperjs/core/dist/umd/popper.js'
     }, {
@@ -173,6 +149,44 @@ task 'javascript:update' do
     },
 
   ]
+end
+
+def node_package_name(f)
+  f[:source].split('/').first
+end
+
+def public_path_name(f)
+  f[:destination] || node_package_name(f)
+end
+
+task 'javascript:update_constants' => :environment do
+  task_name = 'update_constants'
+
+  write_template("discourse/app/lib/constants.js", task_name, <<~JS)
+    export const SEARCH_PRIORITIES = #{Searchable::PRIORITIES.to_json};
+
+    export const SEARCH_PHRASE_REGEXP = '#{Search::PHRASE_MATCH_REGEXP_PATTERN}';
+  JS
+
+  write_template("pretty-text/addon/emoji/data.js", task_name, <<~JS)
+    export const emojis = #{Emoji.standard.map(&:name).flatten.inspect};
+    export const tonableEmojis = #{Emoji.tonable_emojis.flatten.inspect};
+    export const aliases = #{Emoji.aliases.inspect.gsub("=>", ":")};
+    export const searchAliases = #{Emoji.search_aliases.inspect.gsub("=>", ":")};
+    export const translations = #{Emoji.translations.inspect.gsub("=>", ":")};
+    export const replacements = #{Emoji.unicode_replacements_json};
+  JS
+
+  write_template("pretty-text/addon/emoji/version.js", task_name, <<~JS)
+    export const IMAGE_VERSION = "#{Emoji::EMOJI_VERSION}";
+  JS
+end
+
+task 'javascript:update' => 'clean_up' do
+  require 'uglifier'
+
+  yarn = system("yarn install")
+  abort('Unable to run "yarn install"') unless yarn
 
   versions = {}
   start = Time.now
@@ -206,23 +220,29 @@ task 'javascript:update' do
     if f[:public_root]
       dest = "#{public_root}/#{filename}"
     elsif f[:public]
-      unless f[:skip_versioning]
-        package_name = f[:source].split('/').first
-        package_version = JSON.parse(File.read("#{library_src}/#{package_name}/package.json"))["version"]
-        versions[filename] = package_version
-      end
+      if f[:skip_versioning]
+        dest = "#{public_js}/#{filename}"
+      else
+        package_dir_name = public_path_name(f)
+        package_version = JSON.parse(File.read("#{library_src}/#{node_package_name(f)}/package.json"))["version"]
+        versions[filename] = "#{package_dir_name}/#{package_version}/#{filename}"
+
+        path = "#{public_js}/#{package_dir_name}/#{package_version}"
+        dest = "#{path}/#{filename}"
 
-      dest = "#{public_js}/#{filename}"
+        FileUtils.mkdir_p(path) unless File.exists?(path)
+      end
     else
       dest = "#{vendor_js}/#{filename}"
     end
 
     if src.include? "ace.js"
+      versions["ace/ace.js"] = versions.delete("ace.js")
       ace_root = "#{library_src}/ace-builds/src-min-noconflict/"
       addtl_files = [ "ext-searchbox", "mode-html", "mode-scss", "mode-sql", "theme-chrome", "worker-html"]
-      FileUtils.mkdir(dest) unless File.directory?(dest)
+      dest_path = dest.split('/')[0..-2].join('/')
       addtl_files.each do |file|
-        FileUtils.cp_r("#{ace_root}#{file}.js", dest)

[... diff too long, it was truncated ...]

GitHub sha: 8dac8abd

This commit appears in #10707 which was approved by eviltrout, davidtaylorhq, and pmusaraj. It was merged by jbrw.