CoffeeScript - 子目录中的编译顺序

imb*_*izi 5 coffeescript

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

文件:

  • SRC/App.coffee
  • SRC /视图/ B.coffee
  • SRC /视图/一个/ A.coffee

A类延伸B的地方

coffee --join js/app.js --compile src/view/ src/App.coffee
Run Code Online (Sandbox Code Playgroud)

这会在浏览器中引发错误:

Uncaught TypeError: Cannot read property 'prototype' of undefined 
Run Code Online (Sandbox Code Playgroud)

如果我将文件夹a重命名为z,则错误消失,一切正常.

  • src/view/z /A.coffee

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

编辑: PC Windows 7,CoffeeScript版本1.3.3

vik*_*vik 5

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

使用以下内容创建Cakefile,首先检查语法.并运行cake build.这应该工作,蛋糕自带CoffeeScript.

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'
Run Code Online (Sandbox Code Playgroud)

也记录在Coffeescript的Wiki中https://github.com/jashkenas/coffee-script/wiki/[HowTo]-Compiling-and-Setting-Up-Build-Tools

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