繁体   English   中英

如何打包一个npm模块?

[英]How to webpack a npm module?

我想使用npm模块在浏览器中运行simpleWeather jQuery插件。 听说我需要使用捆绑器,所以我安装了Webpack。

这是文件夹结构:

  • 天气
    • js
      • index.js
    • node_modules
      • jQuery的
      • 简单天气
    • 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>

index.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。

我需要做什么?

您需要在index.js中导入节点包依赖项[jquery,simpleweather]并进行webpack构建以生成bundle.js

现在在webpack中加载jQuery有点棘手,因此可以参考以下答案: https ://stackoverflow.com/a/28989476/1681972

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM