2

The code below works fine in VsCode + webpack

Index.ts:

require("./jQueryPlugin-No-Module");
debugger;
$.fn.test();  // fails here

I am using the ProvidePlugin to inject jQuery into this script file.

jQueryPlugin-No-Module.js:

(function ($) {
    $.fn.test = function () {
        console.log("jQuery-Plugin called with options");
    };
})(jQuery);

I have copied the all code from VsCode to VS2017-ASP.NET Core Project and just adapted the paths. Everything runs perfect except the plugin above, I get this error:

enter image description here

In index.ts I put a debugger statement. When I run the application it stops there, the plugin $.fn.test has been successfully attached to jQuery. I switch to the Chrome console and execute $.fn.test(); there, works as expected. I go back to source view and hit F11 to execute the next step and I get the error above.

Perhaps someone has experienced this issue using ASP.NET Core 2 and VS2017 before...?

tsc && webpack -d --env.dev --display-modules --progress --display-error-details --config webpack.config.dev.js

Legends
  • 21,202
  • 16
  • 97
  • 123

1 Answers1

4

Credits go to > spacek33z

The problem was the following:

In my webpack.config.js I had the following jQuery alias pointing to the jquery dist folder:

resolve: {
        alias: {
            'jquery': require.resolve('jquery'), // --> node_modules\jquery\dist\jquery.js

and my plugins section I configured the ProvidePlugin to use the src version of jQuery (commented out code):

plugins: [

           new webpack.ProvidePlugin({
            $: "jquery",   // <-- right!
            jQuery: "jquery",  // <-- right!
            //$: "jquery/src/jquery",  // <-- wrong!
            //jQuery: "jquery/src/jquery"  // <-- wrong!          
        }),

That's why I get another copy of jQuery inside of the plugin code in jQueryPlugin-No-Module.js.

Adding this jQuery alias to my webpack.config.js to point to the jQuery dist folder:

resolve: {
        alias: {
            'jquery': require.resolve('jquery'), // resolve to dist folder

Now I get no error.

Legends
  • 21,202
  • 16
  • 97
  • 123