Skip to content

Commit 1f64a3b

Browse files
committed
Migrate to official SRI support from shakapacker
Since version 8.4.0, Shakapacker has gained support for Subresource Integrity. Now, we switch to their implementation and drop ours, further improving maintainability. As part of this effort, we also align the shakapacker.yml with the latest updates and defaults.
1 parent 78350b1 commit 1f64a3b

File tree

4 files changed

+33
-86
lines changed

4 files changed

+33
-86
lines changed

config/shakapacker.yml

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ default: &default
55

66
# You can have a subdirectory of the source_path, like 'packs' (recommended).
77
# Alternatively, you can use '/' to use the whole source_path directory.
8+
# Notice that this is a relative path to source_path
89
source_entry_path: /
910

1011
# If nested_entries is true, then we'll pick up subdirectories within the source_entry_path.
@@ -37,12 +38,32 @@ default: &default
3738
# Select loader to use, available options are 'babel' (default), 'swc' or 'esbuild'
3839
webpack_loader: 'babel'
3940

40-
# Set to true to enable check for matching versions of shakapacker gem and NPM package - will raise an error if there is a mismatch or wildcard versioning is used
41+
# Raises an error if there is a mismatch in the shakapacker gem and npm package being used
4142
ensure_consistent_versioning: false
4243

43-
# Select whether the compiler will use SHA digest ('digest' option) or most most recent modified timestamp ('mtime') to determine freshness
44+
# Select whether the compiler will use SHA digest ('digest' option) or most recent modified timestamp ('mtime') to determine freshness
4445
compiler_strategy: digest
4546

47+
# Select whether the compiler will always use a content hash and not just in production
48+
# Don't use contentHash except for production for performance
49+
# https://webpack.js.org/guides/build-performance/#avoid-production-specific-tooling
50+
useContentHash: false
51+
52+
# Setting the asset host here will override Rails.application.config.asset_host.
53+
# Here, you can set different asset_host per environment. Note that
54+
# SHAKAPACKER_ASSET_HOST will override both configurations.
55+
# asset_host: custom-path
56+
57+
# Utilizing webpack-subresource-integrity plugin, will generate integrity hashes for all entries in manifest.json
58+
# https://github.com/waysact/webpack-subresource-integrity/tree/main/webpack-subresource-integrity
59+
integrity:
60+
enabled: true
61+
# Which cryptographic function(s) to use, for generating the integrity hash(es). Default sha-384. Other possible values sha256, sha512
62+
hash_functions: ["sha256", "sha384", "sha512"]
63+
# Default "anonymous". Other possible value "use-credentials"
64+
# https://developer.mozilla.org/en-US/docs/Web/Security/Subresource_Integrity#cross-origin_resource_sharing_and_subresource_integrity
65+
cross_origin: "anonymous"
66+
4667
development:
4768
<<: *default
4869
compile: true
@@ -51,13 +72,14 @@ development:
5172
# Reference: https://webpack.js.org/configuration/dev-server/
5273
# Keys not described there are documented inline and in https://github.com/shakacode/shakapacker/
5374
dev_server:
75+
# For running dev server with https, set `server: https`.
5476
server: http
5577
host: localhost
5678
port: 3035
5779
# Hot Module Replacement updates modules while the application is running without a full reload
5880
# Used instead of the `hot` key in https://webpack.js.org/configuration/dev-server/#devserverhot
5981
hmr: false
60-
# If HMR is on, CSS will by inlined by delivering it as part of the script payload via style-loader. Be sure
82+
# If HMR is on, CSS will be inlined by delivering it as part of the script payload via style-loader. Be sure
6183
# that you add style-loader to your project dependencies.
6284
#
6385
# If you want to instead deliver CSS via <link> with the mini-css-extract-plugin, set inline_css to false.
@@ -69,7 +91,10 @@ development:
6991
# live_reload: true
7092
client:
7193
# Should we show a full-screen overlay in the browser when there are compiler errors or warnings?
72-
overlay: true
94+
overlay:
95+
errors: true
96+
warnings: false
97+
runtimeErrors: true
7398
# May also be a string
7499
# webSocketURL:
75100
# hostname: '0.0.0.0'
@@ -101,5 +126,8 @@ production:
101126
# Production depends on precompilation of packs prior to booting for performance.
102127
compile: false
103128

129+
# Use content hash for naming assets. Cannot be overridden in production.
130+
useContentHash: true
131+
104132
# Cache manifest.json for performance
105133
cache_manifest: true

config/webpack/webpack.config.js

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
// See the shakacode/shakapacker README and docs directory for advice on customizing your webpackConfig.
22

3-
const { generateWebpackConfig, config, merge } = require('shakapacker')
3+
const { generateWebpackConfig, merge } = require('shakapacker')
44
const webpackConfig = generateWebpackConfig()
55

66
const CompressionPlugin = require("compression-webpack-plugin");
77
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin");
88
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
99
const TerserPlugin = require("terser-webpack-plugin");
10-
const { WebpackAssetsManifest } = require('webpack-assets-manifest');
11-
const { SubresourceIntegrityPlugin } = require("webpack-subresource-integrity");
1210

1311
// This setting will change the absolute path used to refer
1412
// external files (images, fonts, ...) in the generated assets
@@ -53,8 +51,6 @@ const envConfig = module.exports = {
5351
},
5452
output: {
5553
publicPath: relative_url_root + public_output_path,
56-
// the following setting is required for SRI to work:
57-
crossOriginLoading: 'anonymous',
5854
},
5955
performance: {
6056
// Turn off size warnings for large assets
@@ -65,15 +61,6 @@ const envConfig = module.exports = {
6561
new MiniCssExtractPlugin({
6662
filename: '[name]-[contenthash].css',
6763
}),
68-
new SubresourceIntegrityPlugin(),
69-
new WebpackAssetsManifest({
70-
entrypoints: true,
71-
integrity: true,
72-
writeToDisk: true,
73-
entrypointsUseAssets: true,
74-
publicPath: true,
75-
output: config.manifestPath,
76-
})
7764
],
7865
resolve: {
7966
extensions: ['.css', '.ts', '.tsx'],
@@ -97,13 +84,5 @@ webpackConfig.module.rules
9784
.filter(loaderConfig => loaderConfig.options?.sassOptions)
9885
.forEach(loaderConfig => loaderConfig.options.sassOptions.sourceMapIncludeSources = true);
9986

100-
// Use the following lines below to remove original plugins and replace them with our custom config.
101-
// This is especially needed for the `WebpackAssetsManifest` plugin, which would otherwise run twice.
102-
const customPlugins = envConfig.plugins.map((plugin) => plugin.constructor.name);
103-
const filteredDefaultPlugins = webpackConfig.plugins.filter((plugin) => {
104-
return !customPlugins.includes(plugin.constructor.name);
105-
});
106-
webpackConfig.plugins = filteredDefaultPlugins;
107-
10887
// Create the resulting config by merging the (modified) default config and our custom setup
10988
module.exports = merge(webpackConfig, envConfig)

lib/shakapacker/sri_helper_extensions.rb

Lines changed: 0 additions & 29 deletions
This file was deleted.

lib/shakapacker/sri_manifest_extensions.rb

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)