Asynchronously load JavaScript modules via an HTML attribute.
npm install @degjs/module-loaderWhenever possible, it's best to bundle modules during development. However, in certain situations (such as a restrictive CMS, or when loading JavaScript after a specific user interaction), it may be necessary to load a module asynchronously at runtime.
The moduleLoader module does exactly that, either on page load or on demand via an HTML attribute.
If you're using NPM, you can install moduleLoader with the following command:
```
$ npm install @degjs/module-loader
attribute on your HTML elements:
`html
Component 1
Component 2
`$3
`js
import moduleLoader from "@degjs/module-loader";moduleLoader();
`$3
moduleLoader uses the MutationObserver API to watch for elements with data-module attributes that are added to the DOM by JavaScript after the page is loaded.
`js
import moduleLoader from "@degjs/module-loader";moduleLoader();
document.body.insertAdjacentHTML('beforeend',
);
`Upon each successful load, an object is passed to the loaded module containing the following values:
* containerElement: The element from which the module was called.
Options
#### options.moduleDataAttr
Type: String
The name of the data attribute that defines the module to be loaded.
Default: data-module#### options.elToObserve
Type:
Element
The DOM element to observe for dynamically added elements.
Default: document.body#### options.enableObservation
Type:
Boolean
In some cases, you may know that no elements with modules will be added to the page after page load. Setting to false disables the potentially expensive mutation observer.
Default: true#### options.loadingMethod
Type:
String
By default, moduleLoader will attempt to load native JavaScript modules using the import() method, but will automatically fall back to SystemJS's System.import() method in unsupported browsers. This behavior can be overridden with this setting.
Options: auto, system, esm
Default: auto#### options.basePath
Type:
String
The base path of the JS module. This can be overridden at the element level by adding a data-basepath attribute to the element.
Default: /js/#### options.filenameSuffix
Type:
String
The suffix of the JS bundle being loaded in browsers that support modules. This can be overridden at the element level by adding a data-suffix attribute to the element.
Default: -bundle.js#### options.filenameNoModuleSuffix
Type:
String
The suffix of the JS bundle being loaded in browsers that don't support modules. This can be overridden at the element level by adding a data-no-module-suffix attribute to the element.
Default: -bundle-nomodule.js`To support legacy browsers, you'll need to include polyfills for the above APIs.