From 7f373e8b9315597059a6e23e9f070a4e8a371233 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Fri, 8 May 2020 13:25:25 -0400 Subject: [PATCH] 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` --- .../concerns/category-search-priorities.js.erb | 1 - .../app/components/edit-category-settings.js | 4 ++-- .../app/lib/concerns/search-constants.js.erb | 1 - .../javascripts/discourse/app/lib/constants.js | 13 +++++++++++++ .../discourse/app/lib/highlight-search.js | 4 ++-- .../app/routes/discovery-categories.js | 4 ++-- lib/tasks/javascript.rake | 18 +++++++++++++++++- 7 files changed, 36 insertions(+), 9 deletions(-) delete mode 100644 app/assets/javascripts/discourse/app/components/concerns/category-search-priorities.js.erb delete mode 100644 app/assets/javascripts/discourse/app/lib/concerns/search-constants.js.erb create mode 100644 app/assets/javascripts/discourse/app/lib/constants.js diff --git a/app/assets/javascripts/discourse/app/components/concerns/category-search-priorities.js.erb b/app/assets/javascripts/discourse/app/components/concerns/category-search-priorities.js.erb deleted file mode 100644 index 643b1777f96..00000000000 --- a/app/assets/javascripts/discourse/app/components/concerns/category-search-priorities.js.erb +++ /dev/null @@ -1 +0,0 @@ -export const searchPriorities = <%= Searchable::PRIORITIES.to_json %>; diff --git a/app/assets/javascripts/discourse/app/components/edit-category-settings.js b/app/assets/javascripts/discourse/app/components/edit-category-settings.js index 6ee6d77ffa7..f0ab1cea121 100644 --- a/app/assets/javascripts/discourse/app/components/edit-category-settings.js +++ b/app/assets/javascripts/discourse/app/components/edit-category-settings.js @@ -2,7 +2,7 @@ import discourseComputed from "discourse-common/utils/decorators"; import { empty, and } from "@ember/object/computed"; import { setting } from "discourse/lib/computed"; import { buildCategoryPanel } from "discourse/components/edit-category-panel"; -import { searchPriorities } from "discourse/components/concerns/category-search-priorities"; +import { SearchPriorities } from "discourse/lib/constants"; import Group from "discourse/models/group"; const categorySortCriteria = []; @@ -71,7 +71,7 @@ export default buildCategoryPanel("settings", { searchPrioritiesOptions() { const options = []; - Object.entries(searchPriorities).forEach(entry => { + Object.entries(SearchPriorities).forEach(entry => { const [name, value] = entry; options.push({ diff --git a/app/assets/javascripts/discourse/app/lib/concerns/search-constants.js.erb b/app/assets/javascripts/discourse/app/lib/concerns/search-constants.js.erb deleted file mode 100644 index 4cb005cc687..00000000000 --- a/app/assets/javascripts/discourse/app/lib/concerns/search-constants.js.erb +++ /dev/null @@ -1 +0,0 @@ -export const PHRASE_MATCH_REGEXP_PATTERN = '<%= Search::PHRASE_MATCH_REGEXP_PATTERN %>'; diff --git a/app/assets/javascripts/discourse/app/lib/constants.js b/app/assets/javascripts/discourse/app/lib/constants.js new file mode 100644 index 00000000000..8b752571d55 --- /dev/null +++ b/app/assets/javascripts/discourse/app/lib/constants.js @@ -0,0 +1,13 @@ +// DO NOT EDIT THIS FILE!!! +// Update it by running `rake javascript:update_constants` + +export const SearchPriorities = { + ignore: 1, + very_low: 2, + low: 3, + normal: 0, + high: 4, + very_high: 5 +}; + +export const SearchPhraseRegexp = '"([^"]+)"'; diff --git a/app/assets/javascripts/discourse/app/lib/highlight-search.js b/app/assets/javascripts/discourse/app/lib/highlight-search.js index 0d9318fb7af..e7dfc465ec7 100644 --- a/app/assets/javascripts/discourse/app/lib/highlight-search.js +++ b/app/assets/javascripts/discourse/app/lib/highlight-search.js @@ -1,4 +1,4 @@ -import { PHRASE_MATCH_REGEXP_PATTERN } from "discourse/lib/concerns/search-constants"; +import { SearchPhraseRegexp } from "discourse/lib/constants"; import highlightHTML from "discourse/lib/highlight-html"; export const CLASS_NAME = "search-highlight"; @@ -7,7 +7,7 @@ export default function(elem, term, opts = {}) { if (!_.isEmpty(term)) { // special case ignore "l" which is used for magic sorting let words = _.reject( - term.match(new RegExp(`${PHRASE_MATCH_REGEXP_PATTERN}|[^\\s]+`, "g")), + term.match(new RegExp(`${SearchPhraseRegexp}|[^\\s]+`, "g")), t => t === "l" ); diff --git a/app/assets/javascripts/discourse/app/routes/discovery-categories.js b/app/assets/javascripts/discourse/app/routes/discovery-categories.js index ba07642941a..c1f440d453a 100644 --- a/app/assets/javascripts/discourse/app/routes/discovery-categories.js +++ b/app/assets/javascripts/discourse/app/routes/discovery-categories.js @@ -8,7 +8,7 @@ import { defaultHomepage } from "discourse/lib/utilities"; import TopicList from "discourse/models/topic-list"; import { ajax } from "discourse/lib/ajax"; import PreloadStore from "discourse/lib/preload-store"; -import { searchPriorities } from "discourse/components/concerns/category-search-priorities"; +import { SearchPriorities } from "discourse/lib/constants"; import { hash } from "rsvp"; import Site from "discourse/models/site"; @@ -145,7 +145,7 @@ export function openNewCategoryModal(context) { allow_badges: true, topic_featured_link_allowed: true, custom_fields: {}, - search_priority: searchPriorities.normal + search_priority: SearchPriorities.normal }); showModal("edit-category", { model }).set("selectedTab", "general"); diff --git a/lib/tasks/javascript.rake b/lib/tasks/javascript.rake index d5381834df3..835c83cfca2 100644 --- a/lib/tasks/javascript.rake +++ b/lib/tasks/javascript.rake @@ -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")