Agony
Agony

Reputation: 844

Loading jQuery plugins with webpack 2

After spending most of the day googling and trying - i did not get it to work and at this point i'm not sure what's missing.
I already have jQuery working (and verified it works) in webpack.common.js:

new ProvidePlugin({
        jQuery: 'jquery',
        $: 'jquery',
        jquery: 'jquery'
    }) 

For example i have a "MetisMenu" plugin, where should i configure it?

I tried various combinations of require/include in my app.module.ts.
Like (including assigning them to a constant/var but import/require always give this error):

import 'metismenu';
jQuery(...).metisMenu is not a function

import { metisMenu } from 'metismenu';
Cannot assign to read only property 'exports' of object '#<Object>'

require ('metismenu');
Cannot assign to read only property 'exports' of object '#<Object>'

import * as jQuery from 'jquery';
import "metismenu";
Cannot assign to read only property 'exports' of object '#<Object>'

Upvotes: 4

Views: 6911

Answers (4)

Justin Tanner
Justin Tanner

Reputation: 14352

With webpack 2 you can now use the expose-loader module to accomplish this.

First install expose-loader:

npm install expose-loader --save-dev

Then when change your import (or require) to use the following syntax:

import "expose-loader?$!jquery";

Upvotes: 0

Nicolas Law-Dune
Nicolas Law-Dune

Reputation: 1703

I was faced to the same problem. I can't bundle Jquery and external plugin with webpack 2.

So I "solved" partially using externals options of webpack.

I Link CDN library (jquery and other jquery plugin).

<script src="https://code.jquery.com/jquery-3.2.1.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.9/semantic.min.js" crossorigin="anonymous"></script>

I specify "externals" option on my webpack.config

externals: {
    jquery: 'jQuery'
}

And add the ProvidePlugin

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

In the app file, I import jquery and use the plugin like this

 import * as $ from 'jquery';
 $('.accordion').accordion();

I appreciate if someone can tell me how to bundle jQuery with Webpack 2

Upvotes: 3

user7539781
user7539781

Reputation:

Looks like webpack.ProvidePlugin makes somehow declared globals immutable in Webpack 2.

I was unable to modify jQuery from a module and then use the modified jQuery on the other (hear by that, adding a jQuery plugin that add its own $.fn.).

I suggest you to create a single define that contains the necessary jQuery plugins "requires", it worked for me, e.g.:

((root, factory) => {
    // AMD. Register as an anonymous module.
    define([
        'modernizr'
    ], factory);
})(this, (Modernizr) => {

    // Do the requires here instead of doing it on the main file

    // Fix broken $.animate so Slick can work with
    require('./animatePolyfill');

    // Inject carousel there
    require('slick-carousel');

    class Core extends PluginBase {
    ...

Upvotes: 2

Cenk &#199;etinkaya
Cenk &#199;etinkaya

Reputation: 139

Did you try this version before?

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

Upvotes: 2

Related Questions