如何将npm用于前端依赖? [英] How can I use npm for front-end dependencies?

查看:448
本文介绍了如何将npm用于前端依赖?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想问一下是否有可能(通常是一个好主意)使用npm来处理前端依赖(Backbone,jQuery)。

I want to ask if it is possible (and generally a good idea) to use npm to handle front-end dependencies (Backbone, jQuery).

我有发现Backbone,jQuery等都可以通过npm获得,但我必须设置另一个提取点(默认为 node_modules )或符号链接或其他...

I have found that Backbone, jQuery and so on are all available through npm but I would have to set another extraction point (the default is node_modules) or symlink or something else...

以前有人这样做过吗?

有可能吗?

我需要更改 package.json

推荐答案

简短回答:有点

这主要取决于模块作者支持这一点,但这并不常见。 Socket.io就是这种支持模块的一个例子,如其登陆页面所示。然而,还有其他解决方案。这些是我实际上知道的两个:

It is largely up to the module author to support this, but it isn't common. Socket.io is an example of such a supporting module, as demonstrated on their landing page. There are other solutions however. These are the two I actually know anything about:


  • http ://ender.no.de/ - Ender JS,用于客户端模块的自描述NPM模拟。对我的口味有点过分了。

  • https://github.com/substack/node -browserify - Browserify,一个实用程序,它将遍历您的依赖项,并允许您通过模拟node.js模块模式输出单个脚本。您可以使用jake | cake | rake | make构建脚本来吐出您的application.js,如果您想获得它,甚至可以自动化它。我简单地使用了这个,但确定它有点笨重,并且调试变得烦人。此外,并非所有双环境npm模块都要通过browserify运行。

  • http://ender.no.de/ - Ender JS, self-described NPM analogue for client modules. A bit too involved for my tastes.
  • https://github.com/substack/node-browserify - Browserify, a utility that will walk your dependencies and allow you to output a single script by emulating the node.js module pattern. You can use a jake|cake|rake|make build script to spit out your application.js, and even automate it if you want to get fancy. I used this briefly, but decided it was a bit clunky, and became annoying to debug. Also, not all dual-environment npm modules like to be run through browserify.

就我个人而言,我目前选择使用RequireJS( http: //requirejs.org/ )并手动管理我的模块,类似于Mozilla对BrowserQuest示例应用程序的处理方式( https:// github.com/mozilla/BrowserQuest )。请注意,这带来了必须潜在地填充诸如主干或下划线之类的模块的挑战,这些模块删除了对AMD样式模块加载器的支持。你可以在这里找到一个关于匀场的例子: http://tbranyen.com / post / amdrequirejs-shim-plugin-for-loading-incompatible-javascript

Personally, I am currently opting for using RequireJS ( http://requirejs.org/ ) and manually managing my modules, similar to how Mozilla does with their BrowserQuest sample application ( https://github.com/mozilla/BrowserQuest ). Note that this comes with the challenge of having to potentially shim modules like backbone or underscore which removed support for AMD style module loaders. You can find an example of what is involved in shimming here: http://tbranyen.com/post/amdrequirejs-shim-plugin-for-loading-incompatible-javascript

真的好像不管怎么样都会受伤,这是为什么本机模块支持是一个热门话题。

Really it seems like it is going to hurt no matter what, which is why native module support is such a hot topic.

这篇关于如何将npm用于前端依赖?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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