Skip to content

Commit

Permalink
4.3.0 (#109)
Browse files Browse the repository at this point in the history
* Added module/nomodule path modifier support (#108)
* Bumps version number to 4.3.0
  • Loading branch information
amorey authored Apr 11, 2024
1 parent f0129fa commit 429dd45
Show file tree
Hide file tree
Showing 14 changed files with 6,067 additions and 233 deletions.
15 changes: 11 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@

<img src="https://www.muicss.com/static/images/loadjs.svg" width="250px">

LoadJS is a tiny async loader for modern browsers (899 bytes).
LoadJS is a tiny async loader for modern browsers (961 bytes).

[![Dependency Status](https://david-dm.org/muicss/loadjs.svg)](https://david-dm.org/muicss/loadjs)
[![devDependency Status](https://david-dm.org/muicss/loadjs/dev-status.svg)](https://david-dm.org/muicss/loadjs?type=dev)
[![CDNJS](https://img.shields.io/cdnjs/v/loadjs.svg)](https://cdnjs.com/libraries/loadjs)

## Introduction

LoadJS is a tiny async loading library for modern browsers (IE9+). It has a simple yet powerful dependency management system that lets you fetch JavaScript, CSS and image files in parallel and execute code after the dependencies have been met. The recommended way to use LoadJS is to include the minified source code of [loadjs.js](https://raw.githubusercontent.com/muicss/loadjs/master/dist/loadjs.min.js) in your &lt;html&gt; (possibly in the &lt;head&gt; tag) and then use the `loadjs` global to manage JavaScript dependencies after pageload.

LoadJS is based on the excellent [$script](https://github.com/ded/script.js) library by [Dustin Diaz](https://github.com/ded). We kept the behavior of the library the same but we re-wrote the code from scratch to add support for success/error callbacks and to optimize the library for modern browsers. LoadJS is 899 bytes (minified + gzipped).
LoadJS is based on the excellent [$script](https://github.com/ded/script.js) library by [Dustin Diaz](https://github.com/ded). We kept the behavior of the library the same but we re-wrote the code from scratch to add support for success/error callbacks and to optimize the library for modern browsers. LoadJS is 961 bytes (minified + gzipped).

Here's an example of what you can do with LoadJS:

Expand Down Expand Up @@ -135,6 +133,15 @@ Note: LoadJS treats empty CSS files as load failures in IE9-11 and uses `rel="pr
});
```

1. Load JavaScript files as modules with non-module fallbacks (in browsers without module support)

```javascript
loadjs(['module!/path/to/foo.js', 'nomodule!/path/to/bar.js'], function() {
/* foo.js loaded with type="module" in browsers with module support, skipped silently in browsers without */
/* bar.js loaded with type="text/javascript" in browsers without module support, skipped silently in browsers with */
});
```

1. Add a bundle id

```javascript
Expand Down
18 changes: 16 additions & 2 deletions dist/loadjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ function loadFile(path, callbackFn, args, numTries) {
maxTries = (args.numRetries || 0) + 1,
beforeCallbackFn = args.before || devnull,
pathname = path.replace(/[\?|#].*$/, ''),
pathStripped = path.replace(/^(css|img)!/, ''),
pathStripped = path.replace(/^(css|img|module|nomodule)!/, ''),
isLegacyIECss,
hasModuleSupport,
e;

numTries = numTries || 0;
Expand All @@ -132,8 +133,21 @@ function loadFile(path, callbackFn, args, numTries) {
} else {
// javascript
e = doc.createElement('script');
e.src = path;
e.src = pathStripped;
e.async = async === undefined ? true : async;

// handle es modules
// modern browsers:
// module: add to dom with type="module"
// nomodule: call success() callback without adding to dom
// legacy browsers:
// module: call success() callback without adding to dom
// nomodule: add to dom with default type ("text/javascript")
hasModuleSupport = 'noModule' in e;
if (/^module!/.test(pathname)) {
if (!hasModuleSupport) return callbackFn(path, 'l');
e.type = "module";
} else if (/^nomodule!/.test(pathname) && hasModuleSupport) return callbackFn(path, 'l');
}

e.onload = e.onerror = e.onbeforeload = function (ev) {
Expand Down
2 changes: 1 addition & 1 deletion dist/loadjs.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 16 additions & 2 deletions dist/loadjs.umd.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ function loadFile(path, callbackFn, args, numTries) {
maxTries = (args.numRetries || 0) + 1,
beforeCallbackFn = args.before || devnull,
pathname = path.replace(/[\?|#].*$/, ''),
pathStripped = path.replace(/^(css|img)!/, ''),
pathStripped = path.replace(/^(css|img|module|nomodule)!/, ''),
isLegacyIECss,
hasModuleSupport,
e;

numTries = numTries || 0;
Expand All @@ -140,8 +141,21 @@ function loadFile(path, callbackFn, args, numTries) {
} else {
// javascript
e = doc.createElement('script');
e.src = path;
e.src = pathStripped;
e.async = async === undefined ? true : async;

// handle es modules
// modern browsers:
// module: add to dom with type="module"
// nomodule: call success() callback without adding to dom
// legacy browsers:
// module: call success() callback without adding to dom
// nomodule: add to dom with default type ("text/javascript")
hasModuleSupport = 'noModule' in e;
if (/^module!/.test(pathname)) {
if (!hasModuleSupport) return callbackFn(path, 'l');
e.type = "module";
} else if (/^nomodule!/.test(pathname) && hasModuleSupport) return callbackFn(path, 'l');
}

e.onload = e.onerror = e.onbeforeload = function (ev) {
Expand Down
18 changes: 16 additions & 2 deletions examples/assets/loadjs/loadjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,9 @@ function loadFile(path, callbackFn, args, numTries) {
maxTries = (args.numRetries || 0) + 1,
beforeCallbackFn = args.before || devnull,
pathname = path.replace(/[\?|#].*$/, ''),
pathStripped = path.replace(/^(css|img)!/, ''),
pathStripped = path.replace(/^(css|img|module|nomodule)!/, ''),
isLegacyIECss,
hasModuleSupport,
e;

numTries = numTries || 0;
Expand All @@ -132,8 +133,21 @@ function loadFile(path, callbackFn, args, numTries) {
} else {
// javascript
e = doc.createElement('script');
e.src = path;
e.src = pathStripped;
e.async = async === undefined ? true : async;

// handle es modules
// modern browsers:
// module: add to dom with type="module"
// nomodule: call success() callback without adding to dom
// legacy browsers:
// module: call success() callback without adding to dom
// nomodule: add to dom with default type ("text/javascript")
hasModuleSupport = 'noModule' in e;
if (/^module!/.test(pathname)) {
if (!hasModuleSupport) return callbackFn(path, 'l');
e.type = "module";
} else if (/^nomodule!/.test(pathname) && hasModuleSupport) return callbackFn(path, 'l');
}

e.onload = e.onerror = e.onbeforeload = function (ev) {
Expand Down
2 changes: 1 addition & 1 deletion examples/assets/loadjs/loadjs.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 429dd45

Please sign in to comment.