{"id":21524879,"url":"https://github.com/degjs/moduleloader","last_synced_at":"2025-08-10T23:46:36.097Z","repository":{"id":36869506,"uuid":"41176478","full_name":"DEGJS/moduleLoader","owner":"DEGJS","description":"Asynchronously load JavaScript modules via an HTML attribute.","archived":false,"fork":false,"pushed_at":"2023-01-04T12:59:21.000Z","size":887,"stargazers_count":1,"open_issues_count":10,"forks_count":0,"subscribers_count":12,"default_branch":"main","last_synced_at":"2024-04-26T21:02:58.370Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DEGJS.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-08-21T20:37:33.000Z","updated_at":"2021-01-25T21:30:27.000Z","dependencies_parsed_at":"2023-01-17T06:13:03.059Z","dependency_job_id":null,"html_url":"https://github.com/DEGJS/moduleLoader","commit_stats":null,"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"purl":"pkg:github/DEGJS/moduleLoader","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEGJS%2FmoduleLoader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEGJS%2FmoduleLoader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEGJS%2FmoduleLoader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEGJS%2FmoduleLoader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DEGJS","download_url":"https://codeload.github.com/DEGJS/moduleLoader/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DEGJS%2FmoduleLoader/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269808823,"owners_count":24478509,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-10T02:00:08.965Z","response_time":71,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T01:30:11.639Z","updated_at":"2025-08-10T23:46:36.058Z","avatar_url":"https://github.com/DEGJS.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# moduleLoader\n![Run Tests](https://github.com/DEGJS/moduleLoader/workflows/Run%20Tests/badge.svg)\n\nWhenever 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.\n\nThe moduleLoader module does exactly that, either on page load or on demand via an HTML attribute.\n\n## Install\nmoduleLoader is an ES6 module. Consequently, you may need a transpiler ([Babel](https://babeljs.io) is a nice one) to compile moduleLoader into compatible Javascript for your runtime environment.\n\nIf you're using NPM, you can install moduleLoader with the following command:\n\n```\n$ npm install @degjs/module-loader\n```\n\n## Usage\nRegardless of whether you load modules on page load or on demand, you must set the `data-module` attribute on your HTML elements:\n```html\n\u003cdiv class=\"my-components my-component-1\" data-module=\"components/myComponent1\"\u003e\n    Component 1\n\u003c/div\u003e\n\n\u003cdiv class=\"my-components my-component-2\" data-module=\"components/myComponent2\"\u003e\n    Component 2\n\u003c/div\u003e\n```\n\n### Option A: Load modules on page load\n```js\nimport moduleLoader from \"@degjs/module-loader\";\n\nmoduleLoader();\n```\n\n### Option B: Load modules on demand\nmoduleLoader uses the [MutationObserver](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) API to watch for elements with `data-module` attributes that are added to the DOM by JavaScript after the page is loaded.  \n```js\nimport moduleLoader from \"@degjs/module-loader\";\n\nmoduleLoader();\n\ndocument.body.insertAdjacentHTML('beforeend', `\n    \u003cdiv class=\"my-components my-component-3\" data-module=\"components/myComponent3\"\u003e\n        Component 3\n    \u003c/div\u003e\n`);\n```\n\nUpon each successful load, an object is passed to the loaded module containing the following values:\n\n* **containerElement:** The element from which the module was called.\n\n## Options\n#### options.moduleDataAttr\nType: `String`   \nThe name of the data attribute that defines the module to be loaded.  \nDefault: `data-module`\n\n#### options.elToObserve\nType: `Element`   \nThe DOM element to observe for dynamically added elements.  \nDefault: `document.body`\n\n#### options.enableObservation\nType: `Boolean`   \nIn 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.  \nDefault: `true`\n\n#### options.loadingMethod\nType: `String`   \nBy 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.\nOptions: `auto, system, esm`\nDefault: `auto`\n\n#### options.basePath\nType: `String`   \nThe base path of the JS module. This can be overridden at the element level by adding a `data-basepath` attribute to the element.\nDefault: `/js/`\n\n#### options.filenameSuffix\nType: `String`   \nThe 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.\nDefault: `-bundle.js`\n\n#### options.filenameNoModuleSuffix\nType: `String`   \nThe 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.\nDefault: `-bundle-nomodule.js`\n\n## Browser Support\nmoduleLoader depends on the following browser APIs:\n+ MutationObserver: [Documentation](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) | [Polyfill](https://github.com/megawac/MutationObserver.js) (Note: moduleLoader 4.0.2+ will work in IE10 without a polyfill, but dynamic module loading after the DOM is loaded will not. For IE10, either polyfill or use [moduleLoader 3.0.1](https://github.com/DEGJS/moduleLoader/tree/3.0.1))\n\nTo support legacy browsers, you'll need to include polyfills for the above APIs. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdegjs%2Fmoduleloader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdegjs%2Fmoduleloader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdegjs%2Fmoduleloader/lists"}