这是我想要的。
我所推荐的连接客户端coffeescript的方法是使用连接资产。这似乎需要使用玉助手来实际编译coffeescript,例如。
!=js('monalisa.js')似乎编译monalisa.coffee并生成正确的<script>标记。现在我想使用require.js,在这里我绊倒了。我如何确保连接资产在不使用玉助手的情况下正确地编译所有东西?
下面是我非常简单的app.js:
require('coffee-script');
var express = require('express')
, http = require('http')
, path = require('path')
, connectAssets = require('connect-assets');
var publicDir = path.join(__dirname, 'public');
var app = express();
app.configure(function(){
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use( connectAssets() );
app.use('/public', express.static(publicDir));
app.use(express.logger());
app.use(express.methodOverride());
app.use(app.router);
});
app.configure('development', function(){
app.use(express.errorHandler({
dumpExceptions: true,
showStack: true
}));
});
app.get('/', require('./routes').index);
app.get('/monalisa', require('./routes/monalisa').monalisa);
http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});发布于 2012-12-01 17:19:50
我创建了一个包来帮助解决这个问题,它被称为连接-资产-jspath。
来自自述机构:
安装
npm install connect-assets-jspaths
服务器端使用
assets = require "connect-assets"
jsPaths = require "connect-assets-jspaths"
# Snip ...
app.use assets()
# Exports the global function exportPaths() and jsUrl(); see below in View Helpers.
jsPaths assets
# Optionally, pass a log function to see progress
# jsPaths assets, console.log观察更改和重新编译。
现在,您可以传递一些额外的回调,它将监视您的connect资产目录的更改。
fileChangedCallback = (err, filePath) ->
console.log "File Changed: #{filePath}"
jsPaths assets, console.log, fileChangedCallback, (err, watcher) ->
console.log "Watcher initialized"注意,,您可能希望在生产模式中禁用此功能。
视图使用
该模块输出两个全局函数exportPaths()和jsUrl()。
// Using this in your view
!= exportPaths("jsPaths")
// Turns into this when rendered in production
<script type="text/javascript">
var jsPaths = { "main", "/builtAssets/js/main.13819282742.js" /* snip all the other file paths */ };
</script>
// Using this in your view
- var mainJsPath = jsUrl("/js/main.js")
script(type="text/javascript", data-main="#{mainJsPath}", src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.0.2/require.min.js")
// Turns into this when rendered in production
<script type="text/javascript" data-main="/builtAssets/js/main.13819282742.js" src="//cdnjs.cloudflare.com/ajax/libs/require.js/2.0.2/require.min.js"></script>动态RequireJS路径
现在我们有了一个变量,其中包含了我们的requireJS友好路径,我们可以在RequireJS配置中设置这些路径了。
# Example main.coffee file in /assets/js folder
requirePaths =
paths:
jquery: "//cdnjs.cloudflare.com/ajax/libs/jquery/1.7.2/jquery.min"
underscore: "//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.3.3/underscore-min"
backbone: "//cdnjs.cloudflare.com/ajax/libs/backbone.js/0.9.2/backbone-min"
text: "/js/lib/text"
handlebars: "/js/lib/handlebars"
if jsPaths
for own key, value of jsPaths
# Fix up the lib references
key = key.slice 4 if key.slice(0, 4) == "lib/"
requirePaths.paths[key] = value
require.config
paths: requirePaths.paths
shim:
jquery:
exports: "$"
underscore:
exports: "_"
backbone:
deps: ["underscore", "jquery"]
exports: "Backbone"
require ['app'], (App) ->
new App().initialize()发布于 2012-11-25 05:04:33
试试含羞草,它能帮你把盒子里的每一件东西都拿出来。http://www.mimosajs.com
mimosa new [name]将为您提供一个包含所有这些的初始项目。
发布于 2012-11-26 17:42:52
对新的答案很抱歉,但我决定去算帐。=)
如果您选择Express作为mimosa new工作流的一部分,Mimosa将为您提供一个小型的Express应用程序。如果你选择CoffeeScript,它会在CoffeeScript上给你一个快递应用程序。并且将RequireJS包含在脚手架应用程序中。所以你不需要重写任何东西。你只要把你的东西插进去就行了。如果有什么东西,它给你提供的快递应用将作为一个例子,让你自己去做,而不用米莫萨。
https://stackoverflow.com/questions/13545022
复制相似问题