2014-01-20 22 views
1

我正在寻找一个简单的JavaScript require/concat工具,用于JavaScript中的节点。简单JS需要/包含/导入工具在节点中使用串联

例如,假设我有4个JS文件:

each.js

function each(arr, fn, scope) { 
    for (var i = 0, l = arr.length; i < l; i++) { 
     fn.call(scope, arr[i], i, arr); 
    } 
} 

on.js

// addEventListener wrapper: 
function on(target, type, callback) { 
    target.addEventListener(type, callback, false); 
} 

onEach.js

// require: each.js 


// require: on.js 


// Add an event listener to multiple elements: 
function onEach(targets, type, callback) { 
    each(targets, function(target) { 
     on(target, type, callback); 
    }); 
} 

all.js

// require: each.js 


// require: on.js 


// require: onEach.js 

最后两个文件有依赖性。我想知道是否有一个命令行节点工具将使用连接来构建这些工具,以用适当的依赖关系替换每个需要的注释/语句/函数调用。 each.js和on.js的内置版本将保持不变,并onEach.js和all.js的内置版本都将是这样的:

function each(arr, fn, scope) { 
    for (var i = 0, l = arr.length; i < l; i++) { 
     fn.call(scope, arr[i], i, arr); 
    } 
} 


// addEventListener wrapper: 
function on(target, type, callback) { 
    target.addEventListener(type, callback, false); 
} 


// Add an event listener to multiple elements: 
function onEach(targets, type, callback) { 
    each(targets, function(target) { 
     on(target, type, callback); 
    }); 
} 

我已经看了gruntgulpRequireJS和其他一些工具,但我还没有找到我喜欢的设置。任何建议,将不胜感激。 :)

+1

我觉得你好像在看着他的[Browserify(http://browserify.org/) – elclanrs

+0

@elclanrs browserify增添了不少unneaded包装代码来制作文件。我正在寻找一些比较简单的东西。 –

+0

很多?这是所有Browserify添加AFAIK:https://gist.github.com/elclanrs/cbb2d384d5ee9e3a1a30,以及每个模块的小型需求功能。 – elclanrs

回答

0

我认为Browserify实际上是一个很好的解决方案。我知道你在评论中提到它不是,但你看过这些选项吗?

--no-builtins 

Turn off builtins. This is handy when you want to run a bundle in node which 
provides the core builtins. 

--no-commondir 

Turn off setting a commondir. This is useful if you want to preserve the 
original paths that a bundle was generated with. 

--bare 

Alias for both --no-builtins, --no-commondir, and sets --insert-global-vars 
to just "__filename,__dirname". This is handy if you want to run bundles in 
node. 

https://github.com/substack/node-browserify