我有几个相互依赖的JS前端库。这棵树看起来像:
A
/ \
| B
| /
C
因此,当我使用webpack构建B时,我的webpack配置如下:
module.exports = {
entry: {
'B': [__dirname + '/js/exports.js']
},
output: {
path: __dirname + '/dist',
filename: '[name].js',
// `library` determines the name of the global variable
library: '[name]',
libraryTarget: 'umd'
},
externals: [
/^(jquery|\$)$/i
]
};
我的C的webpack配置看起来一样,(输入刚被命名为'C')。 当B建成时,它将C捆绑到其中。
A和B都使用从C导出的常量,称为清单。 Manifest.js看起来像(有些简化):
class _Manifest {
init(id) {
this.id = id;
}
getManifest() {
return this.id;
}
}
export const Manifest = new _Manifest();
A首先调用Manifest.init,然后B调用getManifest()。但是,在B内部,getManifest()返回未定义。我相信这是因为B捆绑了C(因此捆绑了Manifest),A也最终将C拖入node_modules,因为C在B的package.json中被列为B的依赖项。
所以当我这样做
import {Manifest} from "C" from inside of A
它返回与我执行操作时不同的Manifest对象
import {Manifest} from "C" from inside of B
对于这个冗长的问题,我深表歉意,如果没有道理,我深表歉意。试图解释我所能做到的最好的。