Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webpack bundle - Bootstrap's JavaScript requires jQuery

I am trying to achieve the following:

  • bundle (in this order) jquery, tether, and bootstrap.js.
  • load this bundle within a HTMLpage and beneath it load other page specific scripts.

To achieve this I am using webpack 2 and the CommonsChunkPlugin. Here is my config.

For entries I have:

const scriptsEntry = {
    blog_index: SRC_DIR + "/js/pages/blog_index.js",
    blog_about: SRC_DIR + "/js/pages/blog_about.js",
    vendor: ["jquery", "tether", "bootstrap"]
};

In the plugins section:

scriptsPlugins.push(
    new webpack.optimize.CommonsChunkPlugin({
        name: "vendor", 
        filename:"vendor.bundle.js", 
        minChunks: 2
    }),
    ...
}));

Inside 'index.html' I load the bundles:

<script src="{{ url_for('static', filename='dist/js/vendor.bundle.js') + anti_cache_token }}"></script>
<script src="{{ url_for('static', filename='dist/js/home.js') + anti_cache_token }}"></script>

Inside the source directory in the blog_index.js I make use of jquery:

import $ from "jquery";

$(".home-click").click(function () {
    alert("clicked from .home-click");
});

The result:

  • everything bundles without any errors.
  • when I click .home-click the alert box fires as expected.
  • checking the bundled files I see that:
    • vendor.bundle.js contains: jquery, tether, and bootstrap.
    • looking inside, for instance, blog_index.js (after it was run through webpack 2), I notice that the jquery import is not bundled inside this file, but vendor.bundle.js (this is as expected).

However, I have the following problem when I check the browser console: enter image description here

I tried switching the order of the libraries in this line vendor: ["jquery", "tether", "bootstrap"], but nothing changed--the error is still there.

Do you know how can I solve this, preferably without using additional webpack plugins?

like image 997
Mihai Avatar asked Jan 21 '17 11:01

Mihai


People also ask

How do I integrate bootstrap components into a Webpack bundle?

This allows you to override the built-in variables and integrate Bootstrap components into your Webpack bundle. The source code for this article is available on GitHub. If you have not already, create a directory for your project. Initialize npm to create a package.json file, then install webpack locally.

Does jQuery import work with Webpack import?

It does, but only if module.exports is undefined. If module.exports is defined -- which it will be with our webpack import -- module.exports = jQuery. Unfortunately, to discover this you need to dig into the jquery library (however, the code we're interested in is conveniently at the top and bottom of the file).

What is Webpack and why do we need it?

The goal of Webpack is to make it easy for developers to bundle code together and reduce its size in order to provide the smallest functional possible file to serve to users. In this post, I will show how we can bundle Bootstrap 4.0, both its js and its sass source code in order to provide two bundle, bundle.js and bundle.css.

How to import bootstrap precompiled Sass files into Webpack?

If you choose to import Bootstrap’s JavaScript plugins individually as needed, you must also install exports-loader. You’ll need to install the required loaders and postcss plugins for compiling and bundling Bootstrap precompiled Sass files. Create a webpack configuration file.


Video Answer


1 Answers

Bootstrap's javascript assumes that jQuery is hooked on the window object, it does not require it or anything.

By bundling stuff up, you do not expose variables to the global scope, or at least you should not be doing that. So the bootstrap code cannot find jQuery.

Add this to your plugins and you should be ok

new webpack.ProvidePlugin({
    $: 'jquery',
    jQuery: 'jquery',
    'window.jQuery': 'jquery',
    tether: 'tether',
    Tether: 'tether',
    'window.Tether': 'tether',
})

This will replace all instances where jQuery is assumed as global with the export of the jQuery package, which is the jQuery object. Same for Tether

like image 149
Dimitris Karagiannis Avatar answered Oct 22 '22 22:10

Dimitris Karagiannis