DEV: Replace `version.js.erb` with pre generated file

DEV: Replace version.js.erb with pre generated file

diff --git a/app/assets/javascripts/pretty-text/addon/emoji/version.js b/app/assets/javascripts/pretty-text/addon/emoji/version.js
new file mode 100644
index 0000000..a57796b
--- /dev/null
+++ b/app/assets/javascripts/pretty-text/addon/emoji/version.js
@@ -0,0 +1,4 @@
+// DO NOT EDIT THIS FILE!!!
+// Update it by running `rake javascript:update_constants`
+
+export const IMAGE_VERSION = "9";
diff --git a/app/assets/javascripts/pretty-text/addon/emoji/version.js.erb b/app/assets/javascripts/pretty-text/addon/emoji/version.js.erb
deleted file mode 100644
index b846ca5..0000000
--- a/app/assets/javascripts/pretty-text/addon/emoji/version.js.erb
+++ /dev/null
@@ -1,2 +0,0 @@
-// bump up this number to expire all emojis
-export const IMAGE_VERSION = "<%= Emoji::EMOJI_VERSION %>";
diff --git a/lib/tasks/javascript.rake b/lib/tasks/javascript.rake
index a415d8b..2b57ca0 100644
--- a/lib/tasks/javascript.rake
+++ b/lib/tasks/javascript.rake
@@ -12,30 +12,29 @@ def library_src
   "#{Rails.root}/node_modules"
 end
 
-task 'javascript:update_constants' => :environment do
+def write_template(path, template)
   header = <<~HEADER
     // DO NOT EDIT THIS FILE!!!
     // Update it by running `rake javascript:update_constants`
   HEADER
 
-  constants_js = <<~JS
-    #{header}
+  basename = File.basename(path)
+  output_path = "#{Rails.root}/app/assets/javascripts/#{path}"
+
+  File.write(output_path, "#{header}\n\n#{template}")
+  puts "#{basename} created"
+  %x{yarn run prettier --write #{output_path}}
+  puts "#{basename} prettified"
+end
 
+task 'javascript:update_constants' => :environment do
+  write_template("discourse/app/lib/constants.js", <<~JS)
     export const SEARCH_PRIORITIES = #{Searchable::PRIORITIES.to_json};
 
     export const SEARCH_PHRASE_REGEXP = '#{Search::PHRASE_MATCH_REGEXP_PATTERN}';
   JS
 
-  output_path = "#{Rails.root}/app/assets/javascripts/discourse/app/lib/constants.js"
-  File.write(output_path, constants_js)
-  puts "contants.js created"
-  %x{yarn run prettier --write #{output_path}}
-  puts "constants.js prettified"
-
-  # app/assets/javascripts/pretty-text/addon/emoji/data.js.erb
-  emoji_data_js = <<~JS
-    #{header}
-
+  write_template("pretty-text/addon/emoji/data.js", <<~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("=>", ":")};
@@ -43,11 +42,10 @@ task 'javascript:update_constants' => :environment do
     export const translations = #{Emoji.translations.inspect.gsub("=>", ":")};
     export const replacements = #{Emoji.unicode_replacements_json};
   JS
-  output_path = "#{Rails.root}/app/assets/javascripts/pretty-text/addon/emoji/data.js"
-  File.write(output_path, emoji_data_js)
-  puts "emoji/data.js created"
-  %x{yarn run prettier --write #{output_path}}
-  puts "emoji/data.js prettified"
+
+  write_template("pretty-text/addon/emoji/version.js", <<~JS)
+    export const IMAGE_VERSION = "#{Emoji::EMOJI_VERSION}";
+  JS
 end
 
 task 'javascript:update' do

GitHub sha: 8311374a

I recommend an integrity test here to ensure that server and client do not drift.

Like what we do for localization, it can simply open the file, and confirm that the number matches what it is on the server.

1 Like

Added integrity test here:

1 Like