mirror of
https://github.com/discourse/discourse.git
synced 2025-02-07 12:47:43 +08:00
DEV: Compile theme raw-hbr to modules (#30299)
Previously, theme hbr files were compiled to an IIFE, which would be executed before the app is booted. That is causing silenced deprecations to be printed, because the deprecation-workflow isn't set up when the IIFE is run. This commit updates the theme compiler so that it matches the ember-cli-based raw-hbs compiler. Templates are output to normal modules, which will then be loaded by the existing `eager-load-raw-templates` initializer. This runs after the app has started booting.
This commit is contained in:
parent
6b3e28216c
commit
ea9cdf7d47
|
@ -6,7 +6,7 @@ require "json_schemer"
|
||||||
class Theme < ActiveRecord::Base
|
class Theme < ActiveRecord::Base
|
||||||
include GlobalPath
|
include GlobalPath
|
||||||
|
|
||||||
BASE_COMPILER_VERSION = 86
|
BASE_COMPILER_VERSION = 87
|
||||||
|
|
||||||
class SettingsMigrationError < StandardError
|
class SettingsMigrationError < StandardError
|
||||||
end
|
end
|
||||||
|
|
|
@ -209,8 +209,7 @@ class ThemeJavascriptCompiler
|
||||||
end
|
end
|
||||||
|
|
||||||
def raw_template_name(name)
|
def raw_template_name(name)
|
||||||
name = name.sub(/\.(raw|hbr)\z/, "")
|
name.sub(/\.(raw|hbr)\z/, "")
|
||||||
name.inspect
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def append_raw_template(name, hbs_template)
|
def append_raw_template(name, hbs_template)
|
||||||
|
@ -218,16 +217,27 @@ class ThemeJavascriptCompiler
|
||||||
DiscourseJsProcessor::Transpiler.new.compile_raw_template(hbs_template, theme_id: @theme_id)
|
DiscourseJsProcessor::Transpiler.new.compile_raw_template(hbs_template, theme_id: @theme_id)
|
||||||
source_for_comment = hbs_template.gsub("*/", '*\/').indent(4, " ")
|
source_for_comment = hbs_template.gsub("*/", '*\/').indent(4, " ")
|
||||||
modern_replacement_marker = hbs_template.include?("{{!-- has-modern-replacement --}}")
|
modern_replacement_marker = hbs_template.include?("{{!-- has-modern-replacement --}}")
|
||||||
@output_tree << ["#{name}.js", <<~JS]
|
|
||||||
(function() {
|
source = <<~JS
|
||||||
/*
|
/*
|
||||||
#{source_for_comment}
|
#{source_for_comment}
|
||||||
*/
|
*/
|
||||||
const addRawTemplate = requirejs('discourse-common/lib/raw-templates').addRawTemplate;
|
|
||||||
const template = requirejs('discourse-common/lib/raw-handlebars').template(#{compiled});
|
import { template as compiler } from "discourse-common/lib/raw-handlebars";
|
||||||
addRawTemplate(#{raw_template_name(name)}, template, { themeId: #{@theme_id}, themeName: #{@theme_name.to_json}, hasModernReplacement: #{modern_replacement_marker} });
|
import { addRawTemplate } from "discourse-common/lib/raw-templates";
|
||||||
})();
|
|
||||||
|
let template = compiler(#{compiled});
|
||||||
|
|
||||||
|
addRawTemplate(#{raw_template_name(name).to_json}, template, {
|
||||||
|
themeId: #{@theme_id},
|
||||||
|
themeName: #{@theme_name.to_json},
|
||||||
|
hasModernReplacement: #{modern_replacement_marker}
|
||||||
|
});
|
||||||
|
|
||||||
|
export default template;
|
||||||
JS
|
JS
|
||||||
|
|
||||||
|
append_module source, "raw-templates/#{raw_template_name(name)}", "js", include_variables: false
|
||||||
rescue MiniRacer::RuntimeError, DiscourseJsProcessor::TranspileError => ex
|
rescue MiniRacer::RuntimeError, DiscourseJsProcessor::TranspileError => ex
|
||||||
raise CompileError.new ex.message
|
raise CompileError.new ex.message
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,15 +8,15 @@ RSpec.describe ThemeJavascriptCompiler do
|
||||||
template = "<h1>hello</h1>"
|
template = "<h1>hello</h1>"
|
||||||
name = "/path/to/templates1"
|
name = "/path/to/templates1"
|
||||||
compiler.append_raw_template("#{name}.raw", template)
|
compiler.append_raw_template("#{name}.raw", template)
|
||||||
expect(compiler.raw_content.to_s).to include("addRawTemplate(\"#{name}\"")
|
expect(compiler.raw_content.to_s).to include("addRawTemplate)(\"#{name}\"")
|
||||||
|
|
||||||
name = "/path/to/templates2"
|
name = "/path/to/templates2"
|
||||||
compiler.append_raw_template("#{name}.hbr", template)
|
compiler.append_raw_template("#{name}.hbr", template)
|
||||||
expect(compiler.raw_content.to_s).to include("addRawTemplate(\"#{name}\"")
|
expect(compiler.raw_content.to_s).to include("addRawTemplate)(\"#{name}\"")
|
||||||
|
|
||||||
name = "/path/to/templates3"
|
name = "/path/to/templates3"
|
||||||
compiler.append_raw_template("#{name}.hbs", template)
|
compiler.append_raw_template("#{name}.hbs", template)
|
||||||
expect(compiler.raw_content.to_s).to include("addRawTemplate(\"#{name}.hbs\"")
|
expect(compiler.raw_content.to_s).to include("addRawTemplate)(\"#{name}.hbs\"")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -257,7 +257,7 @@ HTML
|
||||||
expect(theme.javascript_cache.content).to include(
|
expect(theme.javascript_cache.content).to include(
|
||||||
"define(\"discourse/theme-#{theme.id}/discourse/templates/discovery\", [\"exports\", ",
|
"define(\"discourse/theme-#{theme.id}/discourse/templates/discovery\", [\"exports\", ",
|
||||||
)
|
)
|
||||||
expect(theme.javascript_cache.content).to include('addRawTemplate("discovery"')
|
expect(theme.javascript_cache.content).to include('addRawTemplate)("discovery"')
|
||||||
expect(theme.javascript_cache.content).to include(
|
expect(theme.javascript_cache.content).to include(
|
||||||
"define(\"discourse/theme-#{theme.id}/discourse/controllers/discovery\"",
|
"define(\"discourse/theme-#{theme.id}/discourse/controllers/discovery\"",
|
||||||
)
|
)
|
||||||
|
@ -283,8 +283,8 @@ HTML
|
||||||
"discourse/controllers/discovery.blah",
|
"discourse/controllers/discovery.blah",
|
||||||
"discourse/controllers/discovery.js",
|
"discourse/controllers/discovery.js",
|
||||||
"discourse/templates/discovery.js",
|
"discourse/templates/discovery.js",
|
||||||
"discovery.js",
|
"raw-templates/discovery.js",
|
||||||
"other_discovery.js",
|
"raw-templates/other_discovery.js",
|
||||||
)
|
)
|
||||||
expect(map["sourceRoot"]).to eq("theme-#{theme.id}/")
|
expect(map["sourceRoot"]).to eq("theme-#{theme.id}/")
|
||||||
expect(map["sourcesContent"].length).to eq(6)
|
expect(map["sourcesContent"].length).to eq(6)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user