Skip to content

Commit

Permalink
Add support for Propshaft
Browse files Browse the repository at this point in the history
Adds Propshaft support but still maintain Sprockets support.

In order to make this happen we need to deal with Tinymce
assets. They need to be available without the digest. Before
we were using the `NonStupidDigestAssets` gem.

Now we enhance the `assets:precompile` Rake task and
simply copy the digested assets to the non digested name.

In development we return the undigested asset if requested.
  • Loading branch information
tvdeyen committed Oct 6, 2024
1 parent 88075e2 commit 020f08f
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 20 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,5 @@ gem "web-console", "~> 4.2", group: :development
gem "rails_live_reload", "~> 0.3.5"

gem "dartsass-rails", "~> 0.5.0"

gem "propshaft", "~> 1.0"
1 change: 0 additions & 1 deletion alchemy_cms.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency "originator", ["~> 3.1"]
gem.add_runtime_dependency "ransack", [">= 1.8", "< 5.0"]
gem.add_runtime_dependency "simple_form", [">= 4.0", "< 6"]
gem.add_runtime_dependency "sprockets-rails", [">= 3.5", "< 4"]
gem.add_runtime_dependency "turbo-rails", [">= 1.4", "< 2.1"]
gem.add_runtime_dependency "view_component", ["~> 3.0"]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@
font-family: 'tinymce-mobile';
font-style: normal;
font-weight: normal;
src: url('fonts/tinymce-mobile.woff?8x92w3') format('woff');
src: url('/fonts/tinymce-mobile.woff?8x92w3') format('woff');
}
@media (min-device-width: 700px) {
.tinymce-mobile-outer-container,
Expand Down

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions app/views/alchemy/admin/tinymce/_setup.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
<% asset_host = ActionController::Base.config.asset_host %>

<link rel="preload" href="<%= asset_host %><%= assets_prefix %>/tinymce/skins/ui/alchemy/skin.min.css" as="style" />
<link rel="preload" href="<%= asset_path("tinymce/skins/ui/alchemy/skin.min.css") %>" as="style" />
<% if Alchemy::Tinymce.init[:content_css] %>
<link rel="preload" href="<%= asset_host %><%= Alchemy::Tinymce.init[:content_css] %>" as="style" />
<link rel="preload" href="<%= asset_path(Alchemy::Tinymce.init[:content_css]) %>" as="style" />
<% end %>
<% Alchemy::Tinymce.preloadable_plugins.each do |plugin| %>
<link rel="preload" href="<%= asset_host %><%= assets_prefix %>/tinymce/plugins/<%= plugin %>/plugin.min.js" as="script">
<link rel="preload" href="<%= asset_path("tinymce/plugins/#{plugin}/plugin.min.js") %>" as="script">
<% end %>

<script>
// Setting TinyMCE path.
var tinyMCEPreInit = {
<% if ActionController::Base.config.asset_host_set? %>
base: '<%= asset_url(assets_prefix + '/tinymce') %>',
<% tinymce_base_path = "#{Rails.application.config.assets.prefix}/tinymce" %>
<% if ActionController::Base.config.asset_host %>
base: '<%= "#{ActionController::Base.config.asset_host}#{tinymce_base_path}" %>',
<% else %>
base: '<%= asset_path(assets_prefix + '/tinymce') %>',
base: '<%= tinymce_base_path %>',
<% end %>
suffix: '.min'
};
Expand Down
4 changes: 0 additions & 4 deletions config/initializers/assets.rb

This file was deleted.

18 changes: 16 additions & 2 deletions lib/alchemy/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,22 @@ class Engine < Rails::Engine
end
end

initializer "alchemy.non_digest_assets" do
NonStupidDigestAssets.whitelist += [/^tinymce\//]
initializer "alchemy.assets" do
if defined?(Sprockets)
require_relative "../non_stupid_digest_assets"
NonStupidDigestAssets.whitelist += [/^tinymce\//]
Rails.application.config.assets.precompile << "alchemy_manifest.js"
end
end

initializer "alchemy.propshaft" do |app|
if defined?(Propshaft)
if app.config.assets.server
# Monkey-patch Propshaft::Asset to enable access
# of TinyMCE assets without a hash digest.
require_relative "propshaft/tinymce_asset"
end
end
end

initializer "alchemy.importmap" do |app|
Expand Down
15 changes: 15 additions & 0 deletions lib/alchemy/propshaft/tinymce_asset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "propshaft/asset"

module Alchemy
module Propshaft
module TinymceAsset
# Allow TinyMCE assets to be accessed (in development mode) without a digest
def fresh?(digest)
super ||
(digest.blank? && logical_path.to_s.starts_with?("tinymce/"))
end
end
end
end

Propshaft::Asset.prepend Alchemy::Propshaft::TinymceAsset
2 changes: 0 additions & 2 deletions lib/alchemy_cms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
require "gutentag"
require "importmap-rails"
require "kaminari"
require "non_stupid_digest_assets"
require "ransack"
require "simple_form"
require "sprockets/rails"
require "turbo-rails"
require "userstamp"
require "view_component"
Expand Down
14 changes: 14 additions & 0 deletions lib/tasks/alchemy/assets.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
if Rake::Task.task_defined?("assets:precompile")
Rake::Task["assets:precompile"].enhance do
manifest_path = Rails.application.config.assets.manifest_path
assets_path = Rails.root.join("public#{Rails.application.config.assets.prefix}")
manifest = JSON.parse(manifest_path.read)
manifest.select { |k| k.include?("tinymce/") }.each do |k, v|
Propshaft.logger.info "Copying #{v} to #{k}"
FileUtils.cp(
assets_path.join(v),
assets_path.join(k)
)
end
end
end
1 change: 0 additions & 1 deletion spec/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
require "active_job/railtie"
# require "action_cable/engine"
# require "rails/test_unit/railtie"
require "sprockets/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Expand Down

0 comments on commit 020f08f

Please sign in to comment.