A platform for community discussion. Free, open, simple.
DEV: Drop `discourse-plugin` and `x-handlebars` inline scripts (#38856)
David Taylor6496c18b9efc
diff --git a/app/models/theme_field.rb b/app/models/theme_field.rbindex 6901f43d050..01c136f9793 100644--- a/app/models/theme_field.rb+++ b/app/models/theme_field.rb@@ -127,92 +127,8 @@ class ThemeField < ActiveRecord::Baseerrors = []errors << I18n.t("themes.errors.optimized_link") if contains_optimized_link?(html)- js_tree = {}- js_errors = []- deprecated_template_names = []-doc = Nokogiri::HTML5.fragment(html)- doc- .css('script[type="text/x-handlebars"]')- .each do |node|- name = node["name"] || node["data-template-name"] || "broken"- is_raw = name =~ /\.(raw|hbr)\z/- hbs_template = node.inner_html-- if is_raw- js_errors.push(- "[THEME #{theme.id}] [discourse/templates/#{name}] Raw templates are no longer supported",- )- else- js_tree["discourse/templates/#{name.delete_prefix("/")}.hbs"] = hbs_template- deprecated_template_names << name- end-- node.remove- end-- if deprecated_template_names.present? || js_errors.present?- js = <<~JS- import deprecated from "discourse/lib/deprecated";-- export default {- initialize(){- const names = #{deprecated_template_names.to_json};- names.forEach((name) => {- deprecated(- `[${name}] adding templates to a theme using <script type='text/x-handlebars'> is deprecated. Move to dedicated .hbs or .gjs files.`,- {- id: "discourse.script-tag-hbs",- url: "https://meta.discourse.org/t/366482",- }- )- });- const errors = #{js_errors.to_json};- errors.forEach((error) => console.error(error));- }- }- JS- js_tree["discourse/initializers/script-tag-hbs-deprecations.js"] = js- end-- doc- .css('script[type="text/discourse-plugin"]')- .each_with_index do |node, index|- version = node["version"]- next if version.blank?-- initializer_name =- "theme-field" + "-#{self.id}" + "-#{Theme.targets[self.target_id]}" +- "-#{ThemeField.types[self.type_id]}" + "-script-#{index + 1}"-- js = <<~JS- import { withPluginApi } from "discourse/lib/plugin-api";- import deprecated from "discourse/lib/deprecated";-- export default {- name: #{initializer_name.inspect},- after: "inject-objects",-- initialize() {- deprecated(- "Adding JS code using <script type='text/discourse-plugin'> is deprecated. Move this code to a dedicated JavaScript file.",- {- id: "discourse.script-tag-discourse-plugin",- url: "https://meta.discourse.org/t/366482",- }- )- withPluginApi(#{version.inspect}, (api) => {- #{node.inner_html}- });- }- };- JS-- js_tree["discourse/initializers/#{initializer_name}.js"] = js- node.remove- end-unused_raw_caches = Set.new raw_javascript_caches.to_adoc@@ -250,23 +166,6 @@ class ThemeField < ActiveRecord::Baseunused_raw_caches.each(&:destroy!)- if js_tree.present?- js_compiler =- ThemeJavascriptCompiler.new(theme_id, self.theme.name, theme.build_settings_hash)- js_compiler.append_tree(js_tree)-- javascript_cache || build_javascript_cache- javascript_cache.content = js_compiler.content- javascript_cache.source_map = js_compiler.source_map- javascript_cache.save!-- doc.add_child(<<~HTML.html_safe)- <link rel="modulepreload" href="#{javascript_cache.url}" data-theme-id="#{theme_id}" nonce="#{CSP_NONCE_PLACEHOLDER}" />- HTML- else- javascript_cache&.destroy!- end-[doc.to_s, errors&.join("\n")]enddiff --git a/frontend/discourse/app/deprecation-workflow.js b/frontend/discourse/app/deprecation-workflow.jsindex d93a7bcd71f..ea08157c305 100644--- a/frontend/discourse/app/deprecation-workflow.js+++ b/frontend/discourse/app/deprecation-workflow.js@@ -402,8 +402,6 @@ const workflows = [{ handler: "notify-admin", matchId: "discourse.qunit.global-exists" },{ handler: "notify-admin", matchId: "discourse.resolver-resolutions" },{ handler: "notify-admin", matchId: "discourse.safari-hacks-disabled" },- { handler: "notify-admin", matchId: "discourse.script-tag-discourse-plugin" },- { handler: "notify-admin", matchId: "discourse.script-tag-hbs" },{ handler: "notify-admin", matchId: "discourse.user.resolved-timezone" },{ handler: "notify-admin", matchId: "discourse.user.userOptions" },{ handler: "notify-admin", matchId: "discourse.widgets-decommissioned" },diff --git a/lib/asset_processor.rb b/lib/asset_processor.rbindex da5378a9341..e437f894c75 100644--- a/lib/asset_processor.rb+++ b/lib/asset_processor.rb@@ -1,7 +1,7 @@# frozen_string_literal: trueclass AssetProcessor- BASE_COMPILER_VERSION = 105+ BASE_COMPILER_VERSION = 106PROCESSOR_DIR = "tmp/asset-processor"LOCK_FILE = "#{PROCESSOR_DIR}/build.lock"diff --git a/spec/integration/activerecord_preventing_writes_spec.rb b/spec/integration/activerecord_preventing_writes_spec.rbindex e9989a6c8b0..3de301a6c09 100644--- a/spec/integration/activerecord_preventing_writes_spec.rb+++ b/spec/integration/activerecord_preventing_writes_spec.rb@@ -9,8 +9,8 @@ RSpec.describe "When ActiveRecord is preventing writes" dotarget_id: Theme.targets[:common],name: "head_tag",value: <<~HTML,- <script type="text/discourse-plugin" version="0.1">- console.log(settings.uploads.imajee);+ <script>+ console.log('foo');</script>HTML)diff --git a/spec/lib/discourse_spec.rb b/spec/lib/discourse_spec.rbindex 682c926d75e..ab7e6afa7b6 100644--- a/spec/lib/discourse_spec.rb+++ b/spec/lib/discourse_spec.rb@@ -654,8 +654,8 @@ RSpec.describe Discourse dotarget_id: Theme.targets[:common],name: "head_tag",value: <<~HTML,- <script type="text/discourse-plugin" version="0.1">- console.log(settings.uploads.imajee);+ <script>+ console.log("hello world");</script>HTML)@@ -690,14 +690,6 @@ RSpec.describe Discourse doold_upload_url = Discourse.store.cdn_url(upload.url)- head_tag_script =- Nokogiri::HTML5- .fragment(Theme.lookup_field(theme.id, :desktop, "head_tag"))- .css("link[rel=modulepreload]")- .first- head_tag_js = JavascriptCache.find_by(digest: head_tag_script[:href][/\h{40}/]).content- expect(head_tag_js).to include(old_upload_url)-js_file_script =Nokogiri::HTML5.fragment(Theme.lookup_field(theme.id, :extra_js, nil))@@ -719,14 +711,6 @@ RSpec.describe Discourse doSiteSetting.s3_cdn_url = "https://new.s3.cdn.com/gg"new_upload_url = Discourse.store.cdn_url(upload.url)- head_tag_script =- Nokogiri::HTML5- .fragment(Theme.lookup_field(theme.id, :desktop, "head_tag"))- .css("link[rel=modulepreload]")- .first- head_tag_js = JavascriptCache.find_by(digest: head_tag_script[:href][/\h{40}/]).content- expect(head_tag_js).to include(old_upload_url)-js_file_script =Nokogiri::HTML5.fragment(Theme.lookup_field(theme.id, :extra_js, nil))@@ -747,14 +731,6 @@ RSpec.describe Discourse doDiscourse.clear_all_theme_cache!- head_tag_script =- Nokogiri::HTML5- .fragment(Theme.lookup_field(theme.id, :desktop, "head_tag"))- .css("link[rel=modulepreload]")- .first- head_tag_js = JavascriptCache.find_by(digest: head_tag_script[:href][/\h{40}/]).content- expect(head_tag_js).to include(new_upload_url)-js_file_script =Nokogiri::HTML5.fragment(Theme.lookup_field(theme.id, :extra_js, nil))diff --git a/spec/models/theme_field_spec.rb b/spec/models/theme_field_spec.rbindex 42bc286235c..adea6782b93 100644--- a/spec/models/theme_field_spec.rb+++ b/spec/models/theme_field_spec.rb@@ -61,9 +61,6 @@ RSpec.describe ThemeField doit "extracts inline javascript to an external file" dohtml = <<~HTML- <script type="text/discourse-plugin" version="0.8">- console.log("inline discourse plugin");- </script><script type="text/template" data-template="custom-template"><div>custom script type</div></script>@@ -87,12 +84,6 @@ RSpec.describe ThemeField dosimple_extracted_scripts = baked_doc.css("script[src^='/theme-javascripts/']")expect(simple_extracted_scripts.length).to eq(3)- extracted_module_preloads =- baked_doc.css("link[rel=modulepreload][href^='/theme-javascripts/']")- expect(extracted_module_preloads.length).to eq(1)-- expect(theme_field.javascript_cache.content).to include("inline discourse plugin")-raw_js_cache_contents = theme_field.raw_javascript_caches.map(&:content)expect(raw_js_cache_contents).to contain_exactly('console.log("inline raw script");',@@ -103,24 +94,6 @@ RSpec.describe ThemeField doexpect(baked_doc.css("script[type='text/template']").length).to eq(1)end- it "correctly logs errors for transpiled js" do- html = <<HTML-<script type="text/discourse-plugin" version="0.8">- badJavaScript(;-</script>-HTML-- field = ThemeField.create!(theme_id: -1, target_id: 0, name: "header", value: html)- field.ensure_baked!- expect(field.value_baked).to include(- "<link rel=\"modulepreload\" href=\"#{field.javascript_cache.url}\" data-theme-id=\"-1\" nonce=\"#{ThemeField::CSP_NONCE_PLACEHOLDER}\">",- )- expect(field.javascript_cache.content).to include("[THEME -1 'Foundation'] Compile error")-- field.update!(value: "")- field.ensure_baked!- end-it "correctly extracts and generates errors for raw transpiled js" dohtml = <<~HTML<script>@@ -144,32 +117,6 @@ HTMLexpect(field.error).to eq(nil)end- it "allows us to use theme settings in handlebars templates" do- html = <<HTML-<script type='text/x-handlebars' data-template-name='my-template'>- <div class="testing-div">{{themeSettings.string_setting}}</div>-</script>-HTML-- ThemeField.create!(- theme_id: -1,- target_id: 3,- name: "yaml",- value: "string_setting: \"test text \\\" 123!\"",- ).ensure_baked!- theme_field = ThemeField.create!(theme_id: -1, target_id: 0, name: "head_tag", value: html)- theme_field.ensure_baked!- javascript_cache = theme_field.javascript_cache-- expect(theme_field.value_baked).to include(- "<link rel=\"modulepreload\" href=\"#{javascript_cache.url}\" data-theme-id=\"-1\" nonce=\"#{ThemeField::CSP_NONCE_PLACEHOLDER}\">",- )- expect(javascript_cache.content).to include("testing-div")- expect(javascript_cache.content).to include("string_setting")- expect(javascript_cache.content).to include("test text \\\" 123!")- expect(javascript_cache.content).to include('compatModules["discourse/templates/my-template"]')- end-it "correctly generates errors for transpiled css" docss = "body {"field = theme.set_field(target: :common, name: :scss, value: css)@@ -335,35 +282,23 @@ HTMLlet(:key) { "themes.settings_errors" }it "forces re-transpilation of theme JS when settings YAML changes" do- settings_field =- ThemeField.create!(- theme: theme,- target_id: Theme.targets[:settings],- name: "yaml",- value: "setting: 5",- )+ theme.set_field(target: :settings, name: "yaml", value: "setting: 5")+ theme.set_field(+ target: :extra_js,+ name: "discourse/initializers/my-initializer.js",+ value: "console.log(settings.setting);",+ )+ theme.save!- html = <<~HTML- <script type="text/discourse-plugin" version="0.8">- alert(settings.setting);- </script>- HTML+ old_js_cache_content = theme.reload.javascript_cache.content+ expect(old_js_cache_content).to include('"setting": 5')- js_field =- ThemeField.create!(- theme: theme,- target_id: ThemeField.types[:html],- name: "header",- value: html,- )- old_value_baked = js_field.value_baked- settings_field.update!(value: "setting: 66")- js_field.reload+ theme.set_field(target: :settings, name: "yaml", value: "setting: 66")+ theme.save!- expect(js_field.value_baked).to eq(nil)- js_field.ensure_baked!- expect(js_field.value_baked).to be_present- expect(js_field.value_baked).not_to eq(old_value_baked)+ new_js_cache_content = theme.reload.javascript_cache.content+ expect(new_js_cache_content).to include('"setting": 66')+ expect(new_js_cache_content).not_to include('"setting": 5')endit "generates errors for bad YAML" do@@ -681,16 +616,13 @@ HTMLdescribe "prefix injection" doit "injects into JS" do- html = <<~HTML- <script type="text/discourse-plugin" version="0.8">- console.log("inline discourse plugin", themePrefix("foo"));- </script>- HTML-- theme_field =- ThemeField.create!(theme_id: theme.id, target_id: 0, name: "head_tag", value: html)- theme_field.ensure_baked!- javascript_cache = theme_field.javascript_cache+ theme.set_field(+ target: :extra_js,+ name: "discourse/initializers/my-init.js",+ value: 'console.log("inline discourse plugin", themePrefix("foo"));',+ )+ theme.save!+ javascript_cache = theme.reload.javascript_cacheexpect(javascript_cache.content).to include("inline discourse plugin")expect(javascript_cache.content).to include("theme_translations.#{theme.id}.")end@@ -802,14 +734,6 @@ HTMLupload_id: upload.id,)- common_field =- theme.set_field(- target: :common,- name: "head_tag",- value: "<script type='text/discourse-plugin' version='0.1'>let c = 'd';</script>",- type: :html,- )-theme.set_field(target: :settings, type: :yaml, name: "yaml", value: "hello: world")theme.set_field(@@ -820,25 +744,19 @@ HTMLtheme.save!- # a bit fragile, but at least we test it properly- [- theme.reload.javascript_cache.content,- common_field.reload.javascript_cache.content,- ].each do |js|- expected_local_js_cache_url = js_field.javascript_cache.local_url- expect(expected_local_js_cache_url).to start_with("/theme-javascripts/")- expect(js).to include(<<~JS)- registerSettings(#{theme.id}, {- "hello": "world",- "theme_uploads": {- "test_js": "#{js_field.upload.url}"- },- "theme_uploads_local": {- "test_js": "#{js_field.javascript_cache.local_url}"- }- });- JS- end+ expected_local_js_cache_url = js_field.javascript_cache.local_url+ expect(expected_local_js_cache_url).to start_with("/theme-javascripts/")+ expect(theme.reload.javascript_cache.content).to include(<<~JS)+ registerSettings(#{theme.id}, {+ "hello": "world",+ "theme_uploads": {+ "test_js": "#{js_field.upload.url}"+ },+ "theme_uploads_local": {+ "test_js": "#{js_field.javascript_cache.local_url}"+ }+ });+ JS# this is important, we do not want local_js_urls to leak into scssexpect(theme.scss_variables).to include("$hello: unquote(\"world\");")diff --git a/spec/models/theme_spec.rb b/spec/models/theme_spec.rbindex b1768244bea..ca8a479417c 100644--- a/spec/models/theme_spec.rb+++ b/spec/models/theme_spec.rb@@ -126,45 +126,6 @@ RSpec.describe Theme doexpect(Theme.lookup_field(theme.id, :desktop, "head_tag")).to eq("<b>I am bold</b>")end- it "should precompile fragments in body and head tags" do- with_template = <<HTML- <script type='text/x-handlebars' name='template'>- {{hello}}- </script>- <script type='text/x-handlebars' data-template-name='raw_template.raw'>- {{hello}}- </script>-HTML- theme.set_field(target: :common, name: "header", value: with_template)- theme.save!-- field = theme.theme_fields.find_by(target_id: Theme.targets[:common], name: "header")- baked = Theme.lookup_field(theme.id, :mobile, "header")-- expect(baked).to include(field.javascript_cache.url)- expect(field.javascript_cache.content).to include("@ember/template-factory")- expect(field.javascript_cache.content).to include("Raw templates are no longer supported")- end-- it "can destroy unbaked theme without errors" do- with_template = <<HTML- <script type='text/x-handlebars' name='template'>- {{hello}}- </script>- <script type='text/x-handlebars' data-template-name='raw_template.raw'>- {{hello}}- </script>-HTML- theme.set_field(target: :common, name: "header", value: with_template)- theme.save!-- field = theme.theme_fields.find_by(target_id: Theme.targets[:common], name: "header")- baked = Theme.lookup_field(theme.id, :mobile, "header")- ThemeField.where(id: field.id).update_all(compiler_version: 0) # update_all to avoid callbacks-- field.reload.destroy!- end-it "should create body_tag_baked on demand if needed" dotheme.set_field(target: :common, name: :body_tag, value: "<b>test")theme.save@@ -229,43 +190,6 @@ HTMLendend- describe "plugin api" do- def transpile(html)- f =- ThemeField.create!(- target_id: Theme.targets[:mobile],- theme_id: -1,- name: "after_header",- value: html,- )- f.ensure_baked!- [f.value_baked, f.javascript_cache, f]- end-- it "transpiles ES6 code" do- html = <<HTML- <script type='text/discourse-plugin' version='0.1'>- const x = 1;- console.log(x, settings.foo);- </script>-HTML-- baked, javascript_cache, field = transpile(html)- expect(baked).to include(javascript_cache.url)-- expect(javascript_cache.content).to include(- "compatModules[\"discourse/initializers/theme-field-#{field.id}-mobile-html-script-1\"]",- )- expect(javascript_cache.content).to include("getObjectForTheme(#{field.theme_id});")- expect(javascript_cache.content).to include(- "name: \"theme-field-#{field.id}-mobile-html-script-1\",",- )- expect(javascript_cache.content).to include("after: \"inject-objects\",")- expect(javascript_cache.content).to include("withPluginApi(\"0.1\", api =>")- expect(javascript_cache.content).to include("const x = 1;")- end- end-describe "theme upload vars" dolet :image dofile_from_fixtures("logo.png")@@ -385,48 +309,31 @@ HTMLit "allows values to be used in JS" dotheme.name = 'awesome theme"'theme.set_field(target: :settings, name: :yaml, value: "name: bob")- theme_field =- theme.set_field(- target: :common,- name: :after_header,- value:- '<script type="text/discourse-plugin" version="1.0">alert(settings.name); let a = ()=>{}; console.log(a);</script>',- )+ theme.set_field(+ target: :extra_js,+ name: "discourse/initializers/my-init.js",+ value: "alert(settings.name); let a = ()=>{}; console.log(a);",+ )theme.save!- theme_field.reload- expect(Theme.lookup_field(theme.id, :desktop, :after_header)).to include(- theme_field.javascript_cache.url,- )- expect(theme_field.javascript_cache.content).to include <<~JS- registerSettings(#{theme_field.theme.id}, {+ javascript_cache = theme.reload.javascript_cache+ expect(javascript_cache.content).to include <<~JS+ registerSettings(#{theme.id}, {"name": "bob"});JS- expect(theme_field.javascript_cache.content).to include(- "compatModules[\"discourse/initializers/theme-field-#{theme_field.id}-common-html-script-1\"]",- )- expect(theme_field.javascript_cache.content).to include(- "name: \"theme-field-#{theme_field.id}-common-html-script-1\",",- )- expect(theme_field.javascript_cache.content).to include("after: \"inject-objects\",")- expect(theme_field.javascript_cache.content).to include("withPluginApi(\"1.0\", api =>")- expect(theme_field.javascript_cache.content).to include("alert(settings.name)")- expect(theme_field.javascript_cache.content).to include("let a = () => {}")+ expect(javascript_cache.content).to include("alert(settings.name)")+ expect(javascript_cache.content).to include("let a = () => {}")setting = theme.settings[:name]setting.value = "bill"theme.save!- theme_field.reload- expect(theme_field.javascript_cache.content).to include <<~JS- registerSettings(#{theme_field.theme.id}, {+ expect(theme.reload.javascript_cache.content).to include <<~JS+ registerSettings(#{theme.id}, {"name": "bill"});JS- expect(Theme.lookup_field(theme.id, :desktop, :after_header)).to include(- theme_field.javascript_cache.url,- )endit "is empty when the settings are invalid" do@@ -839,25 +746,19 @@ HTMLit "recompiles when the hostname changes" dotheme.set_field(target: :settings, name: :yaml, value: "name: bob")- theme_field =- theme.set_field(- target: :common,- name: :after_header,- value:- '<script type="text/discourse-plugin" version="0.1">console.log("hello world");</script>',- )+ theme.set_field(+ target: :extra_js,+ name: "discourse/initializers/my-init.js",+ value: 'console.log("hello world");',+ )theme.save!- expect(Theme.lookup_field(theme.id, :common, :after_header)).to include(- "_ws=#{Discourse.current_hostname}",- )+ expect(theme.reload.javascript_cache.url).to include("_ws=#{Discourse.current_hostname}")SiteSetting.force_hostname = "someotherhostname.com"Theme.clear_cache!- expect(Theme.lookup_field(theme.id, :common, :after_header)).to include(- "_ws=someotherhostname.com",- )+ expect(theme.reload.javascript_cache.url).to include("_ws=someotherhostname.com")endend