@malko/l.js 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +116 -0
  3. package/l.min.js +2 -0
  4. package/package.json +28 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) <2012-2023> Jonathan Gotti <jgotti at jgotti dot org >
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,116 @@
1
+ # l.js is another simple/tiny javascript/css loader
2
+ [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmalko%2Fl.js.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmalko%2Fl.js?ref=badge_shield)
3
+
4
+
5
+ ## features
6
+ - compatible with all ECMASCRIPT 5.1 browsers (Warning for old users this is a breaking change on supporting long time deprecated browsers)
7
+ - parallel script / css loading
8
+ - callback after script loading (css support callback too but are executed imediately)
9
+ - tiny only 2ko uglifyed, less than 1.1ko gziped (at least for latest revision)
10
+ - may load in order to preserve dependencies
11
+ - support aliases for simpler calling
12
+ - on demand loading
13
+ - only one script tag required
14
+ - clear syntax (to use not in the source code :) )
15
+ - successive load of same file will load it once but execute all callbacks associated
16
+ - can dumbly check already inserted tags at load time
17
+ - may use a fallback url on error (only for js files and with error events compatible browsers)
18
+ - may register error handlers (only for js files and with error events compatible browsers)
19
+ - can load javascripts modules
20
+
21
+ ## examples
22
+
23
+ ### loading ljs and another libs with one single tag
24
+ ```html
25
+ <script src="l.js">
26
+ ljs.load('myLib.js',function(){ /* your callback here */});
27
+ </script>
28
+ ```
29
+ or using jsdeliver CDN:
30
+ ```html
31
+ <script src="https://cdn.jsdelivr.net/gh/malko/l.js@latest/l.min.js">
32
+ ljs.load('myLib.js',function(){ /* your callback here */});
33
+ </script>
34
+ ```
35
+
36
+ > If you're looking for the npm package, you should search for l.js-loader
37
+ > ```npm install @malko/l.js```
38
+
39
+ ### loading some scripts in parallel others in order
40
+ ```html
41
+ <script src="l.js">
42
+ ljs
43
+ .load('myLib.js')
44
+ .load('myRequiredLib.js','myDependentLib.js',function(){ /* your callback here */})
45
+ ;
46
+ </script>
47
+ ```
48
+ second load will be executed in parallel of first load but myDependentLib.js won't load before myRequireLib.js is loaded
49
+
50
+ ```html
51
+ <script src="l.js">
52
+ ljs.load(['myLib.js','myRequiredLib.js'],'myDependentLib.js',function(){ /* your callback here */});
53
+ </script>
54
+ ```
55
+ this will load myLib.js and myRequiredLib.js in parrallel and wait for them before loading myDependentLib.js
56
+
57
+
58
+ ### using some aliases for simpler loading
59
+ ```html
60
+ <script src="l.js?checkLoaded"> // <- adding checkLoaded to the url will dumbly check already inserted script/link tags
61
+ ljs
62
+ .addAliases({
63
+ jQuery:'http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js#jqueryId' // <- script tag will have attribute id=jqueryId
64
+ ,ui:[
65
+ 'jQuery'
66
+ ,'https://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js'
67
+ ,'myUITheme.css'
68
+ ]
69
+ })
70
+ .load('ui',function(){
71
+ /* work with both jquery and jquery-ui here */
72
+ })
73
+ ;
74
+ </script>
75
+ ```
76
+
77
+ ### define a fallback url
78
+ l.js also support a fallback url for javascript files in case you want to try to get the resource from another location on loading failure
79
+ You can define this fallback url parameter like you define ids. The difference is you will prefix with **#=** instead of # alone
80
+ ```html
81
+ <script src="l.js">
82
+ ljs.load('http://domain.com/myLib.js#=/myfallback.js#myid',function(){
83
+ /*
84
+ generated script tag will have myid as id and will try to load /myfallback.js if it fail to load http://domain.com/myLib.js
85
+ */
86
+ });
87
+ </script>
88
+ ```
89
+
90
+ ### register an error handler
91
+ ```html
92
+ <script src="l.js">
93
+ ljs
94
+ .load('missingFile.js',function(){ /* your callback here */})
95
+ .onError(function(url) {
96
+ console.log('error loading', url); // <- will print "error loading missingFile.js"
97
+ })
98
+ ;
99
+ </script>
100
+ ```
101
+
102
+ ### load a js module
103
+ As a default l.js load js scripts with their type attribute set to `text/javascript`. To set type to `module` instead, simply prefix the url with `module:`
104
+
105
+ ```html
106
+ <script src="l.js">
107
+ ljs.load('module:myModuleLib.js',function(){ /* your callback here */});
108
+ </script>
109
+ ```
110
+
111
+ this piece of code is dual licensed under MIT / GPL
112
+ Hope this help, code review, suggestions, bug reports are welcome and appreciated.
113
+
114
+
115
+ ## License
116
+ [![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fmalko%2Fl.js.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fmalko%2Fl.js?ref=badge_large)
package/l.min.js ADDED
@@ -0,0 +1,2 @@
1
+ //https://github.com/malko/l.js v0.2.2 MIT license ©Jonathan Gotti
2
+ !function(e,l){function a(e,n){return e instanceof(n||Array)}function u(e,n,r){var t,o=c.createElement(e);for(t in r&&(o[s]?o[f]=function(){"loaded"!==o[s]&&"complete"!==o[s]||(o[f]=null,r())}:o.onload=r),n)n[t]&&(o[t]=n[t]);p.appendChild(o)}var c=document,n="getElementsByTagName",i="length",s="readyState",f="onreadystatechange",d="script",p=c[n]("head")[0]||c.documentElement,h={},r=c[n](d),t=r[r[i]-1],o=t.innerHTML.replace(/^\s+|\s+$/g,"");if(!e.ljs){function m(e){var o={};return o.u=e.replace(/(^module:)|#(=)?([^#]*)?/g,function(e,n,r,t){return o[n?"m":r?"f":"i"]=!!n||t,""}),o}var v=t.src.match(/checkLoaded/)?1:0,y={},g=[],j={aliases:h,loadjs:function(n,r){function t(e){for(var n=0,r=g.length;n<r;n++)g[n](e)}var e,o,a,c=m(n),i=c.m?"module":"text/javascript";return n=c.u,!0===y[n]?r&&r():y[n]!==l?r&&(y[n]=(e=y[n],o=r,function(){e&&e(),o&&o()})):(y[n]=(a=r,function(){y[n]=!0,a&&a()}),r=function(){y[n]()},u(d,{type:i,src:n,id:c.i,onerror:function(e){t(n);e=e.currentTarget;e.parentNode.removeChild(e),u(d,{type:i,src:c.f,id:c.i,onerror:function(){t(c.f)}},r)}},r)),j},loadcss:function(e,n){var r=m(e);return e=r.u,y[e]||u("link",{type:"text/css",rel:"stylesheet",href:e,id:r.i}),y[e]=!0,n&&n(),j},load:function(){var e=arguments,n=e[i];return 1===n&&a(e[0],Function)?e[0]():function(e,n,r){var t;if(h&&h[n])return t=h[n].slice(0),a(t)||(t=[t]),r&&t.push(r),e.load.apply(e,t);if(a(n)){for(var o=n[i];o--;)e.load(n[o]);return r&&n.push(r),e.load.apply(e,n)}n.match(/\.css\b/)?e.loadcss(n,r):e.loadjs(n,r)}(j,e[0],n<=1?l:function(){j.load.apply(j,[].slice.call(e,1))}),j},addAliases:function(e){for(var n in e)h[n]=a(e[n])?e[n].slice(0):e[n];return j},onError:function(e){return g.push(e),j}};if(v){for(var A,E,T=0,b=r[i];T<b;T++)(E=r[T].getAttribute("src"))&&(y[E.replace(/#.*$/,"")]=!0);for(T=0,b=(A=c[n]("link"))[i];T<b;T++)"stylesheet"!==A[T].rel&&"text/css"!==A[T].type||(y[A[T].getAttribute("href").replace(/#.*$/,"")]=!0)}e.ljs=j}t.src&&o&&u(d,{innerHTML:o})}(window);
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@malko/l.js",
3
+ "version": "0.2.2",
4
+ "description": "another simple/tiny javascript/css loader",
5
+ "files": [
6
+ "l.min.js"
7
+ ],
8
+ "main": "l.min.js",
9
+ "keywords": [
10
+ "loader",
11
+ "script",
12
+ "css",
13
+ "js",
14
+ "load",
15
+ "async",
16
+ "dynamic import",
17
+ "import",
18
+ "require"
19
+ ],
20
+ "author": "Jonathan Gotti <jgotti at jgotti dot org>",
21
+ "license": "MIT",
22
+ "devDependencies": {
23
+ "uglify-js": "^3.17.4"
24
+ },
25
+ "scripts": {
26
+ "build": "echo \"//https://github.com/malko/l.js v$npm_package_version MIT license ©Jonathan Gotti\" > l.min.js && uglifyjs l.js -m -c >> l.min.js && cat l.min.js | gzip > l.min.js.gz && ls -lh"
27
+ }
28
+ }