2017-04-20 94 views
0

我想通過使用npm模塊在瀏覽器中運行simpleWeather jQuery插件。我聽說我需要使用捆綁器,所以我安裝了Webpack。如何webpack npm模塊?

這是文件夾結構:

  • 天氣
    • JS
      • index.js
    • node_modules
      • 的jquery
      • simpleweather
    • 的index.html
    • 的package.json
    • webpack.config.js

的index.html:

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="UTF-8"> 
    <title>simpleWeather.minimum.js</title> 
    <style> 
    </style> 
</head> 
<body> 
    <div id="weather"></div> 
    <script src="js/bundle.js"></script> 
</body> 
</html> 

索引.js:

$(document).ready(function() { 
    $.simpleWeather({ 
    location: 'Austin, TX', 
    woeid: '', 
    unit: 'f', 
    success: function(weather) { 
     html = '<p>'+weather.temp+'&deg;'+weather.units.temp+'</p>'; 

     $("#weather").html(html); 
    }, 
    error: function(error) { 
     $("#weather").html('<p>'+error+'</p>'); 
    } 
    }); 
}); 

webpack.config.js:

module.exports = { 
    entry: "./js/index.js", 
    output: { 
    filename: "js/bundle.js" 
    } 
} 

這不會在瀏覽器中工作,我想,我需要一些不同的方式配置的WebPack。

我需要做什麼?

+0

建議閱讀http://blog.andrewray.me/webpack-when-to-use-and-why/ –

回答

0

您需要導入您的節點包的依賴關係[jQuery的,simpleweather在index.js和做的WebPack建設產生bundle.js

現在的WebPack加載jQuery是一個有點棘手,所以這樣可以參考這個答案: https://stackoverflow.com/a/28989476/1681972

+0

如何導入軟件包相關性? – user371