Skip to content

A platform for community discussion. Free, open, simple.

DEV: Simplify sprockets configuration (#24111)

David Taylorc124c698332a
diff --git a/app/assets/javascripts/discourse-plugins/index.js b/app/assets/javascripts/discourse-plugins/index.js
index 65d6daa821b..0c44dfde5ca 100644
--- a/app/assets/javascripts/discourse-plugins/index.js
+++ b/app/assets/javascripts/discourse-plugins/index.js
@@ -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() {
diff --git a/app/assets/javascripts/discourse/ember-cli-build.js b/app/assets/javascripts/discourse/ember-cli-build.js
index 50c16fab87f..ea7cc306765 100644
--- a/app/assets/javascripts/discourse/ember-cli-build.js
+++ b/app/assets/javascripts/discourse/ember-cli-build.js
@@ -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();
diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb
index b939e5d87e5..1eb5106f2f8 100644
--- a/config/initializers/assets.rb
+++ b/config/initializers/assets.rb
@@ -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
diff --git a/lib/ember_cli.rb b/lib/ember_cli.rb
index 6c7cd26633c..a71f3609f00 100644
--- a/lib/ember_cli.rb
+++ b/lib/ember_cli.rb
@@ -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
diff --git a/lib/freedom_patches/sprockets_patches.rb b/lib/freedom_patches/sprockets_patches.rb
index 449e94de35c..cde14d97a8e 100644
--- a/lib/freedom_patches/sprockets_patches.rb
+++ b/lib/freedom_patches/sprockets_patches.rb
@@ -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,
diff --git a/lib/tasks/assets.rake b/lib/tasks/assets.rake
index b4f64d0a4b6..d08d707c0dd 100644
--- a/lib/tasks/assets.rake
+++ b/lib/tasks/assets.rake
@@ -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