Project

General

Profile

Patch #44016 » 0002-Support-importmap-in-plugins.patch

Takashi Kato, 2026-05-02 09:05

View differences:

config/initializers/30-redmine.rb
end
end
# Automatically execute asset precompilation on startup in case of changes have been detected in assets
Rails.application.config.after_initialize do |app|
# Automatically execute asset precompilation on startup in case of changes have been detected in assets
if app.config.assets.redmine_detect_update && app.assets.needs_precompile?
app.assets.processor.process
end
Redmine::Plugin.loader.directories.each(&:draw_importmap)
end
Rails.application.deprecators[:redmine] = ActiveSupport::Deprecation.new('7.0', 'Redmine')
lib/generators/redmine_plugin/USAGE
create plugins/meetings/README.rdoc
create plugins/meetings/init.rb
create plugins/meetings/config/routes.rb
create plugins/meetings/config/importmap.rb
create plugins/meetings/config/locales/en.yml
create plugins/meetings/test/test_helper.rb
lib/generators/redmine_plugin/redmine_plugin_generator.rb
template 'README.rdoc', "#{plugin_path}/README.rdoc"
template 'init.rb.erb', "#{plugin_path}/init.rb"
template 'routes.rb', "#{plugin_path}/config/routes.rb"
template 'importmap.rb', "#{plugin_path}/config/importmap.rb"
template 'en_rails_i18n.yml', "#{plugin_path}/config/locales/en.yml"
template 'test_helper.rb.erb', "#{plugin_path}/test/test_helper.rb"
end
lib/generators/redmine_plugin/templates/importmap.rb
# frozen_string_literal: true
# pin "foo"
# pin_all_from "app/javascript/locales", under: "locales"
# pin_all_from "app/javascript/controllers", under: "controllers"
lib/redmine/asset_path.rb
def clear_cache
@transition_map = nil
@cached_assets_by_path = nil
super
end
end
lib/redmine/importmap.rb
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006- Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
module Redmine
module Importmap
def draw_importmap
return unless has_importmap?
begin
instance_eval(importmap_path.read, importmap_path.to_s)
rescue => e
Rails.logger.warn "Importmap Error Occured. #{e}"
end
end
def has_importmap?
importmap_path.present?
end
def importmap_path
path = File.join(full_path, 'config/importmap.rb')
if File.exist? path
Pathname.new(path)
else
nil
end
end
# with foo plugin
#
# pin 'bar'
# => import { exampleFunction } from 'plugin_assets/foo/bar'
#
# with baz theme
#
# pin 'qux'
# => import { exampleFunction } from 'themes/baz/qux'
def pin(name, to: nil, preload: true)
modified_name = File.join(asset_prefix, name)
to ||= name + '.js'
asset_name = File.join(asset_prefix, to)
importmap.pin modified_name, to: asset_name, preload: preload
end
# with foo plugin
# pin_all_from 'app/javascript/src', under: 'src'
# => import { exampleFunction } from 'plugin_assets/foo/src/example_module'
#
# with baz theme
# pin_all_from 'javascripts/src', under: 'src'
# => import { exampleFunction } from 'themes/baz/src/example_module'
#
# The path name of the stimulus controller is not changed.
def pin_all_from(directory, under: nil, to: nil, preload: true)
extension_dir = Pathname.new(full_path).relative_path_from(Rails.root).join(directory).to_s
extension_under = under === 'controllers' ? 'controllers'
: under ? File.join(asset_prefix, under)
: nil
extension_to = to ? to : File.join(asset_prefix, under)
importmap.pin_all_from extension_dir, under: extension_under, to: extension_to, preload: preload
end
private
def importmap
Rails.application.importmap
end
end
end
lib/redmine/plugin_loader.rb
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require 'redmine/importmap'
module Redmine
class PluginPath
attr_reader :assets_dir, :initializer, :prefix
include Importmap
def initialize(dir, prefix)
@dir = dir
......
def to_s
@dir
end
alias_method :full_path, :to_s
def has_assets_dir?
File.directory?(@assets_dir)
......
File.file?(@initializer)
end
def asset_prefix
"#{@prefix}/#{File.basename(@dir)}"
end
def base_dir
@base_dir ||= Pathname.new(assets_dir)
end
def asset_paths
paths = base_dir.children.select(&:directory?)
if has_importmap?
plugin_root = Pathname.new(full_path)
['app/javascript', 'vendor/javascript'].each do |dir|
jsdir = plugin_root.join(dir)
paths << jsdir if jsdir.exist?
end
end
paths
end
end
test/fixtures/plugins/foo_plugin/app/javascript/controllers/bar_controller.js
import { Controller } from "@hotwired/stimulus"
// Connects to data-controller="bar"
export default class extends Controller {
connect() {
}
}
test/fixtures/plugins/foo_plugin/app/javascript/locales/en.js
export const lang = {hello: 'hello'}
test/fixtures/plugins/foo_plugin/app/javascript/locales/ja.js
export const lang = {hello: 'こんにちは'}
test/fixtures/plugins/foo_plugin/config/importmap.rb
# frozen_string_literal: true
pin "foo"
pin_all_from "app/javascript/locales", under: "locales"
pin_all_from "app/javascript/controllers", under: "controllers"
test/test_helper.rb
end
end
end
module ImportmapTestHelper
def path_to_asset(asset)
resolver.resolve(asset)
end
end
end
test/unit/lib/redmine/plugin_loader_test.rb
def setup
clear_public
@klass = Redmine::PluginLoader
@klass.directory = Rails.root.join('test/fixtures/plugins')
@klass.load
Redmine::Plugin.clear
@original_loader = ::Redmine::Plugin.loader
loader = Redmine::PluginLoader.new directory: Rails.root.join('test/fixtures/plugins'), prefix: @original_loader.prefix
Redmine::Plugin.loader = loader
loader.public_directory = Rails.public_path.join(@original_loader.prefix)
loader.add_autoload_paths
loader.directories.each(&:run_initializer)
config = Rails.application.config.assets.deep_dup
Redmine::Plugin.all.each do |plugin|
paths = plugin.asset_paths
config.redmine_extension_paths << paths if paths.present?
end
assembly = Propshaft::Assembly.new(config)
assembly.extend Redmine::ImportmapTestHelper
assembly.load_path.clear_cache
loader.directories.each(&:draw_importmap)
json = Rails.application.importmap.to_json(resolver: assembly)
@importmap = JSON.parse(json)['imports']
end
test 'esmodule is loaded' do
assert_match %r{/assets/plugin_assets/foo_plugin/foo-\w{8}\.js}, @importmap['plugin_assets/foo_plugin/foo']
assert_match %r{/assets/plugin_assets/foo_plugin/locales/ja-\w{8}\.js}, @importmap['plugin_assets/foo_plugin/locales/ja']
end
test 'stimulus controller is loaded' do
assert_match %r{/assets/plugin_assets/foo_plugin/controllers/bar_controller-\w{8}\.js}, @importmap['controllers/bar_controller']
end
def teardown
Redmine::Plugin.loader = @original_loader
clear_public
end
(2-2/2)