DEV: Don't use js.erb for constants

Adds a new rake task to auto generate a constants.js file with the
constants present. This makes migrating to Ember CLI easier, but also
slightly speeds up asset compilation by having to do less work.

If the constants change you need to run:
`rake javascripts:update_constants`
This commit is contained in:
Robin Ward
2020-05-08 13:25:25 -04:00
parent dae29afd7d
commit 7f373e8b93
7 changed files with 36 additions and 9 deletions

View File

@ -12,8 +12,24 @@ def library_src
"#{Rails.root}/node_modules"
end
task 'javascript:update' do
task 'javascript:update_constants' => :environment do
constants_js = <<~JS
// DO NOT EDIT THIS FILE!!!
// Update it by running `rake javascript:update_constants`
export const SearchPriorities = #{Searchable::PRIORITIES.to_json};
export const SearchPhraseRegexp = '#{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"
end
task 'javascript:update' do
require 'uglifier'
yarn = system("yarn install")