Skip to content
This repository was archived by the owner on Dec 20, 2022. It is now read-only.

Commit 2c984c4

Browse files
authored
Merge pull request #18 from CapMousse/dev
Version 3.0.1
2 parents 20380ce + 1d86ad4 commit 2c984c4

File tree

5 files changed

+112
-62
lines changed

5 files changed

+112
-62
lines changed

README.markdown

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Include.js [![Build Status](https://travis-ci.org/CapMousse/include.js.svg?branch=master)](https://travis-ci.org/CapMousse/include.js)
22

3-
**Include.js** is a tiny (1,2ko minified and gziped) Javascript loader. It can load normal javascript files or css but is more efficient with **web modules**.
3+
**Include.js** is a tiny (1,3ko minified and gziped) Javascript loader. It can load normal javascript files or css but is more efficient with **web modules**.
44

55
When it's possible, it will use async loading to speed up you page and will ensure the good executions of your script. It support **nested dependencies**, a useful feature to create clean and flexible javascript application.
66

@@ -12,70 +12,77 @@ Include.js was tested on :
1212
- Safari √
1313
- IE (> 6) √
1414

15-
## How to use
15+
## Define modules
1616

17-
To create a module, create a new javascript file and use `include()` as a wrapper :
17+
In the old javascript days, to create a module, you would add a simple wrapper on your code, like a auto-called anonymous functiom.
1818

1919
```javascript
20-
include(function(){
21-
return {
22-
name : "Mars Curiosity"
23-
}
24-
})
20+
(function(env){
21+
//your code here
22+
})(window);
2523
```
2624

27-
You can name your module with the first argument of `include()`. Name are like `PHP` namespace : `Dir/FileName.js` -> `Dir.FileName`
25+
Now, to define a module meeting the [AMD standard](http://requirejs.org/docs/whyamd.<html id="definition"></html>), just use `include()` as the wrapper of your code.
26+
27+
With AMD definition, you can create module having dependencies realy easily, executing code only when the dependencies are available.
2828

2929
```javascript
30-
include('App.Planet', function(){
31-
return {
32-
name : "Mars",
33-
gravity : 0.376,
34-
saletties : 2
35-
}
36-
})
30+
//define a new module with include
31+
include(['dep1', 'dep2'], function (dep1, dep2) {
32+
33+
//define the module value with return
34+
return dep2({
35+
name: dep1('Mars Curiosity')
36+
});
37+
});
3738
```
3839

39-
Modules can have dependencies to work, as an array on second argument off `include()` :
40+
## Named modules
41+
42+
Modules without a name are automatically named with their file name. This is what makes modules very portable and easily usable in others projetcs.
43+
44+
Naming module manually must be avoided to prevent conflicts or loading missing module. Only use module naming in controlled cases or for better performances.
4045

4146
```javascript
42-
include('App.Nasa', ['App/Rover.js', 'App.Planet'], function(Rover, Planet){
43-
return {
44-
rover : Rover.name,
45-
planet : Planet.name,
46-
success : true
47-
}
47+
//define a named module with dependencies
48+
include('name', ['dep1', 'dep2'], function (dep1, dep2) {
49+
return dep2({
50+
name: dep1('Mars Curiosity')
51+
});
4852
});
4953
```
5054

51-
Modules can also be loaded from other url, and named :
55+
## External dependencies
56+
57+
You can also load external dependencies, useful to speed up website loading time and prevent parsing to be blocked.
58+
59+
External dependencies can be AMD module or normal javascripts. If no module is found, argument send to the callback will be the index of the script in the dependencies array instead of the module function.
5260

5361
```javascript
54-
include('App.Nasa', [['Rover', 'http://your/url/here/script/rover/'], 'App.Planet'], function(Rover, Planet){
55-
return {
56-
rover : Rover.name,
57-
planet : Planet.name,
58-
success : true
59-
}
62+
include(['//website/jquery.js', '//website/amd-module.js'], function (indexOfJquery, amdModule) {
63+
$('#block').text(amdModule());
6064
});
6165
```
6266

63-
And you can load CSS
67+
## Load CSS
68+
69+
You can also load css like any other module. The callback will be called when the browser will have parsed the style.
70+
6471
```javascript
65-
include('path/to/css.css', function(){
66-
//do something when style is apply
67-
})
72+
include(['/styles/alternative.css', '//website/external-css.js'], function (css1, css2) {
73+
//css1 and css2 will be null
74+
});
6875
```
6976

70-
7177
## Already using a script loader ?
7278

73-
If you already use a script loader you can replace it with **Include.js** without problemes and without rewriting code. `define()` and `require()` are supported by **Include.js**. Let's be light !
79+
If you already use a script loader you can replace it with Include.js without problemes and without rewriting code. `define()` and `require()` are supported by Include.js. Let's be light !
7480

7581

7682
## Developed by
7783

7884
- Jérémy Barbe
85+
7986
- [jeremy.sh](http://jeremy.sh)
8087
- [@capitainemousse](https://twitter.com/capitainemousse)
8188

include-min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

include.js

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(function (environment) {
22

33
/**
4-
* List a existings modules
4+
* List of existings modules
55
* @type {Object}
66
*/
77
var modules = {};
@@ -51,6 +51,12 @@
5151

5252
waitingModules.unshift([name, deps, module]);
5353

54+
/**
55+
* Uid for script differentiation
56+
* @type {String}
57+
*/
58+
self.uid = Math.random().toString(36).replace(/[^a-z0-9]+/g, '').substr(0, 10);
59+
5460
self.checkModuleLoaded();
5561

5662
if (deps.length) {
@@ -74,6 +80,31 @@
7480
}
7581
}
7682

83+
/**
84+
* Get element data id
85+
* @param {String} name
86+
* @param {Boolean} clean only clean the name
87+
* @return {String}
88+
*/
89+
Include.prototype.getId = function (name, clean) {
90+
return (clean ? '' : this.uid + '-') + name.replace(/[^a-z0-9]+/g, '');
91+
}
92+
93+
/**
94+
* Get current script name
95+
* @return {String}
96+
*/
97+
Include.prototype.getFileName = function () {
98+
var scripts = document.querySelectorAll('script'),
99+
currentScript = scripts[scripts.length - 1],
100+
currentPath = document.location.href.split('/'),
101+
path = currentScript.src;
102+
103+
currentPath.pop();
104+
currentPath = currentPath.join('/');
105+
return path.replace(currentPath, '').substring(1);
106+
};
107+
77108
/**
78109
* Check if a module is loaded
79110
*/
@@ -88,14 +119,15 @@
88119

89120
self.each(dependencies, function (dependencie, n, t) {
90121
n = dependencie.push ? dependencie[0] : dependencie;
91-
t = document.querySelector('[data-module="'+n+'"]');
92-
93-
if (modules[n] !== undefined) {
94-
args.push(modules[n]);
95-
}
122+
t = document.querySelector('[data-id*="' + self.getId(n, 1) + '"]');
96123

97124
if (t && t.nodeName == "LINK") {
98125
args.push(null);
126+
return;
127+
}
128+
129+
if (modules[n] !== undefined) {
130+
args.push(modules[n]);
99131
}
100132
});
101133

@@ -106,10 +138,7 @@
106138
}
107139

108140
exec = typeof exec == 'function' ? exec.apply(this, args) : exec;
109-
110-
if (name !== null) {
111-
modules[name] = exec;
112-
}
141+
modules[name || self.getFileName()] = exec;
113142
}
114143
});
115144
}
@@ -118,9 +147,8 @@
118147
* onModuleLoaded
119148
* @param {String} name name of the module
120149
* @param {Number} index index of the module
121-
* @param {Boolean} isCss
122150
*/
123-
Include.prototype.onModuleLoaded = function (name, index, isCss) {
151+
Include.prototype.onModuleLoaded = function (name, index) {
124152
var self = this;
125153

126154
// Is this script add a waiting module ? If not, that's a "normal" script file
@@ -173,7 +201,7 @@
173201
while (i--) {
174202
if (sheets[i].href.indexOf(href) != -1) {
175203
elem.setAttribute('data-loaded', true);
176-
self.onModuleLoaded(elem.getAttribute('data-module'), elem.getAttribute('data-count'), true);
204+
self.onModuleLoaded(elem.getAttribute('data-module'), elem.getAttribute('data-count'));
177205

178206
self.checkModuleLoaded();
179207
return;
@@ -260,6 +288,7 @@
260288
elem.rel = "stylesheet"
261289
}
262290

291+
elem.setAttribute('data-id', self.getId(moduleName));
263292
elem.setAttribute('data-module', moduleName);
264293
elem.setAttribute('data-count', scriptCounter);
265294
elem.setAttribute('data-loaded', false);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "include.js",
3-
"version": "3.0.0",
3+
"version": "3.0.1",
44
"description": "**Include.js** is a tiny Javascript loader. It can load normal javascript files but is more efficient with **web modules**.",
55
"main": "include-min.js",
66
"dependencies": {},

test/include-specs.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
describe("Replace define", function(){
22
it("include should replace Require and Define", function(){
3-
var test = false;
4-
5-
if (require && define) {
6-
test = true;
7-
}
8-
9-
expect(test).toBe(true);
3+
expect(include).toEqual(jasmine.any(Function));
4+
expect(require).toEqual(jasmine.any(Function));
5+
expect(define).toEqual(jasmine.any(Function));
106
});
117
});
128

@@ -26,6 +22,17 @@ describe("Normal script", function(){
2622
done();
2723
});
2824
});
25+
26+
it("should be able to load script dependencies", function (done){
27+
include(['https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.6.0/highlight.min.js'], function () {
28+
expect(hljs).toEqual(jasmine.any(Object));
29+
30+
include(['https://cdnjs.cloudflare.com/ajax/libs/highlight.js/9.6.0/languages/javascript.min.js'], function () {
31+
expect(hljs.listLanguages()).toContain("javascript");
32+
done();
33+
});
34+
});
35+
})
2936
});
3037

3138
describe("Module name", function(){
@@ -84,9 +91,16 @@ describe("Include css", function(){
8491
});
8592
});
8693

94+
it("should be able to reload css", function(done){
95+
include(['https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/4.1.0/sanitize.css'], function(css){
96+
expect(css).toBe(null);
97+
done();
98+
});
99+
});
100+
87101
it("should be able to load css and module", function(done){
88-
inc = include([
89-
'https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/4.1.0/sanitize.css',
102+
include([
103+
'https://cdnjs.cloudflare.com/ajax/libs/16pixels/0.1.6/16pixels.css',
90104
'data/b.js'
91105
], function(css, b){
92106
expect(css).toBe(null);
@@ -96,8 +110,8 @@ describe("Include css", function(){
96110
});
97111

98112
it("should be able to load css and script as module", function(done){
99-
inc = include([
100-
'https://cdnjs.cloudflare.com/ajax/libs/10up-sanitize.css/4.1.0/sanitize.css',
113+
include([
114+
'https://cdnjs.cloudflare.com/ajax/libs/1140/2.0/1140.css',
101115
['stripe', 'https://js.stripe.com/v2/stripe.js']
102116
], function(css, stripe){
103117
expect(css).toBe(null);

0 commit comments

Comments
 (0)