CoffeeScript——子目录中的编译顺序

CoffeeScript——子目录中的编译顺序,coffeescript,Coffeescript,有没有办法定义子目录中的CoffeeScript编译顺序? 请考虑以下例子: 档案: src/App.coffee src/view/B.coffee src/view/a/a.coffee 其中A类扩展B类 coffee --join js/app.js --compile src/view/ src/App.coffee 这会在浏览器中引发错误: Uncaught TypeError: Cannot read property 'prototype' of undefined 如果

有没有办法定义子目录中的CoffeeScript编译顺序? 请考虑以下例子:

档案:

  • src/App.coffee
  • src/view/B.coffee
  • src/view/a/a.coffee
其中A类扩展B类

coffee --join js/app.js --compile src/view/ src/App.coffee
这会在浏览器中引发错误:

Uncaught TypeError: Cannot read property 'prototype' of undefined 
如果我将文件夹a重命名为z,则错误消失,一切正常

  • src/view/z/A
我希望编译器在进入src/view/sub目录之前读取src/view/中的所有.coffee文件。再说一遍,有没有办法做到这一点

编辑: PC Windows 7,
CoffeeScript 1.3.3版

我认为唯一的解决方案是在构建脚本中手动创建编译顺序。 您将创建一个带有文件名的有序集合,其中循环迭代并连接一个新的大字符串,该字符串可以编译为一个文件

创建包含以下内容的Cakefile,首先检查语法。然后运行
蛋糕构建
。那应该行的,蛋糕是咖啡脚本的

fs = require 'fs'
{exec} = require 'child_process'

viewsDir = "src/view"
coffeeFiles = [
  'B'
  'A'
]

task 'build'
  # loops through coffeeFiles. 
  for file, index in coffeeFiles then do (file, index) ->
    fs.readFile "#{viewsDir}/#{file}", 'utf8', (err, content) ->
      appCoffee[index] = content
      compile() if --remaining is 0

  compile = ->
    fs.writeFile 'js/app.coffee', appCoffee.join('\n\n'), 'utf8', (err)   ->
      throw err if err
      exec 'coffee --compile js/app.coffee', (err, stdout, stderr) ->
        throw err if err
          console.log stdout + stderr

          # you can skip deleting the app.coffee file
          fs.unlink 'js/app.coffee', (err) ->
            throw err if err
            console.log 'Created app.coffe, compiled to app.js and removes app.coffee'

            # maybe additional taks 
            # invoke 'test'
也记录在Coffeescript的Wiki中


在第一次循环之前,您还可以让它在不同的目录中循环。只需在未列出的其他文件之前列出要处理的咖啡文件中的文件名,其余文件可以使用fs.readDir()添加到列表中。

我们创建了一个简单的模块来解决类似的问题:

只要把
#你需要的[filename].coffee
放在你的文件上,你就完成了


我们在具有复杂依赖关系图的产品中使用它。

这是标准*nix行为的结果,其中所有内容都按字母顺序列出,包括文件夹。尝试
——编译src/view/B.coffee src/view/a/src/App.coffee
?仅当我添加所有src/view/[.coffee]文件,然后添加所有src/view/[folders]时才有效。不适用于泛型src/view/
——编译src/view/*。coffee src/view/a/src/App。在本例中,为meIt工作的coffee
确实有效。如果src/view中有更多文件夹怎么办?然后,只有当编译行包含所有这些文件夹时,它才会工作。我宁愿避免那样。嗯,这对你来说是无法猜测的。。您需要按字母顺序命名、传递列表参数或使用构建脚本。