Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Backport code from @vitejs/plugin-legacy to vite_plugin_legacy #296

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions docs/src/guide/plugin-legacy.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[vite_plugin_legacy]: https://github.com/ElMassimo/vite_ruby/tree/main/vite_plugin_legacy
[plugin-legacy]: https://github.com/vitejs/vite/tree/main/packages/plugin-legacy
[vite_legacy_javascript_tag]: https://github.com/ElMassimo/vite_ruby/blob/main/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb
[vite_legacy_typescript_tag]: https://github.com/ElMassimo/vite_ruby/blob/main/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb
[vite_legacy_polyfill_tag]: https://github.com/ElMassimo/vite_ruby/blob/main/vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb

# Plugin Legacy

Expand All @@ -27,11 +25,8 @@ bundle install

In order to include the polyfills and script tags you can using the following helpers:

- <kbd>[vite_legacy_javascript_tag]</kbd>: Render a `<script>` tag referencing a JavaScript file.
- <kbd>[vite_legacy_typescript_tag]</kbd>: Render a `<script>` tag referencing a TypeScript file.
- <kbd>[vite_legacy_polyfill_tag]</kbd>: Renders the polyfills necessary to enable code-splitting in legacy browsers.
- <kbd>[vite_legacy_javascript_tag]</kbd>: Render a `<script>` tag referencing a JavaScript or TypeScript entrypoints.

The polyfill is included by default when using <kbd>[vite_legacy_javascript_tag]</kbd>

```erb
<head>
Expand All @@ -40,11 +35,11 @@ The polyfill is included by default when using <kbd>[vite_legacy_javascript_tag]
<%= csp_meta_tag %>
<%= vite_client_tag %>

<%= vite_legacy_javascript_tag 'application' => :javascript %>
<%= vite_javascript_tag 'application' %>
</head>
<body>
<%= yield %>
<%= vite_legacy_javascript_tag 'application' %>
</body>
```

Expand Down
2 changes: 1 addition & 1 deletion examples/rails/app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

<link rel="icon" type="image/png" href="<%= vite_asset_path('images/logo.png') %>">

<%= vite_legacy_javascript_tag 'application' => :typescript %>
<%= vite_stylesheet_tag 'styles.scss' %>
<%= vite_typescript_tag 'application', 'data-turbo-track': 'reload', media: 'all' %>
</head>

<body>
<%= yield %>
<%= vite_legacy_typescript_tag 'application' %>
</body>
</html>
6 changes: 3 additions & 3 deletions test/helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class LegacyHelperTest < HelperTestCase
})

def test_plugin_legacy
assert_includes vite_legacy_javascript_tag('/app/assets/external'), '/vite-production/assets/external.a35ee0db-legacy.js'
assert_includes vite_legacy_typescript_tag('main.ts'), '/vite-production/assets/main.20bbd3a5-legacy.js'
assert_includes vite_legacy_polyfill_tag, '/vite-production/assets/polyfills-legacy.07477394.js'
assert_includes vite_legacy_javascript_tag('/app/assets/external' => :javascript), '/vite-production/assets/external.a35ee0db-legacy.js'
assert_includes vite_legacy_javascript_tag('main.ts' => :typescript), '/vite-production/assets/main.20bbd3a5-legacy.js'
assert_includes vite_legacy_javascript_tag('main.ts' => :typescript), '/vite-production/assets/polyfills-legacy.07477394.js'
end
end

Expand Down
59 changes: 42 additions & 17 deletions vite_plugin_legacy/lib/vite_plugin_legacy/tag_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,54 @@

# 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?
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

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
}
# Renders code to load vite entrypoints for legacy browsers:
# * Safari NOMODULE fix for Safari 10, which supports modules but not `nomodule`
# * vite-legacy-polyfill (System.import polyfill) for browsers that do not support modules @vitejs/plugin-legacy
# * Dynamic import code for browsers that support modules, but not dynamic imports
# This helper must be called before any other Vite import tags.
# Accepts a hash with entrypoint names as keys and asset types (:javascript or :typescript) as values.
def vite_legacy_javascript_tag(entrypoints)
return if ViteRuby.instance.dev_server_running?

safe_join [vite_legacy_polyfill_tag, import_tag]
tags = []
safari_nomodule_fix = content_tag(:script, nil, nomodule: true) { VITE_SAFARI_NOMODULE_FIX }
tags.push(safari_nomodule_fix)
# for browsers which do not support modules at all
legacy_polyfill = content_tag(:script, nil, nomodule: true, id: 'vite-legacy-polyfill', src: vite_asset_path('legacy-polyfills', type: :virtual))
tags.push(legacy_polyfill)
# for browsers which support modules, but don't support dynamic import
legacy_fallback_tag = content_tag(:script, nil, type: 'module') do
vite_dynamic_fallback_inline_code(entrypoints)
end
entrypoints.each do |name, asset_type|
import_tag = content_tag(:script, nomodule: true) do
vite_legacy_import_body(name, asset_type: asset_type)
end
tags.push(import_tag)
end
tags.push(legacy_fallback_tag)
safe_join(tags, "\n")
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)
def vite_dynamic_fallback_inline_code(entrypoints)
load_body = entrypoints.map do |name, asset_type|
vite_legacy_import_body(name, asset_type: asset_type)
end
load_body = safe_join(load_body, "\n")
# rubocop:disable Layout/LineLength
%{!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(){#{ load_body }},document.body.appendChild(n)}}();}.html_safe
# rubocop:enable Layout/LineLength
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?
def vite_legacy_import_body(name, asset_type: :javascript)
"System.import('#{ vite_asset_path(vite_legacy_name(name), type: asset_type) }')".html_safe
end

content_tag(:script, nil, nomodule: true, src: vite_asset_path('legacy-polyfills', type: :virtual))
def vite_legacy_name(name)
name.sub(/(\..+)|$/, '-legacy\1')
end
end
2 changes: 1 addition & 1 deletion vite_plugin_legacy/lib/vite_plugin_legacy/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module VitePluginLegacy
VERSION = '3.0.2'
VERSION = '4.0.0'
end