Javascript 网页包不';t绑定我的JS文件中的所有函数

Javascript 网页包不';t绑定我的JS文件中的所有函数,javascript,webpack,webpack-4,Javascript,Webpack,Webpack 4,我最近将Webpack集成到我的项目中,为我网站上的JS文件构建捆绑包。在解决了一些小问题之后,我能够构建这个包。在浏览器中检查时,其中一个Javascript代码抛出以下错误 检查后,我意识到addComment.moveform是问题的根源 因此,我检查了生成的bundle,发现变量addComment的定义没有被推送到bundle中。下面编写的Javascript不会捆绑在一起,这有什么原因吗?没有网页包抛出关于它的错误 /** * 'Comment Reply' to each

我最近将Webpack集成到我的项目中,为我网站上的JS文件构建捆绑包。在解决了一些小问题之后,我能够构建这个包。在浏览器中检查时,其中一个Javascript代码抛出以下错误

检查后,我意识到addComment.moveform是问题的根源

因此,我检查了生成的bundle,发现变量addComment的定义没有被推送到bundle中。下面编写的Javascript不会捆绑在一起,这有什么原因吗?没有网页包抛出关于它的错误

 /**
 * 'Comment Reply' to each comment.
 * This script moves the Add Comment section to the position below the appropriate comment.
 * Modified from Wordpress https://core.svn.wordpress.org/trunk/wp-includes/js/comment-reply.js
 * Released under the GNU General Public License - https://wordpress.org/about/gpl/
 */
var addComment = {
  moveForm: function(commId, parentId, respondId, postId) {
    var div,
      element,
      style,
      cssHidden,
      t = this,
      comm = t.I(commId),
      respond = t.I(respondId),
      cancel = t.I("cancel-comment-reply-link"),
      parent = t.I("comment-replying-to"),
      post = t.I("comment-post-slug"),
      commentForm = respond.getElementsByTagName("form")[0];

    if (!comm || !respond || !cancel || !parent || !commentForm) {
      return;
    }

    t.respondId = respondId;
    postId = postId || false;

    if (!t.I("sm-temp-form-div")) {
      div = document.createElement("div");
      div.id = "sm-temp-form-div";
      div.style.display = "none";
      respond.parentNode.insertBefore(div, respond);
    }

    comm.parentNode.insertBefore(respond, comm.nextSibling);
    if (post && postId) {
      post.value = postId;
    }
    parent.value = parentId;
    cancel.style.display = "";

    cancel.onclick = function() {
      var t = addComment,
        temp = t.I("sm-temp-form-div"),
        respond = t.I(t.respondId);

      if (!temp || !respond) {
        return;
      }

      t.I("comment-replying-to").value = "0";
      temp.parentNode.insertBefore(respond, temp);
      temp.parentNode.removeChild(temp);
      this.style.display = "none";
      this.onclick = null;
      return false;
    };

    /*
     * Set initial focus to the first form focusable element.
     */
    document.getElementById("comment-form-message").focus();

    /*
     * Return false so that the page is not redirected to HREF.
     */
    return false;
  },

  I: function(id) {
    return document.getElementById(id);
  }
};

这可能是因为消除了死代码:如果Webpack注意到某个特定的函数没有被使用,它只会将其删除以生成一个较小的包。但Webpack只知道从JavaScript调用函数,而不知道从HTML硬编码的事件处理程序调用函数

解决此问题的最简单方法是按照Webpack的规则进行操作,并通过JavaScript附加事件处理程序。无论如何,这是更好的练习,因为


如果您需要将数据传递给事件处理程序(正如您在这里所做的那样),您可以在元素上使用,并在事件处理程序函数中读取数据。

这可能是因为消除了死代码。也称:如果Webpack注意到某个特定函数未被使用,它只会将其删除以生成一个较小的捆绑包。但Webpack只知道从JavaScript调用函数,而不知道从HTML硬编码的事件处理程序调用函数

解决此问题的最简单方法是按照Webpack的规则进行操作,并通过JavaScript附加事件处理程序。无论如何,这是更好的练习,因为

如果需要将数据传递给事件处理程序(正如您在这里所做的那样),可以在元素上使用,并在事件处理程序函数中读取数据