{"id":13481169,"url":"https://github.com/lrsjng/modulejs","last_synced_at":"2025-09-07T12:39:48.261Z","repository":{"id":3861133,"uuid":"4946262","full_name":"lrsjng/modulejs","owner":"lrsjng","description":"Lightweight JavaScript module system.","archived":false,"fork":false,"pushed_at":"2024-11-09T22:20:02.000Z","size":505,"stargazers_count":128,"open_issues_count":3,"forks_count":12,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-04T02:06:58.229Z","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/lrsjng.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2012-07-08T13:12:18.000Z","updated_at":"2025-01-19T16:54:26.000Z","dependencies_parsed_at":"2024-12-18T06:01:47.077Z","dependency_job_id":"5d646366-d138-4426-af05-1681250d155c","html_url":"https://github.com/lrsjng/modulejs","commit_stats":{"total_commits":86,"total_committers":2,"mean_commits":43.0,"dds":0.03488372093023251,"last_synced_commit":"255ef01a9cb02fe3d46b2154ffba36da1e9a7ae5"},"previous_names":[],"tags_count":43,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lrsjng%2Fmodulejs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lrsjng%2Fmodulejs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lrsjng%2Fmodulejs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/lrsjng%2Fmodulejs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/lrsjng","download_url":"https://codeload.github.com/lrsjng/modulejs/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248633209,"owners_count":21136827,"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","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-07-31T17:00:49.325Z","updated_at":"2025-04-12T21:25:08.695Z","avatar_url":"https://github.com/lrsjng.png","language":"JavaScript","readme":"# modulejs\n\n[![license][license-img]][github] [![github][github-img]][github] [![npm][npm-img]][npm]  \n\nA lightweight JavaScript module system (only ~2kB minified). It is not a module loader, it triggers no file system lookups or HTTP requests. It simply helps organizing code in small, maintainable and easy to use modules. Modules respect and resolve dependencies, the syntax is very similar to that of [RequireJS](https://requirejs.org).\n\n\n## Usage\n\nDefine a module without dependencies.\n```js\nmodulejs.define('a', function () {\n    // do and return whatever you like\n    // ...\n    return {val: 1};\n});\n```\n\nDefine a module with dependencies.\n```js\nmodulejs.define('b', ['a'], function (a) {\n    // ...\n    return [a.val, a.val + 1];\n});\n```\n\nDefine another module.\n```js\nmodulejs.define('main', ['jquery', 'b'], function ($, b) {\n    // ...\n    return {\n        start: function () {\n            console.log(b);\n        }\n    };\n});\n```\n\nIt's easy to register 3rd party objects.\n```js\nmodulejs.define('modernizr', Modernizr);\n```\n\nBut you need to be careful with 'objects' that actually are functions, wrap them in functions.\n```js\nmodulejs.define('jquery', function () {\n    return jQuery;\n});\n```\n\nFinally require one of the defined modules and run some code (for example after all DOM content is loaded).\n```js\ndocument.addEventListener('DOMContentLoaded', function () {\n    var main = modulejs.require('main');\n    main.start();\n});\n```\n\n\n## API\n\n### define\n\nDefines a module through a constructor function. This function will only be called once when module is first required. The return value will be stored and returned whenever this module will be required.\n```js\n// id: string, fn: function  -\u003e  undefined\nmodulejs.define(id, fn)\n```\n\nSame as above but with dependencies that get resolved first and will be passed in as arguments to the constructor.\n```js\n// id: string, deps: array of strings, fn: function  -\u003e  undefined\nmodulejs.define(id, deps, fn)\n```\n\nDefines a module through an already existing object that gets returned whenever the module is required.\n```js\n// id: string, obj: object  -\u003e  undefined\nmodulejs.define(id, obj)\n```\n\nSame as above but with dependencies that get resolved first.\n```js\n// id: string, deps: array of strings, obj: object  -\u003e  undefined\nmodulejs.define(id, deps, obj)\n```\n\n### require\n\nReturns an already defined module. Creates it if necessary.\n```js\n// id: string  -\u003e  object\nmodulejs.require(id)\n```\n\nFor testing purposes it's possible to provide mock instances for selected modules to override original module definitions.\n```js\n// id: string, mocks: object  -\u003e  object\nmodulejs.require(id, mocks)\n```\n\nfor example:\n```js\nmodulejs.require('b', {a: 'testing'})\n```\n\nwill resolve a dependency `a` with the string `testing` instead of the real module.\n\n### state\n\nReturns an object that represents the current state of all modules.\n```js\n//  -\u003e  object\nmodulejs.state()\n```\n\nreturns an object of the form:\n```js\n{\n    // ...\n    main: {\n        deps: ['jquery', 'b']\n        init: true\n        reqd: []\n        reqs: ['jquery', 'a', 'b']\n    }\n    // ...\n}\n```\n\n### log\n\nReturns a string representing module dependencies in a easy to read format. If `inv` is `true` it shows dependents for each module.\n```js\n// inv: boolean  -\u003e  string\nmodulejs.log(inv)\n```\n\nThe result will show all dependencies (transitiv):\n```\n* a -\u003e [  ]\n* b -\u003e [ a ]\n* main -\u003e [ jquery, a, b ]\n  modernizr -\u003e [  ]\n* jquery -\u003e [  ]\n```\n\nand if `inv` is `true` it will show all dependents (transitiv):\n```\n* a -\u003e [ b, main ]\n* b -\u003e [ main ]\n* main -\u003e [  ]\n  modernizr -\u003e [  ]\n* jquery -\u003e [ main ]\n```\n\na `*` indicates whether a module was already instantiated.\n\n### create\n\nReturns a fresh, private instances of `modulejs` with no definitions or instances.\n```js\n//  -\u003e  modulejs\nmodulejs.create()\n```\n\n\n## License\nThe MIT License (MIT)\n\nCopyright (c) 2024 Lars Jung (https://larsjung.de)\n\nPermission is hereby granted, free of charge, to any person obtaining a copy\nof this software and associated documentation files (the \"Software\"), to deal\nin the Software without restriction, including without limitation the rights\nto use, copy, modify, merge, publish, distribute, sublicense, and/or sell\ncopies of the Software, and to permit persons to whom the Software is\nfurnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in\nall copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\nFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\nAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\nLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\nOUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN\nTHE SOFTWARE.\n\n\n[github]: https://github.com/lrsjng/modulejs\n[npm]: https://www.npmjs.org/package/modulejs\n\n[license-img]: https://img.shields.io/badge/license-MIT-a0a060.svg?style=flat-square\n[github-img]: https://img.shields.io/badge/github-lrsjng/modulejs-a0a060.svg?style=flat-square\n[npm-img]: https://img.shields.io/badge/npm-modulejs-a0a060.svg?style=flat-square\n","funding_links":[],"categories":["Loaders","Loaders [🔝](#readme)","加载器"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flrsjng%2Fmodulejs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Flrsjng%2Fmodulejs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Flrsjng%2Fmodulejs/lists"}