简体   繁体   English

您如何将 npm package 与 require 和模块导出一起用作普通 JS 库

[英]How do you use an npm package with require & module export as a plain JS library

I'm not sure I'm even asking the right question here, sorry, but I think the two general ones are:我不确定我什至在这里问了正确的问题,抱歉,但我认为两个一般问题是:

  • In what way do you need to modify a node.js package using require etc to be used as a plain embedded script/library in HTML?您需要以什么方式使用require等修改 node.js package 以用作 HTML 中的普通嵌入式脚本/库?
  • How do you call a class constructor (?) in JS as a function to validate a form field?如何在 JS 中调用 class 构造函数(?)作为 function 来验证表单字段?

I'm trying to use this small JS library NoSwearingPlease (which is an npm package) in an environment with no node or build system – so I'm just trying to call it like you would jQuery or something with a script & src in the HTML, and then utilise it with a small inline script.我正在尝试在没有节点或构建系统的环境中使用这个小型 JS 库NoSwearingPlease (这是一个 npm 包)——所以我只是想像 jQuery 或在HTML,然后用一个小的内联脚本来使用它。

I can see a couple of things are required to get this working:我可以看到要使其正常工作需要做几件事:

  1. the JSON file needs to be called in a different way (not using require etc) JSON 文件需要以不同的方式调用(不使用 require 等)
  2. the checker variable needs to be rewritten, again without require检查器变量需要重写,再次不需要

I attempted using jQuery getJSON but I just don't understand the class & scope bits of the library enough to use it I think:我尝试使用 jQuery getJSON 但我只是不明白 class 和 scope 位足以使用它我认为:

var noswearlist = $.getJSON( "./noswearing-swears.json" );
function() {
    console.log( "got swear list from inline script" );
  })
    .fail(function() {
      console.log( "failed to get swear list" );
    })
  noswearlist.done(function() {
    console.log( "done callback as child of noswearlist variable" );
    var checker = new NoSwearing(noswearlist);
    console.log(checker);
});

Please halp.请停下来。 Thanks!谢谢!

No need to modify, when outside of node the class is just appended to window (global):无需修改,当在节点之外 class 只是附加到window (全局):

 fetch("https://cdn.jsdelivr.net/gh/ThreeLetters/NoSwearingPlease@master/swears.json").then(response => { return response.json(); }).then(data => { var noSwearing = new NoSwearing(data); console.log(noSwearing.check("squarehead")); });
 <script src="https://cdn.jsdelivr.net/gh/ThreeLetters/NoSwearingPlease@master/index.js"></script>

In the future, you can answer this type of question on your own by looking through the source code and looking up things you don't understand.以后,您可以通过查看源代码并查找您不理解的内容来自行回答此类问题。 That being said, here's what I was able to gather doing that myself.话虽这么说,这就是我自己收集到的东西。

For your first question, if you have no build tools you can't use require , you have to hope your NPM package supports adding the class to the window or has a UMD export (which in this case, it does). For your first question, if you have no build tools you can't use require , you have to hope your NPM package supports adding the class to the window or has a UMD export (which in this case, it does). If so, you can download the source code or use a CDN like JSDelivr and add a <script> tag to link it.如果是这样,您可以下载源代码或使用 JSDelivr 之类的 CDN 并添加<script>标签来链接它。

<script src="https://cdn.jsdelivr.net/gh/ThreeLetters/NoSwearingPlease@master/index.js"></script>

I'm having a hard time deciphering your script (it has a few syntax errors as far as I can tell), so here's what you do if you have a variable ns containing the JSON and the string str that you need to check:我很难破译你的脚本(据我所知,它有一些语法错误),所以如果你有一个包含 JSON 和你需要检查的字符串str的变量ns ,你可以这样做:

var checker = new NoSwearing(ns);
checker.check(str);

As an aside, you should really use build tools to optimize your bundle size and make using packages a lot easier.顺便说一句,您应该真正使用构建工具来优化您的包大小并使使用包变得更容易。 And consider dropping jQuery for document.querySelector , fetch / XMLHttpRequest , and other modern JavaScript APIs.并考虑为document.querySelectorfetch / XMLHttpRequest和其他现代 JavaScript API 删除 jQuery。

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

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