有没有办法在咖啡脚本中包含文件?

发布于 2024-12-09 00:41:16 字数 92 浏览 1 评论 0原文

我想知道是否有办法将文件包含在咖啡脚本中。 类似于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

z祗昰~ 2024-12-16 00:41:16

如果您将coffeescript 与node.js 一起使用(例如,当使用命令行工具coffee 时),那么您可以像使用JS 文件一样使用node 的require() 函数。

假设您想将 included-file.coffee 包含在 main.coffee 中:

included-file.coffee 中:声明并导出您想要的对象 在 main.coffee 中,

someVar = ...
exports.someVar = someVar

您可以说:

someVar = require('included-file.coffee').someVar

这为您提供了干净的模块化,并避免了包含外部代码时的命名空间冲突。

If you use coffeescript with node.js (e.g. when using the commandline tool coffee) then you can use node's require() function exactly as you would for a JS-file.

Say you want to include included-file.coffee in main.coffee:

In included-file.coffee: declare and export objects you want to export

someVar = ...
exports.someVar = someVar

In main.coffee you can then say:

someVar = require('included-file.coffee').someVar

This gives you clean modularization and avoids namespace conflicts when including external code.

牛↙奶布丁 2024-12-16 00:41:16

coffeescript-concat 怎么样?

coffeescript-concat 是一个预处理和连接的实用程序
CoffeeScript 源文件。

它可以轻松地将您的 CoffeeScript 代码保存在单独的单元中,并且
仍然可以轻松运行它们。您可以将源逻辑分开
无需将所有内容组合在一起运行或嵌入
一个网页。此外,coffeescript-concat 将为您提供一个
源文件,可以轻松编译为单个 Javascript 文件。

How about coffeescript-concat?

coffeescript-concat is a utility that preprocesses and concatenates
CoffeeScript source files.

It makes it easy to keep your CoffeeScript code in separate units and
still run them easily. You can keep your source logically separated
without the frustration of putting it all together to run or embed in
a web page. Additionally, coffeescript-concat will give you a single
sourcefile that will easily compile to a single Javascript file.

东北女汉子 2024-12-16 00:41:16

Tl;DR:Browserify,可能使用诸如 Grunt...

解决方案回顾

构建工具 + 导入预处理器

如果您想要的是在浏览器中运行单个 JS 文件,我建议使用诸如 Grunt (或 Gulp,或 蛋糕,或含羞草,或 任何其他)来预处理您的 Coffeescript,以及一个包含/需要/导入模块,该模块将连接包含的内容文件到您编译的输出中,例如以下之一:

  • Browserify:可能是不断上升的标准,也是我个人最喜欢的,让您可以使用Node 的 exports/require API 在您的代码中,然后提取所需的所有内容并将其连接到浏览器可包含的文件中。存在于 GruntGulp含羞草 可能还有大多数其他。直到今天,我认为如果您追求 Node 和浏览器的兼容性(甚至其他),它可能是最好的解决方案
  • 一些类似 Rails Sprocket 的解决方案,例如 grunt-sprockets-directivesgulp-include 也将以与 CSS 预处理器一致的方式工作(尽管它们通常有自己的导入机制)
  • 其他解决方案包括 grunt-includesgrunt-import

独立导入预处理器

如果您想避免构建工具的额外复杂性,可以使用 Browserify 独立,或不基于 Node 的 require 的替代方案,如 coffeescript-concatCoffee-Stir

[不推荐] 异步动态加载(AJAX + eval)

如果您专门为浏览器编写并且不介意或者确实希望您的脚本分布在通过 AJAX 获取的多个文件中,那么您可以使用多种工具例如:

  • yepnope.jsModernizr 基于 yepnope 的 .load:请注意,yepnope 现在已被其维护者弃用,他们建议使用构建工具和串联而不是远程加载
  • RequireJS
  • HeadJS
  • jQuery$.getScript
  • Vanilla AJAX + 评估
  • 您自己的 <一个href="https://github.com/amdjs/amdjs-api" rel="nofollow">AMD

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:

  • Browserify: probably the rising standard and my personal favourite, lets you to use Node's 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)
  • Some Rails Sprocket-like solutions like grunt-sprockets-directives or gulp-include will also work in a consistent way with CSS pre-processors (though those generally have their own importing mechanisms)
  • Other solutions include grunt-includes or grunt-import

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:

  • yepnope.js or Modernizr's .load based on yepnope: Please note that yepnope is now deprecated by its maintainer, who recommend using build tools and concatenation instead of remote loading
  • RequireJS
  • HeadJS
  • jQuery's $.getScript
  • Vanilla AJAX + eval
  • your own implementation of AMD
惯饮孤独 2024-12-16 00:41:16

你可以尝试我为解决同样的问题而制作的库coffee-stir
这很简单。
只需键入 #include 和您要包含的文件的名称

#include MyBaseClass.coffee

即可了解详细信息
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

#include MyBaseClass.coffee

For details
http://beastjavascript.github.io/Coffee-Stir/

゛清羽墨安 2024-12-16 00:41:16

我发现在处理它们之前使用“gulp-concat”合并我的咖啡脚本可以达到目的。可以使用 npm 轻松将其安装到您的项目中。

npm install gulp-concat

然后编辑你的 gulpfile.js:

var gulp = require('gulp')
,coffee = require('gulp-coffee')
,concat = require('gulp-concat');

gulp.task('coffee', function(){
  gulp.src('src/*.coffee')
    .pipe(concat('app.coffee')
    .pipe(coffee({bare: true}).on('error', gulp.log))
    .pipe(gulp.dest('build/')
})

这是我在 gulp 将其处理为最终构建 Javascript 之前用来连接所有咖啡脚本的代码。唯一的问题是文件是按字母顺序处理的。您可以明确指定要处理哪个文件来实现您自己的文件顺序,但您会失去添加动态 .coffee 文件的灵活性。

gulp.src(['src/file3.coffee', 'src/file1.coffee', 'src/file2.coffee'])
  .pipe(concat('app.coffee'))
  .pipe(coffee({bare: true}).on('error', gulp.log))
  .pipe(gulp.dest('build/')

截至 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:

var gulp = require('gulp')
,coffee = require('gulp-coffee')
,concat = require('gulp-concat');

gulp.task('coffee', function(){
  gulp.src('src/*.coffee')
    .pipe(concat('app.coffee')
    .pipe(coffee({bare: true}).on('error', gulp.log))
    .pipe(gulp.dest('build/')
})

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.src(['src/file3.coffee', 'src/file1.coffee', 'src/file2.coffee'])
  .pipe(concat('app.coffee'))
  .pipe(coffee({bare: true}).on('error', gulp.log))
  .pipe(gulp.dest('build/')

gulp-concat as of February 25th, 2015 is available at this url.

复古式 2024-12-16 00:41:16

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文