Skip to content

Commit c93a989

Browse files
committed
Fix using trix in sprockets
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
1 parent 4c5c904 commit c93a989

File tree

3 files changed

+11308
-12254
lines changed

3 files changed

+11308
-12254
lines changed

actiontext/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1+
* Upgrade Trix to 2.0.7
2+
3+
*Hartley McGuire*
4+
5+
* Fix using Trix with Sprockets
6+
7+
*Hartley McGuire*
18

29
Please check [7-1-stable](https://github.com/rails/rails/blob/7-1-stable/actiontext/CHANGELOG.md) for previous changes.

actiontext/Rakefile

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,27 @@ namespace :test do
2626
end
2727
end
2828

29+
task :vendor_trix do
30+
module Importmap; end
31+
require "importmap/packager"
32+
33+
packager = Importmap::Packager.new(vendor_path: "app/assets/javascripts")
34+
imports = packager.import("trix", from: "unpkg")
35+
imports.each do |package, url|
36+
url.gsub!("esm.min.js", "umd.js")
37+
puts %(Vendoring "#{package}" to #{packager.vendor_path}/#{package}.js via download from #{url})
38+
packager.download(package, url)
39+
40+
css_url = url.gsub("umd.js", "css")
41+
puts %(Vendoring "#{package}" to #{packager.vendor_path}/#{package}.css via download from #{css_url})
42+
43+
response = Net::HTTP.get_response(URI(css_url))
44+
if response.code == "200"
45+
File.open(Pathname.new("app/assets/stylesheets/trix.css"), "w+") do |file|
46+
file.write response.body
47+
end
48+
end
49+
end
50+
end
51+
2952
task default: :test

0 commit comments

Comments
 (0)