Skip to content

Commit

Permalink
webpack 5 module federation
Browse files Browse the repository at this point in the history
  • Loading branch information
MariaAga committed Nov 27, 2023
1 parent 26bf6bb commit 56b3d04
Show file tree
Hide file tree
Showing 26 changed files with 90 additions and 347 deletions.
7 changes: 5 additions & 2 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
//= require lookup_keys

$(function() {
if(window.allJsLoaded){
$(document).trigger('ContentLoad');
}
else {
$(document).on('loadJS', function() {
$(document).trigger('ContentLoad');
});
});}
});


Expand All @@ -30,7 +34,6 @@ var handleDisabledClick = function(event, element){
$(document).on('click', 'a[disabled="disabled"]', function(event) {
return handleDisabledClick(event, this);
});

function onContentLoad() {
if ($('input[focus_on_load=true]').length > 0) {
$('input[focus_on_load]')
Expand Down
6 changes: 4 additions & 2 deletions app/assets/javascripts/late_load.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ function load_dynamic_javascripts(html) {
function waitForAllLoaded() {
// Wait for all plugins js modules to be loaded before loading the javascript content
return new Promise(function(resolve) {
if (Object.values(window.allPluginsLoaded).every(Boolean)) {
// window.allPluginsLoaded is set to {} when plugins are starting to load
// if there are no plugins window.allPluginsLoaded is never defined
if (window.allPluginsLoaded === undefined || Object.values(window.allPluginsLoaded).every(Boolean)) {
resolve();
} else {
function handleLoad() {
if (Object.values(window.allPluginsLoaded).every(Boolean)) {
if (window.allPluginsLoaded === undefined || Object.values(window.allPluginsLoaded).every(Boolean)) {
resolve();
// Remove the event listener
document.removeEventListener('loadPlugin', handleLoad);
Expand Down
3 changes: 1 addition & 2 deletions app/helpers/layout_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ def javascript_include_tag(*params, **kwargs)
end
end


# @deprecated Previously provided by webpack-rails
def webpack_asset_paths(plugin_name, extension: 'js')
# @deprecated Previously provided by webpack-rails
if extension == 'js'
Foreman::Deprecation.deprecation_warning('3.12', '`webpack_asset_paths` is deprecated, use `content_for(:javascripts) { webpacked_plugins_js_for(plugin_name) }` instead.')
[{
Expand Down
22 changes: 12 additions & 10 deletions app/helpers/reactjs_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ def webpacked_plugins_with_global_js
end

def webpacked_plugins_css_for(*plugin_names)
Foreman::Deprecation.deprecation_warning('3.9', '`webpacked_plugins_css_for` is deprecated, plugin css is already loaded.')
Foreman::Deprecation.deprecation_warning('3.12', '`webpacked_plugins_css_for` is deprecated, plugin css is already loaded.')
end

def read_webpack_manifest
root = File.expand_path(File.dirname(__FILE__) + "/../..")
file = File.read(root + '/public/webpack/manifest.json')
JSON.parse(file)
JSON.parse(Rails.root.join('public/webpack/manifest.json').read)
end

def get_webpack_foreman_vendor_js
data = read_webpack_manifest
foreman_vendor_js = data['assetsByChunkName']['foreman-vendor'].find { |value| value.end_with?('.js') }
javascript_include_tag("/webpack/#{foreman_vendor_js}")
Rails.cache.fetch('webpack_foreman_vendor_js', expires_in: 1.minute) do
data = read_webpack_manifest
foreman_vendor_js = data['assetsByChunkName']['foreman-vendor'].find { |value| value.end_with?('.js') }
javascript_include_tag("/webpack/#{foreman_vendor_js}")
end
end

def get_webpack_foreman_vendor_css
Expand All @@ -54,20 +54,22 @@ def select_requested_plugins(plugin_names)

def js_tags_for(requested_plugins)
requested_plugins.map do |plugin|
javascript_tag("window.tfm.tools.loadPluginModule('/webpack/#{plugin.to_s.tr('-', '_')}','#{plugin.to_s.tr('-', '_')}','./index');".html_safe)
name = plugin.to_s.tr('-', '_')
javascript_tag("window.tfm.tools.loadPluginModule('/webpack/#{name}','#{name}','./index');".html_safe)
end
end

def global_js_tags(requested_plugins)
requested_plugins.map do |plugin|
plugin[:files].map do |file|
javascript_tag("window.tfm.tools.loadPluginModule('/webpack/#{plugin[:id].to_s.tr('-', '_')}','#{plugin[:id].to_s.tr('-', '_')}','./#{file}_index');".html_safe)
name = plugin[:id].to_s.tr('-', '_')
javascript_tag("window.tfm.tools.loadPluginModule('/webpack/#{name}','#{name}','./#{file}_index');".html_safe)
end
end
end

def css_tags_for(requested_plugins)
Foreman::Deprecation.deprecation_warning('3.12', '`css_tags_for` is deprecated, No need to load CSS separately, since it should be referneced from the corresponding JS file.')
Foreman::Deprecation.deprecation_warning('3.12', '`css_tags_for` is deprecated, No need to load CSS separately, since it should be referenced from the corresponding JS file.')
[]
end

Expand Down
2 changes: 0 additions & 2 deletions app/services/foreman/env_settings_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ def settings_map
'FOREMAN_REQUIRE_SSL' => [:boolean, :require_ssl],
'FOREMAN_SUPPORT_JSONP' => [:boolean, :support_jsonp],
'FOREMAN_MARK_TRANSLATED' => [:boolean, :mark_translated],
'FOREMAN_WEBPACK_DEV_SERVER' => [:boolean, :webpack_dev_server],
'FOREMAN_WEBPACK_DEV_SERVER_HTTPS' => [:boolean, :webpack_dev_server_https],
'FOREMAN_ASSETS_DEBUG' => [:boolean, :assets_debug],
'FOREMAN_HSTS_ENABLED' => [:boolean, :hsts_enabled],
'FOREMAN_DOMAIN' => [:string, :domain],
Expand Down
1 change: 0 additions & 1 deletion app/views/layouts/base.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
</script>
<%= javascript_include_tag "locale/#{FastGettext.locale}/app" %>
<%= locale_js_tags %>
<%= stylesheet_link_tag('/webpack/bundle', :extension => 'css') %>
<%= yield(:head) %>
</head>
<body class='<%= body_css_classes %>'>
Expand Down
10 changes: 0 additions & 10 deletions config/initializers/assets.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
module Webpack
module Rails
class Manifest
class << self
attr_writer :manifest
end
end
end
end

# Be sure to restart your server when you modify this file.
Foreman::Application.configure do |app|
# Version of your assets, change this if you want to expire all your assets.
Expand Down
9 changes: 0 additions & 9 deletions config/settings.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,6 @@
# Mark translated strings with X characters (for developers)
:mark_translated: false

# Use the webpack development server? set to false to disable (for developers)
# Make sure to run `rake webpack:compile` if disabled.
:webpack_dev_server: true

# If you run Foreman in development behind some proxy or use HTTPS you need
# to enable HTTPS for webpack dev server too, otherwise you'd get mixed content
# errors in your browser
:webpack_dev_server_https: false

# Assets in development are not bundled/minified
# Do not set this to false if you plan to edit assets (css, js, etc.)
:assets_debug: false
Expand Down
Loading

0 comments on commit 56b3d04

Please sign in to comment.