• 跳转到 … +
    browser.coffee cake.coffee coffee-script.coffee command.coffee grammar.coffee helpers.coffee index.coffee lexer.coffee nodes.coffee optparse.coffee register.coffee repl.coffee rewriter.coffee scope.litcoffee sourcemap.litcoffee
  • register.coffee

  • ¶
    CoffeeScript  = require './coffee-script'
    child_process = require 'child_process'
    helpers       = require './helpers'
    path          = require 'path'
  • ¶

    为 Node 加载并运行 CoffeeScript 文件,去除任何 BOM。

    loadFile = (module, filename) ->
      answer = CoffeeScript._compileFile filename, no, yes
      module._compile answer, filename
  • ¶

    如果安装的 Node 版本支持 require.extensions,则将 CoffeeScript 注册为扩展。

    if require.extensions
      for ext in CoffeeScript.FILE_EXTENSIONS
        require.extensions[ext] = loadFile
  • ¶

    修补 Node 的模块加载器,使其能够处理多点扩展名。这是一种糟糕的做法,不应该被要求。

      Module = require 'module'
    
      findExtension = (filename) ->
        extensions = path.basename(filename).split '.'
  • ¶

    从点文件中删除初始点。

        extensions.shift() if extensions[0] is ''
  • ¶

    从最长的扩展名开始,逐步缩短。

        while extensions.shift()
          curExtension = '.' + extensions.join '.'
          return curExtension if Module._extensions[curExtension]
        '.js'
    
      Module::load = (filename) ->
        @filename = filename
        @paths = Module._nodeModulePaths path.dirname filename
        extension = findExtension filename
        Module._extensions[extension](this, filename)
        @loaded = true
  • ¶

    如果我们使用的是 Node,则修补 child_process.fork,以便 Coffee 脚本能够直接派生 CoffeeScript 文件和 JavaScript 文件。

    if child_process
      {fork} = child_process
      binary = require.resolve '../../bin/coffee'
      child_process.fork = (path, args, options) ->
        if helpers.isCoffee path
          unless Array.isArray args
            options = args or {}
            args = []
          args = [path].concat args
          path = binary
        fork path, args, options