From 1d48273c6edb1b6dc8abb42e04edbd84cca8cc76 Mon Sep 17 00:00:00 2001 From: Andrii Dmytrenko Date: Wed, 27 Oct 2021 15:37:40 +0100 Subject: [PATCH 1/5] Add id to vite-legacy-polyfill tag --- vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb b/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb index eef92c4f..9145924b 100644 --- a/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb +++ b/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb @@ -25,6 +25,6 @@ def vite_legacy_typescript_tag(name) def vite_legacy_polyfill_tag return if ViteRuby.instance.dev_server_running? - content_tag(:script, nil, nomodule: true, src: vite_asset_path('legacy-polyfills', type: :virtual)) + content_tag(:script, nil, nomodule: true, id: 'vite-legacy-polyfill', src: vite_asset_path('legacy-polyfills', type: :virtual)) end end From 828c2f3c1f7cbfc49c97b321d46c04b67790aedb Mon Sep 17 00:00:00 2001 From: Andrii Dmytrenko Date: Wed, 27 Oct 2021 16:00:40 +0100 Subject: [PATCH 2/5] Load legacy assets if dynamic modules are not supported --- .../lib/vite_plugin_legacy/tag_helpers.rb | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb b/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb index 9145924b..591ec10c 100644 --- a/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb +++ b/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb @@ -9,10 +9,10 @@ def vite_legacy_javascript_tag(name, asset_type: :javascript) legacy_name = name.sub(/(\..+)|$/, '-legacy\1') import_tag = content_tag(:script, nomodule: true) { - "System.import('#{ vite_asset_path(legacy_name, type: asset_type) }')".html_safe + vite_legacy_import_body(name, asset_type: asset_type) } - safe_join [vite_legacy_polyfill_tag, import_tag] + import_tag end # Public: Same as `vite_legacy_javascript_tag`, but for TypeScript entries. @@ -20,11 +20,27 @@ def vite_legacy_typescript_tag(name) vite_legacy_javascript_tag(name, asset_type: :typescript) end - # Internal: Renders the vite-legacy-polyfill to enable code splitting in + # Renders the vite-legacy-polyfill to enable code splitting in # browsers that do not support modules. - def vite_legacy_polyfill_tag + # Entrypoints in format: {"entrypoint_name" => asset_type } + # e.g.: { "application" => :typescript } + def vite_legacy_polyfill_tag(entrypoints) return if ViteRuby.instance.dev_server_running? - content_tag(:script, nil, nomodule: true, id: 'vite-legacy-polyfill', src: vite_asset_path('legacy-polyfills', type: :virtual)) + tags = [] + tags.push(content_tag(:script, nil, nomodule: true, id: 'vite-legacy-polyfill', src: vite_asset_path('legacy-polyfills', type: :virtual))) + entrypoints.each do |entrypoint, asset_type| + tags.push(content_tag(:script, nil, type: 'module') { vite_dynamic_fallback_inline_code(entrypoint, asset_type: asset_type) }) + end + safe_join(tags, "\n") + end + + def vite_dynamic_fallback_inline_code(name, asset_type: :javascript) + %Q{!function(){try{new Function("m","return import(m)")}catch(o){console.warn("vite: loading legacy build because dynamic import is unsupported, syntax error above should be ignored");var e=document.getElementById("vite-legacy-polyfill"),n=document.createElement("script");n.src=e.src,n.onload=function(){#{vite_legacy_import_body(name, asset_type: asset_type)}},document.body.appendChild(n)}}();}.html_safe + end + + def vite_legacy_import_body(name, asset_type: :javascript) + legacy_name = name.sub(/(\..+)|$/, '-legacy\1') + "System.import('#{ vite_asset_path(legacy_name, type: asset_type) }')".html_safe end end From 223c4572cd5db243ff2bb8e1be5b60d58734d5ea Mon Sep 17 00:00:00 2001 From: Andrii Dmytrenko Date: Wed, 27 Oct 2021 16:52:41 +0100 Subject: [PATCH 3/5] Add safari nomodule fix --- vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb b/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb index 591ec10c..ab2e124b 100644 --- a/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb +++ b/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb @@ -2,6 +2,9 @@ # Public: Allows to render HTML tags for scripts and styles processed by Vite. module VitePluginLegacy::TagHelpers + VITE_SAFARI_NOMODULE_FIX = <<-JS.html_safe.freeze + !function(){var e=document,t=e.createElement("script");if(!("noModule"in t)&&"onbeforeload"in t){var n=!1;e.addEventListener("beforeload",(function(e){if(e.target===t)n=!0;else if(!e.target.hasAttribute("nomodule")||!n)return;console.log('preventing load',e.target);e.preventDefault()}),!0),t.type="module",t.src=".",e.head.appendChild(t),t.remove()}}(); + JS # Public: Renders a