Backbone.js 如何在CoffeeScript中编译内部匿名函数
我正在创建主干插件,假设我有一个CoffeeScript代码,如下所示Backbone.js 如何在CoffeeScript中编译内部匿名函数,backbone.js,coffeescript,gruntjs,Backbone.js,Coffeescript,Gruntjs,我正在创建主干插件,假设我有一个CoffeeScript代码,如下所示 ((root, factory) -> if typeof exports is "object" and typeof require is "function" # CommonJS module.exports = factory(require("backbone")) else if typeof define is "function" and define.amd # AM
((root, factory) ->
if typeof exports is "object" and typeof require is "function"
# CommonJS
module.exports = factory(require("backbone"))
else if typeof define is "function" and define.amd
# AMD
define [
"backbone"
], (Backbone) ->
factory Backbone or root.Backbone
else
# Browser globals
factory Backbone
) @, (Backbone) ->
console.log Backbone # here will be main code.
然后我编译,结果是:
(function() {
(function(root, factory) {
if (typeof exports === "object" && typeof require === "function") {
return module.exports = factory(require("backbone"));
} else if (typeof define === "function" && define.amd) {
return define(["backbone"], function(Backbone) {
return factory(Backbone || root.Backbone);
});
} else {
return factory(Backbone);
}
})(this, function(Backbone) {
return console.log(Backbone);
});
}).call(this);
然而,我想划分为2个CoffeeScript文件(或任意数量的文件),然后通过grunt contrib coffee对它们进行合并,但是编译后的JS应该是相同的结果。可能吗?你有什么想法吗
a.咖啡
:
((root, factory) ->
if typeof exports is "object" and typeof require is "function"
# CommonJS
module.exports = factory(require("backbone"))
else if typeof define is "function" and define.amd
# AMD
define [
"backbone"
], (Backbone) ->
factory Backbone or root.Backbone
else
# Browser globals
factory Backbone
) @, (Backbone) ->
console.log Backbone
b.咖啡
:
((root, factory) ->
if typeof exports is "object" and typeof require is "function"
# CommonJS
module.exports = factory(require("backbone"))
else if typeof define is "function" and define.amd
# AMD
define [
"backbone"
], (Backbone) ->
factory Backbone or root.Backbone
else
# Browser globals
factory Backbone
) @, (Backbone) ->
console.log Backbone
更新
根据库森的想法,我以某种方式实现了我想做的事情,尽管这是一个棘手的问题;P
grunt缩进
->grunt浓缩咖啡
->grunt咖啡
grunt.initConfig
indent:
scripts:
src: ['src/main.coffee']
dest: 'tmp/'
options:
style: 'space'
size: 2
change: 1
concat:
sources:
options:
separator: ''
src: [
'src/entry.coffee'
'tmp/main.coffee'
]
dest: 'tmp/example.coffee'
coffee:
compile:
files:
'lib/example.js': 'tmp/example.coffee'
这将是困难的,因为与CoffeeScript相关的缩进问题 您可以做的是编写一个脚本来连接这两个文件,但您需要尊重第二个文件中的缩进 连接之后,您可以编译CoffeeScript源代码 咖啡
((root, factory) ->
if typeof exports is "object" and typeof require is "function"
# CommonJS
module.exports = factory(require("backbone"))
else if typeof define is "function" and define.amd
# AMD
define [
"backbone"
], (Backbone) ->
factory Backbone or root.Backbone
else
# Browser globals
factory Backbone
) @, (Backbone) ->
console.log Backbone # With indentation
((root, factory) ->
if typeof exports is "object" and typeof require is "function"
# CommonJS
module.exports = factory(require("backbone"))
else if typeof define is "function" and define.amd
# AMD
define [
"backbone"
], (Backbone) ->
factory Backbone or root.Backbone
else
# Browser globals
factory Backbone
) @, (Backbone) ->
console.log Backbone # With indentation
b.咖啡
((root, factory) ->
if typeof exports is "object" and typeof require is "function"
# CommonJS
module.exports = factory(require("backbone"))
else if typeof define is "function" and define.amd
# AMD
define [
"backbone"
], (Backbone) ->
factory Backbone or root.Backbone
else
# Browser globals
factory Backbone
) @, (Backbone) ->
console.log Backbone # With indentation
((root, factory) ->
if typeof exports is "object" and typeof require is "function"
# CommonJS
module.exports = factory(require("backbone"))
else if typeof define is "function" and define.amd
# AMD
define [
"backbone"
], (Backbone) ->
factory Backbone or root.Backbone
else
# Browser globals
factory Backbone
) @, (Backbone) ->
console.log Backbone # With indentation
连接两个文件,以便: 胶粘咖啡
((root, factory) ->
if typeof exports is "object" and typeof require is "function"
# CommonJS
module.exports = factory(require("backbone"))
else if typeof define is "function" and define.amd
# AMD
define [
"backbone"
], (Backbone) ->
factory Backbone or root.Backbone
else
# Browser globals
factory Backbone
) @, (Backbone) ->
console.log Backbone # With indentation
((root, factory) ->
if typeof exports is "object" and typeof require is "function"
# CommonJS
module.exports = factory(require("backbone"))
else if typeof define is "function" and define.amd
# AMD
define [
"backbone"
], (Backbone) ->
factory Backbone or root.Backbone
else
# Browser globals
factory Backbone
) @, (Backbone) ->
console.log Backbone # With indentation
然后编译:
coffee——用胶水编译。coffee
谢谢!这应该是可行的,但是关注第二个文件的缩进有点奇怪:(哇,有grunt缩进插件(),所以这也应该是可行的,第二个文件的grunt缩进->grunt concat->grunt coffee:CoffeeScript中的PIndentation是语法的一部分;)
grunt.initConfig
indent:
scripts:
src: ['src/main.coffee']
dest: 'tmp/'
options:
style: 'space'
size: 2
change: 1
concat:
sources:
options:
separator: ''
src: [
'src/entry.coffee'
'tmp/main.coffee'
]
dest: 'tmp/example.coffee'
coffee:
compile:
files:
'lib/example.js': 'tmp/example.coffee'