简体   繁体   English

在 NPM 上捆绑并发布客户端 web 代码

[英]Bundle and publish client-side web code on NPM

I made a Javascript file.我制作了一个 Javascript 文件。 Let's say the contents of it are this:假设它的内容是这样的:

 let myCoolAlert = (str) => { alert(str) } // in a different js file (SO doesn't allow you to cross-file as far as I know myCoolAlert('Hello World!')

I already hosted the JS file on a CDN.我已经在 CDN 上托管了 JS 文件。 Now, I want it to be automatically hosted locally by whoever installed it if you install it via NPM.现在,如果您通过 NPM 安装它,我希望它由安装它的人在本地自动托管。 Is there a way to do this?有没有办法做到这一点?

Also, I noticed that to do the same using Socket.io, you have to pass Socket.io to the HTTP/HTTPS server you created.另外,我注意到要使用 Socket.io 执行相同的操作,您必须将 Socket.io 传递给您创建的 HTTP/HTTPS 服务器。 Will I have to do this also?我也必须这样做吗? (I would prefer not). (我宁愿不要)。

Thanks!谢谢!

Edit:编辑:

I am trying to make a better alert system (like sweetalert).我正在尝试制作一个更好的警报系统(如 sweetalert)。 I coded it in Javascript and works when using it through the CDN.我在 Javascript 中对其进行了编码,并在通过 CDN 使用它时工作。 However, I also want users to be able to install this via NPM (kind of like SweetAlert? I am not sure about that last statement however because I do not use it).但是,我也希望用户能够通过 NPM 安装它(有点像 SweetAlert?我不确定最后一条语句,因为我不使用它)。 When they install it with NPM, it's obviously going to be useless because it is for the browser.当他们用 NPM 安装它时,它显然是无用的,因为它是用于浏览器的。 However, I want them to either:但是,我希望他们:

  1. Automatically have the source code needed available at a URL like localhost:3000(or server name)/betterAlert.js and be able to use that URL as a script in the HTML files自动在 URL 中获得所需的源代码,例如localhost:3000(or server name)/betterAlert.js ,并能够将 URL 用作 Z4C4AD5FCA007A18AA44DB 文件中的脚本
  2. OR, have the user pass the HTTP or HTTPS server they created to the module (like socket.io does) and have it automatically host it from there.或者,让用户将他们创建的 HTTP 或 HTTPS 服务器传递给模块(就像 socket.io 一样)并让它从那里自动托管它。

Please note:请注意:

The code I am trying to bundle is native to the web.我试图捆绑的代码是 web 的原生代码。 Will it still work with a bundler like webpack?它仍然可以与 webpack 之类的捆绑器一起使用吗?

Is this possible?这可能吗? Thanks again.再次感谢。

To bundle client-side code and publish it through NPM you'll need to do a couple things: learn how to package and publish modules , and write some code that can be consumed.要捆绑客户端代码并通过 NPM 发布它,您需要做几件事: 学习如何 package 和发布模块,并编写一些可以使用的代码。 This means using module.exports or export to expose your library, depending on whether you want to use CJS or ESM.这意味着使用module.exportsexport来公开您的库,具体取决于您要使用 CJS 还是 ESM。 The consumer of your library can usually be assumed to be using Webpack, Fuse, Rollup, or some other bundler which knows how to deal with modules.通常可以假定您的库的使用者正在使用 Webpack、Fuse、Rollup 或其他知道如何处理模块的捆绑程序。

You can also use a tool like Rollup yourself to build and bundle up your library for different targets and apply various transformations to it ( example from my own library boilerplate ).您还可以自己使用像 Rollup 这样的工具来为不同的目标构建和捆绑您的库,并对它应用各种转换( 来自我自己的库样板的示例)。 Using a bundler like this makes more and more sense as your library inevitably grows larger.随着您的库不可避免地变大,使用这样的捆绑器变得越来越有意义。

Using modules like this rather than distributing through a CDN or in some other way that puts your library code on the global/window object is generally better for consumption in complex apps, large apps, and/or apps already being built with tools like Webpack (so, anything written in React, Angular, Vue, etc.).使用这样的模块而不是通过 CDN 分发或以其他方式将库代码放在全局/窗口上 object 通常更适合在复杂应用程序、大型应用程序和/或已经使用 Webpack 等工具构建的应用程序中使用(所以,任何用 React、Angular、Vue 等编写的东西)。 But having a CDN distribution is still a good idea for something like your library, since it may well be used by people building sites with jQuery and vanilla JS.但是对于像你的库这样的东西来说,拥有一个 CDN 分发仍然是一个好主意,因为它很可能被人们使用 jQuery 和 vanilla JS 构建网站。

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

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