-
Notifications
You must be signed in to change notification settings - Fork 107
/
tag_helpers.rb
30 lines (24 loc) · 1.13 KB
/
tag_helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# frozen_string_literal: true
# Public: Allows to render HTML tags for scripts and styles processed by Vite.
module VitePluginLegacy::TagHelpers
# Public: Renders a <script> tag for the specified Vite entrypoints when using
# @vitejs/plugin-legacy, which injects polyfills.
def vite_legacy_javascript_tag(name, asset_type: :javascript)
return if ViteRuby.instance.dev_server_running?
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
}
safe_join [vite_legacy_polyfill_tag, import_tag]
end
# Public: Same as `vite_legacy_javascript_tag`, but for TypeScript entries.
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
# browsers that do not support modules.
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))
end
end