[英]How can I add UMD so I can use my module in the browser and as an NPM module?
I'm trying to make my NPM module work in the browser, but I'm having a little trouble understanding the UMD syntax.我试图让我的 NPM 模块在浏览器中工作,但我在理解 UMD 语法时遇到了一些麻烦。 Here are my requirements for my module, which I'll call Mod.
这是我对模块的要求,我将其称为 Mod。
Mod.DoSomething()
, Mod.Utils.DoSomethingElse()
, etc. from other files in the browserMod.DoSomething()
,例如Mod.DoSomething()
、 Mod.Utils.DoSomethingElse()
等var mod = require('mod'); var returnedVal = Mod.DoSomething;
I don't have any dependencies, but I'd appreciate an example of how to do it both with and without dependencies.我没有任何依赖项,但我很想知道如何在有和没有依赖项的情况下执行此操作的示例。 One of my main questions is how to export the child functions, so please include them in the example.
我的主要问题之一是如何导出子函数,因此请在示例中包含它们。 Thanks!
谢谢!
I went through the same situation recently so even though it was an old question I would like to share my experience.我最近经历了同样的情况,所以即使这是一个老问题,我也想分享我的经验。
I use webpack and to make my npm module work in browser the solution I can find is to set webpack output setting libraryTarget: 'umd'
, check this article for details我使用 webpack 并使我的 npm 模块在浏览器中工作,我能找到的解决方案是设置webpack 输出设置
libraryTarget: 'umd'
, 查看这篇文章了解详细信息
But one mistake I made after reading that article was I misunderstand the relation between umd and es6 module, thought they were basically the same.但是我在阅读那篇文章后犯的一个错误是我误解了 umd 和 es6 模块之间的关系,认为它们基本相同。 Check my own question for details ES6 modules via script tag has the error "The requested module does not provide an export named 'default' "
检查我自己的问题以获取详细信息ES6 模块通过脚本标记有错误“请求的模块不提供名为‘默认’的导出”
About which module I should use in browser I would like to quote an answer here https://stackoverflow.com/a/55659242/301513关于我应该在浏览器中使用哪个模块我想在这里引用一个答案https://stackoverflow.com/a/55659242/301513
Today, the most likely answer is: use the UMD module.
今天,最可能的答案是:使用 UMD 模块。 Some time in the future it may be: use ECMAScript modules;
未来某个时候可能是:使用 ECMAScript 模块; but we don't yet (2019) have consensus on how those will be distributed.
但我们还没有(2019 年)就这些将如何分配达成共识。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.