如何创建一个环境不可知的JavaScript库 [英] how to create an environment agnostic javascript library

查看:146
本文介绍了如何创建一个环境不可知的JavaScript库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个javascript库,我希望它与环境无关(它不会使用DOM,AJAX或NodeJS api。它将是vanilla javascript)。因此,它应该在任何javascript环境(浏览器,npm,流星智能包,V8 C绑定......)中运行。

I'm creating a javascript library, and i want it to be environment agnostic (It will not use DOM, AJAX, or NodeJS api. It will be vanilla javascript). So, it's supposed to run in any javascript environment (browsers, npm, meteor smart packages, V8 C bindings...).

我目前的方法是创建git repo with该库,所有库都在一个全局变量中,而不考虑像CommonJS或AMD这样的模式。
稍后,我将使用我的库作为git子模块创建另一个git repo,并创建将其作为npm模块发布所需的内容。我担心这是一个好方法,我没有发现任何人这样做。
优点:代码将是vanilla javascript,没有环境模式的意识。它不会将自己绑定到CommonJS。它可以重新打包(复制和粘贴或git子模块)到任何JavaScript环境。它将根据需要发送到浏览器。
缺点:我必须保持与我想要支持的环境一样多的git。至少有第二个git repo在npm上发送。

My currently approach is creating git repo with the library, with all the library inside a single global variable, without thinking about patterns like CommonJS or AMD. Later, i'll create another git repo, using my library as a git submodule, and create what is needed to release it as a npm module. I'm concerned if it's a good approach, i didn't found anyone doing this way. Pros: code will be vanilla javascript, without awareness of environment patterns. It will not bind itself to CommonJS. It will be repackable (copy and paste or git submodule) to any javascript environment. It will be as small as needed to be sent to browsers. Cons: I'll have to maintain as many git as environments i want to support. At least a second git repo to deliver on npm.

以jQuery为例,它只在一个git仓库中运行浏览器和nodejs。有些代码需要注意在nodejs或其他CommonJS兼容环境中运行的exports变量。
优点:只有一个git repo to mantain。
缺点:它将绑定到CommonJS模式(以实现npm兼容性)

Taking jQuery as example, it runs in both browser and nodejs, with just one git repo. There is some code to be aware of the "exports" variable to run on nodejs or other CommonJS compatible enviroment. Pros: Just one git repo to mantain. Cons: It will be binded to CommonJS pattern (to achieve npm compatibility)

我的问题是:我是否遵循正确(或可接受)的方法?或者我应该遵循jquery的路径,并尝试创建一个git仓库?

My question is: Am i following a correct (or acceptable) approach? Or should i follow jquery's path, and try to create a single git repo?

更新1:

Browserify 和其他 require()库不是有效的答案。我的问题不是如何在浏览器上使用 require(),相反,它是关于实现环境不可知论的架构模式。

Update 1:
Browserify and other require() libraries are not valid answers. My question is not how to use require() on the browser, instead, it's about the architecture pattern to achieve enviroment agnosticism.

更新2:

创建一个browser / nodejs模块不是问题众所周知。问题是:可以建立一个真正的环境不可知库吗?此示例绑定到CommonJS模式,在NodeJS中使用。

Update 2:
Create a browser/nodejs module is not the question, it's known. The question is: can make a real enviroment agnostic library? This example is binded to CommonJS pattern, used in NodeJS.

推荐答案

使用构建工具是此问题的答案。使用构建工具,您可以使用最佳代码实践进行开发,而无需将代码应用于当今的某些环境标准(AMD,commonjs ...),并且仍然将代码发布到这些环境中。

"Use a build tool" is the answer for this question. With a build tool, you can develop with the best code pratices, without accopling your code to some enviroment standard of today (AMD, commonjs...) and still publish your code to these kind of enviroments.

例如,我正在使用 Grunt.js 来运行一些任务,比如构建,lint,test等等。

它执行繁琐的操作(缩小,编译......),如Make,Maven,Gulp.js以及其他各种操作。

For example, I'm using Grunt.js to run some tasks, like build, lint, test, etc.
It perform tedious operations (minification, compilation...) like Make, Maven, Gulp.js, and various others.

构建任务可以处理已编译代码的标准(如commonjs)。因此,库可以完全与环境无关,构建过程处理环境适应。

请注意,我不是在讨论编译二进制文件。它正在编译源代码到另一个源,比如CoffeScript到JavaScript。在我的例子中,它是没有环境标准的JavaScript汇编到具有commonjs标准的JavaScript(作为 Node.js运行)模块)。$
最终结果是我可以将我的项目编译成各种标准,而不会弄乱我的代码。

The build task can handle standards (like commonjs) for the compiled code. So, the library can be totally enviroment agnostic, and the build process handle enviroment adaptations.
Note that i'm not talking about compiling to binaries. It's compiling source to another source, like CoffeScript to JavaScript. In my case, it's compilation of JavaScript without enviroment standard to JavaScript with commonjs standard (to run as a Node.js module).
The final result is that i can compile my project to various standards without messing with my code.

Aditional,在构建阶段,我可以思考 - 未来,如xmojmr 回答并使用我的JavaScript代码上的EcmaScript 6功能,使用Grunt插件,如 grunt-es6-transpiler grunt-traceur 进行编译ES 6到5的JavaScript代码(所以它可以在今天的环境中运行)

Aditionally, with a build phase i can "think-future", like xmojmr answered and use the EcmaScript 6 features on my JavaScript code, using Grunt plugins like grunt-es6-transpiler or grunt-traceur to compile JavaScript code from ES 6 to 5 (so it can run on enviroments of today)

这篇关于如何创建一个环境不可知的JavaScript库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆