nibjs 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +11 -0
- data/Gemfile.lock +1 -1
- data/Manifest.txt +2 -1
- data/README.md +10 -0
- data/Rakefile +1 -1
- data/dist/nibjs-1.0.0.js +74 -0
- data/dist/nibjs-1.0.0.min.js +7 -0
- data/dist/nibjs-1.1.0.js +74 -0
- data/dist/nibjs-1.1.0.min.js +7 -0
- data/examples/1-basic/basic.js +27 -0
- data/examples/1-basic/build +7 -0
- data/examples/1-basic/illustrate.png +0 -0
- data/examples/1-basic/index.html +56 -0
- data/examples/1-basic/lib/App.js +9 -0
- data/examples/1-basic/lib/StringUtils.js +7 -0
- data/examples/1-basic/lib/index.js +2 -0
- data/examples/2-coffee/build +7 -0
- data/examples/2-coffee/coffee.js +24 -0
- data/examples/2-coffee/illustrate.png +0 -0
- data/examples/2-coffee/index.html +57 -0
- data/examples/2-coffee/lib/App.coffee +9 -0
- data/examples/2-coffee/lib/StringUtils.coffee +6 -0
- data/examples/2-coffee/lib/index.coffee +2 -0
- data/examples/3-embedded-coffee/build +7 -0
- data/examples/3-embedded-coffee/embedded-coffee.coffee +25 -0
- data/examples/3-embedded-coffee/illustrate.png +0 -0
- data/examples/3-embedded-coffee/index.html +68 -0
- data/examples/3-embedded-coffee/lib/App.coffee +9 -0
- data/examples/3-embedded-coffee/lib/StringUtils.coffee +6 -0
- data/examples/3-embedded-coffee/lib/index.coffee +2 -0
- data/examples/4-coffee-join/build +7 -0
- data/examples/4-coffee-join/coffee-join.js +19 -0
- data/examples/4-coffee-join/illustrate.png +0 -0
- data/examples/4-coffee-join/index.html +55 -0
- data/examples/4-coffee-join/lib/App.coffee +7 -0
- data/examples/4-coffee-join/lib/StringUtils.coffee +6 -0
- data/examples/4-coffee-join/lib/index.coffee +2 -0
- data/examples/5-standalone/build +7 -0
- data/examples/5-standalone/illustrate.png +0 -0
- data/examples/5-standalone/index.html +52 -0
- data/examples/5-standalone/lib/App.coffee +7 -0
- data/examples/5-standalone/lib/StringUtils.coffee +6 -0
- data/examples/5-standalone/lib/index.coffee +2 -0
- data/examples/5-standalone/standalone.js +94 -0
- data/examples/README.md +43 -0
- data/examples/commons.rb +11 -0
- data/{test/integration/integration_test.rb → examples/handler.rb} +5 -12
- data/examples/illustrate.png +0 -0
- data/examples/illustrations.graffle +4382 -0
- data/examples/index.html +32 -0
- data/examples/public/TestSuite.coffee +43 -0
- data/examples/public/TestSuite.js +53 -0
- data/examples/public/coffee-script.js +8 -0
- data/examples/public/forkme.png +0 -0
- data/examples/public/leftnav.png +0 -0
- data/examples/public/leftnav_bw.png +0 -0
- data/examples/public/nibjs.js +74 -0
- data/examples/public/rightnav.png +0 -0
- data/examples/public/rightnav_bw.png +0 -0
- data/examples/public/style.css +82 -0
- data/examples/public/test-false.png +0 -0
- data/examples/public/test-pending.gif +0 -0
- data/examples/public/test-true.png +0 -0
- data/lib/nibjs/main.rb +104 -79
- data/lib/nibjs/version.rb +1 -1
- data/nibjs.noespec +1 -1
- data/src/nibjs.coffee +104 -107
- data/tasks/analytics.txt +10 -0
- data/tasks/examples.rake +25 -0
- data/tasks/gh-pages.rake +15 -0
- data/tasks/test.rake +2 -3
- data/test/command/footer.js +1 -0
- data/test/command/sc_coffee_6.exp +106 -0
- data/test/command/sc_coffee_7.exp +151 -0
- data/test/command/sc_common_6.exp +1 -0
- data/test/command/sc_common_7.exp +33 -0
- data/test/command/sc_common_8.exp +107 -0
- data/test/command/sc_common_9.exp +7 -0
- data/test/command/scenarios.rb +24 -0
- data/test/jasmine/assumption_spec.coffee +23 -0
- data/test/jasmine/nibjs_spec.coffee +14 -15
- data/test/nibjs.js +33 -33
- metadata +81 -14
- data/test/integration/index.html +0 -42
- data/test/integration/integration_test.coffee +0 -29
- data/test/integration/integration_test.js +0 -34
- data/test/integration/jquery-1.4.4.min.js +0 -167
data/lib/nibjs/version.rb
CHANGED
data/nibjs.noespec
CHANGED
data/src/nibjs.coffee
CHANGED
@@ -1,124 +1,121 @@
|
|
1
1
|
exports.NibJS = {
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
3
|
+
#
|
4
|
+
# Package builders.
|
5
|
+
#
|
6
|
+
# Package builders are simply unary functions registered via NibJS.define.
|
7
|
+
# These functions are expected to build the package thanks to a Builder
|
8
|
+
# instance that they receive as first argument.
|
9
|
+
#
|
10
|
+
# @see define
|
11
|
+
# @see Builder
|
12
|
+
#
|
13
|
+
pkgBuilders: []
|
14
|
+
|
15
|
+
#
|
16
|
+
# Built packages.
|
17
|
+
#
|
18
|
+
# Once the package are built (lazily, at first 'require' invocation), the
|
19
|
+
# results is put in the following array for subsequent requiring.
|
20
|
+
#
|
21
|
+
packages: []
|
14
22
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
23
|
+
#
|
24
|
+
# Checks if a package is known.
|
25
|
+
#
|
26
|
+
hasPackage: (name)->
|
27
|
+
NibJS.pkgBuilders[name]? || NibJS.packages[name]?
|
28
|
+
|
29
|
+
#
|
30
|
+
# Defines a package via a name and a builder function.
|
31
|
+
#
|
32
|
+
# Builder functions are expected to build the package thanks to a Builder
|
33
|
+
# instance received as first argument. They are expected to make the first
|
34
|
+
# require to the package index file.
|
35
|
+
#
|
36
|
+
# Example:
|
37
|
+
# NibJS.define('foo', function(nibjs){
|
38
|
+
# nibjs.register('bar', function(exports, require){
|
39
|
+
# /* content of bar.js comes here */
|
40
|
+
# });
|
41
|
+
# nibjs.register('index', function(exports, require){
|
42
|
+
# /* content of index.js come here */
|
43
|
+
# });
|
44
|
+
# /* Load the package at end */
|
45
|
+
# nibjs.require('index');
|
46
|
+
# });
|
47
|
+
#
|
48
|
+
define: (name, buildFn)->
|
49
|
+
NibJS.pkgBuilders[name] = buildFn
|
22
50
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
# nibjs.register('bar', function(exports, require){
|
33
|
-
# /* content of bar.js comes here */
|
34
|
-
# });
|
35
|
-
# nibjs.register('index', function(exports, require){
|
36
|
-
# /* content of index.js come here */
|
37
|
-
# });
|
38
|
-
# /* Load the package at end */
|
39
|
-
# nibjs.require('index');
|
40
|
-
# });
|
41
|
-
#
|
42
|
-
define: (name, buildFn)->
|
43
|
-
NibJS.pkgBuilders[name] = buildFn
|
51
|
+
#
|
52
|
+
# Returns a package, building it if required (equivalent to Node's _require_).
|
53
|
+
#
|
54
|
+
# Example:
|
55
|
+
# Foo = NibJS.require('foo')
|
56
|
+
# Foo.Bar = ...
|
57
|
+
#
|
58
|
+
require: (name)->
|
59
|
+
NibJS.packages[name] ?= NibJS._build_one(name)
|
44
60
|
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
#
|
52
|
-
require: (name)->
|
53
|
-
NibJS.packages[name] ?= NibJS._build_one(name)
|
54
|
-
|
55
|
-
#
|
56
|
-
# Internal implementation of require, when invoked the first time on
|
57
|
-
# a given library.
|
58
|
-
#
|
59
|
-
_build_one: (name)->
|
61
|
+
#
|
62
|
+
# Internal implementation of require, when invoked the first time on
|
63
|
+
# a given library.
|
64
|
+
#
|
65
|
+
_build_one: (name)->
|
66
|
+
if NibJS.pkgBuilders[name]
|
60
67
|
builder = new Builder
|
61
68
|
NibJS.pkgBuilders[name](builder)
|
69
|
+
else
|
70
|
+
throw new Exception("NibJS error: no module '#{name}' has been previously registered.")
|
62
71
|
|
63
|
-
|
64
|
-
pending: []
|
72
|
+
}
|
65
73
|
|
66
|
-
|
74
|
+
class Exception extends Error
|
75
|
+
constructor: (@message)->
|
67
76
|
|
68
|
-
|
69
|
-
|
77
|
+
#
|
78
|
+
# Intra-package builder, passed as first argument of NibJS.define
|
79
|
+
#
|
80
|
+
# This builder helps registrering functions for building individual
|
81
|
+
# files, as well as requiring them.
|
82
|
+
#
|
83
|
+
class Builder
|
70
84
|
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
for c in NibJS.running
|
78
|
-
return true if c is fn
|
79
|
-
return false
|
80
|
-
|
81
|
-
}
|
85
|
+
#
|
86
|
+
# Builds a builder instance
|
87
|
+
#
|
88
|
+
constructor: ()->
|
89
|
+
@builders = []
|
90
|
+
@built = []
|
82
91
|
|
83
92
|
#
|
84
|
-
#
|
93
|
+
# Register a builder function for _file_.
|
85
94
|
#
|
86
|
-
#
|
87
|
-
#
|
95
|
+
# The builder function will be called when the file will be required later.
|
96
|
+
# It takes two parameters as arguments, exports and require. The first one
|
97
|
+
# allows the file to exports artifacts while the second one mimics the
|
98
|
+
# Node's require function.
|
88
99
|
#
|
89
|
-
|
100
|
+
register: (file, builder)=>
|
101
|
+
@builders[file] = builder
|
90
102
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
# Register a builder function for _file_.
|
100
|
-
#
|
101
|
-
# The builder function will be called when the file will be required later.
|
102
|
-
# It takes two parameters as arguments, exports and require. The first one
|
103
|
-
# allows the file to exports artifacts while the second one mimics the
|
104
|
-
# Node's require function.
|
105
|
-
#
|
106
|
-
register: (file, builder)=>
|
107
|
-
@builders[file] = builder
|
103
|
+
#
|
104
|
+
# Mimics Node's require in the context of this building.
|
105
|
+
#
|
106
|
+
# This function returns the result of requiring _file_, building it with
|
107
|
+
# the function previously registered (if not previously done).
|
108
|
+
#
|
109
|
+
require: (file)=>
|
110
|
+
@built[file] ?= this.build_file(file)
|
108
111
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
# Private functions that ensures the building of a file.
|
120
|
-
#
|
121
|
-
build_file: (file)=>
|
122
|
-
exports = {}
|
123
|
-
@builders[file](exports, this.require)
|
124
|
-
exports
|
112
|
+
#
|
113
|
+
# Private functions that ensures the building of a file.
|
114
|
+
#
|
115
|
+
build_file: (file)=>
|
116
|
+
if @builders[file]?
|
117
|
+
file_exp = {}
|
118
|
+
@builders[file](file_exp, this.require)
|
119
|
+
file_exp
|
120
|
+
else
|
121
|
+
throw new Exception("NibJS error: no such file #{file}")
|
data/tasks/analytics.txt
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
<script type="text/javascript">
|
2
|
+
var _gaq = _gaq || [];
|
3
|
+
_gaq.push(['_setAccount', 'UA-16520635-4']);
|
4
|
+
_gaq.push(['_trackPageview']);
|
5
|
+
(function() {
|
6
|
+
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
|
7
|
+
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
|
8
|
+
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
|
9
|
+
})();
|
10
|
+
</script>
|
data/tasks/examples.rake
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
def each_example
|
2
|
+
Dir[_('examples/*')].each{|dir|
|
3
|
+
next unless File.directory?(dir)
|
4
|
+
next if File.basename(dir) == "public"
|
5
|
+
File.basename(dir) =~ /\d-(.*)$/
|
6
|
+
js_file = File.join(dir, "#{$1}.js")
|
7
|
+
yield(dir, js_file)
|
8
|
+
}
|
9
|
+
end
|
10
|
+
|
11
|
+
desc "Cleans the examples test suite"
|
12
|
+
task :"examples:clean" do
|
13
|
+
require 'fileutils'
|
14
|
+
FileUtils.rm_rf _('examples/public/TestSuite.js')
|
15
|
+
each_example{|dir, js_file| FileUtils.rm_rf js_file}
|
16
|
+
end
|
17
|
+
|
18
|
+
desc "Rebuilds the whole examples test suite"
|
19
|
+
task :"examples:build" => :"examples:clean" do
|
20
|
+
shell_safe_exec("coffee --compile --bare #{_('examples/public/TestSuite.coffee')}")
|
21
|
+
dist(_('examples/public/nibjs.js'))
|
22
|
+
each_example do |dir, js_file|
|
23
|
+
shell_safe_exec(File.join(dir, "build"))
|
24
|
+
end
|
25
|
+
end
|
data/tasks/gh-pages.rake
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
desc "Builds the github pages"
|
2
|
+
task :pages do
|
3
|
+
shell_safe_exec("cp -R #{_('examples/*')} #{_('gh-pages')}")
|
4
|
+
shell_safe_exec("rm -rf #{_('gh-pages/illustrations.graffle')}")
|
5
|
+
|
6
|
+
analytics = File.read(File.expand_path('../analytics.txt', __FILE__))
|
7
|
+
analytics.gsub!(/^/m, ' ')
|
8
|
+
Dir[_("gh-pages/**/index.html")].each{|file|
|
9
|
+
puts "On #{file}"
|
10
|
+
content = File.read(file)
|
11
|
+
content.gsub!(/^\s*<\/body>/, analytics + " </body>")
|
12
|
+
File.open(file, 'w'){|io| io << content}
|
13
|
+
}
|
14
|
+
shell_safe_exec("cd #{_('gh-pages')} && git commit -a -m 'Regenerated doc.' && git push origin")
|
15
|
+
end
|
data/tasks/test.rake
CHANGED
@@ -42,12 +42,11 @@ begin
|
|
42
42
|
end
|
43
43
|
|
44
44
|
desc "Run integration tests"
|
45
|
-
task :"test:integration" do
|
46
|
-
shell_safe_exec("coffee --compile #{_('test/integration')}")
|
45
|
+
task :"test:integration" => :"examples:build" do
|
47
46
|
puts "#"*100
|
48
47
|
puts "Please open the URL below in your browser"
|
49
48
|
puts "#"*100
|
50
|
-
puts shell_safe_exec("ruby #{_('
|
49
|
+
puts shell_safe_exec("ruby #{_('examples/handler.rb')}").inspect
|
51
50
|
end
|
52
51
|
|
53
52
|
desc "Bootstrap the test suite (rebuild command/*.exp)"
|
@@ -0,0 +1 @@
|
|
1
|
+
/* and this is a footer */
|
@@ -0,0 +1,106 @@
|
|
1
|
+
/**
|
2
|
+
* nib.js - [Java/Coffee]script application packager from node.js conventions
|
3
|
+
*
|
4
|
+
* Copyright 2011, Bernard Lambeau
|
5
|
+
* Released under the MIT License
|
6
|
+
* http://github.com/blambeau/nib.js
|
7
|
+
*/
|
8
|
+
(function(exports){
|
9
|
+
var Builder, Exception;
|
10
|
+
var __hasProp = Object.prototype.hasOwnProperty, __extends = function(child, parent) {
|
11
|
+
for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; }
|
12
|
+
function ctor() { this.constructor = child; }
|
13
|
+
ctor.prototype = parent.prototype;
|
14
|
+
child.prototype = new ctor;
|
15
|
+
child.__super__ = parent.prototype;
|
16
|
+
return child;
|
17
|
+
}, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
|
18
|
+
exports.NibJS = {
|
19
|
+
pkgBuilders: [],
|
20
|
+
packages: [],
|
21
|
+
hasPackage: function(name) {
|
22
|
+
return (NibJS.pkgBuilders[name] != null) || (NibJS.packages[name] != null);
|
23
|
+
},
|
24
|
+
define: function(name, buildFn) {
|
25
|
+
return NibJS.pkgBuilders[name] = buildFn;
|
26
|
+
},
|
27
|
+
require: function(name) {
|
28
|
+
var _base, _ref;
|
29
|
+
return (_ref = (_base = NibJS.packages)[name]) != null ? _ref : _base[name] = NibJS._build_one(name);
|
30
|
+
},
|
31
|
+
_build_one: function(name) {
|
32
|
+
var builder;
|
33
|
+
if (NibJS.pkgBuilders[name]) {
|
34
|
+
builder = new Builder;
|
35
|
+
return NibJS.pkgBuilders[name](builder);
|
36
|
+
} else {
|
37
|
+
throw new Exception("NibJS error: no module '" + name + "' has been previously registered.");
|
38
|
+
}
|
39
|
+
}
|
40
|
+
};
|
41
|
+
Exception = (function() {
|
42
|
+
__extends(Exception, Error);
|
43
|
+
function Exception(message) {
|
44
|
+
this.message = message;
|
45
|
+
}
|
46
|
+
return Exception;
|
47
|
+
})();
|
48
|
+
Builder = (function() {
|
49
|
+
function Builder() {
|
50
|
+
this.build_file = __bind(this.build_file, this);;
|
51
|
+
this.require = __bind(this.require, this);;
|
52
|
+
this.register = __bind(this.register, this);; this.builders = [];
|
53
|
+
this.built = [];
|
54
|
+
}
|
55
|
+
Builder.prototype.register = function(file, builder) {
|
56
|
+
return this.builders[file] = builder;
|
57
|
+
};
|
58
|
+
Builder.prototype.require = function(file) {
|
59
|
+
var _base, _ref;
|
60
|
+
return (_ref = (_base = this.built)[file]) != null ? _ref : _base[file] = this.build_file(file);
|
61
|
+
};
|
62
|
+
Builder.prototype.build_file = function(file) {
|
63
|
+
var file_exp;
|
64
|
+
if (this.builders[file] != null) {
|
65
|
+
file_exp = {};
|
66
|
+
this.builders[file](file_exp, this.require);
|
67
|
+
return file_exp;
|
68
|
+
} else {
|
69
|
+
throw new Exception("NibJS error: no such file " + file);
|
70
|
+
}
|
71
|
+
};
|
72
|
+
return Builder;
|
73
|
+
})();
|
74
|
+
}).call(this, this);
|
75
|
+
|
76
|
+
NibJS.define('fixture', function(nibjs) {
|
77
|
+
nibjs.register('./app', function(exports, require) {
|
78
|
+
var App;
|
79
|
+
exports.App = App = (function() {
|
80
|
+
function App() {}
|
81
|
+
App.prototype.say_hello = function() {
|
82
|
+
return "Hello from App";
|
83
|
+
};
|
84
|
+
return App;
|
85
|
+
})();
|
86
|
+
return exports;
|
87
|
+
});
|
88
|
+
nibjs.register('./dependent', function(exports, require) {
|
89
|
+
var App, Dependent;
|
90
|
+
App = require('./app').App;
|
91
|
+
exports.Dependent = Dependent = (function() {
|
92
|
+
function Dependent() {}
|
93
|
+
Dependent.prototype.say_hello = function() {
|
94
|
+
return "Hello from Dependent";
|
95
|
+
};
|
96
|
+
return Dependent;
|
97
|
+
})();
|
98
|
+
return exports;
|
99
|
+
});
|
100
|
+
nibjs.register('./index', function(exports, require) {
|
101
|
+
exports.App = require('./app').App;
|
102
|
+
exports.Dependent = require('./dependent').Dependent;
|
103
|
+
return exports;
|
104
|
+
});
|
105
|
+
return nibjs.require('./index');
|
106
|
+
});
|
@@ -0,0 +1,151 @@
|
|
1
|
+
NibJSBuild = (exports)->
|
2
|
+
exports.NibJS = {
|
3
|
+
|
4
|
+
#
|
5
|
+
# Package builders.
|
6
|
+
#
|
7
|
+
# Package builders are simply unary functions registered via NibJS.define.
|
8
|
+
# These functions are expected to build the package thanks to a Builder
|
9
|
+
# instance that they receive as first argument.
|
10
|
+
#
|
11
|
+
# @see define
|
12
|
+
# @see Builder
|
13
|
+
#
|
14
|
+
pkgBuilders: []
|
15
|
+
|
16
|
+
#
|
17
|
+
# Built packages.
|
18
|
+
#
|
19
|
+
# Once the package are built (lazily, at first 'require' invocation), the
|
20
|
+
# results is put in the following array for subsequent requiring.
|
21
|
+
#
|
22
|
+
packages: []
|
23
|
+
|
24
|
+
#
|
25
|
+
# Checks if a package is known.
|
26
|
+
#
|
27
|
+
hasPackage: (name)->
|
28
|
+
NibJS.pkgBuilders[name]? || NibJS.packages[name]?
|
29
|
+
|
30
|
+
#
|
31
|
+
# Defines a package via a name and a builder function.
|
32
|
+
#
|
33
|
+
# Builder functions are expected to build the package thanks to a Builder
|
34
|
+
# instance received as first argument. They are expected to make the first
|
35
|
+
# require to the package index file.
|
36
|
+
#
|
37
|
+
# Example:
|
38
|
+
# NibJS.define('foo', function(nibjs){
|
39
|
+
# nibjs.register('bar', function(exports, require){
|
40
|
+
# /* content of bar.js comes here */
|
41
|
+
# });
|
42
|
+
# nibjs.register('index', function(exports, require){
|
43
|
+
# /* content of index.js come here */
|
44
|
+
# });
|
45
|
+
# /* Load the package at end */
|
46
|
+
# nibjs.require('index');
|
47
|
+
# });
|
48
|
+
#
|
49
|
+
define: (name, buildFn)->
|
50
|
+
NibJS.pkgBuilders[name] = buildFn
|
51
|
+
|
52
|
+
#
|
53
|
+
# Returns a package, building it if required (equivalent to Node's _require_).
|
54
|
+
#
|
55
|
+
# Example:
|
56
|
+
# Foo = NibJS.require('foo')
|
57
|
+
# Foo.Bar = ...
|
58
|
+
#
|
59
|
+
require: (name)->
|
60
|
+
NibJS.packages[name] ?= NibJS._build_one(name)
|
61
|
+
|
62
|
+
#
|
63
|
+
# Internal implementation of require, when invoked the first time on
|
64
|
+
# a given library.
|
65
|
+
#
|
66
|
+
_build_one: (name)->
|
67
|
+
if NibJS.pkgBuilders[name]
|
68
|
+
builder = new Builder
|
69
|
+
NibJS.pkgBuilders[name](builder)
|
70
|
+
else
|
71
|
+
throw new Exception("NibJS error: no module '#{name}' has been previously registered.")
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
class Exception extends Error
|
76
|
+
constructor: (@message)->
|
77
|
+
|
78
|
+
#
|
79
|
+
# Intra-package builder, passed as first argument of NibJS.define
|
80
|
+
#
|
81
|
+
# This builder helps registrering functions for building individual
|
82
|
+
# files, as well as requiring them.
|
83
|
+
#
|
84
|
+
class Builder
|
85
|
+
|
86
|
+
#
|
87
|
+
# Builds a builder instance
|
88
|
+
#
|
89
|
+
constructor: ()->
|
90
|
+
@builders = []
|
91
|
+
@built = []
|
92
|
+
|
93
|
+
#
|
94
|
+
# Register a builder function for _file_.
|
95
|
+
#
|
96
|
+
# The builder function will be called when the file will be required later.
|
97
|
+
# It takes two parameters as arguments, exports and require. The first one
|
98
|
+
# allows the file to exports artifacts while the second one mimics the
|
99
|
+
# Node's require function.
|
100
|
+
#
|
101
|
+
register: (file, builder)=>
|
102
|
+
@builders[file] = builder
|
103
|
+
|
104
|
+
#
|
105
|
+
# Mimics Node's require in the context of this building.
|
106
|
+
#
|
107
|
+
# This function returns the result of requiring _file_, building it with
|
108
|
+
# the function previously registered (if not previously done).
|
109
|
+
#
|
110
|
+
require: (file)=>
|
111
|
+
@built[file] ?= this.build_file(file)
|
112
|
+
|
113
|
+
#
|
114
|
+
# Private functions that ensures the building of a file.
|
115
|
+
#
|
116
|
+
build_file: (file)=>
|
117
|
+
if @builders[file]?
|
118
|
+
file_exp = {}
|
119
|
+
@builders[file](file_exp, this.require)
|
120
|
+
file_exp
|
121
|
+
else
|
122
|
+
throw new Exception("NibJS error: no such file #{file}")
|
123
|
+
NibJSBuild(this)
|
124
|
+
|
125
|
+
|
126
|
+
NibJS.define 'fixture', (nibjs)->
|
127
|
+
nibjs.register './app', (exports, require)->
|
128
|
+
exports.App = class App
|
129
|
+
|
130
|
+
say_hello: ->
|
131
|
+
"Hello from App"
|
132
|
+
|
133
|
+
return exports
|
134
|
+
|
135
|
+
nibjs.register './dependent', (exports, require)->
|
136
|
+
{App} = require('./app')
|
137
|
+
|
138
|
+
exports.Dependent = class Dependent
|
139
|
+
|
140
|
+
say_hello: ->
|
141
|
+
"Hello from Dependent"
|
142
|
+
|
143
|
+
return exports
|
144
|
+
|
145
|
+
nibjs.register './index', (exports, require)->
|
146
|
+
exports.App = require('./app').App
|
147
|
+
exports.Dependent = require('./dependent').Dependent
|
148
|
+
return exports
|
149
|
+
|
150
|
+
nibjs.require './index'
|
151
|
+
fixture = NibJS.require 'fixture'
|