Skip to content

Commit

Permalink
Add rake task to vendor guide javascript
Browse files Browse the repository at this point in the history
The Rails guides were migrated from Turbolinks to Turbo in
0f6575a

The PR discussion explains that the file was grabbed from unpkg, and a
decision was made to not run it through a minifier.

This commit adds a rake task to automate the process of grabbing the
turbo js file using Importmap::Packager, which is what importmap-rails
uses for `bin/importmap pin --download`.

The extra Importmap module definition is necessary because the
Importmap::Packager file uses the shorthand module syntax, meaning that
an error is thrown if the Importmap module is not previously defined.
`require "importmap-rails"` would normally define this module, but one
of its dependent requires will not load outside of a Rails application.

The turbo.css file was removed as it appears to be leftover from
Turbolinks and Turbo does not provide any css files in its dist.
  • Loading branch information
skipkayhil committed Feb 13, 2023
1 parent f838a74 commit a42863f
Show file tree
Hide file tree
Showing 5 changed files with 209 additions and 124 deletions.
13 changes: 13 additions & 0 deletions guides/Rakefile
Expand Up @@ -30,6 +30,19 @@ namespace :guides do
ruby "w3c_validator.rb"
end

task :vendor_javascript do
module Importmap; end
require "importmap/packager"

packager = Importmap::Packager.new(vendor_path: "assets/javascripts")
imports = packager.import("@hotwired/turbo", from: "unpkg")
imports.each do |package, url|
umd_url = url.gsub("esm.js", "umd.js")
puts %(Vendoring "#{package}" to #{packager.vendor_path}/#{package}.js via download from #{umd_url})
packager.download(package, umd_url)
end
end

desc "Show help"
task :help do
puts <<HELP
Expand Down

0 comments on commit a42863f

Please sign in to comment.