From 9e2e85ffedb95f1f2798718329b03113622be3a7 Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Fri, 10 Jan 2025 14:05:51 -0600 Subject: [PATCH] Export the script to importmap paths --- app/assets/javascripts/blacklight_oembed/oembed.esm.js.map | 2 +- app/assets/javascripts/blacklight_oembed/oembed.js.map | 2 +- app/javascript/{ => blacklight-oembed}/oembed.js | 0 config/blacklight/oembed/manifest.js | 1 + config/importmap.rb | 3 +++ lib/blacklight/oembed/engine.rb | 5 +++++ lib/generators/blacklight_oembed/install_generator.rb | 4 ++++ rollup.config.js | 2 +- 8 files changed, 16 insertions(+), 3 deletions(-) rename app/javascript/{ => blacklight-oembed}/oembed.js (100%) create mode 100644 config/blacklight/oembed/manifest.js create mode 100644 config/importmap.rb diff --git a/app/assets/javascripts/blacklight_oembed/oembed.esm.js.map b/app/assets/javascripts/blacklight_oembed/oembed.esm.js.map index 495e871..fc26a85 100644 --- a/app/assets/javascripts/blacklight_oembed/oembed.esm.js.map +++ b/app/assets/javascripts/blacklight_oembed/oembed.esm.js.map @@ -1 +1 @@ -{"version":3,"file":"oembed.esm.js","sources":["../../../javascript/oembed.js"],"sourcesContent":["export default function oEmbed(elements) {\n if (!elements) return;\n\n // Ensure elements is an array-like collection\n elements = elements instanceof NodeList ? Array.from(elements) : [elements];\n\n elements.forEach(function(embedViewer) {\n const embedURL = embedViewer.dataset.embedUrl; // Get the embed URL from the data attribute\n\n if (!embedURL) return;\n\n // Fetch JSON data from the embed URL\n fetch(embedURL)\n .then(response => {\n if (!response.ok) {\n throw new Error('Network response was not ok');\n }\n return response.json();\n })\n .then(data => {\n if (data === null) {\n return;\n }\n // Set the inner HTML of the element\n embedViewer.innerHTML = data.html;\n })\n .catch(error => {\n console.error('Error fetching embed data:', error);\n });\n });\n}\n"],"names":[],"mappings":"AAAe,SAAS,MAAM,CAAC,QAAQ,EAAE;AACzC,EAAE,IAAI,CAAC,QAAQ,EAAE;;AAEjB;AACA,EAAE,QAAQ,GAAG,QAAQ,YAAY,QAAQ,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC;;AAE7E,EAAE,QAAQ,CAAC,OAAO,CAAC,SAAS,WAAW,EAAE;AACzC,IAAI,MAAM,QAAQ,GAAG,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC;;AAElD,IAAI,IAAI,CAAC,QAAQ,EAAE;;AAEnB;AACA,IAAI,KAAK,CAAC,QAAQ;AAClB,OAAO,IAAI,CAAC,QAAQ,IAAI;AACxB,QAAQ,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;AAC1B,UAAU,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC;AACxD;AACA,QAAQ,OAAO,QAAQ,CAAC,IAAI,EAAE;AAC9B,OAAO;AACP,OAAO,IAAI,CAAC,IAAI,IAAI;AACpB,QAAQ,IAAI,IAAI,KAAK,IAAI,EAAE;AAC3B,UAAU;AACV;AACA;AACA,QAAQ,WAAW,CAAC,SAAS,GAAG,IAAI,CAAC,IAAI;AACzC,OAAO;AACP,OAAO,KAAK,CAAC,KAAK,IAAI;AACtB,QAAQ,OAAO,CAAC,KAAK,CAAC,4BAA4B,EAAE,KAAK,CAAC;AAC1D,OAAO,CAAC;AACR,GAAG,CAAC;AACJ;;;;"} \ No newline at end of file +{"version":3,"file":"oembed.esm.js","sources":["../../../javascript/blacklight-oembed/oembed.js"],"sourcesContent":["export default function oEmbed(elements) {\n if (!elements) return;\n\n // Ensure elements is an array-like collection\n elements = elements instanceof NodeList ? Array.from(elements) : [elements];\n\n elements.forEach(function(embedViewer) {\n const embedURL = embedViewer.dataset.embedUrl; // Get the embed URL from the data attribute\n\n if (!embedURL) return;\n\n // Fetch JSON data from the embed URL\n fetch(embedURL)\n .then(response => {\n if (!response.ok) {\n throw new Error('Network response was not ok');\n }\n return response.json();\n })\n .then(data => {\n if (data === null) {\n return;\n }\n // Set the inner HTML of the element\n embedViewer.innerHTML = data.html;\n })\n .catch(error => {\n console.error('Error fetching embed data:', error);\n });\n });\n}\n"],"names":[],"mappings":"AAAe,SAAS,MAAM,CAAC,QAAQ,EAAE;AACzC,EAAE,IAAI,CAAC,QAAQ,EAAE;;AAEjB;AACA,EAAE,QAAQ,GAAG,QAAQ,YAAY,QAAQ,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC;;AAE7E,EAAE,QAAQ,CAAC,OAAO,CAAC,SAAS,WAAW,EAAE;AACzC,IAAI,MAAM,QAAQ,GAAG,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC;;AAElD,IAAI,IAAI,CAAC,QAAQ,EAAE;;AAEnB;AACA,IAAI,KAAK,CAAC,QAAQ;AAClB,OAAO,IAAI,CAAC,QAAQ,IAAI;AACxB,QAAQ,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;AAC1B,UAAU,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC;AACxD;AACA,QAAQ,OAAO,QAAQ,CAAC,IAAI,EAAE;AAC9B,OAAO;AACP,OAAO,IAAI,CAAC,IAAI,IAAI;AACpB,QAAQ,IAAI,IAAI,KAAK,IAAI,EAAE;AAC3B,UAAU;AACV;AACA;AACA,QAAQ,WAAW,CAAC,SAAS,GAAG,IAAI,CAAC,IAAI;AACzC,OAAO;AACP,OAAO,KAAK,CAAC,KAAK,IAAI;AACtB,QAAQ,OAAO,CAAC,KAAK,CAAC,4BAA4B,EAAE,KAAK,CAAC;AAC1D,OAAO,CAAC;AACR,GAAG,CAAC;AACJ;;;;"} \ No newline at end of file diff --git a/app/assets/javascripts/blacklight_oembed/oembed.js.map b/app/assets/javascripts/blacklight_oembed/oembed.js.map index 6a68358..c82562a 100644 --- a/app/assets/javascripts/blacklight_oembed/oembed.js.map +++ b/app/assets/javascripts/blacklight_oembed/oembed.js.map @@ -1 +1 @@ -{"version":3,"file":"oembed.js","sources":["../../../javascript/oembed.js"],"sourcesContent":["export default function oEmbed(elements) {\n if (!elements) return;\n\n // Ensure elements is an array-like collection\n elements = elements instanceof NodeList ? Array.from(elements) : [elements];\n\n elements.forEach(function(embedViewer) {\n const embedURL = embedViewer.dataset.embedUrl; // Get the embed URL from the data attribute\n\n if (!embedURL) return;\n\n // Fetch JSON data from the embed URL\n fetch(embedURL)\n .then(response => {\n if (!response.ok) {\n throw new Error('Network response was not ok');\n }\n return response.json();\n })\n .then(data => {\n if (data === null) {\n return;\n }\n // Set the inner HTML of the element\n embedViewer.innerHTML = data.html;\n })\n .catch(error => {\n console.error('Error fetching embed data:', error);\n });\n });\n}\n"],"names":[],"mappings":";;;;;;EAAe,SAAS,MAAM,CAAC,QAAQ,EAAE;EACzC,EAAE,IAAI,CAAC,QAAQ,EAAE;;EAEjB;EACA,EAAE,QAAQ,GAAG,QAAQ,YAAY,QAAQ,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC;;EAE7E,EAAE,QAAQ,CAAC,OAAO,CAAC,SAAS,WAAW,EAAE;EACzC,IAAI,MAAM,QAAQ,GAAG,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC;;EAElD,IAAI,IAAI,CAAC,QAAQ,EAAE;;EAEnB;EACA,IAAI,KAAK,CAAC,QAAQ;EAClB,OAAO,IAAI,CAAC,QAAQ,IAAI;EACxB,QAAQ,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;EAC1B,UAAU,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC;EACxD;EACA,QAAQ,OAAO,QAAQ,CAAC,IAAI,EAAE;EAC9B,OAAO;EACP,OAAO,IAAI,CAAC,IAAI,IAAI;EACpB,QAAQ,IAAI,IAAI,KAAK,IAAI,EAAE;EAC3B,UAAU;EACV;EACA;EACA,QAAQ,WAAW,CAAC,SAAS,GAAG,IAAI,CAAC,IAAI;EACzC,OAAO;EACP,OAAO,KAAK,CAAC,KAAK,IAAI;EACtB,QAAQ,OAAO,CAAC,KAAK,CAAC,4BAA4B,EAAE,KAAK,CAAC;EAC1D,OAAO,CAAC;EACR,GAAG,CAAC;EACJ;;;;;;;;"} \ No newline at end of file +{"version":3,"file":"oembed.js","sources":["../../../javascript/blacklight-oembed/oembed.js"],"sourcesContent":["export default function oEmbed(elements) {\n if (!elements) return;\n\n // Ensure elements is an array-like collection\n elements = elements instanceof NodeList ? Array.from(elements) : [elements];\n\n elements.forEach(function(embedViewer) {\n const embedURL = embedViewer.dataset.embedUrl; // Get the embed URL from the data attribute\n\n if (!embedURL) return;\n\n // Fetch JSON data from the embed URL\n fetch(embedURL)\n .then(response => {\n if (!response.ok) {\n throw new Error('Network response was not ok');\n }\n return response.json();\n })\n .then(data => {\n if (data === null) {\n return;\n }\n // Set the inner HTML of the element\n embedViewer.innerHTML = data.html;\n })\n .catch(error => {\n console.error('Error fetching embed data:', error);\n });\n });\n}\n"],"names":[],"mappings":";;;;;;EAAe,SAAS,MAAM,CAAC,QAAQ,EAAE;EACzC,EAAE,IAAI,CAAC,QAAQ,EAAE;;EAEjB;EACA,EAAE,QAAQ,GAAG,QAAQ,YAAY,QAAQ,GAAG,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,QAAQ,CAAC;;EAE7E,EAAE,QAAQ,CAAC,OAAO,CAAC,SAAS,WAAW,EAAE;EACzC,IAAI,MAAM,QAAQ,GAAG,WAAW,CAAC,OAAO,CAAC,QAAQ,CAAC;;EAElD,IAAI,IAAI,CAAC,QAAQ,EAAE;;EAEnB;EACA,IAAI,KAAK,CAAC,QAAQ;EAClB,OAAO,IAAI,CAAC,QAAQ,IAAI;EACxB,QAAQ,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE;EAC1B,UAAU,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC;EACxD;EACA,QAAQ,OAAO,QAAQ,CAAC,IAAI,EAAE;EAC9B,OAAO;EACP,OAAO,IAAI,CAAC,IAAI,IAAI;EACpB,QAAQ,IAAI,IAAI,KAAK,IAAI,EAAE;EAC3B,UAAU;EACV;EACA;EACA,QAAQ,WAAW,CAAC,SAAS,GAAG,IAAI,CAAC,IAAI;EACzC,OAAO;EACP,OAAO,KAAK,CAAC,KAAK,IAAI;EACtB,QAAQ,OAAO,CAAC,KAAK,CAAC,4BAA4B,EAAE,KAAK,CAAC;EAC1D,OAAO,CAAC;EACR,GAAG,CAAC;EACJ;;;;;;;;"} \ No newline at end of file diff --git a/app/javascript/oembed.js b/app/javascript/blacklight-oembed/oembed.js similarity index 100% rename from app/javascript/oembed.js rename to app/javascript/blacklight-oembed/oembed.js diff --git a/config/blacklight/oembed/manifest.js b/config/blacklight/oembed/manifest.js new file mode 100644 index 0000000..00cfa01 --- /dev/null +++ b/config/blacklight/oembed/manifest.js @@ -0,0 +1 @@ +//= link_tree ../../../javascript .js diff --git a/config/importmap.rb b/config/importmap.rb new file mode 100644 index 0000000..27125ea --- /dev/null +++ b/config/importmap.rb @@ -0,0 +1,3 @@ +# frozen_string_literal: true + +pin_all_from File.expand_path("../app/javascript/blacklight-oembed", __dir__), under: "blacklight-oembed" diff --git a/lib/blacklight/oembed/engine.rb b/lib/blacklight/oembed/engine.rb index fa17490..00a3ffe 100644 --- a/lib/blacklight/oembed/engine.rb +++ b/lib/blacklight/oembed/engine.rb @@ -10,6 +10,11 @@ class Engine < Rails::Engine # Allows an adopter to pass additional parameters through to an OEmbed # service. Examples of this could include `:canvas_index`, or `:max_width` config.additional_params = [] + + initializer 'blacklight_oembed.importmap', before: 'importmap' do |app| + app.config.assets.paths << Engine.root.join("app/javascript") + app.config.importmap.paths << Engine.root.join('config/importmap.rb') if app.config.respond_to?(:importmap) + end end end end diff --git a/lib/generators/blacklight_oembed/install_generator.rb b/lib/generators/blacklight_oembed/install_generator.rb index 697421f..8fcf55c 100644 --- a/lib/generators/blacklight_oembed/install_generator.rb +++ b/lib/generators/blacklight_oembed/install_generator.rb @@ -18,6 +18,10 @@ def inject_oembed_configuration copy_file "oembed_providers.rb", "config/initializers/oembed_providers.rb" end + def appease_sprockets4 + append_to_file 'app/assets/config/manifest.js', "\n//= link blacklight/oembed/manifest.js" + end + def configuration inject_into_file 'app/controllers/catalog_controller.rb', after: 'configure_blacklight do |config|' do if ENV['CI_TEST_LEGACY_CONFIGURATION'].present? || Gem::Version.new(Blacklight::VERSION) < Gem::Version.new('8.0') diff --git a/rollup.config.js b/rollup.config.js index 1e928e1..64d3ae5 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -14,7 +14,7 @@ let includePathOptions = { }; const rollupConfig = { - input: 'app/javascript/oembed.js', + input: 'app/javascript/blacklight-oembed/oembed.js', output: { file: `app/assets/javascripts/blacklight_oembed/${fileDest}.js`, format: ESM ? 'es' : 'umd',