【发布时间】:2014-02-04 15:29:02
【问题描述】:
我正在创建 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'
【问题讨论】:
标签: backbone.js coffeescript gruntjs