Skip to content

Commit 7e45904

Browse files
committed
Update.
1 parent 255ef01 commit 7e45904

File tree

6 files changed

+5945
-5128
lines changed

6 files changed

+5945
-5128
lines changed

README.md

+163-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,174 @@
11
# modulejs
22

3-
[![license][license-img]][github] [![web][web-img]][web] [![github][github-img]][github] [![npm][npm-img]][npm]
4-
[![version][npm-v-img]][npm] [![downloads][npm-dm-img]][npm]
3+
[![license][license-img]][github] [![github][github-img]][github] [![npm][npm-img]][npm]
54

6-
Lightweight JavaScript module system.
5+
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.
83+
```js
84+
// id: string, deps: array of strings, obj: object -> undefined
85+
modulejs.define(id, deps, obj)
86+
```
87+
88+
### require
89+
90+
Returns an already defined module. Creates it if necessary.
91+
```js
92+
// id: string -> object
93+
modulejs.require(id)
94+
```
95+
96+
For testing purposes it's possible to provide mock instances for selected modules to override original module definitions.
97+
```js
98+
// id: string, mocks: object -> object
99+
modulejs.require(id, mocks)
100+
```
101+
102+
for example:
103+
```js
104+
modulejs.require('b', {a: 'testing'})
105+
```
106+
107+
will resolve a dependency `a` with the string `testing` instead of the real module.
108+
109+
### state
110+
111+
Returns an object that represents the current state of all modules.
112+
```js
113+
// -> object
114+
modulejs.state()
115+
```
116+
117+
returns an object of the form:
118+
```js
119+
{
120+
// ...
121+
main: {
122+
deps: ['jquery', 'b']
123+
init: true
124+
reqd: []
125+
reqs: ['jquery', 'a', 'b']
126+
}
127+
// ...
128+
}
129+
```
130+
131+
### log
132+
133+
Returns a string representing module dependencies in a easy to read format. If `inv` is `true` it shows dependents for each module.
134+
```js
135+
// inv: boolean -> string
136+
modulejs.log(inv)
137+
```
138+
139+
The result will show all dependencies (transitiv):
140+
```
141+
* a -> [ ]
142+
* b -> [ a ]
143+
* main -> [ jquery, a, b ]
144+
modernizr -> [ ]
145+
* jquery -> [ ]
146+
```
147+
148+
and if `inv` is `true` it will show all dependents (transitiv):
149+
```
150+
* a -> [ b, main ]
151+
* b -> [ main ]
152+
* main -> [ ]
153+
modernizr -> [ ]
154+
* jquery -> [ main ]
155+
```
156+
157+
a `*` indicates whether a module was already instantiated.
158+
159+
### create
160+
161+
Returns a fresh, private instances of `modulejs` with no definitions or instances.
162+
```js
163+
// -> modulejs
164+
modulejs.create()
165+
```
7166

8167

9168
## License
10169
The MIT License (MIT)
11170

12-
Copyright (c) 2020 Lars Jung (https://larsjung.de)
171+
Copyright (c) 2024 Lars Jung (https://larsjung.de)
13172

14173
Permission is hereby granted, free of charge, to any person obtaining a copy
15174
of this software and associated documentation files (the "Software"), to deal
@@ -30,14 +189,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30189
THE SOFTWARE.
31190

32191

33-
[web]: https://larsjung.de/modulejs/
34192
[github]: https://github.com/lrsjng/modulejs
35193
[npm]: https://www.npmjs.org/package/modulejs
36194

37195
[license-img]: https://img.shields.io/badge/license-MIT-a0a060.svg?style=flat-square
38-
[web-img]: https://img.shields.io/badge/web-larsjung.de/modulejs-a0a060.svg?style=flat-square
39196
[github-img]: https://img.shields.io/badge/github-lrsjng/modulejs-a0a060.svg?style=flat-square
40197
[npm-img]: https://img.shields.io/badge/npm-modulejs-a0a060.svg?style=flat-square
41-
42-
[npm-v-img]: https://img.shields.io/npm/v/modulejs.svg?style=flat-square
43-
[npm-dm-img]: https://img.shields.io/npm/dm/modulejs.svg?style=flat-square

0 commit comments

Comments
 (0)