构建咖啡脚本代码?
在Rails 3.1下,我试图找出如何将一些coffeescript类从我的控制器默认coffeescript文件(home.js.coffee
)移到另一个文件中,以便构建整个a一点。
有谁知道如何将咖啡脚本文件“包含”到另一个文件中?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您想要做的是导出功能。例如,如果您开始
并决定将
Foo
移至其自己的文件,则该文件应如下所示(其中
window.Foo = Foo
使Foo< /code> 全局),并且
Bar
的文件应以 Sprockets 指令开头(假设您已将
Foo
的文件命名为Foo.js。咖啡
)。每个文件都会独立编译成 JS,但 Sprockets 会确保Foo
包含在Bar
之前。请注意,作为快捷方式,您可以删除
window.Foo = Foo
行,而是编写或简单地
定义一个名为
Foo
的类,该类附加到 <代码>窗口对象。What you want to do is export functionality. For instance, if you start with
and you decide you move
Foo
to its own file, that file should look like(where
window.Foo = Foo
makesFoo
a global), andBar
's file should start with the Sprockets directive(assuming that you've named
Foo
's fileFoo.js.coffee
). Each file is compiled into JS independently, but Sprockets will ensure thatFoo
is included beforeBar
.Note that, as a shortcut, you can get rid of the
window.Foo = Foo
line, and instead writeor simply
to define a class named
Foo
that's attached to thewindow
object.虽然使用
window
对象作为在代码的不同部分之间共享功能的地方可以很好地工作,但当您处理大型/复杂的代码库时,它可能会变得有些难看。此外,您必须手动加载所有依赖项,这也可能会让人有些沮丧。许多人最终都会在每个请求中加载所有内容,而不管该特定页面实际需要什么。有许多库旨在解决这些问题并使文件之间的导出和导入功能更易于管理。如今最常见的一个是 RequireJS ,它是 RequireJS 的实现。 com/amdjs/amdjs-api/wiki/AMD" rel="noreferrer">CommonJS AMD 规范。您可以找到其他库以及它们之间的比较 这里,还有一个关于如何使用这些库编写模块化 JavaScript 的精彩教程,位于 Addy Osmani 博客 - 其中还讨论了 ES.next 中即将推出的新模块系统,非常有趣也。
我个人非常喜欢 NodeJS 的模块系统(使用
exports< /code> 对象和
require
函数),所以我使用 node-browserify 将其打包也适合在客户端工作。虽然这不允许像 AMD 规范那样以异步方式动态加载依赖项,但它确实允许在客户端和服务器端很好地共享相同的 JavaScript 代码,这对我来说是一个巨大的好处(主要是因为我也在服务器端使用 NodeJS,所以我不确定这对你来说有多重要)并且它可以很好地将所有依赖项打包在一起(这样它们就可以同步require() d) 通过解析您的 JavaScript 代码并查找简单的
require()
调用以确定给定脚本运行所需的内容。While using the
window
object as a place to share functionality among different parts of your code can work quite well, it can get somewhat ugly when you're working on a big/complex codebase. Also, you have to handle loading all of the dependencies manually, which can get somewhat frustrating too. Many people just end up loading everything in every request, regardless of what's actually needed for that particular page.There are plenty of libraries built in an attempt to solve those issues and make exporting and importing functionality among files more managable. One of the more common ones today is RequireJS which is an implementation of the CommonJS AMD specs. You can find other libraries and a comparison between them here, and there's a great tutorial on how to write modular JavaScript using those libraries over at Addy Osmani blog - which also talks about the new upcoming modules system in ES.next, which is quite interesting too.
I personally really like NodeJS's modules system (with the
exports
object and therequire
function), so I use node-browserify to package it up for working on the client side too. While that doesn't allow dynamically loading dependencies in an asynchronous way as the AMD specs does, it does allow to nicely share the same JavaScript code on both the client-side and server-side, which is a huge bonus for me (mainly because I'm working with NodeJS on the server side too, so I'm not sure how important that might be for you) and it handles packaging all of the dependencies together nicely for you (so they can be synchronouslyrequire()
d) by parsing your JavaScript code and looking for plainrequire()
calls to determine what a given script requires in order to run.您也可以这样做:
这将使
app
包含所有全局类,而window.app ? {}
确保您只会创建一个应用
。You can also do it like this:
This will make
app
contain all your global classes, andwindow.app ? {}
makes sure that you will only create oneapp
.我不确定这是否直接可能(但有人可以随时纠正我)。
我的理解是,CoffeeScript 解释器在 Sprockets 合并所有资产之前运行。由于 .coffee 文件中的注释不会出现在输出中,并且由于 Sprockets 使用
//=
代码注释指令来构建所有内容,因此我认为没有办法在 CoffeeScript 中创建 Sprockets 指令时间。您仍然可以将您的工作拆分为多个 .coffee 文件,并利用父“指令”javascript 文件来组合各个部分(它可以仅包含 Sprockets 指令,就像 Rails 3.1 中的 Stock application.js 一样)。使用 Sprockets
//= require_tree
指令,您可以拆分您的 CoffeeScript 并仍然保持您的应用程序井井有条,但您仍然会在管理 Sprockets 时保留普通的 Javascript 文件。这个问题可能会解释资产管道好一点了。 Edge 文档中还有一些 Sprockets 帮助程序: http ://edgeapi.rubyonrails.org/classes/ActionView/Helpers/SprocketsHelper.html#method-i-sprockets_javascript_include_tag ,但这会将工作推入您的视图中,这可能会变得丑陋。
希望有帮助!
I'm not sure that's directly possible (but someone feel free to correct me).
My understanding is that the CoffeeScript interpreter runs before Sprockets merges all of your assets. Since comments in .coffee files do not appear in the output, and since Sprockets uses
//=
code comment directives to build everything, I don't think there's a way to create Sprockets directives in CoffeeScript at this time.You could still split up your work into multiple .coffee files and utilize a parent "directive" javascript file to combine the pieces (it could consist of just Sprockets directives, like how the stock application.js ships in Rails 3.1). Using the Sprockets
//= require_tree
directive, you could split your CoffeeScript and still keep your app fairly organized, but you'd still have plain ol' Javascript files lingering around managing Sprockets.This question might explain the asset pipeline a little better. There's also some Sprockets helpers in the Edge documentation here: http://edgeapi.rubyonrails.org/classes/ActionView/Helpers/SprocketsHelper.html#method-i-sprockets_javascript_include_tag , but that then pushes the work into your views which might get ugly.
Hope that helps!
使用 cake,您可以定义文件的顺序以及其他自定义步骤,如 minify 等。
请确保指定所有咖啡文件合并为单个 .js 输出文件。
https://github。 com/jashkenas/coffee-script/wiki/[HowTo]-编译和设置-构建工具
Use cake, you can define the order of files and also other custom steps like minify, etc
Make sure that you specify that all coffee files are combined into single .js output file.
https://github.com/jashkenas/coffee-script/wiki/[HowTo]-Compiling-and-Setting-Up-Build-Tools