• 跳转到 … +
    browser.coffee cake.coffee coffeescript.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
  • index.coffee

  • §

    Node.js 实现

    CoffeeScript  = require './coffeescript'
    fs            = require 'fs'
    vm            = require 'vm'
    path          = require 'path'
    
    helpers       = CoffeeScript.helpers
    
    CoffeeScript.transpile = (js, options) ->
      try
        babel = require '@babel/core'
      catch
        try
          babel = require 'babel-core'
        catch
  • §

    此错误仅针对 Node,因为 CLI 用户如果没有安装 Babel,则会在更早的时候看到不同的错误。

          throw new Error 'To use the transpile option, you must have the \'@babel/core\' module installed'
      babel.transform js, options
  • §

    CLI、Node 和浏览器 API 共享的 compile 方法。

    universalCompile = CoffeeScript.compile
  • §

    Node API 特有的 compile 方法。

    CoffeeScript.compile = (code, options) ->
  • §

    将对 Babel 的引用传递到编译器中,以便在 Node API 中可以使用 transpile 选项。我们需要这样做,以便像 Webpack 这样的工具可以 require('coffeescript') 并正确构建,而不会尝试 require Babel。

      if options?.transpile
        options.transpile.transpile = CoffeeScript.transpile
      universalCompile.call CoffeeScript, code, options
  • §

    编译并执行 CoffeeScript 字符串(在服务器上),正确设置 __filename、__dirname 和相对 require()。

    CoffeeScript.run = (code, options = {}) ->
      mainModule = require.main
  • §

    设置文件名。

      mainModule.filename = process.argv[1] =
        if options.filename then fs.realpathSync(options.filename) else helpers.anonymousFileName()
  • §

    清除模块缓存。

      mainModule.moduleCache and= {}
  • §

    为 node_modules 加载分配路径

      dir = if options.filename?
        path.dirname fs.realpathSync options.filename
      else
        fs.realpathSync '.'
      mainModule.paths = require('module')._nodeModulePaths dir
  • §

    保存用于编译子导入的选项。

      mainModule.options = options
    
      options.filename = mainModule.filename
      options.inlineMap = true
  • §

    编译。

      answer = CoffeeScript.compile code, options
      code = answer.js ? answer
    
      mainModule._compile code, mainModule.filename
  • §

    编译并评估 CoffeeScript 字符串(在类似 Node.js 的环境中)。CoffeeScript REPL 使用它来运行输入。

    CoffeeScript.eval = (code, options = {}) ->
      return unless code = code.trim()
      createContext = vm.Script.createContext ? vm.createContext
    
      isContext = vm.isContext ? (ctx) ->
        options.sandbox instanceof createContext().constructor
    
      if createContext
        if options.sandbox?
          if isContext options.sandbox
            sandbox = options.sandbox
          else
            sandbox = createContext()
            sandbox[k] = v for own k, v of options.sandbox
          sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox
        else
          sandbox = global
        sandbox.__filename = options.filename || 'eval'
        sandbox.__dirname  = path.dirname sandbox.__filename
  • §

    仅在他们选择不指定自己的模块/require 时才定义它们

        unless sandbox isnt global or sandbox.module or sandbox.require
          Module = require 'module'
          sandbox.module  = _module  = new Module(options.modulename || 'eval')
          sandbox.require = _require = (path) ->  Module._load path, _module, true
          _module.filename = sandbox.__filename
          for r in Object.getOwnPropertyNames require when r not in ['paths', 'arguments', 'caller']
            _require[r] = require[r]
  • §

    使用 node 目前用于他们自己的 REPL 的相同 hack

          _require.paths = _module.paths = Module._nodeModulePaths process.cwd()
          _require.resolve = (request) -> Module._resolveFilename request, _module
      o = {}
      o[k] = v for own k, v of options
      o.bare = on # ensure return value
      js = CoffeeScript.compile code, o
      if sandbox is global
        vm.runInThisContext js
      else
        vm.runInContext js, sandbox
    
    CoffeeScript.register = -> require './register'
  • §

    在依赖于隐式 require.extensions 注册时,抛出带有弃用警告的错误

    if require.extensions
      for ext in CoffeeScript.FILE_EXTENSIONS then do (ext) ->
        require.extensions[ext] ?= ->
          throw new Error """
          Use CoffeeScript.register() or require the coffeescript/register module to require #{ext} files.
          """
    
    CoffeeScript._compileRawFileContent = (raw, filename, options = {}) ->
  • §

    如果此文件以 Unicode 字节顺序标记开头,则将其剥离。

      stripped = if raw.charCodeAt(0) is 0xFEFF then raw.substring 1 else raw
    
      options = Object.assign {}, options,
        filename: filename
        literate: helpers.isLiterate filename
        sourceFiles: [filename]
    
      try
        answer = CoffeeScript.compile stripped, options
      catch err
  • §

    由于动态加载文件的 filename 和 code 将与使用 CoffeeScript.run 编译的原始文件不同,因此将该信息添加到错误中,以便稍后可以将其美化打印。

        throw helpers.updateSyntaxError err, stripped, filename
    
      answer
    
    CoffeeScript._compileFile = (filename, options = {}) ->
      raw = fs.readFileSync filename, 'utf8'
    
      CoffeeScript._compileRawFileContent raw, filename, options
    
    module.exports = CoffeeScript
  • §

    显式定义所有命名导出,以便 Node 自动检测 CommonJS 包中的命名导出可以找到所有导出。这使使用包的代码可以编写类似 import { compile } from 'coffeescript' 的代码。不要将其简化为循环或类似操作;module.exports.name 部分对于 Node 的算法成功检测名称至关重要。

    module.exports.VERSION = CoffeeScript.VERSION
    module.exports.FILE_EXTENSIONS = CoffeeScript.FILE_EXTENSIONS
    module.exports.helpers = CoffeeScript.helpers
    module.exports.registerCompiled = CoffeeScript.registerCompiled
    module.exports.compile = CoffeeScript.compile
    module.exports.tokens = CoffeeScript.tokens
    module.exports.nodes = CoffeeScript.nodes
    module.exports.register = CoffeeScript.register
    module.exports.eval = CoffeeScript.eval
    module.exports.run = CoffeeScript.run
    module.exports.transpile = CoffeeScript.transpile
    module.exports.patchStackTrace = CoffeeScript.patchStackTrace
    module.exports._compileRawFileContent = CoffeeScript._compileRawFileContent
    module.exports._compileFile = CoffeeScript._compileFile