无法缩写此文件中的代码

人气:66 发布:2023-01-03 标签: javascript node.js minify uglifyjs2

问题描述

我正在创作一个我想放在npm上的JavaScript库。我目前正在另一个项目中使用该库,并且已使用其GitHub存储库将其添加为依赖项:

"dependencies": {
  // ... others
  "react-web-component": "LukasBombach/react-web-component",
}

我也将webpack与UglifyJsPlugin一起使用。现在,当我想要构建我的项目时,我得到一个错误:

编译失败。

无法缩减此文件中的代码:

./packages/react-scripts/node_modules/react-web-component/src/index.js线路18:0

点击此处阅读更多内容:https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#npm-run-build-fails-to-minify

错误命令失败,退出代码为%1。

这是我的图书馆的问题。当我从我的dep(和我的代码)编译工作中删除它时。

我搞不清楚问题出在哪里,我的代码似乎很简单:

const ReactDOM = require('react-dom');
const retargetEvents = require('./retargetEvents');
const getStyleElementsFromReactWebComponentStyleLoader = require('./getStyleElementsFromReactWebComponentStyleLoader');

module.exports = {
  create: function(app, tagName, options) {
    const proto = Object.create(HTMLElement.prototype, {
      attachedCallback: {
        value: function() {
          const shadowRoot = this.createShadowRoot();
          const mountPoint = document.createElement('div');
          getStyleElementsFromReactWebComponentStyleLoader().forEach(style =>
            shadowRoot.appendChild(style)
          );
          shadowRoot.appendChild(mountPoint);
          ReactDOM.render(app, mountPoint);
          retargetEvents(shadowRoot);
        },
      },
    });
    document.registerElement(tagName, { prototype: proto });
  },
};
retargetEventsgetStyleElementsFromReactWebComponentStyleLoader中需要有简单的module.export命令。您可以看到它们的源代码here和here。

我已尝试使用ES6导出/导入命令发布我的库。

我的库的完整源代码(就是这3个文件)可以在https://github.com/LukasBombach/react-web-component

中找到

推荐答案

我找到解决方案了!

我的代码中有一些ES6功能,即foreach运算符和速记函数语法。优化者并不接受这一点。我需要用ES5代码替换它,现在它工作得很好。

19