简体   繁体   English

Bigcommerce模具包,无需缩小javascript

[英]Bigcommerce stencil bundle without minifying javascript

I'm having a javascript error in production that I can not duplicate in development. 我在生产中遇到javascript错误,无法在开发中重复。 This is the offending module: 这是有问题的模块:

import $ from 'jquery';
import 'foundation-sites/js/foundation.core';
import 'foundation-sites/js/foundation.util.mediaQuery';
import 'foundation-sites/js/foundation.util.keyboard';
import 'foundation-sites/js/foundation.util.box';
import 'foundation-sites/js/foundation.util.motion';
import 'foundation-sites/js/foundation.util.triggers';
import 'foundation-sites/js/foundation.dropdown';

export default function() {
  $(window).on('changed.zf.mediaquery', function(event, newSize, oldSize){
    toggle();
  });

  // toggle on page load
  toggle();
}

function toggle() {
  const $drop = $('[data-more-categories-dropdown]');

  if (Foundation.MediaQuery.atLeast('medium')) {
    if ($drop.data('dropdown')) {
      $drop.foundation('destroy');
      $drop.removeData('dropdown');
      $drop.attr('style', '');
    }
  }
  else {
    if (!$drop.data('dropdown')) {
      new Foundation.Dropdown($drop, {
        closeOnClick: true
      });
    }
  }
}

The purpose of the module is simply to destroy a foundation dropdown menu when the screen breakpoint changes. 该模块的目的仅仅是在屏幕断点更改时破坏基础下拉菜单。 If the new breakpoint is medium or larger, and the dropdown exists, destroy it. 如果新的断点是中等或更大,并且存在下拉列表,请销毁它。 If the breakpoint is smaller than medium and the dropdown does not exist, create it. 如果断点小于中点,并且下拉列表不存在,请创建它。

This works exactly as desired in my local development version of the store. 这完全符合我的商店本地开发版本中的期望。 Then, I run 'stencil bundle', and upload the theme to a live store. 然后,我运行“模具包”,并将主题上载到实时商店。 When I do that, I get an error every time I resize the browser to a breakpoint of medium or larger. 当我这样做时,每次将浏览器的大小调整到中等或更大的断点时,都会出现错误。 The error is: 错误是:

bundle.js:4 Uncaught ReferenceError: We're sorry, 'destroy' is not an available method for this element.

This is very difficult to troubleshoot since all of the javascript is minified into the bundle.js file, and I can't reproduce the issue in development. 由于将所有JavaScript bundle.jsbundle.js文件中,因此很难进行故障排除,并且我无法在开发中重现该问题。

Is there any way (even a hacky way) to tell the stencil bundle to treat the javascript like it does in development? 是否有任何方法(甚至是骇人听闻的方法)告诉stencil bundle将javascript当作开发中一样对待? That way I can leave debugger statements in the code and have source maps to help me figure out why the behavior is different in production. 这样,我可以将debugger语句保留在代码中,并具有源映射以帮助我弄清楚为什么行为在生产中会有所不同。

If anyone has any other ideas as to what might cause this, please let me know. 如果有人对导致此问题的原因有其他想法,请告诉我。

I have no idea why, but I made a small change to the module and it seems to have fixed the problem. 我不知道为什么,但是我对模块做了很小的改动,看来已经解决了这个问题。 I'm now using the zfPlugin data attribute to check if the dropdown is initiated or not. 我现在正在使用zfPlugin数据属性来检查下拉菜单是否已启动。 Here's the new toggle function. 这是新的toggle功能。

function toggle() {
  const $drop = $('[data-more-categories-dropdown]');

  if (Foundation.MediaQuery.atLeast('medium')) {
    if ($drop.data('zfPlugin')) {
      $drop.foundation('destroy');
      $drop.attr('style', '');
    }
  }
  else {
    if (!$drop.data('zfPlugin')) {
      new Foundation.Dropdown($drop, {
        closeOnClick: true
      });
    }
  }
}

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

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