• 跳至... +
    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
  • browser.coffee

  • §

    此瀏覽器相容性層延伸核心 CoffeeScript 函數,讓在瀏覽器中直接編譯程式碼時能順利運作。我們新增了透過XHR載入遠端 Coffee 程式碼和text/coffeescript 程式碼標籤、透過資料 URL 載入原始碼對應表等支援。

    CoffeeScript = require './coffeescript'
    { compile } = CoffeeScript
  • §

    使用 window.eval 來評估程式碼,而不是只使用 eval,以在乾淨的全球範圍內執行程式碼,而不是繼承 CoffeeScript 編譯器的範圍。(因此 cake test:browser 也能在 Node 中運作,請適當地使用 window.eval 或 global.eval)。

    CoffeeScript.eval = (code, options = {}) ->
      options.bare ?= on
      globalRoot = if window? then window else global
      globalRoot['eval'] compile code, options
  • §

    執行程式碼不會提供對此範圍的存取權。

    CoffeeScript.run = (code, options = {}) ->
      options.bare      = on
      options.shiftLine = on
      Function(compile code, options)()
  • §

    匯出比 index.coffee 匯出的 CoffeeScript 更受限,後者是針對 Node 環境設計的。

    module.exports = CoffeeScript
  • §

    如果我們不在瀏覽器環境中,我們已完成公開 API。

    return unless window?
  • §

    在可能的情況下包含原始碼對應表。如果我們有 base64 編碼器、JSON 序列化器和用於跳脫 unicode 字元的工具,我們就可以開始了。移植自 https://mdn.club.tw/en-US/docs/DOM/window.btoa

    if btoa? and JSON?
      compile = (code, options = {}) ->
        options.inlineMap = true
        CoffeeScript.compile code, options
  • §

    透過 XHR 從目前網域載入遠端程式碼。

    CoffeeScript.load = (url, callback, options = {}, hold = false) ->
      options.sourceFiles = [url]
      xhr = if window.ActiveXObject
        new window.ActiveXObject('Microsoft.XMLHTTP')
      else
        new window.XMLHttpRequest()
      xhr.open 'GET', url, true
      xhr.overrideMimeType 'text/plain' if 'overrideMimeType' of xhr
      xhr.onreadystatechange = ->
        if xhr.readyState is 4
          if xhr.status in [0, 200]
            param = [xhr.responseText, options]
            CoffeeScript.run param... unless hold
          else
            throw new Error "Could not load #{url}"
          callback param if callback
      xhr.send null
  • §

    讓 CoffeeScript 編譯並評估所有內容類型為 text/coffeescript 的腳本標籤,以在瀏覽器中啟用 CoffeeScript。這會在載入頁面時發生。

    CoffeeScript.runScripts = ->
      scripts = window.document.getElementsByTagName 'script'
      coffeetypes = ['text/coffeescript', 'text/literate-coffeescript']
      coffees = (s for s in scripts when s.type in coffeetypes)
      index = 0
    
      execute = ->
        param = coffees[index]
        if param instanceof Array
          CoffeeScript.run param...
          index++
          execute()
    
      for script, i in coffees
        do (script, i) ->
          options = literate: script.type is coffeetypes[1]
          source = script.src or script.getAttribute('data-src')
          if source
            options.filename = source
            CoffeeScript.load source,
              (param) ->
                coffees[i] = param
                execute()
              options
              true
          else
  • §

    options.filename 定義原始碼對應表在開發人員工具中顯示的檔名。如果腳本標籤有 id,請將其用作檔名;否則請使用 coffeescript 或 coffeescript1 等,讓第一個保持不編號,以符合只有單一 CoffeeScript 腳本區塊可進行剖析的常見情況。

            options.filename = if script.id and script.id isnt '' then script.id else "coffeescript#{if i isnt 0 then i else ''}"
            options.sourceFiles = ['embedded']
            coffees[i] = [script.innerHTML, options]
    
      execute()
  • §

    偵聽視窗載入,無論是在正常的瀏覽器還是 IE 中。僅在非 ES 模組版本的瀏覽器編譯器啟動時附加此事件處理常式,以保持向後相容性,同時讓 ES 模組版本可匯入且沒有副作用。

    if this is window
      if window.addEventListener
        window.addEventListener 'DOMContentLoaded', CoffeeScript.runScripts, no
      else
        window.attachEvent 'onload', CoffeeScript.runScripts