Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #37775 - include hashes in production builds of assets #10125

Merged
merged 1 commit into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/helpers/reactjs_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def read_webpack_manifest

def get_webpack_chunk(name, extension)
data = read_webpack_manifest
data['assetsByChunkName'][name].find { |value| value.end_with?(".#{extension}") }
data['assetsByChunkName'][name]&.find { |value| value.end_with?(".#{extension}") }
end

def get_webpack_foreman_vendor_js
Expand Down
8 changes: 4 additions & 4 deletions app/views/layouts/base.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@
</script>
<%= javascript_include_tag "locale/#{FastGettext.locale}/app" %>
<%= locale_js_tags %>

<%= yield(:head) %>
</head>

<body class='<%= body_css_classes %>'>
<%= get_webpack_foreman_vendor_js %>
<%= javascript_include_tag('/webpack/vendor.js') %>
<%= javascript_include_tag('/webpack/bundle.js') %>
<%= javascript_include_tag('/webpack/reactExports.js') %>
<%= javascript_include_tag("/webpack/#{get_webpack_chunk('vendor', 'js')}") %>
<%= javascript_include_tag("/webpack/#{get_webpack_chunk('bundle', 'js')}") %>
<%= javascript_include_tag("/webpack/#{get_webpack_chunk('reactExports', 'js')}") %>

<%= javascript_include_tag 'application' %>
<%= webpacked_plugins_with_global_js %>
Expand Down
10 changes: 10 additions & 0 deletions config/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ const coreConfig = function() {
'webpack/assets/javascripts/bundle.js'
);
config.context = path.resolve(__dirname, '..');
if (config.mode == 'production') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for some reason, this is always true, and I don't understand it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at

var production =
process.env.RAILS_ENV === 'production' ||
process.env.NODE_ENV === 'production';
const mode = production ? 'production' : 'development';
we very often set NODE_ENV to production.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ENV["NODE_ENV"] ||= 'production'

that could be it, indeed

var chunkFilename = '[name]-[chunkhash].js'
} else {
var chunkFilename = '[name].js'
}

config.entry = {
bundle: { import: bundleEntry, dependOn: ['vendor', 'reactExports'] },
vendor: vendorEntry,
Expand All @@ -169,6 +175,7 @@ const coreConfig = function() {
name: ['TheForeman', '[name]'],
type: 'var',
},
filename: chunkFilename,
};
var plugins = config.plugins;

Expand Down Expand Up @@ -236,6 +243,7 @@ const pluginConfig = function(plugin) {

if (config.mode == 'production') {
var outputPath = path.join(pluginRoot, 'public', 'webpack', pluginName);
var chunkFilename = '[name]-[chunkhash].js'
} else {
var outputPath = path.join(
__dirname,
Expand All @@ -244,10 +252,12 @@ const pluginConfig = function(plugin) {
'webpack',
pluginName
);
var chunkFilename = '[name].js'
}
config.output = {
path: outputPath,
publicPath: '/webpack/' + pluginName + '/',
filename: chunkFilename,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am almost sure this will break plugins, but I've not tested it yet

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

amazingly, it does not!

old foreman and old foreman_puppet:
Screenshot from 2024-08-29 14-43-13

new foreman (with hashes) and old foreman_puppet:
Screenshot from 2024-08-29 14-42-44

new foreman (with hashes) and new foreman_puppet (with hashes):
Screenshot from 2024-08-29 16-05-09

uniqueName: pluginName,
};
var configModules = config.resolve.modules || [];
Expand Down
Loading