mirror of
https://github.com/atom/atom.git
synced 2026-02-08 05:35:04 -05:00
cleaner, way more awesome, 100x better require()
This commit is contained in:
@@ -1,51 +1,16 @@
|
||||
# This file is the first thing loaded on startup.
|
||||
|
||||
console.log = (thing) -> OSX.NSLog thing.toString()
|
||||
console.log = (thing) -> OSX.NSLog thing.toString() if thing?
|
||||
|
||||
modules = {}
|
||||
paths = ['src', 'vendor']
|
||||
read = (path) ->
|
||||
OSX.NSString.stringWithContentsOfFile path
|
||||
this.require = (file) ->
|
||||
# hack for stupid requirejs
|
||||
if file.indexOf('ace/requirejs/text!') > -1
|
||||
file = file.replace 'ace/requirejs/text!', ''
|
||||
text = true
|
||||
# load require() function
|
||||
root = OSX.NSBundle.mainBundle.resourcePath
|
||||
code = OSX.NSString.stringWithContentsOfFile "#{root}/src/require.js"
|
||||
eval "(function(){ #{code} })(this);"
|
||||
|
||||
return modules[file] if modules[file]
|
||||
|
||||
code = null
|
||||
if file[0] is '/'
|
||||
code = read("#{file}.js") or read("#{file}/index.js")
|
||||
else
|
||||
root = OSX.NSBundle.mainBundle.resourcePath
|
||||
paths.forEach (path) ->
|
||||
return code if code
|
||||
if text
|
||||
code = read "#{root}/#{path}/#{file}"
|
||||
else
|
||||
code = read("#{root}/#{path}/#{file}.js") or
|
||||
read("#{root}/#{path}/#{file}/index.js")
|
||||
|
||||
if text
|
||||
modules[file] = code.toString()
|
||||
return modules[file]
|
||||
|
||||
exports = {}
|
||||
module = exports: exports
|
||||
|
||||
src = "function define(cb){cb.call(this,require,exports)};"
|
||||
src += """(function(exports, define, module){
|
||||
#{code}
|
||||
}).call(exports, exports, define, module);
|
||||
"""
|
||||
eval src
|
||||
|
||||
modules[file] = module.exports or exports
|
||||
modules[file]
|
||||
|
||||
this.require.paths = paths
|
||||
|
||||
this.require.nameToUrl = (path) -> "#{path}.js"
|
||||
console.log 'require tests:'
|
||||
console.log require.resolve 'underscore'
|
||||
console.log require.resolve 'osx'
|
||||
console.log require.resolve 'ace/requirejs/text!ace/css/editor.css'
|
||||
console.log require.resolve 'ace/keyboard/keybinding'
|
||||
|
||||
this._ = require 'underscore'
|
||||
|
||||
79
src/require.coffee
Normal file
79
src/require.coffee
Normal file
@@ -0,0 +1,79 @@
|
||||
resourcePath = OSX.NSBundle.mainBundle.resourcePath
|
||||
|
||||
paths = [
|
||||
"#{resourcePath}/src",
|
||||
"#{resourcePath}/plugins",
|
||||
"#{resourcePath}/vendor"
|
||||
]
|
||||
|
||||
require = (file) ->
|
||||
return __modules[file] if __modules[file]?
|
||||
|
||||
file = resolve file
|
||||
parts = file.split '.'
|
||||
ext = parts[parts.length-1]
|
||||
|
||||
__modules[file] = exts[ext]? file
|
||||
__modules[file]
|
||||
|
||||
exts =
|
||||
css: (file) -> __read file
|
||||
js: (file) ->
|
||||
code = __read file
|
||||
exports = {}
|
||||
module = exports: exports
|
||||
|
||||
src = "function define(cb){cb.call(this,require,exports)};"
|
||||
src += """(function(exports, define, module){
|
||||
#{code}
|
||||
/*close open comments*/
|
||||
}).call(exports, exports, define, module);
|
||||
"""
|
||||
eval src
|
||||
|
||||
module.exports or exports
|
||||
|
||||
resolve = (file) ->
|
||||
if /!/.test file
|
||||
parts = file.split '!'
|
||||
file = parts[parts.length-1]
|
||||
|
||||
if file[0..1] is './'
|
||||
throw "require: ./ prefix not yet implemented"
|
||||
|
||||
if file[0..2] is '../'
|
||||
throw "require: ../ prefix not yet implemented"
|
||||
|
||||
if file[0] isnt '/'
|
||||
paths.forEach (path) ->
|
||||
if /\.(.+)$/.test(file) and __exists "#{path}/#{file}"
|
||||
file = "#{path}/#{file}"
|
||||
else
|
||||
for ext, handler of exts
|
||||
if __exists "#{path}/#{file}.#{ext}"
|
||||
file = "#{path}/#{file}.#{ext}"
|
||||
else if __exists "#{path}/#{file}/index.#{ext}"
|
||||
file = "#{path}/#{file}/index.#{ext}"
|
||||
|
||||
if file[0] isnt '/'
|
||||
throw "require: Can't find '#{file}'"
|
||||
|
||||
return file
|
||||
|
||||
|
||||
__exists = (path) ->
|
||||
OSX.NSFileManager.defaultManager.fileExistsAtPath path
|
||||
|
||||
__read = (path) ->
|
||||
OSX.NSString.stringWithContentsOfFile path
|
||||
|
||||
__modules = {}
|
||||
|
||||
|
||||
this.require = require
|
||||
|
||||
this.require.paths = paths
|
||||
this.require.exts = exts
|
||||
|
||||
this.require.resolve = resolve
|
||||
this.require.nameToUrl = (path) -> "#{path}.js"
|
||||
Reference in New Issue
Block a user