question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

this.jQuery cannot be shimmed with ProvidePlugin

See original GitHub issue

Do you want to request a feature or report a bug?

Bug

What is the current behavior?

The ProvidePlugin does not properly support importing and replacing this.jQuery in legacy apps.

If the current behavior is a bug, please provide the steps to reproduce.

test.js

if (this.jQuery) {
    throw 'Jquery is not available.';
}

console.log('jQuery found.', this.jQuery);

This outputs the following:

webpackJsonp([0],[
/* 0 */
/***/ (function(module, exports) {

if (this.jQuery) {
    throw 'Jquery is not available.';
}

console.log('jQuery found.', this.jQuery);


/***/ })
],[0]);

If you were to define it as this2 instead, you get the correct output:

webpackJsonp([0],[
/* 0 */
/***/ (function(module, exports, __webpack_require__) {

/* WEBPACK VAR INJECTION */(function(__webpack_provided_this2_dot_jQuery) {if (__webpack_provided_this2_dot_jQuery) {
    throw 'Jquery is not available.';
}

console.log('jQuery found.', __webpack_provided_this2_dot_jQuery);

/* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(1)))

config

var webpack = require('webpack');
var path = require('path');

module.exports = {

    entry: {
        test: './test.js'
    },

    output: {
        path: path.resolve(__dirname, './output.js'),
        filename: '[name].js'
    },

    plugins: [
        new webpack.ProvidePlugin({
           'this.jQuery': 'jquery'
       })
    ]

};

What is the expected behavior?

It should replace this.jQuery with the imported version.

If this is a feature request, what is motivation or use case for changing the behavior?

To support shimming legacy apps that use this as the global window context.

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.

Webpack version: 3.11.0

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
sokracommented, Feb 27, 2018

Free means not defined in these module. Coming from the environment.

Only the top level this comes from webpack. Every other this is defined in the module.

0reactions
nveenjaincommented, Feb 27, 2018

@sokra, does free property mean allowed to be renamed? I’m actually confused as to what free represents. Also topLevelScoped this represents exported modules from current module, will it not cause any ambiguity?? Acc. to me, either we should completely support changing this.propertyName to a module using providePlugin(in every scope), or not support it at all. That way, we can be sure that user wants it and it’s developer’s responsibility to not abuse it…

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shim with webpack.ProvidePlugin using a module's exported ...
How do I shim for example Promise with webpack.ProvidePlugin to require('q').Promise ? Shimming $ in webpack.config.js :
Read more >
Shimming window["jQuery"] · Issue #1406 · webpack ... - GitHub
The problem is: knockout is looking for jQuery as window["jQuery"] . I want to avoid exposing jQuery in window by making ProvidePlugin ......
Read more >
ProvidePlugin - webpack
ProvidePlugin. Automatically load modules instead of having to import or require them everywhere. new webpack.ProvidePlugin({ identifier: 'module1', // .
Read more >
Shimming modules - doc_webpack - Read the Docs
Example: Make $ and jQuery available in every module without writing require("jquery") . new webpack.ProvidePlugin({ $: "jquery", jQuery: "jquery", "window.
Read more >
Log-0x13 - Next.js webpack ProvidePlugins - Howling Moon
The downside is, this is not really a global variable. You can't call the reference to "window.$" in the browser because this plugin...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found