discourse/lib/ember_cli.rb
David Taylor c124c69833
DEV: Simplify sprockets configuration (#24111)
- Remove the wildcard crawler. This was already excluding almost all file types, but the exclude list was missing '.gjs' which meant those files were unnecessarily being hoisted into the `public/` directory during precompile

- Automatically include all ember-cli-generated assets without needing them to be listed. The main motivation for this change is to allow us to start using async imports via Embroider/Webpack. The filenames for those new async bundles will not be known in advance.

- Skips sprockets fingerprinting on Embroider/Webpack chunk JS files. Their filenames already include a fingerprint, and having sprockets change the filenames will cause problems for the async import feature (where filenames are included deep inside js bundles)

This commit also updates our ember-cli build so that it skips building plugin tests in the production environment. This should provide a slight build speed improvement.
2023-10-26 17:29:53 +01:00

87 lines
2.1 KiB
Ruby

# frozen_string_literal: true
module EmberCli
def self.dist_dir
"#{Rails.root}/app/assets/javascripts/discourse/dist"
end
def self.assets
@@assets ||= Dir.glob("**/*.{js,map,txt}", base: "#{dist_dir}/assets")
end
def self.script_chunks
return @@chunk_infos if defined?(@@chunk_infos)
chunk_infos = {}
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
rescue Errno::ENOENT
{}
end
def self.parse_source_map_path(file)
File.read("#{dist_dir}/assets/#{file}")[%r{//# sourceMappingURL=(.*)$}, 1]
end
def self.is_ember_cli_asset?(name)
assets.include?(name) || script_chunks.values.flatten.include?(name.delete_suffix(".js"))
end
def self.ember_version
@version ||=
begin
ember_source_package_raw =
File.read("#{Rails.root}/app/assets/javascripts/node_modules/ember-source/package.json")
JSON.parse(ember_source_package_raw)["version"]
end
end
def self.workbox_dir_name
return @workbox_base_dir if defined?(@workbox_base_dir)
@workbox_base_dir =
if (full_path = Dir.glob("app/assets/javascripts/discourse/dist/assets/workbox-*")[0])
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
def self.has_tests?
File.exist?("#{dist_dir}/tests/index.html")
end
def self.clear_cache!
@@chunk_infos = nil
@@assets = nil
end
end