与CommonJS的和Browserify骨干网应用 [英] Backbone app with CommonJS and Browserify
问题描述
我想我把现有的应用程序到使用CommonJS的模块,并使用Browserify到捆绑
了模块到一个文件中。
I thinking of bringing my existing app over to using CommonJS modules and using Browserify to bundle
up the modules into one file.
我得到我的头周围的写作模块,但有一件事我有点怀疑之前,我潜水,并开始重写某些位,是如何优化稍微所以我不必包括骨干网,下划线,jQuery和在每个文件中的任何帮助程序文件,即
I'm getting my head around writing modules but the one thing I'm a little sceptical before I dive in and start re-writing certain bits, is how can I optimise it slightly so I don't have to include Backbone, Underscore, jQuery and any helper files in in each file, ie.
var Backbone = require('/backbone');
var $ = require('/jquery');
var _ = require('/underscore');
在每个文件的顶部是怎么回事了一段时间后变得有点乏味。
At the top of each file is going to get a little tedious after a while.
作为一个完整的CommonJS的,Browserify的n00b,我想知道如果我失去了一些东西很明显的地方?
Being a complete CommonJS, Browserify n00b, I'm wondering if I'm missing something very obvious somewhere?
推荐答案
你错过了很明显的事情是,你可以在Node.js的创建全局变量,并在Browserify环境一样。要么做明确地使用 global.Backbone =要求('/骨干网')
,或者只是做不明确骨干=要求('/骨干网')
(不包括 VAR
在前面)。
The very obvious thing you're missing is that you can create globals in Node.js, and in the Browserify environment just the same. Either do it explicitly by using global.Backbone = require('/backbone')
, or less explicit by just doing Backbone = require('/backbone')
(without var
in front).
请注意,在浏览器中,全球
对象实际上是在窗口
对象。但是,连接到窗口
对象就意味着你失去了兼容Node.js的,因为这通常没有一个全局变量窗口
定义。
Note that in the browser, the global
object is in fact the window
object. However, attaching to the window
object would mean you lose compatibility with Node.js, because that typically doesn't have a global variable window
defined.
这篇关于与CommonJS的和Browserify骨干网应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!