• 跳轉至… +
    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 中可以使用轉譯選項。我們需要這麼做,才能讓 Webpack 等工具可以正確地 require('coffeescript') 和建置,而不會嘗試需要 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
  • §

    僅在使用者選擇不指定自己的情況下定義模組/需要

        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 中使用的相同技巧

          _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
  • §

    由於動態載入檔案的檔名和程式碼會與使用 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