简体   繁体   English

如何添加 UMD 以便我可以在浏览器中使用我的模块并将其用作 NPM 模块?

[英]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。

  • I need to be able to call child functions of Mod, like Mod.DoSomething() , Mod.Utils.DoSomethingElse() , etc. from other files in the browser我需要能够从浏览器中的其他文件调用 Mod 的Mod.DoSomething() ,例如Mod.DoSomething()Mod.Utils.DoSomethingElse()
  • It needs to play nice with Webpack, Browserify, RequireJS, etc.它需要与 Webpack、Browserify、RequireJS 等配合得很好。
  • I need to be able to require it as a module in an NPM package, just like any other package.我需要能够将它作为 NPM 包中的一个模块,就像任何其他包一样。 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.

相关问题 如何将整个 Vue 应用程序捆绑为单个 UMD 模块 - How can I bundle an entire Vue app as a single UMD module 如何使用全局变量,以便我可以在我的 javascript 模块上使用它 - How to use global variable so i can use it on my javascript module 如何将模块添加到我的SystemJs配置文件,以便我可以角度导入它 - How to add a module to my SystemJs config file so I can import it in angular 如何使用browserify模块为浏览器调整xml2js节点模块? - How can I use browserify module for adapting xml2js node module for the browser? 我怎么能传递这个变量来从我的模块中使用它? - How I can pass this variable to use it from my module? 为什么我不能修改module.exports? 为什么我的浏览器在 `npm run build` 没有问题后会抱怨? - Why can't I modify module.exports? Why does my browser complain after no problems from `npm run build`? 遇到错误:如何从打字稿中正确使用已翻译并键入的打字稿npm模块? - Getting errors: How can I properly consume my transpiled and typed typescript npm module from typescript? 如何将 mattjs 作为 npm 模块导入到我的 p5 草图中? - How can I import matterjs as an npm module into my p5 sketch? npm 模块如何支持导入和要求? - How can i do npm module support import and require? 如何调试使用 webpack 编译的 npm 模块? - How can I debug an npm module compiled with webpack?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM