简体   繁体   English

Webpack库重新导出

[英]Webpack library re-export

I have 2 libraries A and B. 我有2个图书馆A和B。

  • A is bundled using webpack in a first pass then published to a npm repository and later consumed by B. A contains let's say a class MyA. 首先使用webpack将A捆绑在一起,然后发布到npm存储库,然后由B使用。A包含一个类MyA。
  • B depends on A among other libraries and all libraries are bundled together in B (=> A is bundled in B). B在其他库中依赖于A,并且所有库都捆绑在B中(=> A捆绑在B中)。
  • B should re-export certain parts of A (eg MyA) to make them visible to the outside world. B应该重新导出A的某些部分(例如MyA)以使其对外界可见。

In webpack.conf.js : webpack.conf.js

module.exports = {
  entry: {
     'b': ['./src/MyB.js', './js/a.js']

In a.js (compiled by webpack) I have: a.js (由webpack编译)中,我有:

var mya = 
.... 
function(module, exports, __webpack_require__) {
   var MyA_1 = __webpack_require__(1);
   exports.MyA = MyA_1.MyA;
}

In b.js bundle I end up with: b.js包中,我最终得到:

function(module, exports) {
   // here we are in MyB
   var a = new MyA(); // this is not found, even if prefixed with mya
},
function(module, exports) {
   // a.js code is bundled with b in b.js
   var mya = ...
   // missing: exports.MyA = mya;
} 

And of course MyA is not visible anywhere outside. 当然,MyA在外面的任何地方都不可见。 Any idea how can I achieve this? 知道我该如何实现吗?

Instead of using an array for your entry points, why not use B as your entry, and in it, require the A library? 为何不使用B作为入口,而不是使用数组作为入口点,而在其中require A库? Then you can use it, export it, make it global, whatever you need. 然后,您可以使用它,将其导出,使其全局化,无论您需要什么。

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

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