From a873b1b24ea298b9f9cb747fc9600ef8f1ea4e9c Mon Sep 17 00:00:00 2001 From: Hans-Georg Winkler Date: Tue, 5 Nov 2024 15:26:53 +0100 Subject: [PATCH] feat(assets) create widget.js files (#1453) --- config/esbuild/build.js | 10 ++-------- config/initializers/assets.rb | 2 +- config/initializers/sprockets_processor.rb | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 9 deletions(-) create mode 100644 config/initializers/sprockets_processor.rb diff --git a/config/esbuild/build.js b/config/esbuild/build.js index a849bb92be..268c265412 100755 --- a/config/esbuild/build.js +++ b/config/esbuild/build.js @@ -14,8 +14,7 @@ const entryPoints = require("./entrypoints") const esbuild = require("esbuild") const args = process.argv.slice(2) const watch = args.indexOf("--watch") >= 0 -const production = - args.indexOf("--production") >= 0 || process.env.RAILS_ENV === "production" +const production = args.indexOf("--production") >= 0 || process.env.RAILS_ENV === "production" const log = console.log.bind(console) const config = { @@ -40,8 +39,6 @@ const config = { ), bundle: true, platform: "browser", - // format: "esm", - // splitting: true, outdir: "app/assets/builds", plugins: [ envFilePlugin, @@ -176,10 +173,7 @@ function compile(options = {}) { return esbuild .build(config) .then(() => { - log( - green, - "◻️ Compile completed successfully with no errors! Don't worry Be Happy 🙂" - ) + log(green, "◻️ Compile completed successfully with no errors! Don't worry Be Happy 🙂") }) .catch((error) => { log(red, "Compile completed with error 😐") diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index 2eeef966fe..fe48fc34ee 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -1,7 +1,7 @@ # Be sure to restart your server when you modify this file. # Version of your assets, change this if you want to expire all your assets. -Rails.application.config.assets.version = "1.0" +Rails.application.config.assets.version = '1.0' # Add additional assets to the asset load path. # Rails.application.config.assets.paths << Emoji.images_path diff --git a/config/initializers/sprockets_processor.rb b/config/initializers/sprockets_processor.rb new file mode 100644 index 0000000000..f95124d3f8 --- /dev/null +++ b/config/initializers/sprockets_processor.rb @@ -0,0 +1,17 @@ +# The main purpose of this code is to create a copy of '_widget.js' files without the digest (hash) in the file name. +# This allows to access these assets with a consistent name without having the hash in the file name. +module NonDigestAssets + def self.call(input) + filename = input[:filename] + # check if the file is a _widget.js file + # copy the file to the public/assets folder + if filename.end_with?('_widget.js') + # without the digest in the file name + path = File.join(Rails.public_path, 'assets', File.basename(filename)) + FileUtils.cp(filename, path) + end + nil + end +end + +Sprockets.register_postprocessor 'application/javascript', NonDigestAssets