2017-08-14 45 views
2

我正在創作一個我想放在npm上的JavaScript庫。我目前使用在其他項目庫,我一直在使用它的GitHub庫增加一條,作爲一個依賴:無法縮小此文件中的代碼

"dependencies": { 
    // ... others 
    "react-web-component": "LukasBombach/react-web-component", 
} 
我也是用的WebPack與 UglifyJsPlugin

。現在,當我想要構建我的項目時,出現錯誤:

Failed to compile.

Failed to minify the code from this file:

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

Read more here: https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#npm-run-build-fails-to-minify

error Command failed with exit code 1.

這是我的圖書館的問題。當我從我的代碼(和我的代碼)中刪除它時,編譯工作。

我想不出是什麼問題,我的代碼似乎很直截了當:

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命令。你可以看到他們的源代碼herehere

我已經嘗試使用ES6導出/導入命令發佈我的庫。

我的庫的完整源代碼(這只是這3個文件)可以在https://github.com/LukasBombach/react-web-component

+0

你忘了添加反應標籤。 –

+0

如果我理解你的意思,你的意思是我沒有要求React庫。這不是問題,我不需要它。 – Lukas

+0

沒有。由於有反應代碼,我認爲反應必須做一些我不確定的事情。如果是的話,添加標籤的反應將幫助你提出這個問題來反應開發者 –

回答

3

發現我找到了解決辦法!

我在我的代碼中有一些ES6功能,即foreach~運算符和速記函數語法。醜陋的人不接受。我需要用ES5代碼取代它,現在它運行良好。