Skip to content

Commit

Permalink
Fix using trix in sprockets
Browse files Browse the repository at this point in the history
When Trix was [updated][1] from 1.3.1 to 2.0.4, the ESM bundle of 2.0.4
was used instead of the UMD bundle (the vendored 1.3.1 file used the
UMD bundle). This leads to issues when trying to use Trix with sprockets
because the ESM bundle declares variables like they are scoped to the
file but sprockets will see them as scoped globally.

This commit fixes the issue by replacing the Trix ESM bundle with the
UMD bundle (and upgrades it from 2.0.4 to 2.0.7). Additionally, a Rake
task has been added similar to one previously [added][2] to the guides
for automatic vendoring using Importmap::Packager.

[1]: fab1b52
[2]: a42863f
  • Loading branch information
skipkayhil committed Oct 25, 2023
1 parent 4c5c904 commit c93a989
Show file tree
Hide file tree
Showing 3 changed files with 11,308 additions and 12,254 deletions.
7 changes: 7 additions & 0 deletions actiontext/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
* Upgrade Trix to 2.0.7

*Hartley McGuire*

* Fix using Trix with Sprockets

*Hartley McGuire*

Please check [7-1-stable](https://github.com/rails/rails/blob/7-1-stable/actiontext/CHANGELOG.md) for previous changes.
23 changes: 23 additions & 0 deletions actiontext/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,27 @@ namespace :test do
end
end

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

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

css_url = url.gsub("umd.js", "css")
puts %(Vendoring "#{package}" to #{packager.vendor_path}/#{package}.css via download from #{css_url})

response = Net::HTTP.get_response(URI(css_url))
if response.code == "200"
File.open(Pathname.new("app/assets/stylesheets/trix.css"), "w+") do |file|
file.write response.body
end
end
end
end

task default: :test
Loading

0 comments on commit c93a989

Please sign in to comment.