有没有办法在咖啡脚本中包含文件?
我想知道是否有办法将文件包含在咖啡脚本中。 类似于 C 中的 #include
或 PHP 中的 require
...
I'd like to know if there is a way to include a file in a coffee script.
Something like #include
in C or require
in PHP...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您将coffeescript 与node.js 一起使用(例如,当使用命令行工具
coffee
时),那么您可以像使用JS 文件一样使用node 的require()
函数。假设您想将
included-file.coffee
包含在main.coffee
中:在
included-file.coffee
中:声明并导出您想要的对象 在main.coffee
中,您可以说:
这为您提供了干净的模块化,并避免了包含外部代码时的命名空间冲突。
If you use coffeescript with node.js (e.g. when using the commandline tool
coffee
) then you can use node'srequire()
function exactly as you would for a JS-file.Say you want to include
included-file.coffee
inmain.coffee
:In
included-file.coffee
: declare and export objects you want to exportIn
main.coffee
you can then say:This gives you clean modularization and avoids namespace conflicts when including external code.
coffeescript-concat 怎么样?
How about coffeescript-concat?
Tl;DR:Browserify,可能使用诸如 Grunt...
解决方案回顾
构建工具 + 导入预处理器
如果您想要的是在浏览器中运行单个 JS 文件,我建议使用诸如 Grunt (或 Gulp,或 蛋糕,或含羞草,或 任何其他)来预处理您的 Coffeescript,以及一个包含/需要/导入模块,该模块将连接包含的内容文件到您编译的输出中,例如以下之一:
exports/require
API 在您的代码中,然后提取所需的所有内容并将其连接到浏览器可包含的文件中。存在于 Grunt、Gulp,含羞草 可能还有大多数其他。直到今天,我认为如果您追求 Node 和浏览器的兼容性(甚至其他),它可能是最好的解决方案独立导入预处理器
如果您想避免构建工具的额外复杂性,可以使用 Browserify 独立,或不基于 Node 的
require
的替代方案,如 coffeescript-concat 或 Coffee-Stir[不推荐] 异步动态加载(AJAX + eval)
如果您专门为浏览器编写并且不介意或者确实希望您的脚本分布在通过 AJAX 获取的多个文件中,那么您可以使用多种工具例如:
$.getScript
Tl;DR: Browserify, possibly with a build tool like Grunt...
Solutions review
Build tool + import pre-processor
If what you want is a single JS file to be run in the browser, I recommend using a build tool like Grunt (or Gulp, or Cake, or Mimosa, or any other) to pre-process your Coffeescript, along with an include/require/import module that will concatenate included files into your compiled output, like one of these:
exports/require
API in your code, then extracts and concatenates everything required into a browser includable file. Exists for Grunt, Gulp, Mimosa and probably most others . To this day I reckon it is probably the best solution if you're after compatibility both Node and the browser (and even otherwise)Standalone import pre-processor
If you'd rather avoid the extra-complexity of a build tool, you can use Browserify stand-alone, or alternatives not based on Node's
require
like coffeescript-concat or Coffee-Stir[Not recommended] Asynchronous dynamic loading (AJAX + eval)
If you're writing exclusively for the browser and don't mind, or rather really want, your script being spread across several files fetched via AJAX, you can use a myriad of tools like:
$.getScript
你可以尝试我为解决同样的问题而制作的库coffee-stir
这很简单。
只需键入 #include 和您要包含的文件的名称
即可了解详细信息
http://beastjavascript.github.io/Coffee-Stir/
You can try this library I made to solve this same problem coffee-stir
its very simple.
Just type #include and the name of the file that you want to include
For details
http://beastjavascript.github.io/Coffee-Stir/
我发现在处理它们之前使用“gulp-concat”合并我的咖啡脚本可以达到目的。可以使用 npm 轻松将其安装到您的项目中。
npm install gulp-concat
然后编辑你的 gulpfile.js:
这是我在 gulp 将其处理为最终构建 Javascript 之前用来连接所有咖啡脚本的代码。唯一的问题是文件是按字母顺序处理的。您可以明确指定要处理哪个文件来实现您自己的文件顺序,但您会失去添加动态 .coffee 文件的灵活性。
截至 2015 年 2 月 25 日,gulp-concat 可在此网址获取。
I found that using "gulp-concat" to merge my coffee scripts before processing them did the trick. It can be easily installed to your project with npm.
npm install gulp-concat
Then edit your gulpfile.js:
This is the code I used to concatenate all my coffee scripts before gulp processed it into the final build Javascript. The only issue is the files are processed in alphabetical order. You can explicitly state which file to process to achieve your own file order, but you lose the flexibility of adding dynamic .coffee files.
gulp-concat as of February 25th, 2015 is available at this url.
Rails 使用链轮来执行此操作,并且此语法已适应 https://www.npmjs .org/package/grunt-sprockets-directives。对我来说效果很好。
Rails uses sprockets to do this, and this syntax has been adapted to https://www.npmjs.org/package/grunt-sprockets-directives. Works well for me.