fix(vite): porte vite fallback logic from vite legacy plugin

This commit is contained in:
Paul Chavard 2022-07-01 10:08:27 +02:00
parent 23fe07bfbd
commit 72636f3332
5 changed files with 39 additions and 27 deletions

View file

@ -86,7 +86,6 @@ gem 'strong_migrations' # lint database migrations
gem 'turbo-rails'
gem 'typhoeus'
gem 'view_component'
gem 'vite_plugin_legacy'
gem 'vite_rails'
gem 'warden'
gem 'zipline'

View file

@ -749,8 +749,6 @@ GEM
axiom-types (~> 0.1)
coercible (~> 1.0)
descendants_tracker (~> 0.0, >= 0.0.3)
vite_plugin_legacy (3.0.2)
vite_ruby (~> 3.0, >= 3.0.4)
vite_rails (3.0.9)
railties (>= 5.1, < 8)
vite_ruby (~> 3.0)
@ -912,7 +910,6 @@ DEPENDENCIES
typhoeus
vcr
view_component
vite_plugin_legacy
vite_rails
warden
web-console

View file

@ -189,21 +189,4 @@ module ApplicationHelper
Rails.env.production? || ENV['VITE_LEGACY'] == 'enabled'
end
end
def ds_vite_legacy_javascript_tag(name, asset_type: :javascript)
legacy_name = name.sub(/(\..+)|$/, '-legacy\1')
import_tag = tag(:script) do
# rubocop:disable Rails/OutputSafety
"System.import('#{vite_asset_path(legacy_name, type: asset_type)}')".html_safe
# rubocop:enable Rails/OutputSafety
end
safe_join [ds_vite_legacy_polyfill_tag, import_tag]
end
# Internal: Renders the vite-legacy-polyfill to enable code splitting in
# browsers that do not support modules.
def ds_vite_legacy_polyfill_tag
tag(:script, nil, src: vite_asset_path('legacy-polyfills', type: :virtual))
end
end

View file

@ -0,0 +1,31 @@
module ViteHelper
SAFARI_10_NO_MODULE_FIX = "!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;e.preventDefault()}),!0),t.type='module',t.src='.',e.head.appendChild(t),t.remove()}}();"
LEGACY_POLYFILL_ID = 'vite-legacy-polyfill'
LEGACY_ENTRY_ID = 'vite-legacy-entry'
SYSTEM_JS_INLINE_CODE = "document.querySelectorAll('script[data-legacy-entry]').forEach((e) => System.import(e.getAttribute('data-src')))"
DETECT_MODERN_BROWSER_VARNAME = '__vite_is_modern_browser'
DETECT_MODERN_BROWSER_CODE = "try{import.meta.url;import('_').catch(()=>1);}catch(e){}window.#{DETECT_MODERN_BROWSER_VARNAME}=true;"
DYNAMIC_FALLBACK_INLINE_CODE = "!function(){if(window.#{DETECT_MODERN_BROWSER_VARNAME})return;console.warn('vite: loading legacy build because dynamic import or import.meta.url is unsupported, syntax error above should be ignored');var e=document.getElementById('#{LEGACY_POLYFILL_ID}'),n=document.createElement('script');n.src=e.src,n.onload=function(){#{SYSTEM_JS_INLINE_CODE}},document.body.appendChild(n)}();"
def vite_legacy_javascript_tag(name, asset_type: :javascript)
legacy_name = name.sub(/(\..+)|$/, '-legacy\1')
src = vite_asset_path(legacy_name, type: :virtual)
javascript_include_tag(src, nomodule: true, 'data-legacy-entry': true, 'data-src': src)
end
def vite_legacy_polyfill_tag
safe_join [
javascript_tag(SAFARI_10_NO_MODULE_FIX, type: :module, nonce: true),
javascript_include_tag(vite_asset_path('legacy-polyfills', type: :virtual), nomodule: true, id: LEGACY_POLYFILL_ID)
]
end
def vite_legacy_fallback_tag
safe_join [
javascript_tag(DETECT_MODERN_BROWSER_CODE, type: :module, nonce: true),
javascript_tag(DYNAMIC_FALLBACK_INLINE_CODE, type: :module, nonce: true)
]
end
end

View file

@ -15,14 +15,16 @@
= vite_client_tag
= vite_react_refresh_tag
= vite_javascript_tag 'application'
- if administrateur_signed_in?
= vite_javascript_tag 'track-admin'
- if vite_legacy?
= ds_vite_legacy_javascript_tag 'application'
= vite_legacy_polyfill_tag
= vite_legacy_javascript_tag 'application'
- if administrateur_signed_in?
= ds_vite_legacy_javascript_tag 'track-admin'
- else
= vite_javascript_tag 'application'
- if administrateur_signed_in?
= vite_javascript_tag 'track-admin'
= vite_legacy_javascript_tag 'track-admin'
= vite_legacy_fallback_tag
= preload_link_tag(asset_url("Muli-Regular.woff2"))
= preload_link_tag(asset_url("Muli-Bold.woff2"))