How can I have code splitting? #90
-
I'm a javascript neophyte, so sorry in advance if I'm misunderstanding something fundamental. My questionI import various packages in my javascript. Is there a way to code split these and load as chunks? Some details and thoughtsI'm using Rails 6 with jsbundling-rails and webpack. Per jsbundling-rails default setup, I have webpack compiling my javascript into (Note: If there is a Rails 7 only answer, I'd be very interested to hear that as well.) Sprockets adds a fingerprint/hash to the filenames of all compiled assets. I believe that if I set up code-splitting in webpack, then webpack doesn't know about the new filenames and so doesn't know which file to load. And my guess is that is why the default webpack setup looks like this:
If my thinking so far is correct and also if code splitting is not support by jsbundling-rails/sprockets, then what would be the best workaround? My initial thought re workaround, is to bypass sprockets and compile into Thanks for the help. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
@flivni Check out https://github.com/shakacode/shakapacker. It's the supported fork and evolution of rails/webpacker which does what you want by default. |
Beta Was this translation helpful? Give feedback.
-
Just need to adjust Webpack's configuration file. Remove the const path = require("path")
const webpack = require("webpack")
module.exports = {
mode: "production",
devtool: "source-map",
entry: {
application: "./app/javascript/application.js"
},
output: {
filename: "[name].js",
chunkFilename: '[name]-[contenthash].digested.js',
sourceMapFilename: "[name].js.map",
path: path.resolve(__dirname, "app/assets/builds"),
}
} |
Beta Was this translation helpful? Give feedback.
Just need to adjust Webpack's configuration file. Remove the
LimitChunkCountPlugin
and add thechunkFilename
attribute to tell it to predigest the chunks and add.digested
. This last keyword will indicate Sprockets that it should not add another digest to the file when runningprecompile
.