4

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>'

4 Answers 4

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

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 {
    ...

Comments

2

Did you try this version before?

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

2 Comments

Yes, i do have "window.jQuery": "jquery", also added now - but that still does not solve the main problem - where do i load the plugin itself. My jquery works fine.
Some plugins give error, because UMD definition uses global scope but global is undefined.
0

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";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.