Skip to content

Commit 5cf6ebf

Browse files
DEV: Deprecate inline script tags for templates & initializers (#32773)
https://meta.discourse.org/t/366482
1 parent 1e368a0 commit 5cf6ebf

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

app/models/theme.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
class Theme < ActiveRecord::Base
77
include GlobalPath
88

9-
BASE_COMPILER_VERSION = 87
9+
BASE_COMPILER_VERSION = 88
1010

1111
class SettingsMigrationError < StandardError
1212
end

app/models/theme_field.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def process_html(html)
123123
errors << I18n.t("themes.errors.optimized_link") if contains_optimized_link?(html)
124124

125125
js_compiler = ThemeJavascriptCompiler.new(theme_id, self.theme.name)
126+
deprecated_template_names = []
126127

127128
doc = Nokogiri::HTML5.fragment(html)
128129

@@ -144,6 +145,7 @@ def process_html(html)
144145
"discourse/templates/#{name.delete_prefix("/")}",
145146
hbs_template,
146147
)
148+
deprecated_template_names << name
147149
end
148150
rescue ThemeJavascriptCompiler::CompileError => ex
149151
js_compiler.append_js_error("discourse/templates/#{name}", ex.message)
@@ -153,6 +155,28 @@ def process_html(html)
153155
node.remove
154156
end
155157

158+
if deprecated_template_names.present?
159+
js = <<~JS
160+
import deprecated from "discourse/lib/deprecated";
161+
162+
export default {
163+
initialize(){
164+
const names = #{deprecated_template_names.to_json};
165+
names.forEach((name) => {
166+
deprecated(
167+
`[${name}] adding templates to a theme using <script type='text/x-handlebars'> is deprecated. Move to dedicated .hbs or .gjs files.`,
168+
{
169+
id: "discourse.script-tag-hbs",
170+
url: "https://meta.discourse.org/t/366482",
171+
}
172+
)
173+
});
174+
}
175+
}
176+
JS
177+
js_compiler.append_module(js, "discourse/initializers/script-tag-hbs-deprecations", "js")
178+
end
179+
156180
doc
157181
.css('script[type="text/discourse-plugin"]')
158182
.each_with_index do |node, index|
@@ -165,12 +189,20 @@ def process_html(html)
165189
begin
166190
js = <<~JS
167191
import { withPluginApi } from "discourse/lib/plugin-api";
192+
import deprecated from "discourse/lib/deprecated";
168193
169194
export default {
170195
name: #{initializer_name.inspect},
171196
after: "inject-objects",
172197
173198
initialize() {
199+
deprecated(
200+
"Adding JS code using <script type='text/x-handlebars'> is deprecated. Move this code to a dedicated JavaScript file.",
201+
{
202+
id: "discourse.script-tag-discourse-plugin",
203+
url: "https://meta.discourse.org/t/366482",
204+
}
205+
)
174206
withPluginApi(#{version.inspect}, (api) => {
175207
#{node.inner_html}
176208
});

0 commit comments

Comments
 (0)