Managing jQuery plugin dependency in webpack

前端 未结 11 1244
陌清茗
陌清茗 2020-11-22 01:17

I\'m using Webpack in my application, in which I create two entry points - bundle.js for all my JavaScript files/codes, and vendors.js for all libraries like jQuery and Reac

11条回答
  •  再見小時候
    2020-11-22 01:39

    For global access to jquery then several options exist. In my most recent webpack project, I wanted global access to jquery so I added the following to my plugins declarations:

     plugins: [
        new webpack.ProvidePlugin({
          $: "jquery",
          jQuery: "jquery"
        })
      ]
    

    This then means that jquery is accessible from within the JavaScript source code via global references $ and jQuery.

    Of course, you need to have also installed jquery via npm:

    $ npm i jquery --save
    

    For a working example of this approach please feel free to fork my app on github

提交回复
热议问题