Skip to content

Commit

Permalink
merge branch 'mini', add standalone support
Browse files Browse the repository at this point in the history
  • Loading branch information
army8735 committed Jul 2, 2014
1 parent 7919096 commit 6b9a87b
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 3 deletions.
78 changes: 78 additions & 0 deletions dist/standalone-debug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var define
var require
(function(global, undefined) {

function isType(type) {
return function(obj) {
return {}.toString.call(obj) == "[object " + type + "]"
}
}

var isFunction = isType("Function")

var cachedMods = {}

function Module() {
}

Module.prototype.exec = function () {
var mod = this

if (this.execed) {
return mod.exports
}
this.execed = true

function require(id) {
return Module.get(id).exec()
}

var factory = mod.factory

var exports = isFunction(factory) ?
factory(require, mod.exports = {}, mod) :
factory

if (exports === undefined) {
exports = mod.exports
}

// Reduce memory leak
delete mod.factory

mod.exports = exports

return exports
}

define = function (id, deps, factory) {
var meta = {
id: id,
deps: deps,
factory: factory
}

Module.save(meta)
}

Module.save = function(meta) {
var mod = Module.get(meta.id)

mod.id = meta.id
mod.dependencies = meta.deps
mod.factory = meta.factory
}

Module.get = function(id) {
return cachedMods[id] || (cachedMods[id] = new Module())
}

require = function(id) {
var mod = Module.get(id)
if(!mod.execed) {
mod.exec()
}
return mod.exports
}

})(this)
2 changes: 2 additions & 0 deletions dist/standalone.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"dependencies": {},
"devDependencies": {
"seatools": "0.5.8"
"seatools": "0.5.10"
},
"repository": {
"type": "git",
Expand Down
78 changes: 78 additions & 0 deletions src/standalone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
var define
var require
(function(global, undefined) {

function isType(type) {
return function(obj) {
return {}.toString.call(obj) == "[object " + type + "]"
}
}

var isFunction = isType("Function")

var cachedMods = {}

function Module() {
}

Module.prototype.exec = function () {
var mod = this

if (this.execed) {
return mod.exports
}
this.execed = true

function require(id) {
return Module.get(id).exec()
}

var factory = mod.factory

var exports = isFunction(factory) ?
factory(require, mod.exports = {}, mod) :
factory

if (exports === undefined) {
exports = mod.exports
}

// Reduce memory leak
delete mod.factory

mod.exports = exports

return exports
}

define = function (id, deps, factory) {
var meta = {
id: id,
deps: deps,
factory: factory
}

Module.save(meta)
}

Module.save = function(meta) {
var mod = Module.get(meta.id)

mod.id = meta.id
mod.dependencies = meta.deps
mod.factory = meta.factory
}

Module.get = function(id) {
return cachedMods[id] || (cachedMods[id] = new Module())
}

require = function(id) {
var mod = Module.get(id)
if(!mod.execed) {
mod.exec()
}
return mod.exports
}

})(this)
4 changes: 3 additions & 1 deletion tests/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ var testSuites = [
'specs/misc/utf8-in-gbk',
'specs/misc/x-ua-compatible',

'research/derive-uri'
'research/derive-uri',

'specs/standalone'
]

// For seajs in node
Expand Down
50 changes: 50 additions & 0 deletions tests/specs/standalone/test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=0">
<title>test</title>
<script src="../../../dist/sea.js"></script>
<script src="../../../dist/standalone.js"></script>
<script src="../../test-standalone.js"></script>
</head>
<body>
<div id="out"></div>
<script>
var seajs = {}
seajs.off = function(){}
seajs.config = function(){}

define('a', [], function() { return 'a' })
define('b', ['c'], function(require) { var c = require('c'); return c })
define('c', [], {})

var test = require('test')

var a = require('a')
test.assert(a === 'a', 'a is a')

var b = require('b')
test.assert(JSON.stringify(b) == '{}', 'b is {}')

var c = require('c')
test.assert(JSON.stringify(c) == '{}', 'c is {}')

test.assert(b === c, 'b is return c')

define('e', [], function(require, exports, module) {
module.exports = 'e'
})

var e = require('e')
test.assert(e === 'e', 'e is e')

define('f', [], function(require) {
test.assert(true, 'f is test.next() call')
test.next()
})

require('f')
</script>
</body>
</html>
2 changes: 1 addition & 1 deletion tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ if (typeof document !== 'undefined') {
(function(factory) {

if (typeof define === 'function') {
define(factory)
define('test', factory)
}
else if (typeof require === 'function') {
factory(require, exports)
Expand Down

0 comments on commit 6b9a87b

Please sign in to comment.