@dalton_moen
To import jQuery only in certain modules using webpack 4, you can follow these steps:
- Install jQuery as a dependency for your project using npm or yarn:
npm install jquery
- In your webpack configuration file (webpack.config.js), add the following code to expose jQuery as a global variable:
const webpack = require('webpack');
module.exports = {
// ...
plugins: [
new webpack.ProvidePlugin({
$: 'jquery',
jQuery: 'jquery'
})
],
};
- In the specific module(s) where you want to import jQuery, use the require statement to import it:
const $ = require('jquery');
// Use jQuery in your module as needed
$(document).ready(function() {
// ...
});
By default, webpack will bundle the required jQuery module only in the specific module(s) where it is imported.