我正在创建Backbone插件,并假设我有一个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 # 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.coffee
:
((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) ->
b.coffee
:
console.log Backbone
根据Kursion的想法,我以某种方式实现了我想做的事,尽管这很棘手; P
grunt indent
- > grunt concat
- > grunt coffee
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'
答案 0 :(得分:1)
由于与CoffeeScript相关的缩进问题,这很难。
你可以做的是连接这两个文件的脚本,但你需要尊重第二个文件中的缩进。
连接后,您可以编译CoffeeScript源。
<强> a.coffee 强>
((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) ->
<强> b.coffee 强>
console.log Backbone # With indentation
两个文件的连接以便: 的 glued.coffee 强>
((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
然后编译:{{1}}