diff --git a/app/assets/javascripts/discourse/app/index.html b/app/assets/javascripts/discourse/app/index.html
index 6cb6f952edb..2ce49cd5b40 100644
--- a/app/assets/javascripts/discourse/app/index.html
+++ b/app/assets/javascripts/discourse/app/index.html
@@ -22,9 +22,11 @@
-
-
+
+
+
+
{{content-for "head"}}
diff --git a/app/assets/javascripts/discourse/package.json b/app/assets/javascripts/discourse/package.json
index 310092f8ec7..43ff2d4d246 100644
--- a/app/assets/javascripts/discourse/package.json
+++ b/app/assets/javascripts/discourse/package.json
@@ -59,7 +59,6 @@
"discourse-common": "1.0.0",
"discourse-plugins": "1.0.0",
"ember-auto-import": "^2.6.3",
- "ember-auto-import-chunks-json-generator": "^1.1.0",
"ember-buffered-proxy": "^2.1.1",
"ember-cached-decorator-polyfill": "^1.0.2",
"ember-cli": "~5.0.0",
diff --git a/app/assets/javascripts/discourse/tests/index.html b/app/assets/javascripts/discourse/tests/index.html
index 6ccffa0d23a..004e7613e16 100644
--- a/app/assets/javascripts/discourse/tests/index.html
+++ b/app/assets/javascripts/discourse/tests/index.html
@@ -49,10 +49,17 @@
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
diff --git a/app/assets/javascripts/yarn.lock b/app/assets/javascripts/yarn.lock
index 9e569273693..20aa81a5d8a 100644
--- a/app/assets/javascripts/yarn.lock
+++ b/app/assets/javascripts/yarn.lock
@@ -3773,16 +3773,6 @@ electron-to-chromium@^1.4.477:
resolved "https://registry.yarnpkg.com/electron-to-chromium/-/electron-to-chromium-1.4.496.tgz#a57534b70d2bdee7e1ad7dbd4c91e560cbd08db1"
integrity sha512-qeXC3Zbykq44RCrBa4kr8v/dWzYJA8rAwpyh9Qd+NKWoJfjG5vvJqy9XOJ9H4P/lqulZBCgUWAYi+FeK5AuJ8g==
-ember-auto-import-chunks-json-generator@^1.1.0:
- version "1.1.0"
- resolved "https://registry.yarnpkg.com/ember-auto-import-chunks-json-generator/-/ember-auto-import-chunks-json-generator-1.1.0.tgz#9b3ee4b6e2f274fdbf722762fc9d72f13942a594"
- integrity sha512-50JvuBVD5mLx+9YMcBLlV8HIVdtdHHzEfVwID8jo8yRo83x5SAKULdSH+ZwwNTYcUNI7amMKDl9RA5LBGzbDWA==
- dependencies:
- broccoli-merge-trees "^4.2.0"
- broccoli-plugin "^4.0.7"
- ember-cli-babel "^7.26.11"
- ember-cli-htmlbars "^6.1.1"
-
ember-auto-import@^2.2.3, ember-auto-import@^2.5.0, ember-auto-import@^2.6.0, ember-auto-import@^2.6.3:
version "2.6.3"
resolved "https://registry.yarnpkg.com/ember-auto-import/-/ember-auto-import-2.6.3.tgz#f18d1b93dd10b08ba5496518436f9d56dd4e000a"
diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb
index 296b91a45be..cdaf0ee9e97 100644
--- a/app/helpers/application_helper.rb
+++ b/app/helpers/application_helper.rb
@@ -133,10 +133,12 @@ module ApplicationHelper
end
def preload_script(script)
- scripts = [script]
+ scripts = []
if chunks = EmberCli.script_chunks[script]
scripts.push(*chunks)
+ else
+ scripts.push(script)
end
scripts
diff --git a/lib/ember_cli.rb b/lib/ember_cli.rb
index 291d4429d99..680c83249af 100644
--- a/lib/ember_cli.rb
+++ b/lib/ember_cli.rb
@@ -1,6 +1,10 @@
# frozen_string_literal: true
module EmberCli
+ def self.dist_dir
+ "#{Rails.root}/app/assets/javascripts/discourse/dist"
+ end
+
def self.assets
@assets ||=
begin
@@ -37,19 +41,17 @@ module EmberCli
def self.script_chunks
return @@chunk_infos if defined?(@@chunk_infos)
- raw_chunk_infos =
- JSON.parse(
- File.read("#{Rails.configuration.root}/app/assets/javascripts/discourse/dist/chunks.json"),
- )
+ chunk_infos = {}
- chunk_infos =
- raw_chunk_infos["scripts"]
- .map do |info|
- logical_name = info["afterFile"][%r{\Aassets/(.*)\.js\z}, 1]
- chunks = info["scriptChunks"].map { |filename| filename[%r{\Aassets/(.*)\.js\z}, 1] }
- [logical_name, chunks]
- end
- .to_h
+ begin
+ test_html = File.read("#{dist_dir}/tests/index.html")
+ chunk_infos.merge! parse_chunks_from_html(test_html)
+ rescue Errno::ENOENT
+ # production build
+ end
+
+ index_html = File.read("#{dist_dir}/index.html")
+ chunk_infos.merge! parse_chunks_from_html(index_html)
@@chunk_infos = chunk_infos if Rails.env.production?
chunk_infos
@@ -78,4 +80,23 @@ module EmberCli
File.basename(full_path)
end
end
+
+ def self.parse_chunks_from_html(html)
+ doc = Nokogiri::HTML5.parse(html)
+
+ chunk_infos = {}
+
+ doc
+ .css("discourse-chunked-script")
+ .each do |discourse_script|
+ entrypoint = discourse_script.attr("entrypoint")
+ chunk_infos[entrypoint] = discourse_script
+ .css("script[src]")
+ .map do |script|
+ script.attr("src").delete_prefix("#{Discourse.base_path}/assets/").delete_suffix(".js")
+ end
+ end
+
+ chunk_infos
+ end
end
diff --git a/spec/lib/ember_cli_spec.rb b/spec/lib/ember_cli_spec.rb
index 3b125311e77..59de34d6709 100644
--- a/spec/lib/ember_cli_spec.rb
+++ b/spec/lib/ember_cli_spec.rb
@@ -1,9 +1,44 @@
# frozen_string_literal: true
describe EmberCli do
- describe "#ember_version" do
+ describe ".ember_version" do
it "works" do
expect(EmberCli.ember_version).to match(/\A\d+\.\d+/)
end
end
+
+ describe ".parse_chunks_from_html" do
+ def generate_html
+ <<~HTML
+
+
+
+
+
+
+
+
+ Hello world
+
+
+ HTML
+ end
+
+ it "can parse chunks for a normal site" do
+ chunks = EmberCli.parse_chunks_from_html generate_html
+ expect(chunks["discourse"]).to eq(%w[firstchunk secondchunk])
+ end
+
+ it "can parse chunks for a subfolder site" do
+ set_subfolder "/discuss"
+
+ html = generate_html
+
+ # sanity check that our fixture is working
+ expect(html).to include("/discuss/assets/firstchunk.js")
+
+ chunks = EmberCli.parse_chunks_from_html html
+ expect(chunks["discourse"]).to eq(%w[firstchunk secondchunk])
+ end
+ end
end