mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 03:31:13 +08:00
DEV: Improve strategy for identifying ember-cli JS chunks (#23336)
Our Ember build compiles assets into multiple chunks. In the past, we used the output from `ember-auto-import-chunks-json-generator` to give Rails a map of those chunks. However, that addon is specific to ember-auto-import, and is not compatible with Embroider. Instead, we can switch to parsing the html files which are output by ember-cli. These are guaranteed to have the correct JS files in the correct place. A `<discourse-chunked-script>` will allow us to easily identify which chunks belong to which entrypoint. In future, as we update more entrypoints to be compiled by Embroider/Webpack, we can easily introduce new wrappers.
This commit is contained in:
@ -37,19 +37,15 @@ 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
|
||||
chunk_infos.merge! parse_chunks_from_html("tests/index.html")
|
||||
rescue Errno::ENOENT
|
||||
# production build
|
||||
end
|
||||
|
||||
chunk_infos.merge! parse_chunks_from_html("index.html")
|
||||
|
||||
@@chunk_infos = chunk_infos if Rails.env.production?
|
||||
chunk_infos
|
||||
@ -78,4 +74,22 @@ module EmberCli
|
||||
File.basename(full_path)
|
||||
end
|
||||
end
|
||||
|
||||
def self.parse_chunks_from_html(path)
|
||||
html = File.read("#{Rails.root}/app/assets/javascripts/discourse/dist/#{path}")
|
||||
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 { |script| script.attr("src")[%r{\A/assets/(.*)\.js\z}, 1] }
|
||||
end
|
||||
|
||||
chunk_infos
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user