【问题标题】:CoffeeScript --compile order in sub-directoriesCoffeeScript——子目录中的编译顺序
【发布时间】:2012-07-08 20:48:16
【问题描述】:

有没有办法在子目录中定义 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.coffee

我希望编译器在进入 src/view/ 子目录之前从 src/view/ 读取所有 .coffee 文件。再说一次,有什么办法吗?

编辑: 电脑视窗 7, CoffeeScript 1.3.3 版

【问题讨论】:

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

标签: coffeescript


【解决方案1】:

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

创建一个包含以下内容的 Cakefile,首先检查语法。并使用cake build 运行。应该可以,CoffeeScript 自带 cake。

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 https://github.com/jashkenas/coffee-script/wiki/[HowTo]-Compiling-and-Setting-Up-Build-Tools

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

【讨论】:

    【解决方案2】:

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

    https://github.com/Vizir/rehab

    只需将#_require [filename].coffee 放在您的文件中即可。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-07
      • 1970-01-01
      • 2014-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多