2017-04-20 96 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