You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A lightweight JavaScript module system (only ~2kB minified). It is not a module loader, it triggers no file system lookups or HTTP requests. It simply helps organizing code in small, maintainable and easy to use modules. Modules respect and resolve dependencies, the syntax is very similar to that of [RequireJS](https://requirejs.org).
6
+
7
+
8
+
## Usage
9
+
10
+
Define a module without dependencies.
11
+
```js
12
+
modulejs.define('a', function () {
13
+
// do and return whatever you like
14
+
// ...
15
+
return {val:1};
16
+
});
17
+
```
18
+
19
+
Define a module with dependencies.
20
+
```js
21
+
modulejs.define('b', ['a'], function (a) {
22
+
// ...
23
+
return [a.val, a.val+1];
24
+
});
25
+
```
26
+
27
+
Define another module.
28
+
```js
29
+
modulejs.define('main', ['jquery', 'b'], function ($, b) {
30
+
// ...
31
+
return {
32
+
start:function () {
33
+
console.log(b);
34
+
}
35
+
};
36
+
});
37
+
```
38
+
39
+
It's easy to register 3rd party objects.
40
+
```js
41
+
modulejs.define('modernizr', Modernizr);
42
+
```
43
+
44
+
But you need to be careful with 'objects' that actually are functions, wrap them in functions.
45
+
```js
46
+
modulejs.define('jquery', function () {
47
+
return jQuery;
48
+
});
49
+
```
50
+
51
+
Finally require one of the defined modules and run some code (for example after all DOM content is loaded).
52
+
```js
53
+
document.addEventListener('DOMContentLoaded', function () {
54
+
var main =modulejs.require('main');
55
+
main.start();
56
+
});
57
+
```
58
+
59
+
60
+
## API
61
+
62
+
### define
63
+
64
+
Defines a module through a constructor function. This function will only be called once when module is first required. The return value will be stored and returned whenever this module will be required.
65
+
```js
66
+
// id: string, fn: function -> undefined
67
+
modulejs.define(id, fn)
68
+
```
69
+
70
+
Same as above but with dependencies that get resolved first and will be passed in as arguments to the constructor.
71
+
```js
72
+
// id: string, deps: array of strings, fn: function -> undefined
73
+
modulejs.define(id, deps, fn)
74
+
```
75
+
76
+
Defines a module through an already existing object that gets returned whenever the module is required.
77
+
```js
78
+
// id: string, obj: object -> undefined
79
+
modulejs.define(id, obj)
80
+
```
81
+
82
+
Same as above but with dependencies that get resolved first.
0 commit comments