I am in the process of moving my entire application.js
application into smaller page bundles using SplitChunks. In my users/show.html.erb
page I am using the following tag to import the specific chunk.
<%= javascript_packs_with_chunks_tag 'users/show' %>
From the looks of my source code when I inspect it there are several bundles included such as the application.js
file that includes jquery like this...
import "bootstrap"
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery")
I am just testing the page to see if jquery is being loaded. My users/show.js
code looks like this:
import "chartkick"
import "chart.js"
window.onload = function() {
if (window.jQuery) {
// jQuery is loaded
alert("Yeah!");
} else {
// jQuery is not loaded
alert("Doesn't Work");
}
}
When I load the page I do see a Doesn't Work
alert pop up indicating that jQuery has not been loaded. I'm newer to webpack so perhaps I have some misconfiguration somewhere, but if jquery is being loaded at the application.js level, why would my other pack not be able to listen to use jquery? Is this a problem with the dependency graphs?
My environment is as follows:
environment.js
const { environment } = require('@rails/webpacker');
const webpack = require('webpack');
environment.plugins.append('Provide', new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery',
Popper: ['popper.js', 'default']
}));
const config = environment.toWebpackConfig();
config.resolve.alias = {
jquery: 'jquery/src/jquery'
};
environment.splitChunks()
module.exports = environment;