PageRenderTime 30ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/index.coffee

http://github.com/jashkenas/coffee-script
CoffeeScript | 139 lines | 91 code | 23 blank | 25 comment | 18 complexity | 9b640a8d51af8503efd72eb551554b5b MD5 | raw file
  1. # Node.js Implementation
  2. CoffeeScript = require './coffeescript'
  3. fs = require 'fs'
  4. vm = require 'vm'
  5. path = require 'path'
  6. helpers = CoffeeScript.helpers
  7. CoffeeScript.transpile = (js, options) ->
  8. try
  9. babel = require '@babel/core'
  10. catch
  11. try
  12. babel = require 'babel-core'
  13. catch
  14. # This error is only for Node, as CLI users will see a different error
  15. # earlier if they dont have Babel installed.
  16. throw new Error 'To use the transpile option, you must have the \'@babel/core\' module installed'
  17. babel.transform js, options
  18. # The `compile` method shared by the CLI, Node and browser APIs.
  19. universalCompile = CoffeeScript.compile
  20. # The `compile` method particular to the Node API.
  21. CoffeeScript.compile = (code, options) ->
  22. # Pass a reference to Babel into the compiler, so that the transpile option
  23. # is available in the Node API. We need to do this so that tools like Webpack
  24. # can `require('coffeescript')` and build correctly, without trying to
  25. # require Babel.
  26. if options?.transpile
  27. options.transpile.transpile = CoffeeScript.transpile
  28. universalCompile.call CoffeeScript, code, options
  29. # Compile and execute a string of CoffeeScript (on the server), correctly
  30. # setting `__filename`, `__dirname`, and relative `require()`.
  31. CoffeeScript.run = (code, options = {}) ->
  32. mainModule = require.main
  33. # Set the filename.
  34. mainModule.filename = process.argv[1] =
  35. if options.filename then fs.realpathSync(options.filename) else '<anonymous>'
  36. # Clear the module cache.
  37. mainModule.moduleCache and= {}
  38. # Assign paths for node_modules loading
  39. dir = if options.filename?
  40. path.dirname fs.realpathSync options.filename
  41. else
  42. fs.realpathSync '.'
  43. mainModule.paths = require('module')._nodeModulePaths dir
  44. # Save the options for compiling child imports.
  45. mainModule.options = options
  46. # Compile.
  47. if not helpers.isCoffee(mainModule.filename) or require.extensions
  48. answer = CoffeeScript.compile code, options
  49. code = answer.js ? answer
  50. mainModule._compile code, mainModule.filename
  51. # Compile and evaluate a string of CoffeeScript (in a Node.js-like environment).
  52. # The CoffeeScript REPL uses this to run the input.
  53. CoffeeScript.eval = (code, options = {}) ->
  54. return unless code = code.trim()
  55. createContext = vm.Script.createContext ? vm.createContext
  56. isContext = vm.isContext ? (ctx) ->
  57. options.sandbox instanceof createContext().constructor
  58. if createContext
  59. if options.sandbox?
  60. if isContext options.sandbox
  61. sandbox = options.sandbox
  62. else
  63. sandbox = createContext()
  64. sandbox[k] = v for own k, v of options.sandbox
  65. sandbox.global = sandbox.root = sandbox.GLOBAL = sandbox
  66. else
  67. sandbox = global
  68. sandbox.__filename = options.filename || 'eval'
  69. sandbox.__dirname = path.dirname sandbox.__filename
  70. # define module/require only if they chose not to specify their own
  71. unless sandbox isnt global or sandbox.module or sandbox.require
  72. Module = require 'module'
  73. sandbox.module = _module = new Module(options.modulename || 'eval')
  74. sandbox.require = _require = (path) -> Module._load path, _module, true
  75. _module.filename = sandbox.__filename
  76. for r in Object.getOwnPropertyNames require when r not in ['paths', 'arguments', 'caller']
  77. _require[r] = require[r]
  78. # use the same hack node currently uses for their own REPL
  79. _require.paths = _module.paths = Module._nodeModulePaths process.cwd()
  80. _require.resolve = (request) -> Module._resolveFilename request, _module
  81. o = {}
  82. o[k] = v for own k, v of options
  83. o.bare = on # ensure return value
  84. js = CoffeeScript.compile code, o
  85. if sandbox is global
  86. vm.runInThisContext js
  87. else
  88. vm.runInContext js, sandbox
  89. CoffeeScript.register = -> require './register'
  90. # Throw error with deprecation warning when depending upon implicit `require.extensions` registration
  91. if require.extensions
  92. for ext in CoffeeScript.FILE_EXTENSIONS then do (ext) ->
  93. require.extensions[ext] ?= ->
  94. throw new Error """
  95. Use CoffeeScript.register() or require the coffeescript/register module to require #{ext} files.
  96. """
  97. CoffeeScript._compileRawFileContent = (raw, filename, options = {}) ->
  98. # Strip the Unicode byte order mark, if this file begins with one.
  99. stripped = if raw.charCodeAt(0) is 0xFEFF then raw.substring 1 else raw
  100. options = Object.assign {}, options,
  101. filename: filename
  102. literate: helpers.isLiterate filename
  103. sourceFiles: [filename]
  104. inlineMap: yes # Always generate a source map, so that stack traces line up.
  105. try
  106. answer = CoffeeScript.compile stripped, options
  107. catch err
  108. # As the filename and code of a dynamically loaded file will be different
  109. # from the original file compiled with CoffeeScript.run, add that
  110. # information to error so it can be pretty-printed later.
  111. throw helpers.updateSyntaxError err, stripped, filename
  112. answer
  113. CoffeeScript._compileFile = (filename, options = {}) ->
  114. raw = fs.readFileSync filename, 'utf8'
  115. CoffeeScript._compileRawFileContent raw, filename, options
  116. module.exports = CoffeeScript