jQuery with Webpack 2

这一生的挚爱 提交于 2019-12-05 12:18:34

Add plugin to plugin section of your webpack config like this:

var webpack = require('webpack')
...

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

EDIT:

You have cyclic dependency:

timeago: jquery
client: timeago jquery

but jquery and client is same bundle. Try to switch of include js bundles in html like this:

<script src="client.min.js"></script>
<script src="jquery.timeago.js"></script>

instead of:

<script src="jquery.timeago.js"></script>
<script src="client.min.js"></script>

If that does not help or you have another errors you need to extract jquery to separated bundle and include it before timeago. Another way is to include jquery like old school for example from cdn and make webpack know that like this:

externals: {
  jquery: 'jQuery'
}

To extract jquery to separated bundle and make it visible for timeago use include sequence like this:

<script src="common.js"></script>
<script src="jquery.timeago.js"></script>
<script src="client.min.js"></script>

And use webpack common chunk plugin like this:

entry: {
  common: ["jquery"],
  ...
},
plugins: [
  new webpack.optimize.CommonsChunkPlugin({
    name: "common",
    filename: "common.js",
    minChunks: Infinity,
  })
],
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!