2016-08-29 37 views
1

我有以下工作...JQuery的HoverIntent不是的WebPack和ProvidePlugin

webpack.config.js

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

deps.ts

require('jquery-hoverintent/jquery.hoverIntent.js'); 

test.coffee

$('.item').hoverIntent 
    over: -> 
    console.log("The hover is working"); 

当我运行应用程序我得到...

$(...).hoverIntent is not a function 

有人可以看到我失踪了吗?

更新

在jquery.hoverIntent我注意到,它是在这里走的是AMD路径...

(function(factory) { 
    'use strict'; 
    if (typeof define === 'function' && define.amd) { 
     define(function() { 
      return factory // Taking this path 
     }); 
    } else if (typeof module === 'object' && typeof module.exports === 'object') { 
     module.exports = factory; 
    } else if (jQuery && !jQuery.fn.hoverIntent) { 
     factory(jQuery); 
    } 
})(function($) { 
    'use strict'; 

    if ($.fn.hoverIntent) { 
     return; 
    } 
    ... 
}) 

所以我在打字稿尝试这个...

var test = require('jquery-hoverintent'); 
test(window['$']); 

但是,当我运行在工厂功能$未定义,尽管window['$']是有效的...

> $ 
undefined 
> window['$'] 
$(selector, [startNode]) { [Command Line API] } 

更新2

这似乎是工作,但是,我认为这是什么插件在做,以及...

window['$'] = window['jQuery'] = require('jquery'); 
... 
require('jquery-hoverintent')(window['$']); 

那么,为什么这项工作,而不是使用Provide Plugin。

回答

2

正如你所说的,hoverIntent没有使用适当的amd样式定义,因此当将它包含在webpack中时需要额外的步骤。然而,它不需要提供插件或需要使用附加到窗口的jquery。我能够得到它的工作:

var $ = require('jquery'); 
require('jquery-hoverintent')($);