[英]Releasing React library with webpack
I created a library with React as its external dependency. 我创建了一个以React为外部依赖的库。 The library is released as UMD: 该库以UMD形式发布:
module.exports = {
entry: [
path.join(__dirname, 'src', 'MyLib.js')
],
output: {
path: path.join(__dirname, 'build'),
libraryTarget: 'umd',
library: 'MyLib',
filename: 'my-lib.js'
},
externals: {
react: true,
'react-dom': true
}
};
Now I would like to be able to use the library in the following scenarios: 现在,我希望能够在以下情况下使用该库:
The first point is actually the simple one - it should works now as it is. 第一点实际上是简单的一点-现在应该可以正常使用了。 But I don't know how to achieve the second point and not breaking the first one. 但是我不知道如何达到第二点,而不是打破第一点。 I tried using ProvidePlugin
but with no luck. 我尝试使用ProvidePlugin
但没有运气。
Answering my own question: I need to specify external
differently. 回答我自己的问题:我需要以不同的方式指定external
。 According to webpack documentation: 根据webpack文档:
Note: If using umd you can specify an object as external value with property commonjs, commonjs2, amd and root to set different values for each import kind. 注意:如果使用umd,则可以使用commonjs,commonjs2,amd和root属性将一个对象指定为外部值,以为每种导入类型设置不同的值。
So whole object should look like this: 因此整个对象应如下所示:
externals: {
'react': {
'commonjs': 'react',
'commonjs2': 'react',
'amd': 'react',
'root': 'React'
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.