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.
This commit is contained in:
David Taylor 2023-10-26 17:29:53 +01:00 committed by GitHub
parent 00dc063d40
commit c124c69833
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 75 deletions

View File

@ -160,14 +160,18 @@ module.exports = {
});
},
generatePluginsTree() {
generatePluginsTree(withTests) {
if (!this.shouldLoadPlugins()) {
return mergeTrees([]);
}
const appTree = this._generatePluginAppTree();
const testTree = this._generatePluginTestTree();
const adminTree = this._generatePluginAdminTree();
return mergeTrees([appTree, testTree, adminTree]);
const trees = [
this._generatePluginAppTree(),
this._generatePluginAdminTree(),
];
if (withTests) {
trees.push(this._generatePluginTestTree());
}
return mergeTrees(trees);
},
_generatePluginAppTree() {

View File

@ -90,7 +90,7 @@ module.exports = function (defaults) {
const discoursePluginsTree = app.project
.findAddonByName("discourse-plugins")
.generatePluginsTree();
.generatePluginsTree(app.tests);
const adminTree = app.project.findAddonByName("admin").treeForAddonBundle();

View File

@ -24,37 +24,18 @@ Rails.application.config.assets.precompile += [
]
Rails.application.config.assets.precompile += %w[
discourse.js
vendor.js
admin.js
wizard.js
test-support.js
tests.js
test-site-settings.js
browser-detect.js
browser-update.js
break_string.js
markdown-it-bundle.js
service-worker.js
google-tag-manager.js
google-universal-analytics-v3.js
google-universal-analytics-v4.js
start-discourse.js
print-page.js
activate-account.js
auto-redirect.js
locales/i18n.js
discourse/app/lib/webauthn.js
confirm-new-email/confirm-new-email.js
confirm-new-email/bootstrap.js
onpopstate-handler.js
embed-application.js
scripts/discourse-test-listen-boot
scripts/discourse-boot
workbox-*/*
]
Rails.application.config.assets.precompile += EmberCli.assets.map { |name| name.sub(".js", ".map") }
Rails.application.config.assets.precompile << lambda do |logical_path, filename|
filename.start_with?(EmberCli.dist_dir) && EmberCli.assets.include?(logical_path)
end
# Precompile all available locales
unless GlobalSetting.try(:omit_base_locales)
@ -75,16 +56,9 @@ Rails.application.config.assets.precompile.delete(Sprockets::Railtie::LOOSE_APP_
# We don't want application from node_modules, only from the root
Rails.application.config.assets.precompile.delete(%r{(?:/|\\|\A)application\.(css|js)$})
Rails.application.config.assets.precompile += ["application.js"]
start_path = ::Rails.root.join("app/assets").to_s
exclude = [".es6", ".hbs", ".hbr", ".js", ".css", ".lock", ".json", ".log", ".html", ""]
Rails.application.config.assets.precompile << lambda do |logical_path, filename|
filename.start_with?(start_path) && !filename.include?("/node_modules/") &&
!filename.include?("/dist/") && !filename.include?("/patches/") &&
!exclude.include?(File.extname(logical_path))
end
Discourse
.find_plugin_js_assets(include_disabled: true)
.each { |file| Rails.application.config.assets.precompile << "#{file}.js" }
.each do |file|
Rails.application.config.assets.precompile << "#{file}.js" if file.end_with?("_extra")
end

View File

@ -6,36 +6,7 @@ module EmberCli
end
def self.assets
@assets ||=
begin
assets = %w[
discourse.js
admin.js
wizard.js
ember_jquery.js
markdown-it-bundle.js
start-discourse.js
vendor.js
]
assets +=
Dir.glob("app/assets/javascripts/discourse/scripts/*.js").map { |f| File.basename(f) }
if workbox_dir_name
assets +=
Dir
.glob("app/assets/javascripts/discourse/dist/assets/#{workbox_dir_name}/*")
.map { |f| "#{workbox_dir_name}/#{File.basename(f)}" }
end
Discourse
.find_plugin_js_assets(include_disabled: true)
.each do |file|
next if file.ends_with?("_extra") # these are still handled by sprockets
assets << "#{file}.js"
end
assets
end
@@assets ||= Dir.glob("**/*.{js,map,txt}", base: "#{dist_dir}/assets")
end
def self.script_chunks
@ -107,4 +78,9 @@ module EmberCli
def self.has_tests?
File.exist?("#{dist_dir}/tests/index.html")
end
def self.clear_cache!
@@chunk_infos = nil
@@assets = nil
end
end

View File

@ -50,11 +50,14 @@ Sprockets::DirectiveProcessor.prepend(
end,
)
# Skip digest path for workbox assets. They are already in a folder with a digest in the name.
# Skip sprockets fingerprinting for some assets
Sprockets::Asset.prepend(
Module.new do
def digest_path
return logical_path if logical_path.match?(%r{^workbox-.*/})
# Workbox assets are already in a folder with a digest in the name
return logical_path if logical_path.start_with?("workbox-")
# Webpack chunks are already named based on their contents
return logical_path if logical_path.start_with?("chunk.")
super
end
end,

View File

@ -34,6 +34,7 @@ task "assets:precompile:build" do
exec "#{compile_command} && SKIP_EMBER_CLI_COMPILE=1 bin/rake assets:precompile"
else
system compile_command, exception: true
EmberCli.clear_cache!
end
end
end
@ -71,11 +72,6 @@ task "assets:precompile:before": %w[
require "sprockets"
require "digest/sha1"
# Add ember cli chunks
chunk_files = EmberCli.script_chunks.values.flatten.map { |name| "#{name}.js" }
map_files = chunk_files.map { |file| EmberCli.parse_source_map_path(file) }
Rails.configuration.assets.precompile.push(*chunk_files, *map_files)
end
task "assets:precompile:css" => "environment" do